LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
lg_audio.h
1 /*
2  * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2025
3  * All rights reserved
4  */
5 
6 #ifndef LG_AUDIO_H
7 #define LG_AUDIO_H
8 
9 /*
10  * See: https://wiki.libsdl.org/SDL2_mixer/CategoryAPI
11  *
12  * Plus, from: https://github.com/libsdl-org/SDL_mixer/blob/SDL2/include/SDL_mixer.h
13  *
14  * The audio device frequency is specified in Hz; in modern times, 48000 is
15  * often a reasonable default.
16  *
17  * The audio device format is one of SDL's AUDIO_* constants. AUDIO_S16SYS
18  * (16-bit audio) is probably a safe default. More modern systems may prefer
19  * AUDIO_F32SYS (32-bit floating point audio).
20  *
21  * The audio device channels are generally 1 for mono output, or 2 for stereo,
22  * but the brave can try surround sound configs with 4 (quad), 6 (5.1), 7
23  * (6.1) or 8 (7.1).
24  *
25  * The audio device's chunk size is the number of sample frames (one sample
26  * per frame for mono output, two samples per frame in a stereo setup, etc)
27  * that are fed to the device at once. The lower the number, the lower the
28  * latency, but you risk dropouts if it gets too low. 2048 is often a
29  * reasonable default, but your app might want to experiment with 1024 or
30  * 4096.
31  */
32 
33 #define LG_AUDIO_FREQ 48000 /* High values reduce latency - previously 44100 */
34 #define LG_AUDIO_FORMAT AUDIO_S16SYS /* Previously AUDIO_F32SYS */
35 #define LG_AUDIO_N_CHANNELS 2
36 #define LG_AUDIO_BUF_SIZE (2 * 1024) /* Low values reduce latency - previously 1024 */
37 
38 #define LG_AUDIO_MAX_VOL MIX_MAX_VOLUME
39 
40 #define SOUND_FILENAME_MAXLEN (64 - 1)
41 
42 typedef struct {
43  char file_name[SOUND_FILENAME_MAXLEN + 1];
44  Mix_Chunk *sample; /* sample->volume in range [0, 128] */
45  int channel; /* 8 available channels */
46 } LG_Sound;
47 
48 typedef enum {
49  LG_PLAY, LG_REPEAT, LG_WAIT, LG_STOP, LG_PAUSE, LG_RESUME
50 } lg_play_mode;
51 
53 
54 int lg_init_audio(int, uint16_t, int, int);
55 
56 void lg_free_audio();
57 
58 int lg_load_play_music(const char *, int);
59 
60 zboolean lg_music_is_playing();
61 
62 void lg_pause_music();
63 
64 void lg_resume_music();
65 
66 void lg_stop_music();
67 
68 void lg_free_music();
69 
70 LG_Sound *lg_sound_new(const char *);
71 
73 
75 
76 int lg_sound_play(lg_play_mode, LG_Sound *);
77 
78 zboolean lg_sound_is_playing(LG_Sound *);
79 
81 
83 
84 void lg_sound_free(LG_Sound *);
85 
86 void lg_audio_set_global_volume(float);
87 
88 void lg_audio_set_music_volume(float);
89 
90 void lg_audio_set_sounds_volume(float);
91 
92 void lg_audio_pause();
93 
94 void lg_audio_resume();
95 
97 
98 #endif /* LG_AUDIO_H */
lg_load_play_music
int lg_load_play_music(const char *file_name, int loops)
Definition: lg_audio.c:72
lg_audio_pause
void lg_audio_pause()
Definition: lg_audio.c:393
lg_sound_channel_get_volume
float lg_sound_channel_get_volume(LG_Sound *sound)
Definition: lg_audio.c:313
lg_sound_new
LG_Sound * lg_sound_new(const char *file_name)
Definition: lg_audio.c:145
lg_audio_get_n_channels
int lg_audio_get_n_channels()
Definition: lg_audio.c:419
lg_enable_cant_play_audio_file
void lg_enable_cant_play_audio_file()
Definition: lg_audio.c:179
LG_Sound
Definition: lg_audio.h:42
lg_resume_music
void lg_resume_music()
Definition: lg_audio.c:108
lg_disable_cant_play_audio_file
void lg_disable_cant_play_audio_file()
Definition: lg_audio.c:169
lg_init_audio_to_defaults
int lg_init_audio_to_defaults()
Definition: lg_audio.c:25
lg_music_is_playing
zboolean lg_music_is_playing()
Definition: lg_audio.c:90
lg_sound_free
void lg_sound_free(LG_Sound *sound)
Definition: lg_audio.c:327
lg_pause_music
void lg_pause_music()
Definition: lg_audio.c:98
lg_init_audio
int lg_init_audio(int freq, uint16_t format, int n_channels, int buffer_size)
Definition: lg_audio.c:45
lg_free_audio
void lg_free_audio()
Definition: lg_audio.c:60
lg_free_music
void lg_free_music()
Definition: lg_audio.c:130
lg_audio_resume
void lg_audio_resume()
Definition: lg_audio.c:401
lg_stop_music
void lg_stop_music()
Definition: lg_audio.c:118
lg_sound_is_playing
zboolean lg_sound_is_playing(LG_Sound *sound)
Definition: lg_audio.c:279
lg_sound_play
int lg_sound_play(lg_play_mode mode, LG_Sound *sound)
Definition: lg_audio.c:233
lg_sound_channel_set_volume
void lg_sound_channel_set_volume(LG_Sound *sound, float v)
Definition: lg_audio.c:296
lg_audio_set_sounds_volume
void lg_audio_set_sounds_volume(float v)
Definition: lg_audio.c:378
lg_audio_set_music_volume
void lg_audio_set_music_volume(float v)
Definition: lg_audio.c:361
lg_audio_set_global_volume
void lg_audio_set_global_volume(float v)
Definition: lg_audio.c:344