Skip to content

Commit

Permalink
music: add support for multiple file formats.
Browse files Browse the repository at this point in the history
This patch adds support for more audio formats when playing audio tracks.
  • Loading branch information
carlo-bramini committed Jan 6, 2023
1 parent d2acd2b commit 682859d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## [Unreleased](https://github.com/rr-/Tomb1Main/compare/stable...develop)
- added support for .ogg, .mp3 and .wav formats for audio tracks (#688)

## [2.12](https://github.com/rr-/Tomb1Main/compare/2.11...2.12) - 2022-12-23
- added collision to save crystals (#654)
Expand Down
24 changes: 18 additions & 6 deletions src/game/music.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#include "game/music.h"

#include "config.h"
#include "filesystem.h"
#include "game/gameflow.h"
#include "game/sound.h"
#include "global/vars.h"
#include "log.h"
#include "memory.h"
#include "specific/s_audio.h"

#include <stdio.h>

static const char *m_Extensions[] = { ".flac", ".ogg", ".mp3", ".wav", NULL };

static float m_MusicVolume = 0.0f;
static int m_AudioStreamID = -1;
static int16_t m_Track = 0;
static int16_t m_TrackLooped = -1;

static void Music_StopActiveStream(void);
static void Music_StreamFinished(int stream_id, void *user_data);
static char *Music_GetTrackFileName(int track);

static void Music_StopActiveStream(void)
{
Expand All @@ -31,6 +36,13 @@ static void Music_StopActiveStream(void)
S_Audio_StreamSoundClose(m_AudioStreamID);
}

static char *Music_GetTrackFileName(int track)
{
char file_path[64];
sprintf(file_path, "music/track%02d.flac", track);
return File_GuessExtension(file_path, m_Extensions);
}

static void Music_StreamFinished(int stream_id, void *user_data)
{
// When a stream finishes, play the remembered background BGM.
Expand Down Expand Up @@ -79,11 +91,11 @@ bool Music_Play(int16_t track)
return false;
}

char file_path[64];
sprintf(file_path, "music/track%02d.flac", track);

Music_StopActiveStream();

char *file_path = Music_GetTrackFileName(track);
m_AudioStreamID = S_Audio_StreamSoundCreateFromFile(file_path);
Memory_FreePointer(&file_path);

if (m_AudioStreamID < 0) {
LOG_ERROR("All music streams are busy");
Expand All @@ -110,11 +122,11 @@ bool Music_PlayLooped(int16_t track)
return false;
}

char file_path[64];
sprintf(file_path, "music/track%02d.flac", track);

Music_StopActiveStream();

char *file_path = Music_GetTrackFileName(track);
m_AudioStreamID = S_Audio_StreamSoundCreateFromFile(file_path);
Memory_FreePointer(&file_path);

if (m_AudioStreamID < 0) {
LOG_ERROR("All music streams are busy");
Expand Down

0 comments on commit 682859d

Please sign in to comment.