Title: Linux Sound Programming
1Linux Sound Programming
Linux Sound Programming
Shuveb Hussain
2Objectives
Linux Sound Programming
- Understand Sound
- Understand Digitized Sound
- Explore the OSS API calls
- Write simple Programs to play Ogg Vorbis and MP3
files
Shuveb Hussain
3What is sound?
Linux Sound Programming
- Any disturbance that travels through an elastic
medium such as air, ground, or water - When a body vibrates, the oscillation causes a
periodic disturbance of the surrounding medium
that radiates outward in straight lines in the
form of a pressure wave .
Shuveb Hussain
4What is sound?
Linux Sound Programming
- The effect these waves produce upon the ear is
perceived as sound.
Shuveb Hussain
5Sound in Computers
Linux Sound Programming
- Sound is converted in a digital format by ADCs
- Sampling Pulse Code Modulation
Shuveb Hussain
6Bits and Channels
Linux Sound Programming
- The number of bits used for the sampling is the
resolution - The sampling rate determines the audio quality
- CD quality audio is sampled at 44,100 Hz
Shuveb Hussain
7Steps for Playing PCM Audio
Linux Sound Programming
1. Open the DSP Device 2. Set the playback
parameters. 3. Write the raw PCM data to the
handle obtained from step 1.
Shuveb Hussain
8The actual code
Linux Sound Programming
1. Open the DSP Device fdopen(/dev/dsp,O_WRONL
Y) 2. Tell the sound card if its Stereo or Mono
fdioctl(fd,SNDCTL_DSP_STEREO,channels) 3.
Inform the format (8 bit, 16 bit) or resolution
ioctl(fd, SNDCTL_DSP_SETFMT,format) 4. Set the
sampling rate ioctl(handle, SNDCTL_DSP_SPEED,rat
e) 5. Write the PCM Data to DSP. (Sweet music
plays) write(fd, data, rateSECSchannels)
Shuveb Hussain
9The problem with PCM audio
Linux Sound Programming
- To play 1 minute of CD Quality audio, you need
- 44,100 x 60 x 2 5292000 bytes, roughly 5 MB!
- (Sampling Rate x Time x Channels)
- Even Silence Takes up space.
- Bytes consumed is constant irrespective of the
sound
Shuveb Hussain
10Intelligent Compression
Linux Sound Programming
- There are certain sounds that the human ear
cannot hear. - There are certain sounds that the human ear
hears much better than others. - If there are two sounds playing simultaneously,
we hear the louder one but cannot hear the softer
one.
Shuveb Hussain
11Intelligent Compression
Linux Sound Programming
Shuveb Hussain
12Ogg Vorbis
Linux Sound Programming
- Free audio format for lossy music compression
- Compression comparable to MP3
- Audio quality often better than MP3
- Open CODEC and open implementation
Shuveb Hussain
13Ogg Vorbis Decoding with libvorbisfile
Linux Sound Programming
1. Declare struct OggVorbis_File vf 2. Open
Ogg Vorbis File ov_open(infile, vf, NULL, 0) 3.
Get File Info and Set DSP params vorbis_info
viov_info(vf,-1) 4. Decode and write to
DSP ov_read(vf,buf,sizeof(buf),0,2,1,cur_sec)
Shuveb Hussain
14MP3 Decoding with smpeg
Linux Sound Programming
1. Declare struct Allocate Space SMPEG mpeg
SMPEG_Info info mpeg SMPEG_new(argv1,
info, 1) 2. Setup DSP SMPEG_enableaudio(mpeg,
1) SMPEG_setvolume(mpeg, volume) 3. Play
SMPEG_play(mpeg) // Asynchronous 4.
Finish SMPEG_delete(mpeg)
Shuveb Hussain
15Thankyou!
Linux Sound Programming
References http//www.xiph.org/ogg/vorbis/ http/
/www.lokigames.com/development/smpeg.php3 http//w
ww.alsa-project.org/ http//www.opensound.com/linu
x.html Contact shuveb_at_winways.co.in
Shuveb Hussain