Playing sound in C++ using native system calls? - c++

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.

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();

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$.

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.

Cross Platform MIDI library supporting iOS CoreMIDI

I'm looking for a cross-platform C or C++ MIDI library. Just to send/receive MIDI note events, control codes and timing, not to generate sound.
The main target is iOS/iPad so it has to support CoreMIDI on iOS, which I know is fairly recent. I can use CoreMidi directly but if there's something lightweight out there it would be nice to use something portable so I could easily port the project to PC's.
Failing that, please mention if there's a good, lightweight one that's open source it might be easier for me to add CoreMIDI support than to roll my own.
Clarification: I'm looking for something similar to MidiIO or PortMIDI that has good iOS support.
You might want to take a look at RtMidi.
It provides a cross-platform API for realtime MIDI input/output, and makes use of the native API's for each platform (winmm, ALSA, CoreMidi).
I haven't used it with CoreAudio/CoreMidi, but it's worked flawlessly for me on Windows and Linux.
I don't know MIDI thing in iOS dev. To my understanding, sending/receiving MIDI note events to/from MIDI devices should be a pure OS API problem. And parsing/generating MIDI messages is about MIDI spec. I suppose neither leads to a need of a library.

Working with audio in 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.