Convert Figma logo to code with AI

thestk logortaudio

A set of C++ classes that provide a common API for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X (CoreAudio and JACK), and Windows (DirectSound, ASIO, and WASAPI) operating systems.

1,489
317
1,489
33

Top Related Projects

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.

C library for cross-platform real-time audio input and output

OpenAL Soft is a software implementation of the OpenAL 3D audio API.

Audio playback and capture library written in C, in a single source file.

A C library for reading and writing sound files containing sampled audio data.

Quick Overview

RtAudio is a cross-platform C++ library for realtime audio input/output. It provides a common API for accessing various audio devices and drivers across different operating systems, including Windows, macOS, and Linux. RtAudio simplifies the process of working with audio streams in C++ applications.

Pros

  • Cross-platform compatibility (Windows, macOS, Linux)
  • Supports multiple audio APIs (ASIO, WASAPI, CoreAudio, ALSA, JACK, etc.)
  • Low-latency audio processing
  • Simple and intuitive API

Cons

  • Limited documentation and examples
  • Requires some knowledge of audio programming concepts
  • May require additional setup for certain audio APIs (e.g., ASIO)
  • Not actively maintained (last major update in 2021)

Code Examples

  1. Opening an audio stream:
#include "RtAudio.h"

RtAudio dac;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;

dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32,
               sampleRate, &bufferFrames, &callback);
dac.startStream();
  1. Implementing a simple sine wave generator:
#include <cmath>

int callback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
             double streamTime, RtAudioStreamStatus status, void *userData) {
    float *buffer = (float *) outputBuffer;
    static double phase = 0.0;
    double increment = 2.0 * M_PI * 440.0 / 44100.0;

    for (unsigned int i = 0; i < nBufferFrames; i++) {
        buffer[i * 2] = buffer[i * 2 + 1] = sin(phase);
        phase += increment;
        if (phase >= 2.0 * M_PI) phase -= 2.0 * M_PI;
    }

    return 0;
}
  1. Enumerating available audio devices:
RtAudio dac;
std::vector<RtAudio::DeviceInfo> devices;
unsigned int deviceCount = dac.getDeviceCount();

for (unsigned int i = 0; i < deviceCount; i++) {
    devices.push_back(dac.getDeviceInfo(i));
    std::cout << "Device " << i << ": " << devices[i].name << std::endl;
}

Getting Started

  1. Download and include the RtAudio library in your project.
  2. Include the RtAudio header: #include "RtAudio.h"
  3. Create an RtAudio object: RtAudio dac;
  4. Set up stream parameters and open a stream:
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;

dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32,
               sampleRate, &bufferFrames, &callback);
  1. Implement the callback function to process audio data.
  2. Start the audio stream: dac.startStream();

Competitor Comparisons

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.

Pros of PortAudio

  • More extensive platform support, including mobile platforms
  • Larger community and wider adoption in various projects
  • More comprehensive documentation and examples

Cons of PortAudio

  • More complex API, which can be harder to learn and use
  • Larger codebase, potentially leading to longer compilation times
  • Less frequent updates and releases compared to RtAudio

Code Comparison

PortAudio:

PaError err;
PaStream *stream;
err = Pa_Initialize();
err = Pa_OpenDefaultStream(&stream, 2, 2, paFloat32, SAMPLE_RATE, 256, audioCallback, NULL);
err = Pa_StartStream(stream);

RtAudio:

RtAudio dac;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, SAMPLE_RATE, &bufferFrames, &audioCallback);
dac.startStream();

Both libraries offer cross-platform audio I/O capabilities, but PortAudio has broader platform support and a larger community. RtAudio, on the other hand, provides a simpler API and more frequent updates. The code comparison shows that RtAudio has a slightly more straightforward setup process, while PortAudio offers more granular control over stream parameters.

C library for cross-platform real-time audio input and output

Pros of libsoundio

  • Simpler API design with a focus on ease of use
  • Better support for raw ALSA on Linux
  • More actively maintained with recent updates

Cons of libsoundio

  • Less mature and less widely used than RtAudio
  • Fewer supported backends (no ASIO support)
  • Limited to C language, while RtAudio supports C++

Code Comparison

libsoundio:

struct SoundIoOutStream *outstream = soundio_outstream_create(device);
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
soundio_outstream_open(outstream);
soundio_outstream_start(outstream);

RtAudio:

RtAudio dac;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &callback);
dac.startStream();

Both libraries provide cross-platform audio I/O capabilities, but libsoundio aims for a simpler API and better raw ALSA support. RtAudio offers broader platform support and a more established ecosystem. The choice between them depends on specific project requirements and target platforms.

OpenAL Soft is a software implementation of the OpenAL 3D audio API.

Pros of OpenAL Soft

  • More comprehensive audio API with 3D sound positioning capabilities
  • Cross-platform support with a wider range of supported operating systems
  • Active development and larger community support

Cons of OpenAL Soft

  • Higher complexity and steeper learning curve
  • Potentially higher resource usage due to additional features
  • May be overkill for simple audio playback needs

Code Comparison

OpenAL Soft:

