aboutsummaryrefslogtreecommitdiff
path: root/lzr.c
diff options
context:
space:
mode:
Diffstat (limited to 'lzr.c')
-rw-r--r--lzr.c140
1 files changed, 88 insertions, 52 deletions
diff --git a/lzr.c b/lzr.c
index 6548f16..ee4d1e4 100644
--- a/lzr.c
+++ b/lzr.c
@@ -8,7 +8,7 @@
# include <SDL2/SDL_image.h>
#endif
#ifdef LZR_ENABLE_MIXER
-# include <SDL2/SDL_mixer.h>
+# include "cmixer.h"
#endif
#ifdef LZR_ENABLE_DEVMODE
# include <sys/stat.h>
@@ -51,9 +51,24 @@ static int mouse_y = 0;
#ifdef LZR_ENABLE_MIXER
static struct {
- Mix_Chunk *ptr;
+ cm_Source *ptr;
} sounds[LZR_MAX_SOUNDS] = {0};
-static Mix_Music *music = NULL;
+static SDL_mutex *audio_mutex = NULL;
+static SDL_AudioDeviceID audio_dev = 0;
+
+static void _lock_handler(cm_Event *e)
+{
+ if (e->type == CM_EVENT_LOCK)
+ SDL_LockMutex(audio_mutex);
+ else
+ SDL_UnlockMutex(audio_mutex);
+}
+
+static void _audio_callback(void *udata, Uint8 *stream, int size)
+{
+ (void)udata;
+ cm_process((void *)stream, size / 2);
+}
#endif
static char *_lzrstrdup(const char *str)
@@ -176,8 +191,7 @@ int LZR_Init(LZR_Config cfg)
(float)config.display_width / (float)config.display_height;
config.ratio /= ratio;
}
-
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
SDL_Log("%s", SDL_GetError());
return -1;
}
@@ -188,14 +202,26 @@ int LZR_Init(LZR_Config cfg)
}
#endif
#ifdef LZR_ENABLE_MIXER
- if (Mix_Init(MIX_INIT_OGG) != MIX_INIT_OGG) {
- SDL_Log("%s", Mix_GetError());
+ audio_mutex = SDL_CreateMutex();
+ if (audio_mutex == NULL) {
+ SDL_Log("%s", SDL_GetError());
return -1;
}
- if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 8, 1024) < 0) {
- SDL_Log("%s", Mix_GetError());
+ SDL_AudioSpec fmt = {.freq = 44100,
+ .format = AUDIO_S16,
+ .channels = 2,
+ .samples = 1024,
+ .callback = _audio_callback};
+ SDL_AudioSpec got;
+ audio_dev = SDL_OpenAudioDevice(NULL, 0, &fmt, &got,
+ SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
+ if (audio_dev == 0) {
+ SDL_Log("%s", SDL_GetError());
return -1;
}
+ cm_init(44100);
+ cm_set_lock(_lock_handler);
+ SDL_PauseAudioDevice(audio_dev, 0);
#endif
basepath = SDL_GetBasePath();
if (basepath == NULL) {
@@ -242,16 +268,25 @@ int LZR_Init(LZR_Config cfg)
void LZR_Quit(void)
{
- LZR_StopMusic();
#ifdef LZR_ENABLE_MIXER
- for (int i = 0; i < LZR_MAX_SOUNDS; i++)
+ for (int i = LZR_MAX_SOUNDS - 1; i >= 0; i--)
+ if (sounds[i].ptr != NULL)
+ cm_stop(sounds[i].ptr);
+ SDL_Delay(100);
+ for (int i = LZR_MAX_SOUNDS - 1; i >= 0; i--)
if (sounds[i].ptr != NULL) {
- Mix_FreeChunk(sounds[i].ptr);
+ cm_destroy_source(sounds[i].ptr);
sounds[i].ptr = NULL;
SDL_Log("destroyed sound %d", i);
}
- Mix_CloseAudio();
- Mix_Quit();
+ if (audio_dev != 0) {
+ SDL_CloseAudioDevice(audio_dev);
+ audio_dev = 0;
+ }
+ if (audio_mutex != NULL) {
+ SDL_DestroyMutex(audio_mutex);
+ audio_mutex = NULL;
+ }
#endif
for (int i = 0; i < LZR_MAX_IMAGES; i++) {
if (images[i].tex != NULL) {
@@ -399,13 +434,13 @@ int LZR_SoundLoad(const char *path, float volume)
SDL_Log("LZR_PathPrefix failed");
return -1;
}
- Mix_Chunk *const chunk = Mix_LoadWAV(apath);
+ cm_Source *const chunk = cm_new_source_from_file(apath);
free(apath);
if (chunk == NULL) {
- SDL_Log("%s: %s", path, Mix_GetError());
+ SDL_Log("%s: %s", path, cm_get_error());
return -1;
}
- Mix_VolumeChunk(chunk, volume * MIX_MAX_VOLUME);
+ cm_set_gain(chunk, volume);
sounds[i].ptr = chunk;
return i;
#else
@@ -781,7 +816,7 @@ int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip)
return 0;
}
-int LZR_PlaySound(int id)
+int LZR_PlaySound(int id, int loops)
{
#ifdef LZR_ENABLE_MIXER
if (id < 0) {
@@ -792,70 +827,71 @@ int LZR_PlaySound(int id)
SDL_Log("no sound with id %d", id);
return -1;
}
- if (Mix_PlayChannel(-1, sounds[id].ptr, 0) < 0) {
- SDL_Log("%s", Mix_GetError());
- return -1;
- }
+ cm_stop(sounds[id].ptr);
+ cm_set_loop(sounds[id].ptr, loops);
+ cm_play(sounds[id].ptr);
return 0;
#else
- (void)id;
+ (void)id, (void)loops;
SDL_Log("LZR MIXER module is disabled");
return -1;
#endif
}
-int LZR_SetMusicVolume(float volume)
+void LZR_StopSound(int id)
{
#ifdef LZR_ENABLE_MIXER
- if (Mix_VolumeMusic(volume * MIX_MAX_VOLUME) < 0) {
- SDL_Log("%s", Mix_GetError());
- return -1;
+ if (id < 0) {
+ SDL_Log("id is negative");
+ return;
}
- return 0;
+ if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) {
+ SDL_Log("no sound with id %d", id);
+ return;
+ }
+ cm_stop(sounds[id].ptr);
#else
- (void)volume;
- SDL_Log("LZR MIXER module is disabled");
- return -1;
+ (void)id;
#endif
}
-int LZR_PlayMusic(const char *path, int loops)
+int LZR_SetSoundVolume(int id, float volume)
{
#ifdef LZR_ENABLE_MIXER
- LZR_StopMusic();
- char *const apath = LZR_PathPrefix(path);
- if (apath == NULL) {
- SDL_Log("LZR_PathPrefix failed");
- return -1;
- }
- music = Mix_LoadMUS(apath);
- free(apath);
- if (music == NULL) {
- SDL_Log("%s: %s", path, Mix_GetError());
+ if (id < 0) {
+ SDL_Log("id is negative");
return -1;
}
- if (Mix_PlayMusic(music, loops) < 0) {
- SDL_Log("%s", Mix_GetError());
+ if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) {
+ SDL_Log("no sound with id %d", id);
return -1;
}
- Mix_RewindMusic();
+ cm_set_gain(sounds[id].ptr, volume);
return 0;
#else
- (void)path, (void)loops;
+ (void)id, (void)volume;
SDL_Log("LZR MIXER module is disabled");
return -1;
#endif
}
-void LZR_StopMusic(void)
+int LZR_SetSoundPan(int id, float pan)
{
#ifdef LZR_ENABLE_MIXER
- if (Mix_PlayingMusic())
- Mix_HaltMusic();
- if (music != NULL) {
- Mix_FreeMusic(music);
- music = NULL;
+ if (id < 0) {
+ SDL_Log("id is negative");
+ return -1;
+ }
+ if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) {
+ SDL_Log("no sound with id %d", id);
+ return -1;
}
+ cm_set_pan(sounds[id].ptr, pan);
+ return 0;
+#else
+ (void)id, (void)pan;
+ SDL_Log("LZR MIXER module is disabled");
+ return -1;
#endif
}