sounds in bada

54
Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. Sounds in bada 1

Upload: samsung

Post on 24-May-2015

510 views

Category:

Education


1 download

DESCRIPTION

sounds in bada

TRANSCRIPT

Page 1: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Sounds in bada

1

Page 2: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Contents

Introduction

Common Sound Classes– AudioRecorder/Player– AudioIn/AudioOut

Simple Mixing

Media Management– Image– Video– Streaming

Summary2

*This material is based on bada SDK 1.0.0b3

Page 3: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Media Introduction

Audio/ video/ image processing functions

Key features

3

• Playback• Record• Streaming• PCM processing

Audio

• Playback• Record• Streaming• Camera capture• Camera preview

Video

• Load• Save• Format conversion

Image

Page 4: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Supported FormatsType Format Encode Decode Streaming*Video H.263, MPEG-4 (.3gp, mp4) O O O

H.264 (.3gp, mp4) O OVC-1 (.wmv, .asf) O

Audio PCM (.wav) O OAMR-NB (.amr, .3ga, .m4a) O O OAAC, AAC+, EAAC+ (.aac, .3ga, .m4a)

O O

MIDI (.mid, .spm, .imy, .mmf, .xmf) OMP3 (.mp3), WMA ( .wma, .asf) O

Image JPEG, PNG, BMP O OGIF, TIFF, WBMP O

*Streaming Protocol: RTSP4

Page 5: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Header & Library

Header file: – #include <FMedia.h>

Library – Simulator: FMedia – Target: FOsp

Privilege groups– RECORDING– IMAGE– CAMERA

5

Page 6: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Common Sound Classes

6

Page 7: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Basic method:General purpose

Complex method:Direct access to device

Audio Recorder & Player

*PCM: Pulse Code Modulation 7

Recording– AudioRecorder : Records

audio data from input source to storage

Playing– Player : Open audio file

and play

Recording– AudioIn : Read raw PCM

data from input srouce

Playing– AudioOut : Play sound with

raw PCM data

Page 8: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Demo

Playback– Player– AudioOut

8

Page 9: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Basic method

9

Page 10: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

AudioRecorder class

Records audio data from input source to storage– Recording format

• WAV, AMR

Event– IAudioRecorderEventListener

• OnAudioRecorderPaused• OnAudioRecorderStarted• OnAudioRecorderStopped• OnAudioRecorderClosed• OnAudioRecorderCanceled• …

"RECORDING" privilege is needed10

Page 11: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Using the AudioRecorder class

Create AudioRecorder instance

Recording

Stop recording & close

__pAR = new AudioRecorder;__pAR->Construct(*pListener);

// IAudioRecorderEventListener

__pAR->CreateAudioFile( __path, true);__pAR->SetFormat(AUDIORECORDING_FORMAT_WAVE);__pAR->Record();

__pAR->Stop();…void EventListener::OnAudioRecorderStopped( result r){

__pAR->Close();}

11

Page 12: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Player class

Can play audio & video– Multiple audio playback is supported(max 10)

• Only 1 compressed audio files– Can play audio/video by streaming

• Streaming Protocol: Real-Time Streaming Protocol (RTSP)

Event– IPlayerEventListener

• OnPlayerBuffering, OnPlayerInterrupted• OnPlayerEndOfClip, OnPlayerErrorOccurred• OnPlayerOpened, OnPlayerReleased• OnPlayerSeekCompleted

12

Page 13: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Using the Player class

Create Player instance

Load file & play

Stop

__pPlayer = new Player;__pPlayer->Construct(*pListener);

// IPlayerEventListener

result r = __pPlayer->OpenFile(__path);if (IsFailed(r)) return;__pPlayer->Play();

__pPlayer->Stop();__pPlayer->Close();

13

Page 14: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Complex method

14

Page 15: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Record PCM data

AudioIn class– Record raw uncompressed PCM data from

microphone– Double buffering is possible

Event Listener– IAudioInEventListener

• OnAudioInBufferIsFilled• OnAudioInInterrupted• OnAudioInReleased

