Browse Source

♻️ Cleanup DISABLE_STREAMING_MUSIC handling

Extracts `LoadMusic` and `CleanupMusic` functions.
pull/1754/head
Gleb Mazovetskiy 5 years ago committed by Anders Jenbo
parent
commit
0dd9aeea69
  1. 84
      Source/sound.cpp
  2. 2
      Source/utils/soundsample.cpp

84
Source/sound.cpp

@ -23,8 +23,10 @@
namespace devilution { namespace devilution {
bool gbSndInited; bool gbSndInited;
/** Specifies whether background music is enabled. */ /** Handle to the music Storm file. */
HANDLE sghMusic; HANDLE sghMusic;
/** The active background music track id. */
_music_id sgnMusicTrack = NUM_MUSIC;
namespace { namespace {
@ -32,15 +34,39 @@ std::optional<Aulib::Stream> music;
#ifdef DISABLE_STREAMING_MUSIC #ifdef DISABLE_STREAMING_MUSIC
char *musicBuffer; char *musicBuffer;
#endif
void LoadMusic()
{
#ifndef DISABLE_STREAMING_MUSIC
SDL_RWops *musicRw = SFileRw_FromStormHandle(sghMusic);
#else
int bytestoread = SFileGetFileSize(sghMusic, 0);
musicBuffer = (char *)DiabloAllocPtr(bytestoread);
SFileReadFile(sghMusic, musicBuffer, bytestoread, NULL, 0);
SFileCloseFile(sghMusic);
sghMusic = NULL;
void FreeMusicBuffer() SDL_RWops *musicRw = SDL_RWFromConstMem(musicBuffer, bytestoread);
#endif
music.emplace(musicRw, std::make_unique<Aulib::DecoderDrwav>(),
std::make_unique<Aulib::ResamplerSpeex>(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true);
}
void CleanupMusic()
{ {
music = std::nullopt;
sgnMusicTrack = NUM_MUSIC;
#ifndef DISABLE_STREAMING_MUSIC
SFileCloseFile(sghMusic);
sghMusic = nullptr;
#else
if (musicBuffer != nullptr) { if (musicBuffer != nullptr) {
mem_free_dbg(musicBuffer); mem_free_dbg(musicBuffer);
musicBuffer = nullptr; musicBuffer = nullptr;
} }
#endif
} }
#endif // DISABLE_STREAMING_MUSIC
} // namespace } // namespace
@ -49,8 +75,7 @@ void FreeMusicBuffer()
bool gbMusicOn = true; bool gbMusicOn = true;
/** Specifies whether sound effects are enabled. */ /** Specifies whether sound effects are enabled. */
bool gbSoundOn = true; bool gbSoundOn = true;
/** Specifies the active background music track id. */
_music_id sgnMusicTrack = NUM_MUSIC;
/** Maps from track ID to track name in spawn. */ /** Maps from track ID to track name in spawn. */
const char *const sgszSpawnMusicTracks[NUM_MUSIC] = { const char *const sgszSpawnMusicTracks[NUM_MUSIC] = {
"Music\\sTowne.wav", "Music\\sTowne.wav",
@ -200,17 +225,8 @@ void snd_deinit() {
void music_stop() void music_stop()
{ {
if (music) { if (music)
music = std::nullopt; CleanupMusic();
#ifndef DISABLE_STREAMING_MUSIC
SFileCloseFile(sghMusic);
sghMusic = nullptr;
#endif
sgnMusicTrack = NUM_MUSIC;
#ifdef DISABLE_STREAMING_MUSIC
FreeMusicBuffer();
#endif
}
} }
void music_start(uint8_t nTrack) void music_start(uint8_t nTrack)
@ -229,47 +245,17 @@ void music_start(uint8_t nTrack)
if (!success) { if (!success) {
sghMusic = nullptr; sghMusic = nullptr;
} else { } else {
#ifndef DISABLE_STREAMING_MUSIC LoadMusic();
SDL_RWops *musicRw = SFileRw_FromStormHandle(sghMusic);
#else
int bytestoread = SFileGetFileSize(sghMusic, 0);
musicBuffer = (char *)DiabloAllocPtr(bytestoread);
SFileReadFile(sghMusic, musicBuffer, bytestoread, NULL, 0);
SFileCloseFile(sghMusic);
sghMusic = NULL;
SDL_RWops *musicRw = SDL_RWFromConstMem(musicBuffer, bytestoread);
if (musicRw == nullptr)
ErrSdl();
#endif
music.emplace(musicRw, std::make_unique<Aulib::DecoderDrwav>(),
std::make_unique<Aulib::ResamplerSpeex>(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true);
if (!music->open()) { if (!music->open()) {
LogError(LogCategory::Audio, "Aulib::Stream::open (from music_start): {}", SDL_GetError()); LogError(LogCategory::Audio, "Aulib::Stream::open (from music_start): {}", SDL_GetError());
music = std::nullopt; CleanupMusic();
#ifndef DISABLE_STREAMING_MUSIC
SFileCloseFile(sghMusic);
sghMusic = nullptr;
#endif
sgnMusicTrack = NUM_MUSIC;
#ifdef DISABLE_STREAMING_MUSIC
FreeMusicBuffer();
#endif
return; return;
} }
music->setVolume(1.F - static_cast<float>(sgOptions.Audio.nMusicVolume) / VOLUME_MIN); music->setVolume(1.F - static_cast<float>(sgOptions.Audio.nMusicVolume) / VOLUME_MIN);
if (!music->play(/*iterations=*/0)) { if (!music->play(/*iterations=*/0)) {
LogError(LogCategory::Audio, "Aulib::Stream::play (from music_start): {}", SDL_GetError()); LogError(LogCategory::Audio, "Aulib::Stream::play (from music_start): {}", SDL_GetError());
music = std::nullopt; CleanupMusic();
#ifndef DISABLE_STREAMING_MUSIC
SFileCloseFile(sghMusic);
sghMusic = nullptr;
#endif
sgnMusicTrack = NUM_MUSIC;
#ifdef DISABLE_STREAMING_MUSIC
FreeMusicBuffer();
#endif
return; return;
} }

2
Source/utils/soundsample.cpp

@ -91,7 +91,7 @@ int SoundSample::SetChunk(std::unique_ptr<std::uint8_t[]> fileData, DWORD dwByte
if (!stream_->open()) { if (!stream_->open()) {
stream_ = std::nullopt; stream_ = std::nullopt;
file_data_ = nullptr; file_data_ = nullptr;
LogError(LogCategory::Audio, "Aulib::Stream::open (from SoundSample::SetChunkStream): {}", SDL_GetError()); LogError(LogCategory::Audio, "Aulib::Stream::open (from SoundSample::SetChunk): {}", SDL_GetError());
return -1; return -1;
} }

Loading…
Cancel
Save