Systems Programming 7. Sound file processing Guillaume Pierre Fall - - PDF document

systems programming
SMART_READER_LITE
LIVE PREVIEW

Systems Programming 7. Sound file processing Guillaume Pierre Fall - - PDF document

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2
  • 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)

slide-3
SLIDE 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

pressure Air Time

◮ How do you transform an analog wave into digital representation?

slide-4
SLIDE 4
  • 2. Basics of audio encoding

6

Audio Encoding

◮ Solution: sample the wave at regular intervals

pressure Air Time

10 7 −2 −7 −8 −6 6 9 −3 −3 4 7 10 10 9 5 −7 −11 −12 −12−11 2 17 16 12 −3 −8 −10−11 −10 −7

◮ 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

slide-5
SLIDE 5
  • 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)

slide-6
SLIDE 6
  • 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 left Sample2 left Sample2 right Sample1 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. . .

slide-7
SLIDE 7
  • 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.