Skip to content

Commit

Permalink
Safely unload sound banks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziemas committed Apr 25, 2022
1 parent e6af8f8 commit 1354177
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
9 changes: 9 additions & 0 deletions game/sound/989snd/blocksound_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ class blocksound_handler : public sound_handler {
m_grain_handler.insert(std::make_pair(grain_type::rand_play, &blocksound_handler::rand_play));
}

~blocksound_handler() override {
for (auto& p : m_voices) {
auto v = p.lock();
if (v != nullptr) {
v->stop();
}
}
}

bool tick() override;
u32 bank() override { return m_bank; };

Expand Down
8 changes: 8 additions & 0 deletions game/sound/989snd/midi_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class midi_handler : public sound_handler {
u32 bank,
std::optional<ame_handler*> parent);

~midi_handler() override {
for (auto& p : m_voices) {
auto v = p.lock();
if (v != nullptr) {
v->stop();
}
}
}
void init_midi();
void start();
bool tick() override;
Expand Down
4 changes: 2 additions & 2 deletions game/sound/common/synth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ static s16 ApplyVolume(s16 sample, s32 volume) {

s16_output synth::tick() {
s16_output out{};

m_voices.remove_if([](std::shared_ptr<voice>& v) { return v->dead(); });
for (auto& v : m_voices) {
out += v->run();
}

m_voices.remove_if([](std::shared_ptr<voice>& v) { return v->dead(); });

out.left = ApplyVolume(out.left, m_Volume.left.Get());
out.right = ApplyVolume(out.right, m_Volume.right.Get());

Expand Down
2 changes: 2 additions & 0 deletions game/sound/common/voice.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class voice {
m_CustomLoop = true;
}

void stop() { m_ADSR.Stop(); }

private:
union ADPCMHeader {
u16 bits;
Expand Down

0 comments on commit 1354177

Please sign in to comment.