Skip to content

Commit

Permalink
Reuse sample channels in BassSoundProvider
Browse files Browse the repository at this point in the history
Fixes an issue where sample streams are not cleared, resulting in memory leak.
  • Loading branch information
Rian8337 committed Nov 24, 2024
1 parent 76e443c commit df44e68
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/ru/nsu/ccfit/zuev/audio/BassSoundProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@ public void play(float volume) {
return;
}

// Ensure the current channel is stopped first.
if (volume == 0) {
stop();
return;
}

if (channel == 0) {
channel = BASS.BASS_SampleGetChannel(sample, BASS.BASS_SAMCHAN_STREAM);

if (channel == 0) {
return;
}

applyAudioEffectsToChannel();
BASS.BASS_ChannelSetAttribute(channel, BASS.BASS_ATTRIB_NOBUFFER, 1);
}

if (channel == 0) {
return;
}

// Ensure the current playback is stopped first.
stop();

channel = BASS.BASS_SampleGetChannel(sample, BASS.BASS_SAMCHAN_STREAM | BASS.BASS_STREAM_AUTOFREE);
applyAudioEffectsToChannel();
BASS.BASS_ChannelSetAttribute(channel, BASS.BASS_ATTRIB_NOBUFFER, 1);
BASS.BASS_ChannelPlay(channel, false);
BASS.BASS_ChannelPlay(channel, true);
BASS.BASS_ChannelSetAttribute(channel, BASS.BASS_ATTRIB_VOL, volume * Config.getSoundVolume());
}

Expand All @@ -77,11 +94,16 @@ public void stop() {
return;
}

BASS.BASS_ChannelStop(channel);
channel = 0;
if (BASS.BASS_ChannelIsActive(channel) == BASS.BASS_ACTIVE_PLAYING) {
BASS.BASS_ChannelStop(channel);
}
}

public void free() {
if (sample == 0) {
return;
}

BASS.BASS_SampleFree(sample);
sample = 0;
channel = 0;
Expand Down

0 comments on commit df44e68

Please sign in to comment.