C++ library for generating and reading audio - c++

I'm looking for a C++ library (for Linux, but preferably cross-platform) that will easily let me read audio files in some trivial format such as a raw byte stream, generate audio data in the same trivial format, and write audio files. I don't care what format the reading and writing happens in, as long as it's free (beer and speech) and commonly supported. I'm going to be reading, transforming, and generating many small audio clips in a very parallel program, so I need the library to be fast and thread-safe.
Additional features I'd like, but do not require, are simple DSP functions (FFT, filters), and recording/playback through the sound card.
A bit of Googling didn't turn anything up, but perhaps I'm using the wrong search terms. I've almost exclusively found libraries for use in apps that record or playback, and it's unclear if they have the ability to generate sound from raw bytes, and even if they do they seem like overkill for my purpose. I've been considering just writing my own library to manipulate WAV files, since they seem simple enough, but I'd rather not reinvent the wheel if I can avoid it.

I may be completely off here, but from your description it sounds like you are looking for something like OpenAL or FMOD. OpenAL is completely free, but FMOD is free for non-commercial use only. Both are thread-safe and are cross-platform.
As you can guess, getting started with OpenAL is much harder than FMOD due to lack of (good) documentation and proper examples.

File R/W: http://www.mega-nerd.com/libsndfile/
FFT: http://www.fftw.org/
Hardware Interfacing: http://jackaudio.org/

GStreamer is cross-platform and widely used:
GStreamer is a library for constructing graphs of media-handling components. The applications it supports range from simple Ogg/Vorbis playback, audio/video streaming to complex audio (mixing) and video (non-linear editing) processing.

I would recommend trying STK:
https://ccrma.stanford.edu/software/stk/
You can just copy-past things you need into your program(lets say reading and writing .wav files). It is fairly simple to use. It has tutorials(with C++ code). They say it is cross-platform. If you are going to compile it for Linux just remember to add '-D__LITTLE_ENDIAN__' to your g++ command line.
P.S. Just take a look at first tutorials. They are really short, simple and straightforward.

How about SDL and SDL_Mixer? No DSP functionality though.

Take a look at the BASS library www.un4seen.com
BASS is free for non-commercial use.
Platforms: Win32, OSX, Linux
BASS is also available for the Win64, WinCE, iOS, Android, and ARM Linux platforms.
BASS is an audio library for use in software on several platforms. Its
purpose is to provide developers with powerful and efficient sample,
stream (MP3, MP2, MP1, OGG, WAV, AIFF, custom generated, and more via
OS codecs and add-ons), MOD music (XM, IT, S3M, MOD, MTM, UMX), MO3
music (MP3/OGG compressed MODs), and recording functions. All in a
compact DLL/LIB that won't bloat your distribution.
BASS supports lots of addons, easy to use, flexible, good documentation/examples and the best support I have ever seen.
As you like it has:
Custom generated samples
And also WAV/AIFF/MP3/MP2/MP1/OGG
Example: Create a 440hz sine wave sample.
HSAMPLE sample=BASS_SampleCreate(256, 28160, 1, 1,
BASS_SAMPLE_LOOP|BASS_SAMPLE_OVER_POS); // create sample
short data[128]; // data buffer
int a;
for (a=0; a<128; a++)
data[a]=(short)(32767.0*sin((double)a*6.283185/64)); // sine wave
BASS_SampleSetData(sample, data); // set the sample's data
Playback through the sound card
Simultaneously use multiple soundcards, and move channels between them
Custom DSP
Apply any effects that you want, in any order you want
DSP is set up using:
HDSP BASS_ChannelSetDSP(
DWORD handle,
DSPPROC *proc,
void *user,
int priority
);
Example: A simple DSP function to swap the left/right channels of a stereo 16-bit channel.
void CALLBACK SwapDSP(HDSP handle, DWORD channel, void *buffer, DWORD length, void *user)
{
short *s=buffer;
for (; length; length-=4, s+=2) {
short temp=s[0];
s[0]=s[1];
s[1]=temp;
}
}
Recording
Flexible recording system, with multiple device support and input selection, (WMA encoding & broadcasting via the add-on, and other formats via BASSenc)
FFT
Example: Perform a 1024 sample FFT on a channel and list the result.
float fft[512]; // fft data buffer
BASS_ChannelGetData(channel, fft, BASS_DATA_FFT1024);
for (int a=0; a<512; a++)
printf("%d: %f\n", a, fft[a]);

Related

C++ playing audio live from byte array

