Audio

Defines

MA_NO_JACK

Todo:

The old audio system used to have a centralized manager so that we could pause, stop and play all sounds and musics together. It’s been temporarily removed while I rewrote this to use miniaudio instead of SDL_Mixer. It’s planned to bring it back at some point so that we can control the whole sound system at once when needed, i.e. when we pause the game or switch to a different window.

MA_NO_WAV
MA_NO_FLAC
MA_NO_MP3
BINOCLE_AUDIO_DEVICE_FORMAT
BINOCLE_AUDIO_DEVICE_CHANNELS
BINOCLE_AUDIO_DEVICE_SAMPLE_RATE
BINOCLE_AUDIO_MAX_AUDIO_BUFFER_POOL_CHANNELS
BINOCLE_AUDIO_DEFAULT_AUDIO_BUFFER_SIZE

Typedefs

typedef enum binocle_audio_buffer_usage binocle_audio_buffer_usage

The kind of buffer usage. It can be static for short sounds and stream for big files and sounds that spawn for over a few seconds.

typedef enum binocle_audio_music_context_type binocle_audio_music_context_type

The context type of the music file.

typedef struct binocle_audio_buffer binocle_audio_buffer

An audio buffer Contains information like the PCM converter, volume, pitch, playing status, and pointers to other buffers as a linked list.

typedef struct binocle_audio_stream binocle_audio_stream

represents an audio stream Contains information about the sample rate,bit depth, number of channels, audio format and the buffers

typedef struct binocle_audio_music binocle_audio_music

A music instance Contains the actual context of the music file, the audio stream and information like looping and the number of samples played and left to play.

typedef struct binocle_audio_wave binocle_audio_wave

an intermediate structure that contains data loaded from a sound file

typedef struct binocle_audio_sound binocle_audio_sound

The representation of a sound in the audio system.

typedef struct binocle_audio binocle_audio

The audio system.

Enums

enum binocle_audio_buffer_usage

The kind of buffer usage. It can be static for short sounds and stream for big files and sounds that spawn for over a few seconds.

Values:

enumerator BINOCLE_AUDIO_BUFFER_USAGE_STATIC
enumerator BINOCLE_AUDIO_BUFFER_USAGE_STREAM
enum binocle_audio_music_context_type

The context type of the music file.

Values:

enumerator BINOCLE_AUDIO_MUSIC_AUDIO_OGG
enumerator BINOCLE_AUDIO_MUSIC_AUDIO_FLAC
enumerator BINOCLE_AUDIO_MUSIC_AUDIO_MP3
enumerator BINOCLE_AUDIO_MUSIC_AUDIO_WAV
enumerator BINOCLE_AUDIO_MUSIC_MODULE_XM
enumerator BINOCLE_AUDIO_MUSIC_MODULE_MOD

Functions

binocle_audio binocle_audio_new()

Creates a new audio system.

Returns

The audio system

bool binocle_audio_init(binocle_audio *audio)

Initialize a newly created audio system.

Parameters

audio – the audio system

Returns

true everything worked fine

void binocle_audio_destroy(binocle_audio *audio)

Destroys an audio system Releases all the resources allocated by the audio system.

Parameters

audio – the audio system

binocle_audio_buffer *binocle_audio_load_audio_buffer(binocle_audio *audio, ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage)

Load an audio buffer

Parameters
  • audio – the audio system

  • format – the format

  • channels – number of channels

  • sampleRate – sample rate

  • sizeInFrames – size in frames

  • usage – usage

Returns

the audio buffer

void binocle_audio_unload_audio_buffer(binocle_audio *audio, binocle_audio_buffer *buffer)

Unload an audio buffer

Parameters
  • audio – the audio system

  • buffer – the audio buffer

void binocle_audio_data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount)

The audio callback as required by miniaudio.

Parameters
  • pDevice – the miniaudio device

  • pOutput – the output buffer

  • pInput – the input buffer

  • frameCount – the current frame count

static void binocle_audio_mix_audio_frames(void *pUserData, float *framesOut, const float *framesIn, ma_uint32 frameCount, binocle_audio_buffer *buffer)

Internal function that performs mixing of audio buffers.

Parameters
  • pUserData – the audio system

  • framesOut – the output buffer

  • framesIn – the input buffer

  • frameCount – the frame count

  • localVolume – the buffer local volume

