C++ Sample Code for Playing a WAV file with Pulse-Audio API (Tested on Ubuntu 11.1)

Introduction

Why we don't want to upgrade the operating system?  Because some software that was working flawlessly in an older version of operation system stops working.  Especially if it happens to a software that I am developing, that gives me a big headache.

When I was using Ubuntu 8.10, I was able to play a WAV file with a toolkit called OSS.  Not any more in Ubuntu 11.1.  Looks like now Ubuntu is using a toolkit called Pulse Audio.  (Which seemed to be my mistake.  I was told by a reader that the standard API is called ALSA.  But, I think you can easily set up Pulse-Audio in Ubuntu 11.1 anyway).  If I leave it as is, YSFLIGHT for Linux will have no sound effect.  I looked into Pulse Audio to recover sound effect in YSFLIGHT for Linux.

Google quickly found a reference manual of Pulse Audio for me.  But, I couldn't find a good sample code.  An audio-playback sample code available from the official Pulse Audio page was a synchronous playback example, which is not suitable for sound effect of a flight simulator.  You don't want the program to stop every time a sound effect is played.

With no choice, I jumped into the reference manual.  Turned out, it was straight forward.  At first, I didn't understand the concept of Main Loop, but it is reasonable.

In fact, Pulse Audio is much better than OSS.  Well, OSS was too obsolete, I guess.

But, if only an asynchronous playback example is available from the official site, someone may be having a same problem as mine.  So, I just made what I wrote for learning how to asynchronously play a WAV file available here.


By the way, most of the sample codes I found on the Internet required to a raw 22.5kHz stream.  How can I get such a raw data?  Maybe I need to rip it from a WAV file.  It seems to me more straight-forward if I can rather feed a WAV file directly.  Such an example should actually help many programmers.  And that's why I uploaded mine here.

Download

[Click Here to Download the Code]

Compile and Run

After downloading, extract all three files in the same directory.  Then type:

# g++ pulse_async_test.cpp yswavfile.cpp /I. -lpulse

I assume you have already installed Pulse Audio libraries and headers.  To run the program, type:

# ./a.out sampleWaveFile.wav

The program will play a WAV file specified as a first parameter for up to five seconds.  If you want to play it to the end, simply delete the check for five seconds in the code, and break the loop as soon as buffer underflow is detected.

Copyright

I retain the copyright of the materials available from this page.  But, you can use the source files included in the zip file freely, for commercial or non-commercial purposes.  However, I will not take responsibility for any consequence coming from my code.  If you cannot agree on this point, please do not use the sample code available from here.

[Update Information]

2012/02/19

Implementation of YsWavFile class in the previous version was too sloppy.  I made it work better.  Now you should be able to use Resample and ConvertToXX function.  Also NTimeStep and DataPointerAtTimeStep function may be useful if you use it with ALSA.

2012/02/09 [Download this version]

First release