You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
333 lines
7.7 KiB
333 lines
7.7 KiB
|
6 years ago
|
/**
|
||
|
|
* @file sound.cpp
|
||
|
|
*
|
||
|
|
* Implementation of functions setting up the audio pipeline.
|
||
|
|
*/
|
||
|
4 years ago
|
#include "engine/sound.h"
|
||
|
5 years ago
|
|
||
|
5 years ago
|
#include <cstdint>
|
||
|
5 years ago
|
#include <list>
|
||
|
5 years ago
|
#include <memory>
|
||
|
5 years ago
|
#include <mutex>
|
||
|
5 years ago
|
|
||
|
7 years ago
|
#include <SDL.h>
|
||
|
|
|
||
|
4 years ago
|
#include "engine/assets.hpp"
|
||
|
5 years ago
|
#include "init.h"
|
||
|
|
#include "options.h"
|
||
|
5 years ago
|
#include "utils/log.hpp"
|
||
|
5 years ago
|
#include "utils/math.h"
|
||
|
5 years ago
|
#include "utils/sdl_mutex.h"
|
||
|
5 years ago
|
#include "utils/stdcompat/algorithm.hpp"
|
||
|
5 years ago
|
#include "utils/stdcompat/optional.hpp"
|
||
|
5 years ago
|
#include "utils/stdcompat/shared_ptr_array.hpp"
|
||
|
5 years ago
|
#include "utils/stubs.h"
|
||
|
5 years ago
|
|
||
|
5 years ago
|
namespace devilution {
|
||
|
7 years ago
|
|
||
|
5 years ago
|
bool gbSndInited;
|
||
|
5 years ago
|
/** The active background music track id. */
|
||
|
|
_music_id sgnMusicTrack = NUM_MUSIC;
|
||
|
7 years ago
|
|
||
|
5 years ago
|
bool gbMusicOn = true;
|
||
|
|
/** Specifies whether sound effects are enabled. */
|
||
|
|
bool gbSoundOn = true;
|
||
|
|
|
||
|
5 years ago
|
namespace {
|
||
|
|
|
||
|
4 years ago
|
SoundSample music;
|
||
|
4 years ago
|
|
||
|
|
std::string GetMp3Path(const char *path)
|
||
|
|
{
|
||
|
|
std::string mp3Path = path;
|
||
|
|
const std::string::size_type dot = mp3Path.find_last_of('.');
|
||
|
|
mp3Path.replace(dot + 1, mp3Path.size() - (dot + 1), "mp3");
|
||
|
|
return mp3Path;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
bool LoadAudioFile(const char *path, bool stream, bool errorDialog, SoundSample &result)
|
||
|
5 years ago
|
{
|
||
|
4 years ago
|
#ifndef STREAM_ALL_AUDIO
|
||
|
|
if (stream) {
|
||
|
5 years ago
|
#endif
|
||
|
4 years ago
|
if (result.SetChunkStream(GetMp3Path(path), /*isMp3=*/true, /*logErrors=*/false) != 0) {
|
||
|
|
SDL_ClearError();
|
||
|
|
if (result.SetChunkStream(path, /*isMp3=*/false, /*logErrors=*/true) != 0) {
|
||
|
|
if (errorDialog)
|
||
|
|
ErrSdl();
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#ifndef STREAM_ALL_AUDIO
|
||
|
|
} else {
|
||
|
|
bool isMp3 = true;
|
||
|
|
SDL_RWops *file = OpenAsset(GetMp3Path(path).c_str());
|
||
|
|
if (file == nullptr) {
|
||
|
|
SDL_ClearError();
|
||
|
|
isMp3 = false;
|
||
|
|
file = OpenAsset(path);
|
||
|
|
if (file == nullptr) {
|
||
|
|
if (errorDialog)
|
||
|
|
ErrDlg("OpenAsset failed", path, __FILE__, __LINE__);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
size_t dwBytes = SDL_RWsize(file);
|
||
|
|
auto waveFile = MakeArraySharedPtr<std::uint8_t>(dwBytes);
|
||
|
|
if (SDL_RWread(file, waveFile.get(), dwBytes, 1) == 0) {
|
||
|
|
if (errorDialog)
|
||
|
|
ErrDlg("Failed to read file", fmt::format("{}: {}", path, SDL_GetError()), __FILE__, __LINE__);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
int error = result.SetChunk(waveFile, dwBytes, isMp3);
|
||
|
|
SDL_RWclose(file);
|
||
|
|
if (error != 0) {
|
||
|
|
if (errorDialog)
|
||
|
|
ErrSdl();
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
5 years ago
|
#endif
|
||
|
4 years ago
|
return true;
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
std::list<std::unique_ptr<SoundSample>> duplicateSounds;
|
||
|
5 years ago
|
std::optional<SdlMutex> duplicateSoundsMutex;
|
||
|
5 years ago
|
|
||
|
5 years ago
|
SoundSample *DuplicateSound(const SoundSample &sound)
|
||
|
|
{
|
||
|
5 years ago
|
auto duplicate = std::make_unique<SoundSample>();
|
||
|
|
if (duplicate->DuplicateFrom(sound) != 0)
|
||
|
|
return nullptr;
|
||
|
|
auto *result = duplicate.get();
|
||
|
5 years ago
|
decltype(duplicateSounds.begin()) it;
|
||
|
5 years ago
|
{
|
||
|
5 years ago
|
const std::lock_guard<SdlMutex> lock(*duplicateSoundsMutex);
|
||
|
5 years ago
|
duplicateSounds.push_back(std::move(duplicate));
|
||
|
5 years ago
|
it = duplicateSounds.end();
|
||
|
5 years ago
|
--it;
|
||
|
|
}
|
||
|
5 years ago
|
result->SetFinishCallback([it]([[maybe_unused]] Aulib::Stream &stream) {
|
||
|
5 years ago
|
const std::lock_guard<SdlMutex> lock(*duplicateSoundsMutex);
|
||
|
5 years ago
|
duplicateSounds.erase(it);
|
||
|
|
});
|
||
|
5 years ago
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
/** Maps from track ID to track name in spawn. */
|
||
|
5 years ago
|
const char *const SpawnMusicTracks[NUM_MUSIC] = {
|
||
|
7 years ago
|
"Music\\sTowne.wav",
|
||
|
|
"Music\\sLvlA.wav",
|
||
|
6 years ago
|
"Music\\sLvlA.wav",
|
||
|
|
"Music\\sLvlA.wav",
|
||
|
|
"Music\\sLvlA.wav",
|
||
|
5 years ago
|
"Music\\DLvlF.wav",
|
||
|
4 years ago
|
"Music\\DLvlE.wav",
|
||
|
6 years ago
|
"Music\\sintro.wav",
|
||
|
5 years ago
|
};
|
||
|
|
/** Maps from track ID to track name. */
|
||
|
5 years ago
|
const char *const MusicTracks[NUM_MUSIC] = {
|
||
|
7 years ago
|
"Music\\DTowne.wav",
|
||
|
|
"Music\\DLvlA.wav",
|
||
|
|
"Music\\DLvlB.wav",
|
||
|
|
"Music\\DLvlC.wav",
|
||
|
|
"Music\\DLvlD.wav",
|
||
|
6 years ago
|
"Music\\DLvlF.wav",
|
||
|
4 years ago
|
"Music\\DLvlE.wav",
|
||
|
6 years ago
|
"Music\\Dintro.wav",
|
||
|
7 years ago
|
};
|
||
|
|
|
||
|
5 years ago
|
int CapVolume(int volume)
|
||
|
5 years ago
|
{
|
||
|
5 years ago
|
return clamp(volume, VOLUME_MIN, VOLUME_MAX);
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace
|
||
|
|
|
||
|
5 years ago
|
void ClearDuplicateSounds()
|
||
|
|
{
|
||
|
5 years ago
|
const std::lock_guard<SdlMutex> lock(*duplicateSoundsMutex);
|
||
|
5 years ago
|
duplicateSounds.clear();
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void snd_play_snd(TSnd *pSnd, int lVolume, int lPan)
|
||
|
|
{
|
||
|
5 years ago
|
if (pSnd == nullptr || !gbSoundOn) {
|
||
|
7 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
uint32_t tc = SDL_GetTicks();
|
||
|
7 years ago
|
if (tc - pSnd->start_tc < 80) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
SoundSample *sound = &pSnd->DSB;
|
||
|
5 years ago
|
if (sound->IsPlaying()) {
|
||
|
|
sound = DuplicateSound(*sound);
|
||
|
5 years ago
|
if (sound == nullptr)
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
sound->PlayWithVolumeAndPan(lVolume, *sgOptions.Audio.soundVolume, lPan);
|
||
|
7 years ago
|
pSnd->start_tc = tc;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
std::unique_ptr<TSnd> sound_file_load(const char *path, bool stream)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
auto snd = std::make_unique<TSnd>();
|
||
|
|
snd->start_tc = SDL_GetTicks() - 80 - 1;
|
||
|
4 years ago
|
#ifndef NOSOUND
|
||
|
|
LoadAudioFile(path, stream, /*errorDialog=*/true, snd->DSB);
|
||
|
5 years ago
|
#endif
|
||
|
5 years ago
|
return snd;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
TSnd::~TSnd()
|
||
|
7 years ago
|
{
|
||
|
4 years ago
|
if (DSB.IsLoaded())
|
||
|
|
DSB.Stop();
|
||
|
5 years ago
|
DSB.Release();
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
void snd_init()
|
||
|
7 years ago
|
{
|
||
|
4 years ago
|
sgOptions.Audio.soundVolume.SetValue(CapVolume(*sgOptions.Audio.soundVolume));
|
||
|
|
gbSoundOn = *sgOptions.Audio.soundVolume > VOLUME_MIN;
|
||
|
6 years ago
|
sgbSaveSoundOn = gbSoundOn;
|
||
|
7 years ago
|
|
||
|
4 years ago
|
sgOptions.Audio.musicVolume.SetValue(CapVolume(*sgOptions.Audio.musicVolume));
|
||
|
|
gbMusicOn = *sgOptions.Audio.musicVolume > VOLUME_MIN;
|
||
|
7 years ago
|
|
||
|
5 years ago
|
// Initialize the SDL_audiolib library. Set the output sample rate to
|
||
|
|
// 22kHz, the audio format to 16-bit signed, use 2 output channels
|
||
|
|
// (stereo), and a 2KiB output buffer.
|
||
|
4 years ago
|
if (!Aulib::init(*sgOptions.Audio.sampleRate, AUDIO_S16, *sgOptions.Audio.channels, *sgOptions.Audio.bufferSize, *sgOptions.Audio.device)) {
|
||
|
5 years ago
|
LogError(LogCategory::Audio, "Failed to initialize audio (Aulib::init): {}", SDL_GetError());
|
||
|
|
return;
|
||
|
7 years ago
|
}
|
||
|
5 years ago
|
LogVerbose(LogCategory::Audio, "Aulib sampleRate={} channels={} frameSize={} format={:#x}",
|
||
|
|
Aulib::sampleRate(), Aulib::channelCount(), Aulib::frameSize(), Aulib::sampleFormat());
|
||
|
7 years ago
|
|
||
|
5 years ago
|
duplicateSoundsMutex.emplace();
|
||
|
7 years ago
|
gbSndInited = true;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
void snd_deinit()
|
||
|
|
{
|
||
|
5 years ago
|
if (gbSndInited) {
|
||
|
|
Aulib::quit();
|
||
|
5 years ago
|
duplicateSoundsMutex = std::nullopt;
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
|
gbSndInited = false;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
_music_id GetLevelMusic(dungeon_type dungeonType)
|
||
|
|
{
|
||
|
|
switch (dungeonType) {
|
||
|
|
case DTYPE_TOWN:
|
||
|
|
return TMUSIC_TOWN;
|
||
|
|
case DTYPE_CATHEDRAL:
|
||
|
|
return TMUSIC_CATHEDRAL;
|
||
|
|
case DTYPE_CATACOMBS:
|
||
|
|
return TMUSIC_CATACOMBS;
|
||
|
|
case DTYPE_CAVES:
|
||
|
|
return TMUSIC_CAVES;
|
||
|
|
case DTYPE_HELL:
|
||
|
|
return TMUSIC_HELL;
|
||
|
|
case DTYPE_NEST:
|
||
|
|
return TMUSIC_NEST;
|
||
|
|
case DTYPE_CRYPT:
|
||
|
|
return TMUSIC_CRYPT;
|
||
|
|
default:
|
||
|
|
return TMUSIC_INTRO;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
void music_stop()
|
||
|
|
{
|
||
|
4 years ago
|
music.Release();
|
||
|
|
sgnMusicTrack = NUM_MUSIC;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
4 years ago
|
void music_start(_music_id nTrack)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
const char *trackPath;
|
||
|
7 years ago
|
|
||
|
5 years ago
|
assert(nTrack < NUM_MUSIC);
|
||
|
7 years ago
|
music_stop();
|
||
|
4 years ago
|
if (!gbMusicOn)
|
||
|
|
return;
|
||
|
|
if (spawn_mpq)
|
||
|
|
trackPath = SpawnMusicTracks[nTrack];
|
||
|
|
else
|
||
|
|
trackPath = MusicTracks[nTrack];
|
||
|
4 years ago
|
|
||
|
4 years ago
|
#ifdef DISABLE_STREAMING_MUSIC
|
||
|
4 years ago
|
const bool stream = false;
|
||
|
4 years ago
|
#else
|
||
|
4 years ago
|
const bool stream = true;
|
||
|
4 years ago
|
#endif
|
||
|
4 years ago
|
if (!LoadAudioFile(trackPath, stream, /*errorDialog=*/false, music)) {
|
||
|
|
music_stop();
|
||
|
|
return;
|
||
|
|
}
|
||
|
7 years ago
|
|
||
|
4 years ago
|
music.SetVolume(*sgOptions.Audio.musicVolume, VOLUME_MIN, VOLUME_MAX);
|
||
|
|
if (!diablo_is_focused())
|
||
|
|
music_mute();
|
||
|
|
if (!music.Play(/*numIterations=*/0)) {
|
||
|
|
LogError(LogCategory::Audio, "Aulib::Stream::play (from music_start): {}", SDL_GetError());
|
||
|
|
music_stop();
|
||
|
|
return;
|
||
|
7 years ago
|
}
|
||
|
4 years ago
|
|
||
|
4 years ago
|
sgnMusicTrack = nTrack;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
void sound_disable_music(bool disable)
|
||
|
7 years ago
|
{
|
||
|
|
if (disable) {
|
||
|
|
music_stop();
|
||
|
6 years ago
|
} else if (sgnMusicTrack != NUM_MUSIC) {
|
||
|
7 years ago
|
music_start(sgnMusicTrack);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int sound_get_or_set_music_volume(int volume)
|
||
|
|
{
|
||
|
|
if (volume == 1)
|
||
|
4 years ago
|
return *sgOptions.Audio.musicVolume;
|
||
|
7 years ago
|
|
||
|
4 years ago
|
sgOptions.Audio.musicVolume.SetValue(volume);
|
||
|
7 years ago
|
|
||
|
4 years ago
|
if (music.IsLoaded())
|
||
|
|
music.SetVolume(*sgOptions.Audio.musicVolume, VOLUME_MIN, VOLUME_MAX);
|
||
|
7 years ago
|
|
||
|
4 years ago
|
return *sgOptions.Audio.musicVolume;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
|
int sound_get_or_set_sound_volume(int volume)
|
||
|
|
{
|
||
|
|
if (volume == 1)
|
||
|
4 years ago
|
return *sgOptions.Audio.soundVolume;
|
||
|
7 years ago
|
|
||
|
4 years ago
|
sgOptions.Audio.soundVolume.SetValue(volume);
|
||
|
7 years ago
|
|
||
|
4 years ago
|
return *sgOptions.Audio.soundVolume;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
void music_mute()
|
||
|
|
{
|
||
|
4 years ago
|
if (music.IsLoaded())
|
||
|
|
music.Mute();
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
|
void music_unmute()
|
||
|
|
{
|
||
|
4 years ago
|
if (music.IsLoaded())
|
||
|
|
music.Unmute();
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace devilution
|