Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I play .wav files in C++ on Linux?

Tags:

I have a question about playing .wav files in C++. I search for a method to play it on Linux and Windows and I want to break up it while a function has been ended, did you know a sample for this?

Thanks for any Answer :)!

I am from Germany, please don't be angry about my grammar or spelling :D.

like image 946
Survari Avatar asked Jun 24 '16 04:06

Survari


2 Answers

There are several ways to do this.

The simplest, ugliest and most hackish way to do this is to write directly to your sound device located in /dev/snd/. However this really depends on your linux distribution and some modern ones no longer allows you to do this. In general, direct read / write to /dev/ devices is fading away. Here is a question answering this.

For a very long time, the "official" way was to use alsa library which uses a C style interface. It is usually pretty tricky, full of pitfalls and "workarounds" that depends on your exact audio hardware.

I think that alsa is getting gradually replaced by jack, which I hope is easier to use. I don't have any experience with this one.

The last method I know is with portaudio, which as the name implies, is somewhat portable between at least windows, linux and osx.

All of these library will allow you to control your audio hardware, init / setup / play. What is simple about wav files is that the content is raw pcm, which is usually the format used by those libraries. The wav format is usually like this :

[wav header] [audio data until the end of the file]

If you can manage a few milliseconds of garbage when you start playing, you can "safely" send the header for playback as well and avoid parsing it. You will have to understand PCM formats however and should bring some additional readings.

As an added "trick" which doesn't directly concern C++, I strongly suggest using Audacity. It is a small program which I see like the notepad / vim of audio files. It is small, it loads fast, allows you to copy / paste segments of audio and detect pcm formats. (Actually, you just change pcm settings until the audio "sounds" right, but still useful)

like image 178
Eric Avatar answered Sep 29 '22 07:09

Eric


The title mentioned Linux, but then you mentioned Windows and Linux in the post.

  • For Linux, best is to use gstreamer if you insist on C++. Look through the gstreamer code for gst-launch. It is run as below in a Linux machine.

    $ gst-launch filesrc location="location of file" ! wavparse ! alsasink

From, http://sanchayanmaity.github.io/beagleboard-xm/linux/programming/software/2014/08/07/playing-wavmp3-file-using-gstreamer-in-code.html

  • For windows, or if you want to use OS agnostic code on both Windows and Linux, you can use SDL,

http://lazyfoo.net/SDL_tutorials/lesson11/

  • Another alternative (cross-platform, Object oriented), is SFML. Check the audio wav file playback example at,

    http://www.sfml-dev.org/tutorials/2.0/audio-sounds.php

like image 38
Prabindh Avatar answered Sep 29 '22 05:09

Prabindh