3
votes

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.

6
I know you said you want C++ for speed, but I would recommend prototyping with octave and then porting to C++ for speed gnu.org/software/octave/doc/interpreter/Audio-Processing.html. For C++ you could use PortAudio portaudio.com/docs.htmlTJD
@TJD: most of the heavy processing is going to be done with a genetic algorithm, so there isn't much prototyping to do, and also I don't think octave is well suited to that task.rmeador
@Samaursa: I haven't yet decided if this will be commercial or open source or just a hobby project... it probably depends on how successful it is. A free for all uses license would be best.rmeador

6 Answers

1
votes

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.

1
votes

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.

1
votes

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.

0
votes

How about SDL and SDL_Mixer? No DSP functionality though.

0
votes

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