steed/src/sdl-instead/sound.c

216 lines
3.5 KiB
C
Raw Normal View History

2009-02-21 12:52:44 +02:00
#include "sound.h"
2009-08-26 08:25:53 +03:00
#include "input.h"
2009-02-21 12:52:44 +02:00
#include <SDL.h>
#include <SDL_mixer.h>
#include <stdio.h>
#include <stdlib.h>
Mix_Music *music = NULL;
int audio_rate = 22050;
Uint16 audio_format = MIX_DEFAULT_FORMAT;
int audio_channels = 2;
int audio_buffers = 8192;
static Mix_Music *mus;
static char *next_mus = NULL;
static int next_fadein = 0;
2009-08-26 08:25:53 +03:00
static int next_loop = -1;
2009-02-21 12:52:44 +02:00
static SDL_TimerID timer_id = NULL;
2009-08-26 08:25:53 +03:00
static int sound_on = 0;
static void mus_callback(void *aux)
2009-02-21 12:52:44 +02:00
{
2009-08-26 08:25:53 +03:00
if (!timer_id)
return;
if (snd_playing_mus())
return;
if (mus)
snd_free_mus(mus);
mus = NULL;
if (next_mus) {
snd_play_mus(next_mus, next_fadein, next_loop);
free(next_mus);
next_mus = NULL;
2009-02-21 12:52:44 +02:00
}
2009-08-26 08:25:53 +03:00
SDL_RemoveTimer(timer_id);
timer_id = NULL;
}
static Uint32 callback(Uint32 interval, void *aux)
{
push_user_event(mus_callback, aux);
return interval;
2009-02-21 12:52:44 +02:00
}
int snd_hz(void)
{
int freq = 0;
2009-08-26 08:25:53 +03:00
if (sound_on)
Mix_QuerySpec(&freq, NULL, NULL);
2009-02-21 12:52:44 +02:00
return freq;
}
2009-08-26 08:25:53 +03:00
int alsa_sw = 0;
int nosound_sw = 0;
2009-02-21 12:52:44 +02:00
int snd_init(int hz)
{
2009-08-26 08:25:53 +03:00
if (nosound_sw)
return -1;
2009-02-21 12:52:44 +02:00
if (!hz)
hz = audio_rate;
else
audio_rate = hz;
2009-08-26 08:25:53 +03:00
audio_buffers = (audio_rate / 11025) * 2048;
if (!audio_buffers) /* wrong parameter? */
audio_buffers = 8192;
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
fprintf(stderr, "Unable to init audio!\n");
return -1;
}
if (alsa_sw) {
SDL_AudioInit("alsa");
}
2009-02-21 12:52:44 +02:00
if (Mix_OpenAudio(hz, audio_format, audio_channels, audio_buffers)) {
fprintf(stderr, "Unable to open audio!\n");
return -1;
}
2009-08-26 08:25:53 +03:00
sound_on = 1;
2009-02-21 12:52:44 +02:00
return 0;
}
int snd_volume_mus(int vol)
{
2009-08-26 08:25:53 +03:00
if (!sound_on)
return 0;
2009-02-21 12:52:44 +02:00
Mix_Volume(-1, vol);
return Mix_VolumeMusic(vol);
}
2009-08-26 08:25:53 +03:00
wav_t snd_load_wav(const char *fname)
{
if (!sound_on)
return NULL;
if (!fname)
return NULL;
return (wav_t)Mix_LoadWAV(fname);
}
void snd_free_wav(wav_t w)
{
if (!w)
return;
Mix_HaltChannel(-1);
Mix_FreeChunk((Mix_Chunk*)w);
}
2009-02-21 12:52:44 +02:00
Mix_Music *snd_load_mus(const char *fname)
{
Mix_Music *mus;
2009-08-26 08:25:53 +03:00
if (!sound_on)
return NULL;
2009-02-21 12:52:44 +02:00
mus = Mix_LoadMUS(fname);
return mus;
}
2009-08-26 08:25:53 +03:00
extern void game_music_finished(void);
2009-02-21 12:52:44 +02:00
2009-08-26 08:25:53 +03:00
int snd_play_mus(char *fname, int ms, int loop)
2009-02-21 12:52:44 +02:00
{
2009-08-26 08:25:53 +03:00
if (!sound_on)
return 0;
2009-02-21 12:52:44 +02:00
if (snd_playing_mus()) {
if (next_mus) {
free(next_mus);
}
next_mus = strdup(fname);
next_fadein = ms;
2009-08-26 08:25:53 +03:00
next_loop = loop;
2009-02-21 12:52:44 +02:00
if (!timer_id)
timer_id = SDL_AddTimer(200, callback, NULL);
return 1;
}
if (mus)
snd_free_mus(mus);
mus = snd_load_mus(fname);
2009-08-26 08:25:53 +03:00
if (!mus) {
fprintf(stderr,"Can't load '%s'.\n", fname);
2009-02-21 12:52:44 +02:00
return 0;
2009-08-26 08:25:53 +03:00
}
if (loop >= 0)
Mix_HookMusicFinished(game_music_finished);
else
Mix_HookMusicFinished(NULL);
2009-02-21 12:52:44 +02:00
if (ms)
2009-08-26 08:25:53 +03:00
Mix_FadeInMusic((Mix_Music*)mus, loop, ms);
2009-02-21 12:52:44 +02:00
else
2009-08-26 08:25:53 +03:00
Mix_PlayMusic((Mix_Music*)mus, loop);
snd_volume_mus(snd_volume_mus(-1)); // SDL hack?
2009-02-21 12:52:44 +02:00
return 0;
}
void snd_stop_mus(int ms)
{
2009-08-26 08:25:53 +03:00
if (!sound_on)
return;
Mix_HookMusicFinished(NULL);
2009-02-21 12:52:44 +02:00
if (ms)
Mix_FadeOutMusic(ms);
else
Mix_HaltMusic();
}
int snd_playing_mus(void)
{
2009-08-26 08:25:53 +03:00
if (!sound_on)
return 0;
2009-02-21 12:52:44 +02:00
if (Mix_PlayingMusic() | Mix_FadingMusic())
return 1;
return 0;
}
void snd_free_mus(mus_t mus)
{
2009-08-26 08:25:53 +03:00
if (!sound_on)
return;
2009-02-21 12:52:44 +02:00
Mix_HaltMusic();
Mix_FreeMusic((Mix_Music*) mus);
}
2009-08-26 08:25:53 +03:00
void snd_play(void *chunk)
{
if (!sound_on)
return;
if (!chunk)
return;
Mix_PlayChannel(-1, (Mix_Chunk*)chunk, 0);
}
2009-02-21 12:52:44 +02:00
void snd_done(void)
{
2009-08-26 08:25:53 +03:00
if (!sound_on)
return;
2009-02-21 12:52:44 +02:00
if (timer_id)
2009-08-26 08:25:53 +03:00
Mix_HaltChannel(-1);
Mix_HaltMusic();
2009-02-21 12:52:44 +02:00
timer_id = NULL;
if (mus)
Mix_FreeMusic((Mix_Music*) mus);
mus = NULL;
if (next_mus)
free(next_mus);
next_mus = NULL;
Mix_CloseAudio();
2009-08-26 08:25:53 +03:00
SDL_QuitSubSystem(SDL_INIT_AUDIO);
2009-02-21 12:52:44 +02:00
}