Skip to content

Commit

Permalink
989snd: Pull stuff out of pointless classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziemas committed Dec 8, 2023
1 parent 168afa0 commit b88b0c3
Show file tree
Hide file tree
Showing 20 changed files with 402 additions and 503 deletions.
15 changes: 7 additions & 8 deletions game/sound/989snd/ame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ u64 SoundFlavaHack = 0;
u8 GlobalExcite = 0;

AmeHandler::AmeHandler(MultiMidi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: m_sound(sound), m_bank(bank), m_header(block), m_vm(vm), m_repeats(sound.Repeats) {
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: m_sound(sound), m_bank(bank), m_header(block), m_repeats(sound.Repeats) {
if (vol == VOLUME_DONT_CHANGE) {
vol = 1024;
}
Expand Down Expand Up @@ -58,8 +57,8 @@ void AmeHandler::StartSegment(u32 id) {
// Skip adding if not midi type
u32 type = (midi.SoundHandle >> 24) & 0xf;
if (type == 1 || type == 3) {
m_midis.emplace(id, std::make_unique<MidiHandler>(static_cast<Midi*>(&midi), m_vm, m_sound,
m_vol, m_pan, m_bank, this));
m_midis.emplace(id, std::make_unique<MidiHandler>(static_cast<Midi*>(&midi), m_sound, m_vol,
m_pan, m_bank, this));
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions game/sound/989snd/ame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

#include "loader.h"
#include "midi_handler.h"
#include "musicbank.h"
#include "sound_handler.h"
#include "vagvoice.h"
#include "musicbank.h"

#include "common/common_types.h"

Expand All @@ -23,12 +23,7 @@ class AmeHandler : public SoundHandler {
friend class MidiHandler;

public:
AmeHandler(MultiMidi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank);
AmeHandler(MultiMidi* block, MusicBank::MIDISound& sound, s32 vol, s32 pan, SoundBank& bank);
bool Tick() override;
SoundBank& Bank() override { return m_bank; };

Expand Down Expand Up @@ -66,7 +61,6 @@ class AmeHandler : public SoundHandler {
SoundBank& m_bank;

MultiMidi* m_header{nullptr};
VoiceManager& m_vm;
s32 m_vol{0};
s32 m_pan{0};
s8 m_repeats{0};
Expand Down
21 changes: 10 additions & 11 deletions game/sound/989snd/blocksound_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ namespace snd {
std::array<s8, 32> g_block_reg{};

BlockSoundHandler::BlockSoundHandler(SoundBank& bank,
SFXBlock::SFX& sfx,
VoiceManager& vm,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params)
: m_group(sfx.VolGroup), m_sfx(sfx), m_vm(vm), m_bank(bank) {
SFXBlock::SFX& sfx,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params)
: m_group(sfx.VolGroup), m_sfx(sfx), m_bank(bank) {
s32 vol, pan, pitch_mod, pitch_bend;
if (sfx_vol == -1) {
sfx_vol = sfx.Vol;
Expand Down Expand Up @@ -154,7 +153,7 @@ void BlockSoundHandler::Pause() {
continue;
}

m_vm.Pause(voice);
PauseTone(voice);
}
}

Expand All @@ -171,7 +170,7 @@ void BlockSoundHandler::Unpause() {
continue;
}

m_vm.Unpause(voice);
UnpauseTone(voice);
}
}

Expand Down Expand Up @@ -232,9 +231,9 @@ void BlockSoundHandler::SetVolPan(s32 vol, s32 pan) {
continue;
}

auto volume = m_vm.MakeVolume(127, 0, m_cur_volume, m_cur_pan, voice->g_vol, voice->g_pan);
auto left = m_vm.AdjustVolToGroup(volume.left, m_group);
auto right = m_vm.AdjustVolToGroup(volume.right, m_group);
auto volume = MakeVolume(127, 0, m_cur_volume, m_cur_pan, voice->g_vol, voice->g_pan);
auto left = AdjustVolToGroup(volume.left, m_group);
auto right = AdjustVolToGroup(volume.right, m_group);

voice->SetVolume(left >> 1, right >> 1);
}
Expand Down
2 changes: 0 additions & 2 deletions game/sound/989snd/blocksound_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class BlockSoundHandler : public SoundHandler {
public:
BlockSoundHandler(SoundBank& bank,
SFXBlock::SFX& sfx,
VoiceManager& vm,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params);
Expand Down Expand Up @@ -54,7 +53,6 @@ class BlockSoundHandler : public SoundHandler {
bool m_skip_grains{false};

SFXBlock::SFX& m_sfx;
VoiceManager& m_vm;

std::list<std::weak_ptr<BlockSoundVoice>> m_voices;

Expand Down
30 changes: 16 additions & 14 deletions game/sound/989snd/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace snd {

std::vector<std::unique_ptr<SoundBank>> gBanks;

enum chunk : u32 { bank, samples, midi };

inline static constexpr u32 fourcc(std::string_view p) {
Expand Down Expand Up @@ -405,7 +407,7 @@ MusicBank* MusicBank::ReadBank(nonstd::span<u8> bank_data,
return bank;
}

BankHandle Loader::BankLoad(nonstd::span<u8> bank) {
BankHandle BankLoad(nonstd::span<u8> bank) {
BinaryReader reader(bank);
FileAttributes fa;
fa.Read(reader);
Expand All @@ -430,31 +432,31 @@ BankHandle Loader::BankLoad(nonstd::span<u8> bank) {
nonstd::span<u8>(bank).subspan(fa.where[2].offset, fa.where[2].size));

auto bank = MusicBank::ReadBank(bank_data, sample_data, midi_data);
mBanks.emplace_back(bank);
gBanks.emplace_back(bank);

return bank;
} else if (fourcc == snd::fourcc("SBlk")) {
auto block = SFXBlock::ReadBlock(bank_data, sample_data);
mBanks.emplace_back(block);
gBanks.emplace_back(block);

return block;
}

return nullptr;
}

SoundBank* Loader::GetBankByHandle(BankHandle handle) {
auto bank = std::find_if(mBanks.begin(), mBanks.end(),
SoundBank* GetBankByHandle(BankHandle handle) {
auto bank = std::find_if(gBanks.begin(), gBanks.end(),
[handle](auto& bank) { return bank.get() == handle; });
if (bank == mBanks.end()) {
if (bank == gBanks.end()) {
return nullptr;
}

return bank->get();
}

SoundBank* Loader::GetBankByName(const char* name) {
for (auto& b : mBanks) {
SoundBank* GetBankByName(const char* name) {
for (auto& b : gBanks) {
auto bankname = b->GetName();
if (bankname.has_value()) {
if (bankname->compare(name) == 0) {
Expand All @@ -466,8 +468,8 @@ SoundBank* Loader::GetBankByName(const char* name) {
return nullptr;
}

SoundBank* Loader::GetBankWithSound(const char* name) {
for (auto& b : mBanks) {
SoundBank* GetBankWithSound(const char* name) {
for (auto& b : gBanks) {
auto sound = b->GetSoundByName(name);
if (sound.has_value()) {
return b.get();
Expand All @@ -477,11 +479,11 @@ SoundBank* Loader::GetBankWithSound(const char* name) {
return nullptr;
}

void Loader::UnloadBank(BankHandle handle) {
auto bank = std::find_if(mBanks.begin(), mBanks.end(),
void BankLoad(BankHandle handle) {
auto bank = std::find_if(gBanks.begin(), gBanks.end(),
[handle](auto& bank) { return bank.get() == handle; });
if (bank != mBanks.end()) {
mBanks.erase(bank);
if (bank != gBanks.end()) {
gBanks.erase(bank);
}
}

Expand Down
17 changes: 5 additions & 12 deletions game/sound/989snd/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ class FileAttributes {
void Read(BinaryReader& data);
};

class Loader {
public:
SoundBank* GetBankByHandle(BankHandle id);
SoundBank* GetBankByName(const char* name);
SoundBank* GetBankWithSound(const char* name);

void UnloadBank(BankHandle id);
SoundBank* GetBankByHandle(BankHandle id);
SoundBank* GetBankByName(const char* name);
SoundBank* GetBankWithSound(const char* name);
void BankLoad(BankHandle id);
BankHandle BankLoad(nonstd::span<u8> bank);

BankHandle BankLoad(nonstd::span<u8> bank);

private:
std::vector<std::unique_ptr<SoundBank>> mBanks;
};
} // namespace snd
43 changes: 20 additions & 23 deletions game/sound/989snd/midi_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ namespace snd {
*/

MidiHandler::MidiHandler(Midi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: m_sound(sound), m_repeats(sound.Repeats), m_bank(bank), m_header(block), m_vm(vm) {
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: m_sound(sound), m_repeats(sound.Repeats), m_bank(bank), m_header(block) {
if (vol == VOLUME_DONT_CHANGE) {
vol = 1024;
}
Expand All @@ -48,20 +47,18 @@ MidiHandler::MidiHandler(Midi* block,
}

MidiHandler::MidiHandler(Midi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank,
std::optional<AmeHandler*> parent)
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank,
std::optional<AmeHandler*> parent)
: m_parent(parent),
m_sound(sound),
m_vol(vol),
m_pan(pan),
m_repeats(sound.Repeats),
m_bank(bank),
m_header(block),
m_vm(vm) {
m_header(block) {
InitMidi();
}

Expand Down Expand Up @@ -99,7 +96,7 @@ void MidiHandler::Pause() {
continue;
}

m_vm.Pause(voice);
PauseTone(voice);
}
}

Expand All @@ -112,7 +109,7 @@ void MidiHandler::Unpause() {
continue;
}

m_vm.Unpause(voice);
UnpauseTone(voice);
}
}

Expand Down Expand Up @@ -162,11 +159,11 @@ void MidiHandler::SetVolPan(s32 vol, s32 pan) {
}

voice->basevol =
m_vm.MakeVolumeB(m_vol, voice->velocity * m_chanvol[voice->channel] / 127, pan,
voice->prog.Vol, voice->prog.Pan, voice->tone.Vol, voice->tone.Pan);
MakeVolumeB(m_vol, voice->velocity * m_chanvol[voice->channel] / 127, pan, voice->prog.Vol,
voice->prog.Pan, voice->tone.Vol, voice->tone.Pan);

auto left = m_vm.AdjustVolToGroup(voice->basevol.left, voice->group);
auto right = m_vm.AdjustVolToGroup(voice->basevol.right, voice->group);
auto left = AdjustVolToGroup(voice->basevol.left, voice->group);
auto right = AdjustVolToGroup(voice->basevol.right, voice->group);
voice->SetVolume(left >> 1, right >> 1);
}
}
Expand Down Expand Up @@ -231,8 +228,8 @@ void MidiHandler::NoteOn() {
}

auto voice = std::make_shared<midi_voice>(t, program);
voice->basevol = m_vm.MakeVolumeB(m_vol, (velocity * m_chanvol[channel]) / 0x7f, pan,
program.Vol, program.Pan, t.Vol, t.Pan);
voice->basevol = MakeVolumeB(m_vol, (velocity * m_chanvol[channel]) / 0x7f, pan, program.Vol,
program.Pan, t.Vol, t.Pan);

voice->note = note;
voice->channel = channel;
Expand All @@ -245,7 +242,7 @@ void MidiHandler::NoteOn() {
voice->current_pb = m_cur_pm;

voice->group = m_sound.VolGroup;
m_vm.StartTone(voice);
StartTone(voice);
m_voices.emplace_front(voice);
}
}
Expand Down
20 changes: 6 additions & 14 deletions game/sound/989snd/midi_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,14 @@ class midi_voice : public VagVoice {
class AmeHandler;
class MidiHandler : public SoundHandler {
public:
MidiHandler(Midi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank);
MidiHandler(Midi* block, MusicBank::MIDISound& sound, s32 vol, s32 pan, SoundBank& bank);

MidiHandler(Midi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank,
std::optional<AmeHandler*> parent);
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank,
std::optional<AmeHandler*> parent);

~MidiHandler() override {
for (auto& p : m_voices) {
Expand Down Expand Up @@ -115,8 +109,6 @@ class MidiHandler : public SoundHandler {

std::array<u8, 16> m_programs{};

VoiceManager& m_vm;

void Step();
void NewDelta();

Expand Down
Loading

0 comments on commit b88b0c3

Please sign in to comment.