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

Slides:



Advertisements
Similar presentations
How a MP3 Player Works and Operates. By: Kyle Anderson & Michael Theis.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Digital Audio Teppo Räisänen LIIKE/OAMK. General Information Auditive information is transmitted by vibrations of air molecules The speed of sound waves.
Audio Programming With Python Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Multimedia Authoring1 Introduction to Garageband Garageband is both a: MIDI sequencer Digital audio recorder Garageband: Real Instruments Tracks displayed.
Pro Tools 7 Session Secrets Chapter 6: After the Bounce or Life Outside of Pro Tools Life Outside of Pro Tools.
A stereo audio file 1. Audio Channels Number of audio channels determines number of waveforms in a recording Two relevant types of recording Stereo recording.
Sound in multimedia How many of you like the use of audio in The Universal Machine? What about The Universal Computer? Why or why not? Does your preference.
Sound can make multimedia presentations dynamic and interesting.
4.1Different Audio Attributes 4.2Common Audio File Formats 4.3Balancing between File Size and Audio Quality 4.4Making Audio Elements Fit Our Needs.
Digital Audio Production Munsang College Information and Communication Technology S2.
GCSE Computing#BristolMet Session Objectives#10 MUST define the term sample rate COULD explain how sound can be sampled and stored in digital form SHOULD.
1 Multimedia on the Web: Issues of Bandwidth Bandwidth is a measure of the amount of data that can be sent through a communication pipeline each second.
Free open source audio recording and editing software 1Using Audacity.
Part A Multimedia Production Rico Yu. Part A Multimedia Production Ch.1 Text Ch.2 Graphics Ch.3 Sound Ch.4 Animations Ch.5 Video.
Chapter 4: Representation of data in computer systems: Sound OCR Computing for GCSE © Hodder Education 2011.
Audio 2 Subject:T0934 / Multimedia Programming Foundation Session:9 Tahun:2009 Versi:1/0.
I Power Higher Computing Multimedia technology Audio.
Technology ICT Option: Audio.
Audio 靜宜大學資工系 蔡奇偉 副教授 © 內容大綱 簡介 Recording Playback Sample Rate and Sample Quality Mono & Stereo Sound Sound Formats, Compression, and Codecs XNA.
C Programming - Lecture 5
The Online Guide to Creating Podcasts Presented by The Portland Public Schools Multimedia Library Recording and Saving your Podcast.
Image and Sound Editing Raed S. Rasheed Digital Sound Digital sound types – Monophonic sound – Stereophonic sound – Quadraphonic sound – Surround.
Audio Basic Concepts. Audio in Multimedia Digital Audio: Sound that has been captured or created electronically by a computer In a multimedia production,
Image and Sound Editing Raed S. Rasheed Sound What is sound? How is sound recorded? How is sound recorded digitally ? How does audio get digitized.
Java Audio.
AUDIO VIDEO FLASH DIGITAL MEDIA: COMMUNICATION AND DESIGN
Chapter 9 Audio.
Sound Chapter Types of Sound Waveforms MIDI Sound is related to many things in computers but only Wav and MIDI exist in PCs.
Digital Audio Multimedia Systems (Module 1 Lesson 1)
Representing Sound in a computer Analogue  Analogue sound is produced by being picked up by a transducer (microphone) and converted in an electrical current.
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
Representation of Data in Computer Systems
 Continuous sequence of vibrations of air  (Why no sound in space? Contrary to Star Wars etc.)  Abstraction of an audio wave:  Ear translates vibrations.
Media File Formats Jon Ivins, DMU. Text Files n Two types n 1. Plain text (unformatted) u ASCII Character set is most common u 7 bits are used u This.
Introduction to Interactive Media 10: Audio in Interactive Digital Media.
GODIAN MABINDAH RUTHERFORD UNUSI RICHARD MWANGI.  Differential coding operates by making numbers small. This is a major goal in compression technology:
COMP Representing Sound in a ComputerSound Course book - pages
Digital Music to Sheet Music Hugh Smith. Abstract Electronic music has been steadily expand- ing over the past years. Many file formats have come into.
Audio. Why Audio Essential tool for – Interface – Narrative – Setting & Mood.
Audio and Video CGS Some Common Audio Formats Format Use Extension MIDI instrumental music.mid MPEG songs.mp3 RealAudio live broadcasts.ra Wave.
Playing Music in Alice By David Yan Under the direction of Professor Susan Rodger July 2015.
Chapter 11 Audio © 2013 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in.
AUDIO MEDIA 1 Created } “Borrowed” } Microphone MIDI keyboard CD’s & flash drives Internet Audio Sources 2.
Signal Digitization Analog vs Digital Signals An Analog Signal A Digital Signal What type of signal do we encounter in nature?
Sound or Audio, whichever you prefer –MIDI Files.midi or.mid (Musical Instrument Digital Interface) use for instrumental music. –This format is supported.
Overview of Multimedia A multimedia presentation might contain: –Text –Animation –Digital Sound Effects –Voices –Video Clips –Photographic Stills –Music.
Anatomy of a Sound File v © Allan C. Milne Abertay University.
Chapter 15 Recording and Editing Sound. 2Practical PC 5 th Edition Chapter 15 Getting Started In this Chapter, you will learn: − How sound capability.
Playing Music in Alice By David Yan Under the direction of Professor Susan Rodger July 2015.
Digital Recording. Digital recording is different from analog in that it doesn’t operate in a continuous way; it breaks a continuously varying waveform.
Introduction to Digital Media. What is it? Digital media is what computers use to; Store, transmit, receive and manipulate data Raw data are numbers,
Digital Music to Sheet Music Hugh Smith. Abstract Electronic music has been steadily expand- ing over the past years. Many file formats have come into.
Media Types Information Systems can contain the following types of media: Sound, graphics, video & text.
Sound and the Web. Transferring sound Download Progressive Download Stream.
Sound. Sound Capture We capture, or record, sound by a process called sampling: “measuring” the sound some number of times per second. Sampling rate is.
How to Create a Podcast. Podcasting “is the distribution of audio or video files, such as radio programs or music videos, over the Internet using either.
Audio Formats. Digital sound files must be organized and structured so that your media player can read them. It's just like being able to read and understand.
Editing Digital AudioLab#7 Audacity is a free, easy-to-use and an open source platform audio editor and recorder for Windows, Mac OS, Linux and other operating.
Chapter 15 Recording and Editing Sound
3.3 Fundamentals of data representation
Image and Audio File Formats
Multimedia: Digitised Sound Data
Multimedia: making it Work
Technology ICT Option: Audio.
Digital Music Scott Bennett 1/3/2019.
Assist. Lecturer Safeen H. Rasool Collage of SCIENCE IT Dept.
Technology ICT Option: Audio.
Chapter 11 Audio © 2013 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in.
C Programming - Lecture 5
Presentation transcript:

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

內容大綱 簡介 Sampling SDL_mixer

簡介

Recording

Playback

Sample Rate and Sample Quality 7, 9, 11, 12, 13, 14, 14, 15, 15, 15, , 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 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...

開啟音效裝置 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

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

關閉音效裝置 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. 範例

取得音效裝置的格式 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.

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

播放音樂

輸入音樂檔 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.

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

播放音樂 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.

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

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

釋放音樂資源 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... 範例

終止播放音樂後的自動執行函式 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.

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