LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_audio.c File Reference

Functions

int lg_init_audio_to_defaults ()
 
int lg_init_audio (int freq, uint16_t format, int n_channels, int buffer_size)
 
void lg_free_audio ()
 
int lg_load_play_music (const char *file_name, int loops)
 
zboolean lg_music_is_playing ()
 
void lg_pause_music ()
 
void lg_resume_music ()
 
void lg_stop_music ()
 
void lg_free_music ()
 
LG_Soundlg_sound_new (const char *file_name)
 
void lg_disable_cant_play_audio_file ()
 
void lg_enable_cant_play_audio_file ()
 
int lg_sound_play (lg_play_mode mode, LG_Sound *sound)
 
zboolean lg_sound_is_playing (LG_Sound *sound)
 
void lg_sound_channel_set_volume (LG_Sound *sound, float v)
 
float lg_sound_channel_get_volume (LG_Sound *sound)
 
void lg_sound_free (LG_Sound *sound)
 
void lg_audio_set_global_volume (float v)
 
void lg_audio_set_music_volume (float v)
 
void lg_audio_set_sounds_volume (float v)
 

Detailed Description

 === SDL2 Mixer audio system ===

 Unlike sounds, only *one* music playing at a time

 Should maybe use a linked list or sth, to get some kind of sound pool

Function Documentation

◆ lg_init_audio_to_defaults()

int lg_init_audio_to_defaults ( )

Init SDL2 Mixer audio system (using default values)

Returns
LG_OK if OK

◆ lg_init_audio()

int lg_init_audio ( int  freq,
uint16_t  format,
int  n_channels,
int  buffer_size 
)

Init SDL2 Mixer audio system

See: https://wiki.libsdl.org/SDL2_mixer/Mix_OpenAudio

Parameters
freqFrequency to playback audio (in Hz)
formatAudio format (one of SDL's AUDIO_* values)
n_channelsNum of audio channels
buffer_sizeSize of audio buffer
Returns
LG_OK if OK

◆ lg_free_audio()

void lg_free_audio ( )

Free SDL2 Mixer audio system resources

Returns
LG_OK if OK

◆ lg_load_play_music()

int lg_load_play_music ( const char *  file_name,
int  loops 
)

Load and play music

Parameters
file_nameA music file in the 'music' assets (don't use lg_mus() as it's already used internally)
loopsNumber of times to play, -1 = don't stop
Returns
LG_OK if OK

◆ lg_music_is_playing()

zboolean lg_music_is_playing ( )

Check state of music playing

Returns
TRUE if music is playing, FALSE otherwise

◆ lg_pause_music()

void lg_pause_music ( )

Pause music playing

◆ lg_resume_music()

void lg_resume_music ( )

Resume music playing

◆ lg_stop_music()

void lg_stop_music ( )

Stop music playing

◆ lg_free_music()

void lg_free_music ( )

Free music

◆ lg_sound_new()

LG_Sound* lg_sound_new ( const char *  file_name)

Load a new sound

Parameters
file_nameA sound file in the 'sounds' assets (don't use lg_snd() as it's already used internally)
Returns
A new LG_Sound instance if OK, NULL on error

◆ lg_disable_cant_play_audio_file()

void lg_disable_cant_play_audio_file ( )

Disable "Can't play audio file" logging - can be quite verbose if some latency issues

◆ lg_enable_cant_play_audio_file()

void lg_enable_cant_play_audio_file ( )

Enable "Can't play audio file" logging - can be quite verbose if some latency issues

This is the default state

◆ lg_sound_play()

int lg_sound_play ( lg_play_mode  mode,
LG_Sound sound 
)

Play sound

Example use of lg_sound_*() funcs in file level_1.c:

// Global vars
LG_Sound *jet_engine_starting, *jet_engine, *contact;
static void init_level1_sounds()
{
jet_engine_starting = lg_sound_new("jet_engine_starting.ogg");
jet_engine = lg_sound_new("jet_engine.ogg");
contact = lg_sound_new("contact.ogg");
}
static void free_level1_sounds()
{
lg_sound_free(jet_engine_starting);
lg_sound_free(jet_engine);
lg_sound_free(contact);
}
int level_1()
{
// ...
init_level1_sounds();
// ...
while (1) {
// ...
lg_sound_play(LG_PLAY, jet_engine_starting);
// ...
lg_sound_play(LG_STOP, jet_engine_starting);
lg_sound_play(LG_REPEAT, jet_engine);
// ...
if (lg_bboxes_collide(&bbox_jet, &bbox1)) {
lg_sound_play(LG_PLAY, contact);
// ...
}
// ...
free_level1_sounds();
Parameters
modeA lg_play_mode, one of LG_PLAY, LG_REPEAT, LG_WAIT, LG_STOP
soundA LG_Sound instance
Returns
LG_OK if OK

◆ lg_sound_is_playing()

zboolean lg_sound_is_playing ( LG_Sound sound)

Check state of sound playing

Parameters
soundA LG_Sound instance
Returns
TRUE if sound is playing, FALSE otherwise

◆ lg_sound_channel_set_volume()

void lg_sound_channel_set_volume ( LG_Sound sound,
float  v 
)

Set volume of specific sound->channel

=== EXPERIMENTAL ===

Parameters
soundA LG_Sound instance
Returns
Volume in range [0.0, 1.0]

◆ lg_sound_channel_get_volume()

float lg_sound_channel_get_volume ( LG_Sound sound)

Get volume of specific sound->channel

=== EXPERIMENTAL ===

Parameters
soundA LG_Sound instance
Returns
Volume in range [0.0, 1.0], -1.0 on error

◆ lg_sound_free()

void lg_sound_free ( LG_Sound sound)

Free sound

Parameters
soundA LG_Sound instance

◆ lg_audio_set_global_volume()

void lg_audio_set_global_volume ( float  v)

Set audio (both music AND sounds) volume

Use lg_audio_set_music_volume() or lg_audio_set_sounds_volume() if you need more granular control

Parameters
vVolume, in range [0.0, 1.0]

◆ lg_audio_set_music_volume()

void lg_audio_set_music_volume ( float  v)

Set music volume

Parameters
vVolume, in range [0.0, 1.0]

◆ lg_audio_set_sounds_volume()

void lg_audio_set_sounds_volume ( float  v)

Set sounds volume

Parameters
vVolume, in range [0.0, 1.0]
lg_sound_new
LG_Sound * lg_sound_new(const char *file_name)
Definition: lg_audio.c:143
LG_Sound
Definition: lg_audio.h:40
lg_sound_free
void lg_sound_free(LG_Sound *sound)
Definition: lg_audio.c:323
lg_bboxes_collide
zboolean lg_bboxes_collide(LG_BBox *bbox1, LG_BBox *bbox2)
Definition: lg_collision_detect.c:216
lg_sound_play
int lg_sound_play(lg_play_mode mode, LG_Sound *sound)
Definition: lg_audio.c:231