Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

989snd: General rework and instance limits #3252

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions game/overlord/common/srpc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "srpc.h"

#include <cstring>

#include "game/sound/sndshim.h"

// added
Expand Down
6 changes: 3 additions & 3 deletions game/overlord/jak2/srpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ void* RPC_Player(unsigned int /*fno*/, void* data, int size) {
}
// lg::warn("RPC: PLAY {} v:{}, p:{}", sound->name, GetVolume(sound), GetPan(sound));

s32 handle = snd_PlaySoundByNameVolPanPMPB(0, nullptr, sound->name, GetVolume(sound),
GetPan(sound), sound->params.pitch_mod,
sound->params.bend);
s32 handle = snd_PlaySoundByNameVolPanPMPB(nullptr, nullptr, sound->name,
GetVolume(sound), GetPan(sound),
sound->params.pitch_mod, sound->params.bend);
sound->sound_handle = handle;
if (handle != 0) {
sound->id = cmd->play.sound_id;
Expand Down
18 changes: 9 additions & 9 deletions game/sound/989snd/ame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace snd {
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) {
AmeHandler::AmeHandler(SoundHandle oid,
MultiMidi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: SoundHandler(oid), 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 +58,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>(0, static_cast<Midi*>(&midi), m_sound,
m_vol, m_pan, m_bank, this));
}
}
}
Expand Down
21 changes: 13 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,12 @@ class AmeHandler : public SoundHandler {
friend class MidiHandler;

public:
AmeHandler(MultiMidi* block,
VoiceManager& vm,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank);
AmeHandler(SoundHandle oid,
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 +66,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 All @@ -77,4 +76,10 @@ class AmeHandler : public SoundHandler {
std::array<u8, 16> m_register{};
std::array<u8*, 16> m_macro{};
};

AmeHandler* AllocAmeSound(MultiMidi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank);
} // namespace snd
87 changes: 48 additions & 39 deletions game/sound/989snd/blocksound_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@

#include "util.h"

#include "common/log/log.h"
#include "game/sound/989snd/player.h"

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) {
BlockSoundHandler* BlockSoundHandler::MakeBlockSound(SoundBank& bank,
SFXBlock::SFX& sfx,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params) {
s32 vol, pan, pitch_mod, pitch_bend;
if (sfx_vol == -1) {
sfx_vol = sfx.Vol;
Expand Down Expand Up @@ -61,42 +59,52 @@ BlockSoundHandler::BlockSoundHandler(SoundBank& bank,
pan = sfx_pan;
}

m_orig_volume = sfx_vol;
m_orig_pan = sfx_pan;
if (sfx.Flags.solo()) {
StopAllHandlersForSound(sfx);
}

auto* hnd = AllocBlockSound(bank, sfx, vol);
if (hnd == nullptr) {
return nullptr;
}

hnd->m_orig_volume = sfx_vol;
hnd->m_orig_pan = sfx_pan;

m_cur_volume = play_vol;
m_cur_pan = pan;
m_cur_pb = pitch_bend;
m_cur_pm = pitch_mod;
hnd->m_cur_volume = play_vol;
hnd->m_cur_pan = pan;
hnd->m_cur_pb = pitch_bend;
hnd->m_cur_pm = pitch_mod;

m_app_volume = vol;
m_app_pan = pan;
m_app_pb = pitch_bend;
m_app_pm = pitch_mod;
hnd->m_app_volume = vol;
hnd->m_app_pan = pan;
hnd->m_app_pb = pitch_bend;
hnd->m_app_pm = pitch_mod;

m_lfo_volume = 0;
m_lfo_pan = 0;
m_lfo_pb = 0;
m_lfo_pm = 0;
hnd->m_lfo_volume = 0;
hnd->m_lfo_pan = 0;
hnd->m_lfo_pb = 0;
hnd->m_lfo_pm = 0;

if (params.registers.has_value()) {
m_registers = params.registers.value();
hnd->m_registers = params.registers.value();
}

// Figure this stuff out properly someday
// if (m_sfx.d.Flags & 2) {
// fmt::print("solo flag\n");
// m_done = true;
// return;
// }
// Bug from PS2, this was never set
hnd->m_start_tick = GetTick();

m_next_grain = 0;
m_countdown = m_sfx.Grains[0].Delay;
while (m_countdown <= 0 && !m_done) {
DoGrain();
hnd->m_next_grain = 0;
hnd->m_countdown = hnd->m_sfx.Grains[0].Delay;
while (hnd->m_countdown <= 0 && !hnd->m_done) {
hnd->DoGrain();
}

return hnd;
}

BlockSoundHandler::BlockSoundHandler(SoundHandle oid, SoundBank& bank, SFXBlock::SFX& sfx)
: SoundHandler(oid), m_group(sfx.VolGroup), m_sfx(sfx), m_bank(bank) {}

BlockSoundHandler::~BlockSoundHandler() {
for (auto& p : m_voices) {
auto v = p.lock();
Expand All @@ -114,8 +122,9 @@ bool BlockSoundHandler::Tick() {
}

for (auto it = m_children.begin(); it != m_children.end();) {
bool done = it->get()->Tick();
bool done = (*it)->Tick();
if (done) {
FreeSound(*it);
it = m_children.erase(it);
} else {
++it;
Expand Down Expand Up @@ -154,7 +163,7 @@ void BlockSoundHandler::Pause() {
continue;
}

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

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

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

Expand Down Expand Up @@ -232,9 +241,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
21 changes: 12 additions & 9 deletions game/sound/989snd/blocksound_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class BlockSoundVoice : public VagVoice {

class BlockSoundHandler : public SoundHandler {
public:
BlockSoundHandler(SoundBank& bank,
SFXBlock::SFX& sfx,
VoiceManager& vm,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params);
static BlockSoundHandler* MakeBlockSound(SoundBank& bank,
SFXBlock::SFX& sfx,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params);

BlockSoundHandler(SoundHandle oid, SoundBank& bank, SFXBlock::SFX& sfx);

~BlockSoundHandler() override;
bool Tick() override;
Expand Down Expand Up @@ -54,11 +55,9 @@ class BlockSoundHandler : public SoundHandler {
bool m_skip_grains{false};

SFXBlock::SFX& m_sfx;
VoiceManager& m_vm;

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

std::list<std::unique_ptr<SoundHandler>> m_children;
std::vector<SoundHandler*> m_children;

s32 m_orig_volume{0};
s32 m_orig_pan{0};
Expand Down Expand Up @@ -86,5 +85,9 @@ class BlockSoundHandler : public SoundHandler {

s32 m_countdown{0};
u32 m_next_grain{0};
u32 m_start_tick{0};
};

BlockSoundHandler* AllocBlockSound(SoundBank& bank, SFXBlock::SFX& sfx, s32 sfx_vol);

} // namespace snd
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
Loading