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_audio_init(int, uint16_t, int, int);
55 
56 void lg_audio_free();
57 
58 void lg_audio_set_global_volume(float);
59 
60 void lg_audio_set_music_volume(float);
61 
62 void lg_audio_set_sounds_volume(float);
63 
64 void lg_audio_pause();
65 
66 void lg_audio_resume();
67 
68 void lg_audio_stop();
69 
71 
73 
75 
76 LG_Sound *lg_sound_new(const char *);
77 
78 int lg_sound_play(lg_play_mode, LG_Sound *);
79 
80 zboolean lg_sound_is_playing(LG_Sound *);
81 
83 
85 
86 void lg_sound_free(LG_Sound *);
87 
88 int lg_music_load_and_play(const char *, int);
89 
90 zboolean lg_music_is_playing();
91 
92 void lg_music_pause();
93 
94 void lg_music_resume();
95 
96 void lg_music_stop();
97 
98 void lg_music_free();
99 
100 #endif /* LG_AUDIO_H */
lg_music_load_and_play
int lg_music_load_and_play(const char *file_name, int loops)
Definition: lg_audio.c:356
lg_audio_pause
void lg_audio_pause()
Definition: lg_audio.c:123
lg_sound_channel_get_volume
float lg_sound_channel_get_volume(LG_Sound *sound)
Definition: lg_audio.c:325
lg_sound_new
LG_Sound * lg_sound_new(const char *file_name)
Definition: lg_audio.c:178
lg_audio_get_n_channels
int lg_audio_get_n_channels()
Definition: lg_audio.c:149
LG_Sound
Definition: lg_audio.h:42
lg_music_resume
void lg_music_resume()
Definition: lg_audio.c:392
lg_audio_init
int lg_audio_init(int freq, uint16_t format, int n_channels, int buffer_size)
Definition: lg_audio.c:47
lg_music_is_playing
zboolean lg_music_is_playing()
Definition: lg_audio.c:374
lg_music_pause
void lg_music_pause()
Definition: lg_audio.c:382
lg_sound_free
void lg_sound_free(LG_Sound *sound)
Definition: lg_audio.c:339
lg_audio_resume
void lg_audio_resume()
Definition: lg_audio.c:131
lg_audio_init_to_defaults
int lg_audio_init_to_defaults()
Definition: lg_audio.c:27
lg_audio_free
void lg_audio_free()
Definition: lg_audio.c:62
lg_audio_disable_cant_play_file
void lg_audio_disable_cant_play_file()
Definition: lg_audio.c:157
lg_audio_stop
void lg_audio_stop()
Definition: lg_audio.c:139
lg_sound_is_playing
zboolean lg_sound_is_playing(LG_Sound *sound)
Definition: lg_audio.c:291
lg_music_stop
void lg_music_stop()
Definition: lg_audio.c:402
lg_sound_play
int lg_sound_play(lg_play_mode mode, LG_Sound *sound)
Definition: lg_audio.c:245
lg_sound_channel_set_volume
void lg_sound_channel_set_volume(LG_Sound *sound, float v)
Definition: lg_audio.c:308
lg_audio_set_sounds_volume
void lg_audio_set_sounds_volume(float v)
Definition: lg_audio.c:108
lg_music_free
void lg_music_free()
Definition: lg_audio.c:414
lg_audio_set_music_volume
void lg_audio_set_music_volume(float v)
Definition: lg_audio.c:91
lg_audio_enable_cant_play_file
void lg_audio_enable_cant_play_file()
Definition: lg_audio.c:167
lg_audio_set_global_volume
void lg_audio_set_global_volume(float v)
Definition: lg_audio.c:74