Presentation is loading. Please wait.

Presentation is loading. Please wait.

Audio (音效) 靜宜大學資工系 蔡奇偉 副教授 2008. 內容大綱 簡介 Sampling SDL_mixer.

Similar presentations

Presentation on theme: "Audio (音效) 靜宜大學資工系 蔡奇偉 副教授 2008. 內容大綱 簡介 Sampling SDL_mixer."— Presentation transcript:

1 Audio (音效) 靜宜大學資工系 蔡奇偉 副教授 2008

2 內容大綱 簡介 Sampling SDL_mixer

3 簡介

4 Recording

5 Playback

6 Sample Rate and Sample Quality 7, 9, 11, 12, 13, 14, 14, 15, 15, 15, 14 0111, 1001, 1011, 1100, 1101, 1110, 1110, 1111, 1111, 1111, 1110 Sample Rate: 每秒取樣的個數,單位 為 Hz 。常見值為 11025Hz, 22,050Hz, 或 44,100Hz (CD 音質 ) Sample Quality: 取樣值的 bits 數,通常 為 8 或 16 bits 。

7 11025 Hz22050 Hz44100 Hz 8 bits11025 byte/s22050 Byte/s44100 byte/s 16 bits22050 byte/s44100 byte/s88200 byte/s Why 44100 Hertz? Why are CDs sampled at 44,100 Hertz, anyway? It seems like such an odd number. To answer this question, we have to dive back into audio history. Before CDs, digital audio was stored on video tape—a hack rivaling the best of them. The tapes were designed to store and play back 60 frames per second of video data. Each frame of video had 245 lines, and each line had three samples (for red, green, and blue). That gives us 245 × 3 × 60, or 44,100 samples.

8 Mono & Stereo Sound The Story behind CDs My father-in-law, a musician, once told me that the reason CDs hold 74 minutes of music is because the powers that be wanted to listen to Beethoven's Ninth Symphony in its entirety, without interruption. The engineers, always anxious to please, calculated the length of this symphony as 74 minutes, and came up with a physical specification for a disc that could store that much audio. I find it fascinating that a classical musician who died several hundred years ago had a very large hand in shaping one of today's most omnipresent pieces of audio technology. -- Mason McCuskey 單聲道與立體聲

9 Sound Formats, Compression, and Codecs

10 MP3 is a "lossy" compression algorithm, which means that some information is lost when you compress a WAV into MP3. MP3 works by making sure that the information that's lost is information you can probably live without. For example, a lot of high frequency things, such as cymbal crashes and vocalizations of the hiss of an "s" sound or a crisp "k" sound, are lost. Usually this loss is imperceptible to the average listener, but with a good pair of speakers and a keen ear, you can hear the difference. Try it sometime—go to a quiet place, and listen to your favorite song on CD, then listen to the same song in MP3. If your speakers are good and you're young or have taken good care of your ears, you'll be able to hear the difference.