15

Page 16: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Using the AudioIn class

Create AudioIn instance

Feed buffers and start recording

Feed next buffers

__pAudioIn = new AudioIn();__pAudioIn->Construct(*__pListener);__pAudioIn-> Prepare (AUDIO_INPUT_DEVICE_MIC, AUDIO_TYPE_PCM_U8, AUDIO_CHANNEL_TYPE_STEREO,8000);

__pAudioIn->AddBuffer(__pByteBuffer1);__pAudioIn->Start();

void Listener::OnAudioInBufferIsFilled( Osp::Base::ByteBuffer* pData){ // save result.

pData->Clear();__pAudioIn->AddBuffer(pData);

16

Page 17: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Play PCM data

AudioOut– Play raw uncompressed PCM data– Double buffering is possible

Event listener– IAudioOutEventListener

• OnAudioOutBufferEndReached• OnAudioOutInterrupted• OnAudioOutReleased

17

Page 18: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Using the AudioOut class

Create AudioOut instance

Feed PCM data

Play sound

__pAO = new AudioOut();__pAO->Construct(*pListener);// IAudioOutEventListener__pAO->Prepare(AUDIO_TYPE_PCM_S16_LE,

AUDIO_CHANNEL_TYPE_MONO, SAMPLERATE);__pOutBuffer = new ByteBuffer();__pOutBuffer->Construct(BUFSIZE);

// Fill buffer__pAO->WriteBuffer(*__pOutBuffer);

__pAO->Start();

18

Page 19: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Using the AudioOut class ctd.

Process events

Stop sound__pAO->Stop(); //or Reset()