ALCdevice *device = alcOpenDevice(NULL);
ALCcontext *context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);

RtAudio:

RtAudio dac;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &callback);
dac.startStream();

OpenAL Soft provides a more abstracted API for 3D audio positioning, while RtAudio offers a simpler interface for basic audio output. OpenAL Soft is better suited for complex audio scenarios, especially in gaming or spatial audio applications. RtAudio, on the other hand, is more straightforward for simple audio playback or recording tasks.

Audio playback and capture library written in C, in a single source file.

Pros of miniaudio

  • Single-file, header-only library, making integration simpler
  • Supports a wider range of platforms, including mobile and web
  • More feature-rich, including audio format decoding and effects processing

Cons of miniaudio

  • Less mature and potentially less stable than RtAudio
  • Documentation may be less comprehensive
  • Larger codebase due to additional features, which may increase complexity

Code Comparison

RtAudio:

RtAudio dac;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &callback);
dac.startStream();

miniaudio:

ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format   = ma_format_f32;
config.playback.channels = 2;
config.sampleRate        = 44100;
config.dataCallback      = data_callback;
ma_device device;
ma_device_init(NULL, &config, &device);
ma_device_start(&device);

Both libraries offer straightforward APIs for audio playback, but miniaudio's initialization is slightly more concise. RtAudio uses a more object-oriented approach, while miniaudio follows a C-style API. The core functionality is similar, with both requiring device configuration, stream setup, and a callback function for audio processing.

A C library for reading and writing sound files containing sampled audio data.

Pros of libsndfile

  • Supports a wide range of audio file formats for reading and writing
  • Provides high-level abstractions for audio file handling
  • Well-documented API with extensive examples

Cons of libsndfile

  • Focused on file I/O, not real-time audio processing
  • Larger library size compared to RtAudio
  • May require additional dependencies for certain file formats

Code Comparison

RtAudio (basic audio output):

RtAudio dac;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &callback);
dac.startStream();

libsndfile (basic file reading):

SF_INFO sfinfo;
SNDFILE *file = sf_open("audio.wav", SFM_READ, &sfinfo);
float buffer[1024];
sf_count_t frames_read = sf_readf_float(file, buffer, 1024);
sf_close(file);

RtAudio is designed for real-time audio input/output across various platforms, while libsndfile specializes in reading and writing audio files. RtAudio is better suited for applications requiring low-latency audio processing, whereas libsndfile excels in scenarios involving audio file manipulation and format conversion.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

RtAudio

Build Status

A set of C++ classes that provide a common API for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X (CoreAudio and JACK), and Windows (DirectSound, ASIO and WASAPI) operating systems.

By Gary P. Scavone, 2001-2023 (and many other developers!)

This distribution of RtAudio contains the following:

  • doc: RtAudio documentation (see doc/html/index.html)
  • tests: example RtAudio programs
  • include: header and source files necessary for ASIO, DS & OSS compilation
  • tests/Windows: Visual C++ .net test program workspace and projects

Overview

RtAudio is a set of C++ classes that provides a common API (Application Programming Interface) for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X and Windows (DirectSound, ASIO and WASAPI) operating systems. RtAudio significantly simplifies the process of interacting with computer audio hardware. It was designed with the following objectives:

  • object-oriented C++ design
  • simple, common API across all supported platforms
  • only one source and one header file for easy inclusion in programming projects
  • allow simultaneous multi-api support
  • support dynamic connection of devices
  • provide extensive audio device parameter control
  • allow audio device capability probing
  • automatic internal conversion for data format, channel number compensation, (de)interleaving, and byte-swapping

RtAudio incorporates the concept of audio streams, which represent audio output (playback) and/or input (recording). Available audio devices and their capabilities can be enumerated and then specified when opening a stream. Where applicable, multiple API support can be compiled and a particular API specified when creating an RtAudio instance. See the \ref apinotes section for information specific to each of the supported audio APIs.

Building

Several build systems are available. These are:

  • autotools (./autogen.sh; make from git, or ./configure; make from tarball release)
  • CMake (mkdir build; cd build; ../cmake; make)
  • meson (meson build; cd build; ninja)
  • vcpkg (./bootstrap-vcpkg.sh; ./vcpkg integrate install; ./vcpkg install rtaudio)

See install.txt for more instructions about how to select the audio backend API. By default all detected APIs will be enabled.

We recommend using the autotools-based build for packaging purposes. Please note that RtAudio is designed as a single .cpp and .h file so that it is easy to copy directly into a project. In that case you need to define the appropriate flags for the desired backend APIs.

FAQ

Why does audio only come to one ear when I choose 1-channel output?

RtAudio doesn't automatically turn 1-channel output into stereo output with copied values to two channels, since there may be cases when a user truly wants 1-channel behaviour. If you want monophonic data to be projected to stereo output, open a 2-channel stream and copy the data to both channels in your audio stream callback.

Further Reading

For complete documentation on RtAudio, see the doc directory of the distribution or surf to http://www.music.mcgill.ca/~gary/rtaudio/.

Legal and ethical:

The RtAudio license is similar to the MIT License. Please see LICENSE.