Skip to content

Commit

Permalink
989snd: General rework and instance limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziemas committed Dec 8, 2023
1 parent b88b0c3 commit dc08c73
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 152 deletions.
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
9 changes: 5 additions & 4 deletions game/sound/989snd/ame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ namespace snd {
u64 SoundFlavaHack = 0;
u8 GlobalExcite = 0;

AmeHandler::AmeHandler(MultiMidi* block,
AmeHandler::AmeHandler(SoundHandle oid,
MultiMidi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: m_sound(sound), m_bank(bank), m_header(block), m_repeats(sound.Repeats) {
: 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 @@ -57,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_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
13 changes: 12 additions & 1 deletion game/sound/989snd/ame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class AmeHandler : public SoundHandler {
friend class MidiHandler;

public:
AmeHandler(MultiMidi* block, 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 @@ -71,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
73 changes: 40 additions & 33 deletions game/sound/989snd/blocksound_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
namespace snd {
std::array<s8, 32> g_block_reg{};

BlockSoundHandler::BlockSoundHandler(SoundBank& bank,
SFXBlock::SFX& sfx,
s32 sfx_vol,
s32 sfx_pan,
SndPlayParams& params)
: m_group(sfx.VolGroup), m_sfx(sfx), 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 @@ -60,42 +59,49 @@ BlockSoundHandler::BlockSoundHandler(SoundBank& bank,
pan = sfx_pan;
}

m_orig_volume = sfx_vol;
m_orig_pan = sfx_pan;
if (sfx.Flags.solo()) {
lg::warn("989snd: Unsupported solo sound flag");
}

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

m_cur_volume = play_vol;
m_cur_pan = pan;
m_cur_pb = pitch_bend;
m_cur_pm = pitch_mod;
hnd->m_orig_volume = sfx_vol;
hnd->m_orig_pan = sfx_pan;

m_app_volume = vol;
m_app_pan = pan;
m_app_pb = pitch_bend;
m_app_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_lfo_volume = 0;
m_lfo_pan = 0;
m_lfo_pb = 0;
m_lfo_pm = 0;
hnd->m_app_volume = vol;
hnd->m_app_pan = pan;
hnd->m_app_pb = pitch_bend;
hnd->m_app_pm = pitch_mod;

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;
// }

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 @@ -113,8 +119,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
18 changes: 11 additions & 7 deletions game/sound/989snd/blocksound_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class BlockSoundVoice : public VagVoice {

class BlockSoundHandler : public SoundHandler {
public:
BlockSoundHandler(SoundBank& bank,
SFXBlock::SFX& sfx,
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 @@ -55,8 +57,7 @@ class BlockSoundHandler : public SoundHandler {
SFXBlock::SFX& m_sfx;

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 @@ -85,4 +86,7 @@ class BlockSoundHandler : public SoundHandler {
s32 m_countdown{0};
u32 m_next_grain{0};
};

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

} // namespace snd
11 changes: 7 additions & 4 deletions game/sound/989snd/midi_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ namespace snd {
**
*/

MidiHandler::MidiHandler(Midi* block,
MidiHandler::MidiHandler(SoundHandle oid,
Midi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank)
: m_sound(sound), m_repeats(sound.Repeats), m_bank(bank), m_header(block) {
: SoundHandler(oid), m_sound(sound), m_repeats(sound.Repeats), m_bank(bank), m_header(block) {
if (vol == VOLUME_DONT_CHANGE) {
vol = 1024;
}
Expand All @@ -46,13 +47,15 @@ MidiHandler::MidiHandler(Midi* block,
InitMidi();
}

MidiHandler::MidiHandler(Midi* block,
MidiHandler::MidiHandler(SoundHandle oid,
Midi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank,
std::optional<AmeHandler*> parent)
: m_parent(parent),
: SoundHandler(oid),
m_parent(parent),
m_sound(sound),
m_vol(vol),
m_pan(pan),
Expand Down
17 changes: 15 additions & 2 deletions game/sound/989snd/midi_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ class midi_voice : public VagVoice {
class AmeHandler;
class MidiHandler : public SoundHandler {
public:
MidiHandler(Midi* block, MusicBank::MIDISound& sound, s32 vol, s32 pan, SoundBank& bank);
MidiHandler(SoundHandle oid,
Midi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank);

MidiHandler(Midi* block,
MidiHandler(SoundHandle oid,
Midi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
Expand Down Expand Up @@ -123,4 +129,11 @@ class MidiHandler : public SoundHandler {

static std::pair<size_t, u32> ReadVLQ(u8* value);
};

MidiHandler* AllocMidiSound(Midi* block,
MusicBank::MIDISound& sound,
s32 vol,
s32 pan,
SoundBank& bank);

} // namespace snd
23 changes: 8 additions & 15 deletions game/sound/989snd/musicbank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

namespace snd {

std::optional<std::unique_ptr<SoundHandler>> MusicBank::MakeHandler(u32 sound_id,
s32 vol,
s32 pan,
s32 pm,
s32 pb) {
SoundHandler* MusicBank::MakeHandler(u32 sound_id, s32 vol, s32 pan, s32 pm, s32 pb) {
auto& sound = Sounds[sound_id];

// FIXME: global midi list
Expand All @@ -21,27 +17,24 @@ std::optional<std::unique_ptr<SoundHandler>> MusicBank::MakeHandler(u32 sound_id
if (sound.Type == 4) {
auto& midi = std::get<Midi>(MidiData);
if (sound.MIDIID == midi.ID) {
return std::make_unique<MidiHandler>(&midi, sound, vol, pan, *this);
return AllocMidiSound(&midi, sound, vol, pan, *this);
}
return std::nullopt;
return nullptr;
} else if (sound.Type == 5) {
auto& midi = std::get<MultiMidi>(MidiData);
if (sound.MIDIID == midi.ID) {
return std::make_unique<AmeHandler>(&midi, sound, vol, pan, *this);
return AllocAmeSound(&midi, sound, vol, pan, *this);
}
return std::nullopt;
return nullptr;
} else {
lg::error("Invalid music sound type");
return std::nullopt;
return nullptr;
// error
}
}

std::optional<std::unique_ptr<SoundHandler>> MusicBank::MakeHandler(u32 sound_id,
s32 vol,
s32 pan,
SndPlayParams& params) {
return std::nullopt;
SoundHandler* MusicBank::MakeHandler(u32 sound_id, s32 vol, s32 pan, SndPlayParams& params) {
return nullptr;
}

} // namespace snd
12 changes: 2 additions & 10 deletions game/sound/989snd/musicbank.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ class MusicBank : public SoundBank {
nonstd::span<u8> samples,
nonstd::span<u8> midi_data);

std::optional<std::unique_ptr<SoundHandler>> MakeHandler(u32 sound_id,
s32 vol,
s32 pan,
s32 pm,
s32 pb) override;

std::optional<std::unique_ptr<SoundHandler>> MakeHandler(u32 sound_id,
s32 vol,
s32 pan,
SndPlayParams& params) override;
SoundHandler* MakeHandler(u32 sound_id, s32 vol, s32 pan, s32 pm, s32 pb) override;
SoundHandler* MakeHandler(u32 sound_id, s32 vol, s32 pan, SndPlayParams& params) override;
};
} // namespace snd
Loading

0 comments on commit dc08c73

Please sign in to comment.