Working with audio in C++ - c++

I need to be able to get the length of an audio file (preferably in milliseconds, the timing is very important for the application), and play back the audio. The application is written in C++ on the windows XP platform. Any suggestions for audio formats/3rd party libraries that would do the trick?

If I have to do anything related to audio, on any platform, I just use FMOD. It's very fast, cross-platform, and supports a wide range of formats. Note that it's not free for commercial uses.

The Phonon library (part of Qt) is very powerful and comfortable.
It is LGPL.
Downside: it's not as performant as some of the game-oriented libraries such as FMod, SDL_Mixer and OpenAL. However, the latency is generally acceptable for desktop apps.
As for audio formats: use Ogg Vorbis by default. It's an open standard, royalty free and has better quality than MP3.

Related

Library for reading audio files

I want to process audio online/live where I constantly read audio samples from an audio file, process these (e.g. apply some effect), and forward the processed samples to an audio output device like a soundcard. The input files have common formats such as wav, mp3, perhaps even ogg.
Is there a library available similar to libav/ffmpeg for audio files which simplifies reading various audio formats and provides me a constant stream of raw audio samples? Or is the best solution to use individual libraries for each format?
The libraries should be c/c++ and cross-plattform compatible (Mac, Win, Linux, ARM).
EDIT Thanks for all answers. I have evaluated all libraries and came to the conclusion that it is best to just stick with libav/ffmpeg because most of the libraries require ffmpeg as a backend.
Check out Juce. It is a big library that has been used to develop VST audio plug-ins for music software. There is a lot of stuff you don't need in there, but I think you can pick and choose only the audio parts to include in your build. The AudioFormatReader and its associated classes can do the file reading, and there are also classes for outputting to the sound card. There's a lot of other audio processing tools as well. It's GPL licensed, cross platform, and they claim experimental Android support. I haven't used it for a project yet, but I am waiting for the inspiration!
I can recommend RtAudio or PortAudio for cross-platform audio I/O. For audio decoding you might want to have a look at libsndfile or libaudiodecoder.
I'd check out libSDL, it has an audio subsystem that is built for doing things like that and handles ogg,mp3,flac,wav, etc..
LibVLC can do this. libvlc supports the most various audio (and video) formats. It's a C/C++ crossplatform library. It should also support Arm code generation.
You can use irrKlang library. I have used it for my games. It is very simple library to use, for example to play some file "somefile.mp3" you just need to write
engine->play2D("somefile.mp3", true);
And this library is cross-platform, too. And works with C++, C# and all .NET languages.
More features of this library (from its own site)
It has all the features known from low level audio libraries as well
as lots of useful features like a sophisticated streaming engine,
extendable audio reading, single and multithreading modes, 3d audio
emulation for low end hardware, a plugin system, multiple rolloff
models and more. All this can be accessed via an extremely simple API.
GAudio Library maybe is one you persuit.
It is simple, powerfull, cross-platform and extendable
The hello world of GAudio like this:
gaudio_init("addons");
const char* filename = "..\\media\\trek12.wav";
gsource* source = gaudio_source_create_from_file(filename,FALSE);
if(source == NULL)
{
printf("load file:%s error!\n",filename);
printf("bad source.\nerror code:%d.\n",gaudio_error_get());
gaudio_deinit();
return -1;
}
printf("play filename:%s\n",filename);
gaudio_source_play(source,FALSE);
printf("\nplaying, press any key to quit.\n");
getch();
gaudio_source_stop(source);
gaudio_source_destroy(source);
gaudio_deinit();

Capture video to a file from webcam using C++ (MFC)