bool binocle_audio_is_audio_device_ready(binocle_audio *audio)

Returns true if the audio device is ready.

Parameters

audio – the audio system

Returns

true if the audio device is ready

void binocle_audio_set_master_volume(binocle_audio *audio, float volume)

Sets the master volume of the audio system.

Parameters
  • audio – the audio system

  • volume – the volume [0..1]

bool binocle_audio_is_audio_buffer_playing(binocle_audio_buffer *audio_buffer)

Returns true if the audio buffer is playing.

Parameters

audio_buffer – the audio buffer

Returns

true if playing

void binocle_audio_play_audio_buffer(binocle_audio_buffer *audio_buffer)

Plays the audio buffer.

Parameters

audio_buffer – the audio buffer

void binocle_audio_stop_audio_buffer(binocle_audio_buffer *audio_buffer)

Stops the audio buffer.

Parameters

audio_buffer – the audio buffer

void binocle_audio_pause_audio_buffer(binocle_audio_buffer *audio_buffer)

Pauses the audio buffer.

Parameters

audio_buffer – the audio buffer

void binocle_audio_resume_audio_buffer(binocle_audio_buffer *audio_buffer)

Resumes the audio buffer.

Parameters

audio_buffer – the audio buffer

void binocle_audio_set_audio_buffer_volume(binocle_audio_buffer *audio_buffer, float volume)

Sets the volume of the audio buffer.

Parameters
  • audio_buffer – the audio buffer

  • volume – the folume [0..1]

void binocle_audio_set_audio_buffer_pitch(binocle_audio_buffer *audio_buffer, float pitch)

Sets the audio buffer pitch.

Parameters
  • audio_buffer – the audio buffer

  • pitch – the pitch level

void binocle_audio_track_audio_buffer(binocle_audio *audio, binocle_audio_buffer *audio_buffer)

Adds the audio buffer to the list of audio buffers.

Parameters
  • audio – the audio system

  • audio_buffer – the audio buffer

void binocle_audio_untrack_audio_buffer(binocle_audio *audio, binocle_audio_buffer *audio_buffer)

Stops tracking an audio buffer and removes it from the list.

Parameters
  • audio – the audio system

  • audio_buffer – the audio buffer

bool binocle_audio_is_file_extension(const char *file_name, const char *ext)

Returns true if the file extension is the one specified.

Parameters
  • file_name – the file name

  • ext – the extension including the starting dot

Returns

true if the filename has the specified extension

static binocle_audio_wave binocle_audio_load_ogg(const char *file_name)

Load an OGG audio file.

Parameters

file_name – the file name

Returns

a binocle_audio_wave instance

static binocle_audio_wave binocle_audio_load_flac(const char *file_name)

Load a FLAC audio file.

Parameters

file_name – the file name

Returns

a binocle_audio_wave instance

static binocle_audio_wave binocle_audio_load_mp3(const char *file_name)

Load an MP3 audio file.

Parameters

file_name – the file name

Returns

a binocle_audio_wave instance

static binocle_audio_wave binocle_audio_load_wav(const char *file_name)

Load a WAV audio file.

Parameters

file_name – the file name

Returns

a binocle_audio_wave instance

binocle_audio_sound binocle_audio_load_sound(binocle_audio *audio, const char *file_name)

Loads a sound file.

Parameters
  • audio – the audio system

  • file_name – the file to read

Returns

a binocle_audio_sound instance

binocle_audio_sound binocle_audio_load_sound_from_wave(binocle_audio *audio, binocle_audio_wave wave)

Loads a sound file from an intermediate wave format.

Parameters
Returns

a binocle_audio_sound instance

void binocle_audio_unload_wave(binocle_audio_wave wave)

Releases the resources allocated within a binocle_audio_wave.

Parameters

wave – the wave struct

void binocle_audio_unload_sound(binocle_audio *audio, binocle_audio_sound sound)

Releases the audio buffer of the sound.

Parameters
  • audio – the audio system

  • sound – the sound

void binocle_audio_update_sound(binocle_audio_sound sound, const void *data, int samplesCount)

Updates the audio buffer of the sound.

Parameters
  • sound – the sound

  • data – the buffer with the data to copy from

  • samplesCount – the number of samples