I am using C++ and have the sample rate, number of channels, and bit depth for my audio. I also have a char array containing the audio that I want to play. I am look for something along the lines of, sending a quarter of a second (or some other short amount of audio) to be played, then sending some more, etc. Is this possible, and if it is how would it be done.
Thanks for any help.
I've done this before with the library OpenAL.
This would require a pretty involved answer and hopefully the OpenAL documentation can walk you through it all, but here is the source example which I wrote that plays audio streaming in from a mumble server in nodejs.
You may need to ask a more specific question to get a better answer as this is a fairly large topic. It may also help to list other technologies you may be using such as target operating system(s) and if you are using any libraries already. Many desktop and game engines already have api's for playing simple sounds and using OpenAL may be much more complex than you really need.
But, briefly, the steps of the solution are:
Enumerate devices
Capture a device
Stream data to device
enqueue audio to buffer alSourceQueueBuffers
play queued buffer alSourcePlay

Audio Waveform Synthesizer in C++ that outputs to soundcard. Where do I begin?

I managed to write a PCM sine wave synthesizer in Java using the javax package. Basically, the program generates a sine wave at a sample rate of 44100, and writes the byte data to the SourceDataLine.
I'm trying to achieve the same thing in C++ but I have not been able to find any answers here that do not require the use of an extensive library such as OpenAL.
I would like to know if it is possible to send byte data directly to the sound card using just the Standard library.
[working on windows 10 and Netbeans IDE]
Short answer: No.
You will need a platform library. Audio is very finicky and platform dependent. If I can suggest an alternative: Look into writing a VST plugin!

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

Portable library to play samples on individual 5.1 channels with C/C++?

I'm looking for a free, portable C or C++ library which allows me to play mono sound samples on specific channels in a 5.1 setup. For example the sound should be played with the left front speaker whereby all other speakers remain silent. Is there any library capable of doing this?
I had a look at OpenAL. However, I can only specify the position from which the sound should come, but it seems to me that I cannot say something like "use only the front left channel to play this sound".
Any hints are welcome!
I had a look at OpenAL. However, I can only specify the position from which the sound should come, but it seems to me that I cannot say something like "use only the front left channel to play this sound".
I don't think this is quite true. I think you can do it with OpenAL, although it's not trivial. OpenAL only does the positional stuff if you feed it mono format data. If you give it stereo or higher, it plays the data the way it was provided. However, you're only guaranteed stereo support. You'll need to check to see if the 5.1 channel format extension is available on your system (AL_FORMAT_51CHN16). If so, then, I think that you feed your sound to the channel you want and feed zeroes to all the others channels when you buffer the samples. Note that you need hardware support for this on the sound card. A "generic software" device won't cut it.
See this discussion from the OpenAL mailing list.
Alternatively, I think that PortAudio is Open, cross-platform, and supports multiple channel output. You do still have to interleave the data so that if you're sending a sound to a single channel, you have to send zeroes to all the others. You'll also still need to do some checking when opening a stream on a device to make sure the device supports 6 channels of output.
A long time ago I used RTAudio. But I cannot say if this lib can do what you want to archive, but maybe it helps.
http://fmod.org could do the trick too
I use the BASS Audio Library http://www.un4seen.com for all my audio, sound and music projects. I am very happy with it.
BASS is an audio library to provide developers with powerful and efficient sample, stream (MP3, MP2, MP1, OGG, WAV, AIFF, custom generated, and more via add-ons), MOD music (XM, IT, S3M, MOD, MTM, UMX), MO3 music (MP3/OGG compressed MODs), and recording functions. All in a tiny DLL, under 100KB* in size. C/C++, Delphi, Visual Basic, MASM, .Net and other APIs are available. BASS is available for the Windows, Mac, Win64, WinCE, Linux, and iOS platforms.
I have never used it to play different samples in a 5.1 configuration. But, according their own documentation, it should be possible.
Main features
Samples Support for WAV/AIFF/MP3/MP2/MP1/OGG and custom generated samples
Sample streams Stream any sample data in 8/16/32 bit, with both "push" and "pull" systems. File streams MP3/MP2/MP1/OGG/WAV/AIFF file streaming. Internet file streaming. Stream data from HTTP and FTP servers (inc. Shoutcast, Icecast & Icecast2), with IDN and proxy server support and adjustable buffering. ** Custom file streaming ** Stream data from anywhere using any delivery method, with both "push" and "pull" systems
Multi-channel Support for more than plain stereo, including multi-channel OGG/WAV/AIFF files
...
Multiple outputs Simultaneously use multiple soundcards, and move channels between them
Speaker assignment Assign streams and MOD musics to specific speakers to take advantage of hardware capable of more than plain stereo (up to 4 separate stereo outputs with a 7.1 soundcard)
3D sound Play samples/streams/musics in any 3D position
Licensing
BASS is free for non-commercial use. If you are a non-commercial entity (eg. an individual) and you are not making any money from your product (through sales, advertising, etc), then you can use BASS in it for free. Otherwise, one of the following licences will be required.

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?