1 / 31

Audio (音效)

Audio (音效). 靜宜大學資工系 蔡奇偉 副教授 2008. 內容大綱. 簡介 Sampling SDL_mixer. 簡介. Recording. Playback. Sample Rate and Sample Quality. Sample Rate: 每秒取樣的個數,單位為 Hz 。常見值為 11025Hz, 22,050Hz, 或 44,100Hz (CD 音質 ) Sample Quality: 取樣值的 bits 數,通常為 8 或 16 bits 。.

bess
Download Presentation

Audio (音效)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 內容大綱 • 簡介 • Sampling • SDL_mixer

  3. 簡介

  4. Recording

  5. Playback

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

  7. 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, OggVorbis (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 (.mod .xm .s3m .669 .it .med 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. 開啟音效裝置 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. 取得音效裝置的格式 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 • intnumtimesopened, 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. 輸入音樂檔 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. 播放音樂 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. 停止播放音樂 Halt playback of music. This interrupts music fader effects. Returns: always returns zero.

  25. 釋放音樂資源 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. 終止播放音樂後的自動執行函式 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_Mixerfunctions, nor SDL_LockAudio, from a callback function.

  27. 範例 #include <stdio.h> #include <stdlib.h> #include <SDL/SDL.h> #include <SDL/SDL_mixer.h> /* Mix_Music actually holds the music information. */ Mix_Music *music = NULL; void handleKey(SDL_KeyboardEvent key); void musicDone(); int main (intargc, 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 */ intaudio_rate = 22050; Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ intaudio_channels = 2; intaudio_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; • }

More Related