Skip to content

Commit

Permalink
Refactor sound device code
Browse files Browse the repository at this point in the history
  • Loading branch information
bvschaik committed Feb 6, 2020
1 parent 2f1ae99 commit 84b695c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 71 deletions.
109 changes: 52 additions & 57 deletions src/platform/sound_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
#define USE_SDL_AUDIOSTREAM
#endif

static int initialized = 0;
static Mix_Music *music = 0;
static Mix_Chunk *channels[MAX_CHANNELS];
static struct {
int initialized;
Mix_Music *music;
Mix_Chunk *chunks[MAX_CHANNELS];
} data;

static struct {
#ifdef USE_SDL_AUDIOSTREAM
SDL_AudioStream *stream;
Expand All @@ -36,7 +39,6 @@ static struct {
SDL_AudioFormat format;
} custom_music;


static int percentage_to_volume(int percentage)
{
return percentage * SDL_MIX_MAXVOLUME / 100;
Expand All @@ -45,9 +47,9 @@ static int percentage_to_volume(int percentage)
void sound_device_open(void)
{
if (0 == Mix_OpenAudio(AUDIO_RATE, AUDIO_FORMAT, AUDIO_CHANNELS, AUDIO_BUFFERS)) {
initialized = 1;
data.initialized = 1;
for (int i = 0; i < MAX_CHANNELS; i++) {
channels[i] = 0;
data.chunks[i] = 0;
}
} else {
log_error("SOUND: not initialized", 0, 0);
Expand All @@ -56,45 +58,45 @@ void sound_device_open(void)

void sound_device_close(void)
{
if (initialized) {
if (data.initialized) {
for (int i = 0; i < MAX_CHANNELS; i++) {
sound_device_stop_channel(i);
}
Mix_CloseAudio();
initialized = 0;
data.initialized = 0;
}
}

static void load_file_for_channel(const char *filename, int channel)
{
if (filename[0]) {
#ifdef __vita__
FILE *fp = file_open(filename, "rb");
SDL_RWops *sdl_fp = SDL_RWFromFP(fp, SDL_TRUE);
data.chunks[channel] = Mix_LoadWAV_RW(sdl_fp, 1);
#else
data.chunks[channel] = Mix_LoadWAV(filename);
#endif
}
}

void sound_device_init_channels(int num_channels, char filenames[][CHANNEL_FILENAME_MAX])
{
if (initialized) {
if (data.initialized) {
if (num_channels > MAX_CHANNELS) {
num_channels = MAX_CHANNELS;
}
Mix_AllocateChannels(num_channels);
log_info("Loading audio files", 0, 0);
for (int i = 0; i < num_channels; i++) {
if (filenames[i][0]) {
#ifdef __vita__
FILE *fp = file_open(filenames[i], "rb");
SDL_RWops *sdl_fp = SDL_RWFromFP(fp, SDL_TRUE);

channels[i] = Mix_LoadWAV_RW(sdl_fp, 1);
#else
channels[i] = Mix_LoadWAV(filenames[i]);
#endif
}
load_file_for_channel(filenames[i], i);
}
}
}

int sound_device_has_channel(int channel)
{
return channels[channel] ? 1 : 0;
}

int sound_device_is_channel_playing(int channel)
{
return Mix_Playing(channel);
return data.chunks[channel] && Mix_Playing(channel);
}

void sound_device_set_music_volume(int volume_percentage)
Expand All @@ -104,21 +106,21 @@ void sound_device_set_music_volume(int volume_percentage)

void sound_device_set_channel_volume(int channel, int volume_percentage)
{
if (channels[channel]) {
Mix_VolumeChunk(channels[channel], percentage_to_volume(volume_percentage));
if (data.chunks[channel]) {
Mix_VolumeChunk(data.chunks[channel], percentage_to_volume(volume_percentage));
}
}

void sound_device_set_channel_panning(int channel, int left_pct, int right_pct)
{
if (channels[channel]) {
if (data.chunks[channel]) {
Mix_SetPanning(channel, left_pct * 255 / 100, right_pct * 255 / 100);
}
}

int sound_device_play_music(const char *filename)
{
if (initialized) {
if (data.initialized) {
sound_device_stop_music();

#ifdef __vita__
Expand All @@ -127,12 +129,12 @@ int sound_device_play_music(const char *filename)
const char *resolved_filename = filename;
#endif

music = Mix_LoadMUS(resolved_filename);
if (!music) {
data.music = Mix_LoadMUS(resolved_filename);
if (!data.music) {
SDL_LogWarn(SDL_LOG_CATEGORY_AUDIO, "Error opening music file '%s'. Reason: %s", resolved_filename, Mix_GetError());
} else {
if (Mix_PlayMusic(music, -1) == -1) {
music = 0;
if (Mix_PlayMusic(data.music, -1) == -1) {
data.music = 0;
SDL_LogWarn(SDL_LOG_CATEGORY_AUDIO, "Error playing music file '%s'. Reason: %s", resolved_filename, Mix_GetError());
}
}
Expand All @@ -141,58 +143,51 @@ int sound_device_play_music(const char *filename)
free(resolved_filename);
#endif

return music ? 1 : 0;
return data.music ? 1 : 0;
}
return 0;
}

void sound_device_play_file_on_channel(const char *filename, int channel)
{
if (initialized) {
if (channels[channel]) {
if (data.initialized) {
if (data.chunks[channel]) {
sound_device_stop_channel(channel);
}
#ifdef __vita__
FILE *fp = file_open(filename, "rb");
SDL_RWops *sdl_fp = SDL_RWFromFP(fp, SDL_TRUE);

channels[channel] = Mix_LoadWAV_RW(sdl_fp, 1);
#else
channels[channel] = Mix_LoadWAV(filename);
#endif
if (channels[channel]) {
Mix_PlayChannel(channel, channels[channel], 0);
load_file_for_channel(filename, channel);
if (data.chunks[channel]) {
Mix_PlayChannel(channel, data.chunks[channel], 0);
}
}
}

void sound_device_play_channel(int channel)
{
if (initialized) {
if (channels[channel]) {
Mix_PlayChannel(channel, channels[channel], 0);
if (data.initialized) {
if (data.chunks[channel]) {
Mix_PlayChannel(channel, data.chunks[channel], 0);
}
}
}

void sound_device_stop_music(void)
{
if (initialized) {
if (music) {
if (data.initialized) {
if (data.music) {
Mix_HaltMusic();
Mix_FreeMusic(music);
music = 0;
Mix_FreeMusic(data.music);
data.music = 0;
}
}
}

void sound_device_stop_channel(int channel)
{
if (initialized) {
if (channels[channel]) {
if (data.initialized) {
if (data.chunks[channel]) {
Mix_HaltChannel(channel);
Mix_FreeChunk(channels[channel]);
channels[channel] = 0;
Mix_FreeChunk(data.chunks[channel]);
data.chunks[channel] = 0;
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/sound/city.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ void sound_city_init(void)
void sound_city_set_volume(int percentage)
{
for (int i = SOUND_CHANNEL_CITY_MIN; i <= SOUND_CHANNEL_CITY_MAX; i++) {
if (sound_device_has_channel(i)) {
sound_device_set_channel_volume(i, percentage);
}
sound_device_set_channel_volume(i, percentage);
}
}

Expand Down Expand Up @@ -239,7 +237,7 @@ static void play_channel(int channel, int direction)
if (!setting_sound(SOUND_CITY)->enabled) {
return;
}
if (!sound_device_has_channel(channel) || sound_device_is_channel_playing(channel)) {
if (sound_device_is_channel_playing(channel)) {
return;
}
int left_pan;
Expand Down
1 change: 0 additions & 1 deletion src/sound/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ void sound_device_open(void);
void sound_device_close(void);

void sound_device_init_channels(int num_channels, char filenames[][CHANNEL_FILENAME_MAX]);
int sound_device_has_channel(int channel);
int sound_device_is_channel_playing(int channel);

void sound_device_set_music_volume(int volume_percentage);
Expand Down
6 changes: 2 additions & 4 deletions src/sound/effect.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
void sound_effect_set_volume(int percentage)
{
for (int i = SOUND_CHANNEL_EFFECTS_MIN; i <= SOUND_CHANNEL_EFFECTS_MAX; i++) {
if (sound_device_has_channel(i)) {
sound_device_set_channel_volume(i, percentage);
}
sound_device_set_channel_volume(i, percentage);
}
}

Expand All @@ -18,7 +16,7 @@ void sound_effect_play(int effect)
if (!setting_sound(SOUND_EFFECTS)->enabled) {
return;
}
if (!sound_device_has_channel(effect) || sound_device_is_channel_playing(effect)) {
if (sound_device_is_channel_playing(effect)) {
return;
}
sound_device_play_channel(effect);
Expand Down
5 changes: 0 additions & 5 deletions test/stub/sound_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ void sound_device_close(void)
void sound_device_init_channels(int numChannels, char filenames[][CHANNEL_FILENAME_MAX])
{}

int sound_device_has_channel(int channel)
{
return 0;
}

int sound_device_is_channel_playing(int channel)
{
return 0;
Expand Down

0 comments on commit 84b695c

Please sign in to comment.