I need to add webcam video capture to a legacy MFC C++ application. The video needs to be saved as MP4. Did a bit of googling but didn't come across anything that looked promising. Any suggestions on the best approach?
EDIT:
Windows platform.
EDIT:
Must be compatible with XP
There are a few popular options to choose from:
DirectShow API - it does not have stock MPEG-4 compressors for video and audio, neither it has a stock multiplexor for .MP4 format, though there is an excellent free multiplexor from GDCL: http://www.gdcl.co.uk/mpeg4/. Also there is decent documentation, a lot of samples
Media Foundation API - it has everything you need (codecs, multiplexor) but only in Windows 7 (maybe even not all edtions)
FFmpeg and libavcodec/libavformat are definitely relevant, however H.264 encoder is only available under GPL license, not sure about video capture part right there, and you might have hard time looking for documentation and samples.
I'd say look at OpenCV as a library, hook into their video capture for that aspect, it can write out to mp4 but you'll need a couple of other libs for handling the output stream (on Linux I'd say ffmpeg and x264), that should get the buffer into the container with a reasonable amount of hassle.

Sound API Ubuntu Linux

I want to write some native C/C++ code that takes some uncompressed PCM audio data and plays it out through my speakers on Ubuntu Linux 12.04. Can you tell me what are the different default audio APIs/libraries that are installed? What is their low-level architecture and how do they interarct with the kernel? (which syscalls)
(I've heard different terms like ALSA, PulseAudio, OSS, and so on, but I don't really have a clear picture of which does what and how they fit together.)
On Linux, you have LOTS of Linux audio APIs to choose from. Here is a graph I made a few years ago demonstrating the relationships among the various Linux audio APIs.
However, if you are only targeting Ubuntu Linux and using C/C++, and you don't need anything too fancy (just taking raw PCM data and shoving it out to the speakers), I would recommend using PulseAudio and sticking to the "simple" API. A trivial example can be found here. It's the path of least resistance to getting a proof of concept working.
In very simple terms, ALSA provides the low level infrastructure for audio in Linux, and Pulse provides the higher (more desktop-friendly) level. As far as most mainstream Linux distributions are concerned (Ubuntu included), OSS is obsolete.
You ask about syscalls. You don't use audio in Linux via syscalls. If you choose to use ALSA directly (not a good idea for simple audio playback), you'd employ its userspace library libasound2 (even OSS would be used via /dev/ files rather than syscalls).
If you are targeting modern Linux distros only, consider using the Pulse API. If you want more flexibility as to what distro you are targeting and the potential for cross-platform support, you could try a library which abstracts away the OS-specific audio API - for example, PortAudio (http://www.portaudio.com/).
If you've never done any audio development, you may find a library such as PortAudio easier to work with than addressing PulseAudio directly.
FMOD should be great for you. It's fairly easy to integrate to any C++ project (and many more languages) and runs under Windows, Linux (32bit and 64bit), Mac, Android, PS3, Xbox, etc. Also FMOD Ex provides both a low-level API and data-driven API.
It is free to use if you are not planning on making money with your application. Otherwise you might have to pay for a license starting at about 100$.

Playing sound in C++ using native system calls?

So in Windows, you can use the PlaySound function to play an MP3 file in a C++ application. What would be some similar functions available for use in Mac OS X/Linux? I would also appreciate it if you could link to some sample Hello-World type programs demonstrating this.
You can just open("/dev/dsp") and write to it. That's as "native syscall" as you can get, I believe.
Concerning Audio: Linux != Mac, so I will answer both separately.
MacOS uses the core audio framework for low level audio. Building on that there are several higher level APIs. If you just want to play a sound file the AV Foundation Framework is most likely the way to go. Here you find an example to build a simple app for iOS, which can be ported to MacOS easily.
For Linux the whole situation is quite delicate. There exist many different frameworks and libraries and it very unclear which is low and which is high level, since some of them are emulating the others. Basically there is ALSA and OSS for low level audio. Both of them are crap. Jack is aiming to provide a framework for professional audio but it's not very portable (at least if you are interested in embedded devices). Most Linux OS currently are using PulseAudio as their audio server, which has also a simple API for just playing a single sound and a very difficult API for low level stuff. And then there is the gstreamer library, which is rather portable and has a half way descent API and is especially designed for playing audio & video files and streams, and so probably the way to go on linux.
I personally recommend to use PortAudio, which is a library that provides – as the name states – an audio framework which is portable across several operating systems and audio frameworks. PortAudio is also well designed and has a simple but flexible API.
Well, I'm not sure if current desktops have some more advanced services/libraries that play audio files, but if you really want to do some low-level OS audio output, start looking here. Maybe this is a little too low-level for your needs.

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?