Presentation is loading. Please wait.

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

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

Audio （音效） 靜宜大學資工系 蔡奇偉 副教授 2008

Recording

Playback

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 。

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.

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 單聲道與立體聲

Sound Formats, Compression, and Codecs

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.

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.

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.

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...

// 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); } 範例

// 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); } 範例

// 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... } 範例

// 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... } 範例

#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); 範例

/* 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);

/* 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; }

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; }

/* 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