0 Systems Programming 7. Sound file processing Guillaume Pierre Fall 2007 http://www.cs.vu.nl/ ∼ gpierre/courses/sysprog/ 1 Table of contents 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . 2 • 2. Basics of audio encoding . . . . . . . . . . . . . . . . 4 • 3. Manipulating WAV files . . . . . . . . . . . . . . . . . 7 •
1. Introduction 2 1. Introduction 1. Introduction 3 Introduction ◮ Assignments 2 and 3 will make you manipulate sound files . ⊲ How do you do that? ◮ Do not panic, it is not as hard as it looks! ⊲ We have built a small library to handle parts of the work ⊲ But you need to understand WAV file format to process sound (in assignment 3)
2. Basics of audio encoding 4 2. Basics of audio encoding 2. Basics of audio encoding 5 Audio Encoding ◮ Sound is a mechanical wave Air pressure Time ◮ How do you transform an analog wave into digital representation?
2. Basics of audio encoding 6 Audio Encoding ◮ Solution: sample the wave at regular intervals 17 16 Air pressure 12 10 10 9 9 7 10 7 6 5 4 Time 2 −2 −3 −3 −3 −6 −7 −8 −7 −7 −8 −11 −12 −12−11 −10−11 −10 ◮ An audio file contains a list of air pressure values ⊲ Here: 10, 7, -2, -7, -8, -6, 6, 9, -3, -3, 4, etc. ◮ Most audio formats compress these values to gain space ⊲ But WAV contains the samples one after the other with no compression whatsoever (simpler!) 3. Manipulating WAV files 7 3. Manipulating WAV files
3. Manipulating WAV files 8 WAV header ◮ Any audio file start with a header to explain how it was encoded: ⊲ How many channels? (1=mono; 2=stereo) ⊲ Sampling frequency? (Audio CDs are encoded at 44100Hz; other WAV files may use different frequencies) ⊲ How do you store one sample? (8 bits unsigned int vs. 16 bits signed int) ◮ Note: all values are stored in Intel x86 byte order (i.e., little-endian) ⊲ You can use them without conversion on Intel-based machines! 3. Manipulating WAV files 9 Reading a WAV file [1/2] ◮ The audio library contains a function to help you: #include "audio.h" int aud_readinit(char *filename, int *sample_rate, int *sample_size, int *channels); ⊲ Give the name of the file to open (filename) ⊲ Give pointers to three integers ⊲ The function will: 1. Open the file for you 2. Read the WAV header, store the sample rate, sample size and number of channels into your variables 3. Return the file descriptor to you (pointing just after the WAV header)
3. Manipulating WAV files 10 Reading a WAV file [2/2] ◮ You can then read the audio file yourself: ⊲ Mono: Sample1 Sample2 Sample3 Sample4 1 or 2 bytes ⊲ Stereo: Sample1 Sample1 Sample2 Sample2 left right left right 1 or 2 bytes ◮ Don’t forget to close the file when you are finished! 3. Manipulating WAV files 11 Playing a WAV file ◮ You must first open the audio device and declare which kind of WAV file you are going to give: #include "audio.h" int aud_writeinit(int sample_rate, int sample_size, int channels); ⊲ Pass the sample rate, sample size, number of channels that the audio device should expect ⊲ Attention: you cannot pass any value and expect the audio device to like it. If you pass “strange” values, the device will select something close for you. . . ◮ The function returns a file descriptor ⊲ Just write your samples into it! ⊲ And don’t forget to close it when you are finished. . .
3. Manipulating WAV files 12 Processing an audio file ◮ You will be expect to build filters to process the sound 1. Easy: lie to the audio device ⊲ The file is encoded at 44100Hz but you pretend it is 22050Hz ⇒ The audio device will play the audio two times slower! (but this will not give you a lot of points for your assignment) 2. More interesting: manipulate the samples! ⊲ To adjust volume: multiply samples by a given factor ⊲ Swap the two channels ⊲ Add echo ⊲ Compress the data ⊲ Etc.
Recommend
More recommend