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

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.

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)

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

Related

OpenAL C++ on Linux

I just need a simple program which allows me to play and stop an audio file. I'm guessing OpenAL is the way to go? All I need is this functionality - start audio file with spacebar and stop audio file with a second press of the spacebar. I notice the OpenAL documentation is quite involved. Can someone point me to something really simple in order to just get the start/stop functionality of a .wav file?
OpenAL may not be the simplest choice. If you use a gui-framework like QT, check for what their ecosystems provide for playing sound (e.g. qsound). Another choice may be Allegro which may feel more straightforward.
For OpenAL, there is a working example to play a wav in the example repositories. Playback can be paused using alSourcePause.
Edit:
For choosing libraries I like to consult Awesome-cpp. The simple_playback.c example of mini_al looks extremely straightforward. Simply use mal_device_stop(&device) to pause the running playback and mal_device_start(&device) to continue. Works perfectly on my machine and seems very portable.

Cross-platform real-time MP3 recorder

I have an SDL app, that works under Linux, Mac and Windows. It's something like a media player, and can play audio just fine. I'd like to add audio recording feature to it, but I'd like to encode it in real time to MP3. Can anyone point me to an example how can I use LibLame, LibSoX, or possibly some other library to achieve this?
-- OR --
I'm also willing to rewrite the whole thing into something easier to manage than C++. I've looked at Kivy and Love2d which uses Lua, but audio recording it's still an issue there. If you know ANY toolkit that:
is cross platform
helps you build GUI using your own graphics
can play AND record mp3 files
ideally can operate under framebuffer (no X Window server under Linux)
Please let me know. I'm looking at Python + Pygame + Pyaudio, it can do graphics and output sound, but still can't record MP3's, only WAV's. Any way to integrate LAME into this to make it work?
FMOD can play practically anything, and handle audio input as well, although I don't know if integrating an entire audio engine is a bit overkill for your project.
It's free for non-commercial usage.
As for encoding, LAME is definitely the de-facto choice for MP3.
There's a very simple library called lame_enc.dll which wraps LAME's capabilities in a simple API. It's Windows only, but you could look at it's source for a good reference on how to use LAME.

How to play sound files on multiple OSes in C++?

I am looking for a portable (Linux, Windows, Mac OS X) way to play either OGG or AAC (and WAV) files.
I have tried closed source options like FMOD but the license is too costly.
I have tried open source projects like Audiere but it doesnt work well in post OSS Linux distros and has not been updated, according to the site since 2006.
I have considered trying to find a solution for each platform and just abstract them in a class, but I havent found info on the best way to do this on each platform.
I do not need any special functionality, i just want to play/stop the audio files. (In C++)
Any recommendations?
How about OpenAL?
http://connect.creativelabs.com/openal/
Getting audio out to the device portably can be done using PortAudio. For a fully decoding and playback solution have a look at GStreamer.
Consider GStreamer, a cross-platform solution for dealing with multimedia stuff. There are C++ bindings through QtGstreamer.
This post discuss the setup on Windows.

Best practice for C++ audio capture API under Linux?

I need to create a C++ application with a simple audio recording from microphone functionality.
I can't say that there aren't enough audio APIs to do this! Pulse, ALSA, /dev/dsp, OpenAL, etc. My question is what is the current "Best practice" API?
Pulse seems supported by most modern distros, but seems almost devoid of documentation.
Will OpenAL be supported across different distros, or is it too obscure?
Have I missed any?
Is there not a simple answer?
Lennart Pottering has a guide here:
http://0pointer.de/blog/projects/guide-to-sound-apis
basically use the 'safe' subset of alsa then all the other important ones should work too.
Barring that OpenAL works on Linux
EDIT:
Also pulseaudio has reasonably good doxygen generated documentation here and some other stuff here and some of the utilities are good examples, you can see them in gitweb here
Also I've written some code that uses pulse for audio input you can look at it here, however I'm not sure I handle everything totally correctly, and I know I'm missing code to handle certain situations... but that should get you started should you go with pulse.
there is no simple answer; best practise depends on the context and specific use cases that your application will be exposed to.
for the greatest ease of programming and widest audience, i would choose gstreamer as it is quick and straightforward, well integrated in gnome and ubuntu and supports a wide range of audio subsystems and configurations. it is very flexible.
for example, the command:
gst-launch autoaudiosrc ! audioconvert ! flacenc ! filesink location="foo.flac"
will create a .flac file from microphone input, regardless of the audio system (Pulse, ALSA, ESD ...). an equivalent pipeline can be written using the c / gobject bindings as documented here.
p.s the above command requires the gstreamer-tools package
GStreamer will allow you and the end user the most flexibility with regards to sound capture, but its cross-platform viability is not that great, particularly on OS X.

Simple cross-platform free audio library for raw PCM?

I'm writing a cross-platform Qt-based program that from time to time needs to play back audio supplied externally (outside my control) as raw PCM. The exact format is 16 bit little-endian PCM at various common sample rates.
My first obvious idea was to use Qt's own Phonon for audio playback, but there are two problems with this approach:
As far as I can see, Phonon does not support headerless PCM data. I would have to hack around this and fake a WAV header every time playback starts. Not a showstopper, though.
More importantly: There doesn't seem to be any way to control how Phonon (and its backends such as xine, PulseAudio, DirectX, whatever) prebuffers. Its default behaviour seems to be something like 5 seconds of prebuffering, which is way too much for me. I'd prefer about 1 second, and I'd definitely like to be able to control this!
I'm currently looking at Gstreamer, FFMPEG and libvlc. Any thoughts? Since my audio is in a very simple format and I don't need to do fancy mixing stuff (just volume control), I'd like a simple, free (as in freedom), cross-platform and widely available library.
Qt 4.6 has the new QtMultimedia module.
https://doc.qt.io/archives/4.6/qtmultimedia.html
The QAudioOutput class would seem to do what you want - it just plays raw PCM data.
ffmpeg, libvlc and gstreamer have abilities beyond raw pcm, such as codec support.
For your purposes, SDL (example 1, example 2), OpenAL, QAudioOutput are sufficient. SDL is probably the most popular option.
Also, why do you want to control buffering? Buffering a lot means less interrupts and lower power consumption.
Have you looked at OpenAL?