void binocle_audio_play_sound(binocle_audio_sound sound)

Plays a sound.

Parameters

sound – the sound

void binocle_audio_pause_sound(binocle_audio_sound sound)

Pauses a sound.

Parameters

sound – the sound

void binocle_audio_resume_sound(binocle_audio_sound sound)

Resumes a sound.

Parameters

sound – the sound

void binocle_audio_stop_sound(binocle_audio_sound sound)

Stops a sound.

Parameters

sound – the sound

bool binocle_audio_is_sound_playing(binocle_audio_sound sound)

Returns true if the sound is playing.

Parameters

sound – the sound

Returns

true if the sound is playing

void binocle_audio_set_sound_volume(binocle_audio_sound sound, float volume)

Sets the volume of the sound.

Parameters
  • sound – the sound

  • volume – the volume [0..1]

void binocle_audio_set_sound_pitch(binocle_audio_sound sound, float pitch)

Sets the pitch of the sound.

Parameters
  • sound – the sound

  • pitch – the pitch

binocle_audio_music binocle_audio_load_music_stream(binocle_audio *audio, const char *file_name)

Loads a music stream from the given file.

Parameters
  • audio – the audio system

  • file_name – the filename of the audio file

Returns

the music struct

void binocle_audio_unload_music_stream(binocle_audio *audio, binocle_audio_music *music)

Releases the music stream.

Parameters
  • audio – the audio system

  • music – the music struct

void binocle_audio_play_music_stream(binocle_audio_music *music)

Plays a music stream.

Parameters

music – the music stream

void binocle_audio_pause_music_stream(binocle_audio_music *music)

Pauses a music stream.

Parameters

music – the music stream

void binocle_audio_resume_music_stream(binocle_audio_music *music)

Resumes a music stream.

Parameters

music – the music stream

void binocle_audio_stop_music_stream(binocle_audio_music *music)

Stops a music stream.

Parameters

music – the music stream

void binocle_audio_seek_music_stream(binocle_audio_music *music, unsigned int frame)

Seeks a music stream.

Parameters
  • music – the music stream

  • frame – the frame to seek to

void binocle_audio_update_music_stream(binocle_audio_music *music)

Updates a music stream.

Parameters

music – the music stream

bool binocle_audio_is_music_stream_playing(binocle_audio_music *music)

Returns true if the music stream is playing.

Parameters

music – the music stream

Returns

true if the music stream is playing

void binocle_audio_set_music_volume(binocle_audio_music *music, float volume)

Sets the music stream volume.

Parameters
  • music – the music stream

  • volume – the volume [0..1]

void binocle_audio_set_music_pitch(binocle_audio_music *music, float pitch)

Sets the music stream pitch.

Parameters
  • music – the music stream

  • pitch – the pitch

void binocle_audio_set_music_loop_count(binocle_audio_music *music, int count)

Sets the number of loops of the music stream If count is -1 then it loops indefinitely.

Parameters
  • music – the music stream

  • count – how many times to loop

float binocle_audio_get_music_time_length(binocle_audio_music *music)

Gets the length of the music stream in seconds.

Parameters

music – the music stream

Returns

the length of the music stream in seconds

float binocle_audio_get_music_time_played(binocle_audio_music *music)

Gets the length of the music stream already played in seconds.

Parameters

music – the music stream

Returns

the length of the music stream already played in seconds

binocle_audio_stream binocle_audio_load_audio_stream(binocle_audio *audio, unsigned int sample_rate, unsigned int sample_size, unsigned int channels)

Initialize an audio stream.

Parameters
  • audio – the audio system

  • sample_rate – the sample rate

  • sample_size – the sample size

  • channels – the number of channels [1..2]

Returns

an initialized audio stream

void binocle_audio_unload_audio_stream(binocle_audio *audio, binocle_audio_stream stream)

Closes an audio stream and frees its buffer.

Parameters
  • audio – the audio system

  • stream – an audio stream

void binocle_audio_update_audio_stream(binocle_audio_stream stream, const void *data, int frame_count)

Updates an audio stream.

Parameters
  • stream – the audio stream

  • data – the buffer with audio samples

  • frame_count – the number of frames

bool binocle_audio_is_audio_stream_processed(binocle_audio_stream stream)

