1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

Isolate all SDL_mixer references to SoundTrack/MusicTrack.

This looks redundant but will actually help in the transition to FAudio; we
mostly want to keep the game logic the same while reimplementing the current
mixer, weirdness and all. Once that's done and confirmed to be stable and
consistent we can start cutting out the workarounds and quirks.
This commit is contained in:
Ethan Lee 2022-01-14 16:46:04 -05:00
parent 017d54adb0
commit df618e6d22

View file

@ -18,10 +18,75 @@
#include <SDL_mixer.h> #include <SDL_mixer.h>
#include <vector> #include <vector>
#define VVV_MAX_VOLUME MIX_MAX_VOLUME
class MusicTrack class MusicTrack
{ {
public: public:
MusicTrack(SDL_RWops *rw); MusicTrack(SDL_RWops *rw)
{
m_music = Mix_LoadMUS_RW(rw, 1);
m_isValid = true;
if (m_music == NULL)
{
vlog_error("Unable to load Magic Binary Music file: %s", Mix_GetError());
m_isValid = false;
}
}
void Dispose()
{
Mix_FreeMusic(m_music);
}
bool Play(bool loop)
{
if (Mix_PlayMusic(m_music, loop ? -1 : 0) == -1)
{
vlog_error("Mix_PlayMusic: %s", Mix_GetError());
return false;
}
return true;
}
static void Init(int audio_rate, int audio_channels)
{
const Uint16 audio_format = AUDIO_S16SYS;
const int audio_buffers = 1024;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
{
vlog_error("Unable to initialize audio: %s", Mix_GetError());
SDL_assert(0 && "Unable to initialize audio!");
}
}
static void Halt()
{
Mix_HaltMusic();
}
static bool IsHalted()
{
return Mix_PausedMusic() == 1;
}
static void Pause()
{
Mix_PauseMusic();
}
static void Resume()
{
Mix_ResumeMusic();
}
static void SetVolume(int musicVolume)
{
Mix_VolumeMusic(musicVolume);
}
private:
Mix_Music *m_music; Mix_Music *m_music;
bool m_isValid; bool m_isValid;
}; };
@ -29,45 +94,60 @@ public:
class SoundTrack class SoundTrack
{ {
public: public:
SoundTrack(const char* fileName); SoundTrack(const char* fileName)
Mix_Chunk *sound; {
unsigned char *mem;
size_t length;
FILESYSTEM_loadAssetToMemory(fileName, &mem, &length, false);
if (mem == NULL)
{
m_sound = NULL;
vlog_error("Unable to load WAV file %s", fileName);
SDL_assert(0 && "WAV file missing!");
return;
}
SDL_RWops *fileIn = SDL_RWFromConstMem(mem, length);
m_sound = Mix_LoadWAV_RW(fileIn, 1);
FILESYSTEM_freeMemory(&mem);
if (m_sound == NULL)
{
vlog_error("Unable to load WAV file: %s", Mix_GetError());
}
}
void Dispose()
{
Mix_FreeChunk(m_sound);
}
void Play()
{
if (Mix_PlayChannel(-1, m_sound, 0) == -1)
{
vlog_error("Unable to play WAV file: %s", Mix_GetError());
}
}
static void Pause()
{
Mix_Pause(-1);
}
static void Resume()
{
Mix_Resume(-1);
}
static void SetVolume(int soundVolume)
{
Mix_Volume(-1, soundVolume);
}
private:
Mix_Chunk *m_sound;
}; };
MusicTrack::MusicTrack(SDL_RWops *rw)
{
m_music = Mix_LoadMUS_RW(rw, 1);
m_isValid = true;
if(m_music == NULL)
{
vlog_error("Unable to load Magic Binary Music file: %s", Mix_GetError());
m_isValid = false;
}
}
SoundTrack::SoundTrack(const char* fileName)
{
unsigned char *mem;
size_t length;
sound = NULL;
FILESYSTEM_loadAssetToMemory(fileName, &mem, &length, false);
if (mem == NULL)
{
vlog_error("Unable to load WAV file %s", fileName);
SDL_assert(0 && "WAV file missing!");
return;
}
SDL_RWops *fileIn = SDL_RWFromConstMem(mem, length);
sound = Mix_LoadWAV_RW(fileIn, 1);
FILESYSTEM_freeMemory(&mem);
if (sound == NULL)
{
vlog_error("Unable to load WAV file: %s", Mix_GetError());
}
}
static std::vector<MusicTrack> musicTracks; static std::vector<MusicTrack> musicTracks;
static std::vector<SoundTrack> soundTracks; static std::vector<SoundTrack> soundTracks;
@ -75,16 +155,7 @@ static std::vector<SoundTrack> soundTracks;
musicclass::musicclass(void) musicclass::musicclass(void)
{ {
int audio_rate = 44100; MusicTrack::Init(44100, 2);
Uint16 audio_format = AUDIO_S16SYS;
int audio_channels = 2;
int audio_buffers = 1024;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
{
vlog_error("Unable to initialize audio: %s", Mix_GetError());
SDL_assert(0 && "Unable to initialize audio!");
}
safeToProcessMusic= false; safeToProcessMusic= false;
m_doFadeInVol = false; m_doFadeInVol = false;
@ -244,17 +315,17 @@ void musicclass::destroy(void)
{ {
for (size_t i = 0; i < soundTracks.size(); ++i) for (size_t i = 0; i < soundTracks.size(); ++i)
{ {
Mix_FreeChunk(soundTracks[i].sound); soundTracks[i].Dispose();
} }
soundTracks.clear(); soundTracks.clear();
// Before we free all the music: stop playing music, else SDL2_mixer // Before we free all the music: stop playing music, else SDL2_mixer
// will call SDL_Delay() if we are fading, resulting in no-draw frames // will call SDL_Delay() if we are fading, resulting in no-draw frames
Mix_HaltMusic(); MusicTrack::Halt();
for (size_t i = 0; i < musicTracks.size(); ++i) for (size_t i = 0; i < musicTracks.size(); ++i)
{ {
Mix_FreeMusic(musicTracks[i].m_music); musicTracks[i].Dispose();
} }
musicTracks.clear(); musicTracks.clear();
@ -306,16 +377,12 @@ void musicclass::play(int t)
if (currentsong == 0 || currentsong == 7 || (!map.custommode && (currentsong == 0+num_mmmmmm_tracks || currentsong == 7+num_mmmmmm_tracks))) if (currentsong == 0 || currentsong == 7 || (!map.custommode && (currentsong == 0+num_mmmmmm_tracks || currentsong == 7+num_mmmmmm_tracks)))
{ {
// Level Complete theme, no fade in or repeat // Level Complete theme, no fade in or repeat
if (Mix_PlayMusic(musicTracks[t].m_music, 0) == -1) if (musicTracks[t].Play(false))
{
vlog_error("Mix_PlayMusic: %s", Mix_GetError());
}
else
{ {
m_doFadeInVol = false; m_doFadeInVol = false;
m_doFadeOutVol = false; m_doFadeOutVol = false;
musicVolume = MIX_MAX_VOLUME; musicVolume = VVV_MAX_VOLUME;
Mix_VolumeMusic(musicVolume); MusicTrack::SetVolume(musicVolume);
} }
} }
else else
@ -336,11 +403,7 @@ void musicclass::play(int t)
quick_fade = true; quick_fade = true;
} }
} }
else if (Mix_PlayMusic(musicTracks[t].m_music, -1) == -1) else if (musicTracks[t].Play(true))
{
vlog_error("Mix_PlayMusic: %s", Mix_GetError());
}
else
{ {
m_doFadeInVol = false; m_doFadeInVol = false;
m_doFadeOutVol = false; m_doFadeOutVol = false;
@ -351,7 +414,7 @@ void musicclass::play(int t)
void musicclass::resume() void musicclass::resume()
{ {
Mix_ResumeMusic(); MusicTrack::Resume();
} }
void musicclass::resumefade(const int fadein_ms) void musicclass::resumefade(const int fadein_ms)
@ -367,7 +430,7 @@ void musicclass::fadein(void)
void musicclass::pause(void) void musicclass::pause(void)
{ {
Mix_PauseMusic(); MusicTrack::Pause();
} }
void musicclass::haltdasmusik(void) void musicclass::haltdasmusik(void)
@ -441,12 +504,12 @@ void musicclass::fadeMusicVolumeIn(int ms)
musicVolume = 0; musicVolume = 0;
/* Fix 1-frame glitch */ /* Fix 1-frame glitch */
Mix_VolumeMusic(0); MusicTrack::SetVolume(0);
fade.step_ms = 0; fade.step_ms = 0;
fade.duration_ms = ms; fade.duration_ms = ms;
fade.start_volume = 0; fade.start_volume = 0;
fade.end_volume = MIX_MAX_VOLUME; fade.end_volume = VVV_MAX_VOLUME;
} }
void musicclass::fadeMusicVolumeOut(const int fadeout_ms) void musicclass::fadeMusicVolumeOut(const int fadeout_ms)
@ -461,7 +524,7 @@ void musicclass::fadeMusicVolumeOut(const int fadeout_ms)
fade.step_ms = 0; fade.step_ms = 0;
/* Duration is proportional to current volume. */ /* Duration is proportional to current volume. */
fade.duration_ms = fadeout_ms * musicVolume / MIX_MAX_VOLUME; fade.duration_ms = fadeout_ms * musicVolume / VVV_MAX_VOLUME;
fade.start_volume = musicVolume; fade.start_volume = musicVolume;
fade.end_volume = 0; fade.end_volume = 0;
} }
@ -618,48 +681,42 @@ void musicclass::playef(int t)
{ {
return; return;
} }
int channel; soundTracks[t].Play();
channel = Mix_PlayChannel(-1, soundTracks[t].sound, 0);
if(channel == -1)
{
vlog_error("Unable to play WAV file: %s", Mix_GetError());
}
} }
void musicclass::pauseef(void) void musicclass::pauseef(void)
{ {
Mix_Pause(-1); SoundTrack::Pause();
} }
void musicclass::resumeef(void) void musicclass::resumeef(void)
{ {
Mix_Resume(-1); SoundTrack::Resume();
} }
bool musicclass::halted(void) bool musicclass::halted(void)
{ {
return Mix_PausedMusic() == 1; return MusicTrack::IsHalted();
} }
void musicclass::updatemutestate(void) void musicclass::updatemutestate(void)
{ {
if (game.muted) if (game.muted)
{ {
Mix_VolumeMusic(0); MusicTrack::SetVolume(0);
Mix_Volume(-1, 0); SoundTrack::SetVolume(0);
} }
else else
{ {
Mix_Volume(-1, MIX_MAX_VOLUME * user_sound_volume / USER_VOLUME_MAX); SoundTrack::SetVolume(VVV_MAX_VOLUME * user_sound_volume / USER_VOLUME_MAX);
if (game.musicmuted) if (game.musicmuted)
{ {
Mix_VolumeMusic(0); MusicTrack::SetVolume(0);
} }
else else
{ {
Mix_VolumeMusic(musicVolume * user_music_volume / USER_VOLUME_MAX); MusicTrack::SetVolume(musicVolume * user_music_volume / USER_VOLUME_MAX);
} }
} }
} }