void EventListener::OnAudioOutBufferEndReached(Osp::Media::AudioOut &src){ //Feed Next PCM data }

19

Page 20: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

20

AudioIn DeviceStep 1. Initialize AudioIn Recording

application

Page 21: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

21

AudioIn DeviceStep 1. Initialize AudioIn

Step 2. Add two buffers

Buffer1

Buffer2

Recordingapplication

Page 22: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

22

AudioIn DeviceStep 1. Initialize AudioIn

Step 2. Add two buffers

Step 3. Start recording

Step 4. Buffer1 is filled

Buffer1

Buffer2

Recordingapplication

Page 23: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

23

AudioIn DeviceStep 1. Initialize AudioIn

Step 2. Add two buffers

Step 3. Start recording

Step 4. Buffer1 is filled

Step 5. Event is fired Buffer2 is used by AudioIn

Buffer1

Buffer2

Recordingapplication

Event: Buffer is filled

Page 24: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

24

AudioIn DeviceStep 1. Initialize AudioIn

Step 2. Add two buffers

Step 3. Start recording

Step 4. Buffer1 is filled

Step 5. Event is fired Buffer2 is used by AudioIn

Step 6. App. reads Buffer1and AudioIn writes to Buffer2

Buffer1

Buffer2

Recordingapplication

Page 25: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

25

AudioIn DeviceStep 1. Initialize AudioIn

Step 2. Add two buffers

Step 3. Start recording

Step 4. Buffer1 is filled

Step 5. Event is fired Buffer2 is used by AudioIn

Step 6. App. reads Buffer1and AudioIn writes to Buffer2

Buffer1

Buffer2

Recordingapplication

Event: Buffer is filled

Step 7. Event is firedAudioIn switches to Buffer1

Page 26: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioIn

26

AudioIn DeviceStep 1. Initialize AudioIn

Step 2. Add two buffers

Step 3. Start recording

Step 4. Buffer1 is filled

Step 5. Event is fired Buffer2 is used by AudioIn

Step 6. App. reads Buffer1and AudioIn writes to Buffer2

Buffer1

Buffer2

Recordingapplication

Step 7. Event is firedAudioIn switches to Buffer1Step 8. Read Buffer2

Page 27: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

27

AudioOut DeviceStep 1. Initialize AudioOut Player

application

Page 28: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

28

AudioOut DeviceStep 1. Initialize AudioOut

Step 2. Add two buffers

Buffer1

Buffer2

Playerapplication

Page 29: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

29

AudioOut DeviceStep 1. Initialize AudioOut

Step 2. Add two buffers

Step 3. Start playback

Step 4. Buffer1 is read

Buffer1

Buffer2

Playerapplication

Page 30: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

30

AudioOut DeviceStep 1. Initialize AudioOut

Step 2. Add two buffers

Step 3. Start playback

Step 4. Buffer1 is read

Step 5. Event is fired Buffer2 is used by AudioOut

Buffer1

Buffer2

Event: Buffer end is reached

Playerapplication

Page 31: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

31

AudioOut DeviceStep 1. Initialize AudioOut

Step 2. Add two buffers

Step 3. Start playback

Step 4. Buffer1 is read

Step 5. Event is fired Buffer2 is used by AudioOut

Step 6. AudioOut reads Buffer2 and app writes to Buffer1

Buffer1

Buffer2

Playerapplication

Page 32: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

32

AudioOut DeviceStep 1. Initialize AudioOut

Step 2. Add two buffers

Step 3. Start playback

Step 4. Buffer1 is read

Step 5. Event is fired Buffer2 is used by AudioOut

Step 6. AudioOut reads Buffer2 and app writes to Buffer1

Buffer1

Buffer2

Event: Buffer end is reached

Step 7. Event is firedAudioOut switches to Buffer1

Playerapplication

Page 33: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Double buffering in AudioOut

33

AudioOut DeviceStep 1. Initialize AudioOut

Step 2. Add two buffers

Step 3. Start playback

Step 4. Buffer1 is read

Step 5. Event is fired Buffer2 is used by AudioOut

Step 6. AudioOut reads Buffer2 and app writes to Buffer1

Buffer1

Buffer2

Step 7. Event is firedAudioOut switches to Buffer1Step 8. Write to Buffer2

Playerapplication

Page 34: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Simple Mixing

34

Page 35: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Why mixing?

Play many sounds simultaneously– Limited number of player instances– System resource limited

Performance issue– Consume less computing power

Flexibility– Finer control of PCM data is possible

35

Page 36: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

.wav file

Important .wav header fields

36

Field Name

Chunk IDChuck Size

FormatSubchunk1 ID

Subchunk1 SizeAudio Format

Num ChannelsSample Rate

Byte RateBlock Align

Bits Per SampleSubchunk2 ID

Subchunk2 Size

Data

4

RIFFHeader444

WavFormatInfo

42244224

ChnkHeader4

Subc

hunk

2siz

e

Field Offset(bytes)

Field Size

(bytes)

Field ValueChannels mono/stereoSample rate 8/22/44.1KhzBit per sample 8bit /16bitSize raw data sizePCM data raw data

0

4

8

12

16

20

22

24

28

32

34

36

40

44

Page 37: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Loading .wav file

Open file

Read header

Read PCM data

File f;f.Construct(/*File Path*/, String(L"r"));

f.Read(&__RIFFHeader, sizeof(RIFFHEader));f.Read(&__WavFormat, sizeof(WavFormatInfo));

ChnkHeader tmp;f.Read((void *)&tmp, sizeof(ChnkHeader));__pData = new Osp::Base::ByteBuffer;__pData->Construct(tmp.size);f.Read(*__pData);__pData->Rewind();

37

Page 38: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Simple mixing

Step 1: Create buffers

Step 2: Add the buffers

__pTmpBuf = new ByteBuffer();__pTmpBuf->Construct(size*4); //int type

int* pTmp = static_cast<int *> (__pTmpBuf->GetPointer());

short* pData1 =static_cast<short *> (__pData1->GetPointer());

short* pData2 =static_cast<short *> (__pData2->GetPointer());

for (int i = 0; i < size; i++)pTmp[i] = pData1[i] + pData2[i] ;

38

Page 39: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Simple mixing

Step 3: Clip the buffers

Step 4: Write the buffer to AudioOut

short * pOut = static_cast<short *> (__pOutBuf->GetPointer());

for( int i=0;i<size;i++){

if (pTmp[i] >= SHRT_MAX) pOut[i] = SHRT_MAX*;else if ( pTmp[i] <= SHRT_MIN) pOut[i] = SHRT_MIN*;else pOut[i] = pTmp[i];

}

__pAO->WriteBuffer(*__pOutBuf);

39

*SHRT_MAX, SHRT_MIN: maximum and minimum values of short type

Page 40: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Image & Video management• Image

• Video

• Streaming

40

Page 41: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Image

Can encode, decode, convert images

Supported pixel format– RGB565/ARGB8888/R8G8B8A8

Decoding– BMP, GIF, PNG, TIFF, WBMP

Encoding– BMP, JPEG, PNG

"IMAGE" privilege is needed

41

Page 42: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Image class decode/encode

Load image

Save image

Bitmap* LoadBitmapN(const String& name){

Bitmap* pBitmap = null;Image* pImage = new Image();pImage->Construct();pBitmap = pImage->DecodeN(name,

BITMAP_PIXEL_FORMAT_ARGB8888);delete pImage;return pBitmap;

}

pImage->EncodeToFile(*pBitmap, IMG_FORMAT_PNG, name, true);

42

Page 43: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Video playing

Can play video using Player class– Using OverlayPanel to display video

Code snippet– Create player

– Open file

– Play/stop/pause/close

pPlayer = new Player();pPlayer->Construct(*pListener, &bufferInfo);

pPlayer->OpenFile(String(L"/Res/badaBI.mp4"));

pPlayer->Play(); //Pause() or Stop() or Close()

43

Page 44: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

OverlayPanel

Primarily used for the playback of camera previews or video– Foreground panel

• Overlay graphics and controls– Background buffer

• Support H/W accelerated rendering

44

Foreground panel

Input bufferMasking color

Background buffer

Page 45: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

OverlayPanel ctd.

Create a panel and get bufferInfo

Use the bufferInfo in the Player class

45

pPanel= new OverlayPanel();pPanel->Construct(rect);AddControl(*pPanel);pPanel->GetBackgroundBufferInfo(bufferInfo);

pPlayer->Construct(*pListener, &bufferInfo);

Page 46: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Streaming

Can play audio/video streaming content– Supported protocol : RTSP – Supported video codecs : MPEG4, H.263,

H.264 – Supported audio codecs : AAC, HE-AAC,

Enhanced-AAC+, AMR-NB

pPlayer->OpenURL(String(L“RTSP://IP address/content"));

46

Page 47: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Demo

MediaPlayer– Video Playback– OverlayPanel

47

Page 48: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Tips for Performance

Preffered PCM format to reduce internal operations

Double buffering to reduce latency

Sound mixing can reduce internal resources and event processing

Field ValuesChannels stereoSample rate 44.1KhzBit per sample 16bit

48

Page 49: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Tips for Programming

Handled automatically by platformHeadset • Plug and unplug events are not fired

• Platform changes the sound path automaticallyIncoming call

• Player, AudioIn, and AudioOut are stopped by platform• Resuming should be done by application

(Osp::App::Application::OnForeground)

49

Page 50: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Tips for Programming

50

Handled by applicationSilent mode • Event is not fired

• Application should check silent mode (Osp::System::SettingInfo )

Side volume key

• Application should deal with volume controls• Application handles key event

(Osp::Ui::IKeyEventListener)Foreground/ background

• Player consumes too much power• On background, stop playing audio and video

Page 51: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Summary

51

Page 52: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

What we have learned

Basic way to record and play audio– AudioRecorder / Player classes

Playing and recording PCM data– AudioIn / AudioOut classes– Wav file format / Simple mixing

Image and Video management– Image encode/decode – OverlayPanel for video– Streaming both audio and video

52

Page 53: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Find out more

Tutorial– bada Tutorial.Media.pdf

Samples – MediaPlayer– VoiceRecorder

53

Page 54: sounds in bada

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

Dive intohttp://www.goprodiver.com54