Audio Library
A minimalistic audio library for games.
Play a sound in one line of C++ or C.
#include <Audio/Audio.hpp>
int main()
{
Audio::Sound music { "music.ogg", Audio::Sound::Type::Music };
music.setLooping( true );
music.play();
Audio::playSound( "coin.wav" ); // Fire and forget.
...
}
No audio device to set up or shut down. The library does that for you.
Play the demo
Brick Bounce is a minimal game that uses the Audio library. Every sound you hear in it is played by the Audio library. It's the same C++ code as the desktop version, compiled to WebAssembly so it runs in your browser. On the desktop, the game window is made with fenster, a tiny single-header library by Serge Zaitsev.
Streamed music
The background music is streamed from an OGG file. It fades down when you pause, and fades out when the game is over.
Panned effects
Every bounce is panned to where it happens. Hit bricks in a row and each one sounds a little higher.
Waveform jingles
The level-clear and game-over tunes are made with a Waveform, without any sound files.
Features
Simple
Load a sound and play it. Copies of a sound share it, just like objects in C#, Java, Python or JavaScript.
Sounds and music
WAV, MP3, FLAC and OGG. Volume, panning, pitch, looping, fading and scheduled start and stop.
Spatial audio
Place sounds in 3D space around the player, with up to 4 listeners for split-screen games.
Retro waveforms
Generate sine, square, triangle and sawtooth tones for sound effects like early video games.
C++ and C
A header-only C++17 API on top of a stable C99 API. One library for both Debug and Release builds.
Runs everywhere
Windows, Linux and the web (Emscripten). Built on the excellent miniaudio.
Examples
Fire and forget, or load the sound for more control.
Audio::playSound( "explosion.wav" ); // Fire and forget.
Audio::Sound coin { "coin.wav" };
coin.setVolume( 0.5f ); // 0 is silent, 1 is 100%.
coin.setPan( -1.0f ); // -1 is left, 0 is centered, 1 is right.
coin.setPitch( 1.5f ); // Higher values sound higher.
coin.play();
Music is streamed from the file while it plays, so it loads fast and uses little memory.
using namespace std::chrono_literals;
Audio::Sound music { "music.ogg", Audio::Sound::Type::Music };
music.setLooping( true );
music.play();
music.setFade( 0.0f, 2s ); // Fade out over 2 seconds.
What you hear depends on where the sound is, relative to the listener.
Audio::Sound coin { "coin.wav" };
coin.setPosition( { x, y, z } ); // Where the sound is in the world.
coin.play();
Audio::Listener listener = Audio::getListener();
listener.setPosition( player.getPosition() );
Generate a tone instead of playing a file. Change the frequency to change the pitch.
// A square wave with an amplitude of 0.2, at 440 Hz.
Audio::Waveform beep { Audio::Waveform::Type::Square, 0.2f, 440.0f };
beep.start();
...
beep.setFrequency( 880.0f ); // One octave higher.
...
beep.stop();
The same library from C, or any language that can call C functions.
#include <Audio/audio.h>
...
audio_play_sound( NULL, "coin.wav" ); // NULL: the default device.
audio_sound* music = NULL;
if ( audio_sound_create( NULL, "music.ogg", AUDIO_SOUND_FLAGS_MUSIC, &music ) == AUDIO_SUCCESS )
{
audio_sound_play( music );
}
...
audio_sound_destroy( music );
Get the library
Your project needs C++17 or newer (or C99 for the C API).
Prebuilt package
For Visual Studio 2022 and newer. Download the static package from the Releases page, and follow the step-by-step guide.
CMake
Let CMake download and build it:
include(FetchContent)
FetchContent_Declare(Audio
GIT_REPOSITORY https://github.com/3dgep/Audio.git
GIT_TAG v2.0.1)
FetchContent_MakeAvailable(Audio)
target_link_libraries(MyGame PRIVATE Audio::Audio)
vcpkg
Install the port from the repository's ports folder:
vcpkg install 3dgep-audio --overlay-ports=path/to/Audio/ports
find_package(Audio CONFIG REQUIRED)
target_link_libraries(MyGame PRIVATE Audio::Audio)
Questions? Ask on the 3dgep.com Discord server.