Skip to content

Commit

Permalink
Moved out SoundList, dropped mapSounds global.
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Nov 9, 2023
1 parent debe437 commit 5617f65
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 73 deletions.
2 changes: 2 additions & 0 deletions src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "GUI/GUIMessageQueue.h"

#include "Media/Audio/AudioPlayer.h"
#include "Media/Audio/SoundList.h"
#include "Media/MediaPlayer.h"

#include "Io/Mouse.h"
Expand Down Expand Up @@ -758,6 +759,7 @@ void Engine::MM7_Initialize() {
Blob sounds_mm6 = pIcons_LOD_mm6 ? pIcons_LOD_mm6->LoadCompressedTexture("dsounds.bin") : Blob();
Blob sounds_mm8;
Blob sounds_mm7 = engine->_gameResourceManager->getEventsFile("dsounds.bin");

pSoundList = new SoundList;
pSoundList->FromFile(sounds_mm6, sounds_mm7, sounds_mm8);

Expand Down
85 changes: 30 additions & 55 deletions src/Media/Audio/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string>
#include <filesystem>
#include <utility>
#include <vector>
#include <thread>

#include "Engine/Graphics/Indoor.h"
Expand All @@ -14,57 +13,33 @@
#include "Engine/Objects/SpriteObject.h"
#include "Engine/Spells/Spells.h"
#include "Engine/Party.h"
#include "Engine/Snapshots/EntitySnapshots.h"
#include "Engine/Engine.h"
#include "Engine/MapInfo.h"

#include "Media/Audio/OpenALSoundProvider.h"

#include "GUI/GUIWindow.h"

#include "Library/Snapshots/SnapshotSerialization.h"
#include "Library/Compression/Compression.h"
#include "Library/Logger/Logger.h"

#include "Utility/DataPath.h"

#include "SoundInfo.h"
#include "SoundList.h"

std::unique_ptr<AudioPlayer> pAudioPlayer;
SoundList *pSoundList;

static constexpr std::array<float, 10> pSoundVolumeLevels = {{
0.0000000f, 0.1099999f, 0.2199999f, 0.3300000f, 0.4399999f,
0.5500000f, 0.6600000f, 0.7699999f, 0.8799999f, 0.9700000f
}};

std::map<uint32_t, SoundInfo> mapSounds;

constexpr Pid FAKE_HOUSE_DOOR_PID = Pid::fromPacked(-7);
constexpr Pid FAKE_HOUSE_SPEECH_PID = Pid::fromPacked(-6);

void SoundList::FromFile(const Blob &data_mm6, const Blob &data_mm7, const Blob &data_mm8) {
std::vector<SoundInfo> sounds;

if (data_mm6)
deserialize(data_mm6, &sounds, tags::append, tags::via<SoundInfo_MM6>);
if (data_mm7)
deserialize(data_mm7, &sounds, tags::append, tags::via<SoundInfo_MM7>);
if (data_mm8)
deserialize(data_mm8, &sounds, tags::append, tags::via<SoundInfo_MM7>);

assert(!sounds.empty());

for (const SoundInfo &sound : sounds)
mapSounds[sound.uSoundID] = sound;
}

extern OpenALSoundProvider *provider;

AudioPlayer::~AudioPlayer() {
// TODO(captainurist): actually get rid of this global
mapSounds.clear();
}
AudioPlayer::~AudioPlayer() = default;

void AudioPlayer::MusicPlayTrack(MusicID eTrack) {
if (currentMusicTrack == eTrack) {
Expand Down Expand Up @@ -217,36 +192,36 @@ void AudioPlayer::playSound(SoundID eSoundID, SoundPlaybackMode mode, Pid pid) {
return;
}

if (mapSounds.find(eSoundID) == mapSounds.end()) {
SoundInfo *si = pSoundList->soundInfo(eSoundID);
if (!si) {
logger->warning("AudioPlayer: sound id {} not found", eSoundID);
return;
}

SoundInfo &si = mapSounds[eSoundID];
//logger->Info("AudioPlayer: sound id {} found as '{}'", eSoundID, si.sName);

if (!si.dataSource) {
if (!si->dataSource) {
Blob buffer;

if (si.sName == "") { // enable this for bonus sound effects
if (si->sName == "") { // enable this for bonus sound effects
//logger->Info("AudioPlayer: trying to load bonus sound {}", eSoundID);
//buffer = LoadSound(int(eSoundID));
} else {
buffer = LoadSound(si.sName);
buffer = LoadSound(si->sName);
}

if (!buffer) {
logger->warning("AudioPlayer: failed to load sound {} ({})", eSoundID, si.sName);
logger->warning("AudioPlayer: failed to load sound {} ({})", eSoundID, si->sName);
return;
}

si.dataSource = CreateAudioBufferDataSource(std::move(buffer));
if (!si.dataSource) {
logger->warning("AudioPlayer: failed to create sound data source {} ({})", eSoundID, si.sName);
si->dataSource = CreateAudioBufferDataSource(std::move(buffer));
if (!si->dataSource) {
logger->warning("AudioPlayer: failed to create sound data source {} ({})", eSoundID, si->sName);
return;
}

si.dataSource = PlatformDataSourceInitialize(si.dataSource);
si->dataSource = PlatformDataSourceInitialize(si->dataSource);
}

PAudioSample sample = CreateAudioSample();
Expand All @@ -255,31 +230,31 @@ void AudioPlayer::playSound(SoundID eSoundID, SoundPlaybackMode mode, Pid pid) {
sample->SetVolume(uMasterVolume);

if (mode == SOUND_MODE_UI) {
result = _regularSoundPool.playNew(sample, si.dataSource);
result = _regularSoundPool.playNew(sample, si->dataSource);
} else if (mode == SOUND_MODE_EXCLUSIVE) {
_regularSoundPool.stopSoundId(eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si.dataSource, eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si->dataSource, eSoundID);
} else if (mode == SOUND_MODE_NON_RESETTABLE) {
result = _regularSoundPool.playUniqueSoundId(sample, si.dataSource, eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si->dataSource, eSoundID);
} else if (mode == SOUND_MODE_WALKING) {
if (_currentWalkingSample) {
_currentWalkingSample->Stop();
}
_currentWalkingSample = sample;
_currentWalkingSample->Open(si.dataSource);
_currentWalkingSample->Open(si->dataSource);
_currentWalkingSample->Play();
} else if (mode == SOUND_MODE_MUSIC) {
sample->SetVolume(uMusicVolume);
_regularSoundPool.stopSoundId(eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si.dataSource, eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si->dataSource, eSoundID);
} else if (mode == SOUND_MODE_SPEECH) {
sample->SetVolume(uVoiceVolume);
_regularSoundPool.stopSoundId(eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si.dataSource, eSoundID);
result = _regularSoundPool.playUniqueSoundId(sample, si->dataSource, eSoundID);
} else if (mode == SOUND_MODE_HOUSE_DOOR || mode == SOUND_MODE_HOUSE_SPEECH) {
pid = mode == SOUND_MODE_HOUSE_DOOR ? FAKE_HOUSE_DOOR_PID : FAKE_HOUSE_SPEECH_PID;
_regularSoundPool.stopPid(pid);
_regularSoundPool.playUniquePid(sample, si.dataSource, pid);
_regularSoundPool.playUniquePid(sample, si->dataSource, pid);
} else {
assert(pid);

Expand All @@ -294,14 +269,14 @@ void AudioPlayer::playSound(SoundID eSoundID, SoundPlaybackMode mode, Pid pid) {
pIndoor->pDoors[object_id].pYOffsets[0],
pIndoor->pDoors[object_id].pZOffsets[0], MAX_SOUND_DIST);

result = _regularSoundPool.playUniquePid(sample, si.dataSource, pid, true);
result = _regularSoundPool.playUniquePid(sample, si->dataSource, pid, true);

break;
}

case OBJECT_Character: {
sample->SetVolume(uVoiceVolume);
result = _voiceSoundPool.playUniquePid(sample, si.dataSource, pid);
result = _voiceSoundPool.playUniquePid(sample, si->dataSource, pid);

break;
}
Expand All @@ -313,7 +288,7 @@ void AudioPlayer::playSound(SoundID eSoundID, SoundPlaybackMode mode, Pid pid) {
pActors[object_id].pos.y,
pActors[object_id].pos.z, MAX_SOUND_DIST);

result = _regularSoundPool.playUniquePid(sample, si.dataSource, pid, true);
result = _regularSoundPool.playUniquePid(sample, si->dataSource, pid, true);

break;
}
Expand All @@ -325,7 +300,7 @@ void AudioPlayer::playSound(SoundID eSoundID, SoundPlaybackMode mode, Pid pid) {
pLevelDecorations[object_id].vPosition.y,
pLevelDecorations[object_id].vPosition.z, MAX_SOUND_DIST);

result = _loopingSoundPool.playNew(sample, si.dataSource, true);
result = _loopingSoundPool.playNew(sample, si->dataSource, true);

break;
}
Expand All @@ -337,35 +312,35 @@ void AudioPlayer::playSound(SoundID eSoundID, SoundPlaybackMode mode, Pid pid) {
pSpriteObjects[object_id].vPosition.y,
pSpriteObjects[object_id].vPosition.z, MAX_SOUND_DIST);

result = _regularSoundPool.playUniquePid(sample, si.dataSource, pid, true);
result = _regularSoundPool.playUniquePid(sample, si->dataSource, pid, true);
break;
}

case OBJECT_Face: {
result = _regularSoundPool.playUniquePid(sample, si.dataSource, pid);
result = _regularSoundPool.playUniquePid(sample, si->dataSource, pid);

break;
}

default: {
result = _regularSoundPool.playNew(sample, si.dataSource);
result = _regularSoundPool.playNew(sample, si->dataSource);
logger->warning("Unexpected object type from Pid in playSound");
break;
}
}
}

if (!result) {
if (si.sName.empty()) {
logger->warning("AudioPlayer: failed to play audio {} with name '{}'", eSoundID, si.sName);
if (si->sName.empty()) {
logger->warning("AudioPlayer: failed to play audio {} with name '{}'", eSoundID, si->sName);
} else {
logger->warning("AudioPlayer: failed to play audio {}", eSoundID);
}
} else {
if (si.sName.empty()) {
if (si->sName.empty()) {
logger->trace("AudioPlayer: playing sound {}", eSoundID);
} else {
logger->trace("AudioPlayer: playing sound {} with name '{}'", eSoundID, si.sName);
logger->trace("AudioPlayer: playing sound {} with name '{}'", eSoundID, si->sName);
}
}
}
Expand Down
19 changes: 5 additions & 14 deletions src/Media/Audio/AudioPlayer.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#pragma once

#include <map>
#include <unordered_set>
#include <string>
#include <memory>
#include <list>

#include "Utility/Workaround/ToUnderlying.h"
#include "Engine/Pid.h"
#include "Engine/Spells/SpellEnums.h"
#include "Engine/Objects/ActorEnums.h"

#include "Media/Media.h"

#include "Utility/String.h"
#include "Utility/Memory/Blob.h"
#include "Utility/Streams/FileInputStream.h"
#include "Media/Media.h"
#include "Engine/Pid.h"
#include "Engine/Spells/SpellEnums.h"
#include "Engine/Objects/ActorEnums.h"

#include "SoundEnums.h"

Expand Down Expand Up @@ -183,14 +182,6 @@ class AudioPlayer {
PAudioSample _currentWalkingSample;
};

class SoundList {
public:
inline SoundList() {}

void FromFile(const Blob &data_mm6, const Blob &data_mm7, const Blob &data_mm8);
};

extern std::unique_ptr<AudioPlayer> pAudioPlayer;
extern SoundList *pSoundList;

void PlayLevelMusic();
6 changes: 4 additions & 2 deletions src/Media/Audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ cmake_minimum_required(VERSION 3.24 FATAL_ERROR)

set(MEDIA_AUDIO_SOURCES
AudioPlayer.cpp
OpenALSoundProvider.cpp)
OpenALSoundProvider.cpp
SoundList.cpp)

set(MEDIA_AUDIO_HEADERS
AudioPlayer.h
OpenALSoundProvider.h
SoundEnums.h
SoundInfo.h)
SoundInfo.h
SoundList.h)

add_library(media_audio STATIC ${MEDIA_AUDIO_SOURCES} ${MEDIA_AUDIO_HEADERS})
target_check_style(media_audio)
Expand Down
31 changes: 31 additions & 0 deletions src/Media/Audio/SoundList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "SoundList.h"

#include "Engine/Snapshots/EntitySnapshots.h"

#include "Library/Snapshots/SnapshotSerialization.h"

#include "Utility/MapAccess.h"

#include "SoundInfo.h"

SoundList *pSoundList;

void SoundList::FromFile(const Blob &data_mm6, const Blob &data_mm7, const Blob &data_mm8) {
std::vector<SoundInfo> sounds;

if (data_mm6)
deserialize(data_mm6, &sounds, tags::append, tags::via<SoundInfo_MM6>);
if (data_mm7)
deserialize(data_mm7, &sounds, tags::append, tags::via<SoundInfo_MM7>);
if (data_mm8)
deserialize(data_mm8, &sounds, tags::append, tags::via<SoundInfo_MM7>);

assert(!sounds.empty());

for (const SoundInfo &sound : sounds)
_mapSounds[sound.uSoundID] = sound;
}

SoundInfo *SoundList::soundInfo(int soundId) {
return valuePtr(_mapSounds, soundId);
}
20 changes: 20 additions & 0 deletions src/Media/Audio/SoundList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <unordered_map>

#include "Utility/Memory/Blob.h"

#include "SoundInfo.h"

class SoundList {
public:
void FromFile(const Blob &data_mm6, const Blob &data_mm7, const Blob &data_mm8);

// TODO(captainurist): should be const
SoundInfo *soundInfo(int soundId);

private:
std::unordered_map<int, SoundInfo> _mapSounds;
};

extern SoundList *pSoundList;
4 changes: 2 additions & 2 deletions src/Utility/MapAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Value valueOr(const Map &map, const Key &key, const std::type_identity_t<Value>
* @return Pointer to the value for the provided key, or `nullptr` if the key was not
* found in the map.
*/
template<class Map, class Key, class Value = typename Map::mapped_type>
const Value *valuePtr(const Map &map, const Key &key) {
template<class Map, class Key>
auto *valuePtr(Map &&map, const Key &key) {
auto pos = map.find(key);
return pos == map.end() ? nullptr : &pos->second;
}
Expand Down

0 comments on commit 5617f65

Please sign in to comment.