11 Other compression formats are out there. There's ADPCM, an acronym for Advanced Differential Pulse Code Modulation, Ogg Vorbis (an open source, patent-free audio compression algorithm that's quickly gaining popularity), and several lesser- known formats. These pieces of code that implement these algorithms are called codecs, an acronym for compressor/decompressor (don't you love all these audio acronyms?). Contained in a WAV file is the name of the codec it was compressed with; Windows provides several of the most common codecs, and automatically uses the right one based on the tag in the WAV file. If you try to play a WAV file that contains a tag for a codec not installed on your system, you won't be able to until you hunt down the codec it was made with. Happily, this is not a common occurance—99 percent of the WAVs out there are PCM or ADPCM.

12 Mixing Sound Often you'll want to play more than one sound effect at once, and that's where audio mixing comes in. The easiest way to play two sounds at the same time is simply to add their samples together.

13 SDL_mixer Jonathan Atkins supports the following formats: - WAVE/RIFF (.wav) - AIFF (.aiff) - VOC (.voc) - MOD ( and more - MIDI (.mid) using timidity or native midi hardware - OggVorbis (.ogg) requiring ogg/vorbis libraries on system - MP3 (.mp3) requiring SMPEG library on system - also any command-line player, which is not mixed by SDL mixer...

14 開啟音效裝置 int Mix_OpenAudio int frequencyOutput sampling frequency in samples per second (Hz). you might use MIX_DEFAULT_FREQUENCY (22050) since that is a good value for most games. Uint16 formatOutput sample format: AUDIO_U8, AUDIO_S8, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB,, AUDIO_U16, AUDIO_S16,, AUDIO_U16SYS, AUDIO_S16SYS, MIX_DEFAULT_FORMAT (= AUDIO_S16SYS) int channels Number of sound channels in output 。 Set to 2 for stereo, 1 for mono. int chunksizeBytes used per output sample. Returns: 0 on success, -1 on errors

15 // start SDL with audio support If (SDL_Init(SDL_INIT_AUDIO)==-1) { printf("SDL_Init: %s\n", SDL_GetError()); exit(1); } // open 44.1KHz, signed 16bit, system byte order, // stereo audio, using 1024 byte chunks If (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) { printf("Mix_OpenAudio: %s\n", Mix_GetError()); exit(2); } 範例

16 關閉音效裝置 void Mix CloseAudio () Shutdown and cleanup the mixer API. After calling this all audio is stopped, the device is closed, and the SDL mixer functions should not be used. Mix_CloseAudio(); // you could SDL_Quit(); here...or not. 範例

17 取得音效裝置的格式 int Mix_ QuerySpec int frequencyA pointer to an int where the frequency actually used by the opened audio device will be stored. Uint16 formatA pointer to a Uint16 where the output format actually being used by the audio device will be stored. int channelsA pointer to an int where the number of audio channels will be stored. 2 will mean stereo, 1 will mean mono. Get the actual audio format in use by the opened audio device. This may or may not match the parameters you passed to Mix_OpenAudio. Returns: 0 on error. If the device was open the number of times it was opened will bereturned.

18 // get and print the audio format in use int numtimesopened, frequency, channels; Uint16 format; numtimesopened=Mix_QuerySpec(&frequency, &format, &channels); if(!numtimesopened) { printf("Mix_QuerySpec: %s\n",Mix_GetError()); } else { char *format_str="Unknown"; switch(format) { case AUDIO_U8: format_str="U8"; break; case AUDIO_S8: format_str="S8"; break; case AUDIO_U16LSB: format_str="U16LSB"; break; case AUDIO_S16LSB: format_str="S16LSB"; break; case AUDIO_U16MSB: format_str="U16MSB"; break; case AUDIO_S16MSB: format_str="S16MSB"; break; } printf("opened=%d times frequency=%dHz format=%s channels=%d“, numtimesopened, frequency, format_str, channels); } 範例

19 播放音樂

20 輸入音樂檔 Mix_Music *Mix_LoadMUS const char *fileName of music file to use. Load music file to use. This can load WAVE, MOD, MIDI, OGG, MP3, and any file that you use a command to play with. Returns: A pointer to a Mix Music. NULL is returned on errors.

21 // load the MP3 file "music.mp3" to play as music Mix_Music *music; Music = Mix_LoadMUS("music.mp3"); if(!music) { printf("Mix_LoadMUS(\"music.mp3\"): %s\n", Mix_GetError()); // this might be a critical error... } 範例

22 播放音樂 int Mix_PlayMusic Mix_Music *musicPointer to Mix Music to play. int loopsnumber of times to play through the music. 0 plays the music once -1 plays the music forever Play the loaded music loop times through from start to finish. The previous music will be halted, or if fading out it waits (blocking) for that to finish. Returns: 0 on success, or -1 on errors.

23 // play music forever // Mix_Music *music; // I assume this has been loaded already if (Mix_PlayMusic(music, -1)==-1) { printf("Mix_PlayMusic: %s\n", Mix_GetError()); // well, there’s no music, but most games don’t break // without music... } 範例

24 停止播放音樂 int Mix_HaltMusic () Halt playback of music. This interrupts music fader effects. Returns: always returns zero.

25 釋放音樂資源 void Mix_FreeMusic Mix_Music *musicPointer to Mix Music to free. Free the loaded music. If music is playing it will be halted. If music is fading out, then this function will wait (blocking) until the fade out is complete. // free music Mix_Music *music; Mix_FreeMusic(music); music=NULL; // so we know we freed it... 範例

26 終止播放音樂後的自動執行函式 void Mix_HookMusicFinished void (*music_finished)Function pointer to a void function(). NULL will remove the hook. This sets up a function to be called when music playback is halted. Any time music stops, the music finished function will be called. Call with NULL to remove the callback. NOTE: NEVER call SDL_Mixer functions, nor SDL_LockAudio, from a callback function.

27 #include /* Mix_Music actually holds the music information. */ Mix_Music *music = NULL; void handleKey(SDL_KeyboardEvent key); void musicDone(); int main (int argc, char **argv) { SDL_Surface *screen; SDL_Event event; int done = 0; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); 範例

28 /* We're going to be requesting certain things from our audio device, so we set them up beforehand */ int audio_rate = 22050; Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ int audio_channels = 2; int audio_buffers = 4096; /* This is where we open up our audio device. Mix_OpenAudio takes as its parameters the audio format we'd like to have. */ if( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { printf("Unable to open audio!\n"); exit(1); } /* If we actually care about what we got, we can ask here. In this program we don't, but I'm showing the function call here anyway in case we'd want to know later. */ Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); Mix_HookMusicFinished(musicDone);

29 /* We're going to be using a window onscreen to register keypresses in. We don't really care what it has in it, since we're not doing graphics, so we'll just throw something up there. */ screen = SDL_SetVideoMode(320, 240, 0, 0); while(!done) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: case SDL_KEYUP: handleKey(event.key); break; } /* So we don't hog the CPU */ SDL_Delay(50); } /* This is the cleaning up part */ Mix_CloseAudio(); SDL_Quit(); return 0; }

30 void handleKey(SDL_KeyboardEvent key) { switch(key.keysym.sym) { case SDLK_m: if(key.state == SDL_PRESSED) { if(music == NULL) { music = Mix_LoadMUS("music.ogg"); Mix_PlayMusic(music, 0); } else { Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; } break; }

31 /* This is the function that we told SDL_Mixer to call when the music was finished. In our case, we're going to simply unload the music as though the player wanted it stopped. In other applications, a different music file might be loaded and played. */ void musicDone() { Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; }

Download ppt "Audio (音效) 靜宜大學資工系 蔡奇偉 副教授 2008. 內容大綱 簡介 Sampling SDL_mixer."

Similar presentations

Ads by Google