Returns true if the audio buffer of the stream has been processed.

Parameters

stream – the audio stream

Returns

true if the audio buffer has already been processed

void binocle_audio_play_audio_stream(binocle_audio_stream stream)

Plays an audio stream.

Parameters

stream – the audio stream

void binocle_audio_pause_audio_stream(binocle_audio_stream stream)

Pauses an audio stream.

Parameters

stream – the audio stream

void binocle_audio_resume_audio_stream(binocle_audio_stream stream)

Resumes an audio stream.

Parameters

stream – the audio stream

bool binocle_audio_is_audio_stream_playing(binocle_audio_stream stream)

Returns true if the audio stream is playing.

Parameters

stream – the audio stream

Returns

true if the audio stream is playing

void binocle_audio_stop_audio_stream(binocle_audio_stream stream)

Stops an audio stream.

Parameters

stream – the audio stream

void binocle_audio_set_audio_stream_volume(binocle_audio_stream stream, float volume)

Sets an audio stream volume.

Parameters
  • stream – the audio stream

  • volume – the volume [0..1]

void binocle_audio_set_audio_stream_pitch(binocle_audio_stream stream, float pitch)

Sets an audio stream pitch.

Parameters
  • stream – the audio stream

  • pitch – the pitch

uint32_t binocle_audio_convert_time_to_sample(float time_in_seconds, uint32_t sample_rate)

Converts time in seconds to the number of samples.

Parameters
  • time_in_seconds – the time expressed in seconds

  • sample_rate – the sample rate

Returns

the sample number

uint32_t binocle_audio_convert_beat_to_sample(uint32_t beat, uint32_t bpm, uint32_t sample_rate)

Converts a beat to the number of samples.

Parameters
  • beat – the beat

  • bpm – the BPM

  • sample_rate – the sample rate

Returns

the sample number

struct binocle_audio_buffer
#include <binocle_audio.h>

An audio buffer Contains information like the PCM converter, volume, pitch, playing status, and pointers to other buffers as a linked list.

Public Members

ma_data_converter converter
float volume
float pitch
float pan
bool playing
bool paused
bool looping
binocle_audio_buffer_usage usage
bool is_sub_buffer_processed[2]
unsigned int size_in_frames
unsigned int frame_cursor_pos
unsigned int frames_processed
struct binocle_audio_buffer *next
struct binocle_audio_buffer *prev
unsigned char *data
struct binocle_audio_stream
#include <binocle_audio.h>

represents an audio stream Contains information about the sample rate,bit depth, number of channels, audio format and the buffers

Public Members

unsigned int sample_rate
unsigned int sample_size
unsigned int channels
binocle_audio_buffer *buffer
struct binocle_audio_music
#include <binocle_audio.h>

A music instance Contains the actual context of the music file, the audio stream and information like looping and the number of samples played and left to play.

Public Members

binocle_audio_stream stream
unsigned int frame_count
bool looping
binocle_audio_music_context_type ctx_type
stb_vorbis *ctx_ogg
drflac *ctx_flac
drmp3 ctx_mp3
drwav ctx_wav
jar_xm_context_t *ctx_xm
jar_mod_context_t ctx_mod
struct binocle_audio_wave
#include <binocle_audio.h>

an intermediate structure that contains data loaded from a sound file

Public Members

unsigned int sample_count
unsigned int sample_rate
unsigned int sample_size
unsigned int channels
void *data
struct binocle_audio_sound
#include <binocle_audio.h>

The representation of a sound in the audio system.

Public Members

binocle_audio_stream stream
unsigned int frame_count
struct binocle_audio
#include <binocle_audio.h>

The audio system.

Public Members

uint32_t active_sounds_counter
bool paused
ma_decoder decoder
ma_device_config device_config
ma_device device
ma_context context
float master_volume
binocle_audio_buffer *first_audio_buffer
binocle_audio_buffer *last_audio_buffer
int default_size
bool is_audio_initialized
ma_mutex lock
unsigned int pool_counter
binocle_audio_buffer *pool[BINOCLE_AUDIO_MAX_AUDIO_BUFFER_POOL_CHANNELS]
unsigned int channels[BINOCLE_AUDIO_MAX_AUDIO_BUFFER_POOL_CHANNELS]
struct binocle_audio::[anonymous] multi_channel