Presentation is loading. Please wait.

Presentation is loading. Please wait.

Audio 靜宜大學資工系 蔡奇偉 副教授 © 2011. 內容大綱 簡介 Recording Playback Sample Rate and Sample Quality Mono & Stereo Sound Sound Formats, Compression, and Codecs XNA.

Similar presentations


Presentation on theme: "Audio 靜宜大學資工系 蔡奇偉 副教授 © 2011. 內容大綱 簡介 Recording Playback Sample Rate and Sample Quality Mono & Stereo Sound Sound Formats, Compression, and Codecs XNA."— Presentation transcript:

1 Audio 靜宜大學資工系 蔡奇偉 副教授 © 2011

2 內容大綱 簡介 Recording Playback Sample Rate and Sample Quality Mono & Stereo Sound Sound Formats, Compression, and Codecs XNA Audio

3 Recording

4 Playback

5 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 。

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

7 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

8 Sound Formats, Compression, and Codecs

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

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

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

12 XNA Audio Microsoft.Xna.Framework.Audio Namespace Supports.wav,.wma, and.mp3 formats Simple Audio Playback SoundEffect Class SoundEffectInstance Class XACT (Cross-Platform Audio Creation Tool)

13 Simple Audio Playback Step 1: 把音效檔加入方案中。

14 Simple Audio Playback Step 2: 宣告 SoundEffect 與 SoundEffectInstance 物件 public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SoundEffect heartBeat; SoundEffectInstance heartBeatInst;

15 Simple Audio Playback Step 3: 讀入音效檔並建立其 instance protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); heartBeat = Content.Load ("heartbeat"); heartBeatInst = heartBeat.CreateInstance(); }

16 Simple Audio Playback Step 4: 播放 instance protected override void Update(GameTime gameTime) { GamePadState gamepad = GamePad.GetState(PlayerIndex.One); KeyboardState keyboard = Keyboard.GetState();... //A button or key plays sound effect if (gamepad.Buttons.A == ButtonState.Pressed) heartBeatInst.Play(); if (Keyboard.GetState().IsKeyDown(Keys.A)) heartBeatInst.Play();... }

17 SoundEffect Class Public Properties DistanceScale Gets or sets a value that adjusts the effect of distance calculations on the sound. S DopplerScale Gets or sets a value that adjusts the effect of doppler calculations on the sound. S Duration Gets the duration of the SoundEffect. MasterVolume Gets or sets the master volume that affects all SoundEffectInstance sounds. S Provides a loaded sound resource.

18 SoundEffectInstance Class 1/2 Provides a single playing, paused, or stopped instance of a SoundEffect sound. Public Properties IsLooped Gets a value that indicates whether looping is enabled for the SoundEffectInstance. Pan Gets or sets the panning for the SoundEffectInstance. Pitch Gets or sets the pitch adjustment for the SoundEffectInstance. State Gets the current state (playing, paused, or stopped) of the SoundEffectInstance. Volume Gets or sets the volume of the SoundEffectInstance.

19 SoundEffectInstance Class 2/2 Public Methods Pause Pauses a SoundEffectInstance. Play Plays or resumes a SoundEffectInstance. Resume Resumes playback for a SoundEffectInstance. Stop Overloaded. Stops playing a SoundEffectInstance.

20 參考資訊 Web Pages Wiki: Panning (audio) Wiki: Pitch (music)


Download ppt "Audio 靜宜大學資工系 蔡奇偉 副教授 © 2011. 內容大綱 簡介 Recording Playback Sample Rate and Sample Quality Mono & Stereo Sound Sound Formats, Compression, and Codecs XNA."

Similar presentations


Ads by Google