Skip to content

Commit

Permalink
Fixed BASS sound system race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 31, 2024
1 parent c4ce205 commit 99fb64a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static boolean isLoaded() {

int BASS_ChannelSetSync(final int handle, final int type, final long param, final SYNCPROC proc, final Pointer user);

boolean BASS_ChannelStop(final int handle);
boolean BASS_ChannelFree(final int handle);

interface SYNCPROC extends Callback {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static BassSoundSystem createPlayback(final int maxSounds) {
private Thread shutdownHook;

@SuppressWarnings("FieldCanBeLocal")
private final BassLibrary.SYNCPROC channelEndSync = (handle, channel, data, user) -> this.playingChannels.remove((Integer) channel);
private final BassLibrary.SYNCPROC channelFreeSync = (handle, channel, data, user) -> this.playingChannels.remove((Integer) channel);

private BassSoundSystem(final int maxSounds) {
super(maxSounds);
Expand Down Expand Up @@ -97,8 +97,10 @@ public void playSound(final String sound, final float pitch, final float volume,
if (!this.soundSamples.containsKey(sound)) return;

if (this.playingChannels.size() >= this.maxSounds) {
if (!BassLibrary.INSTANCE.BASS_ChannelStop(this.playingChannels.remove(0))) {
this.checkError("Could not stop audio channel");
if (!BassLibrary.INSTANCE.BASS_ChannelFree(this.playingChannels.remove(0))) {
if (BassLibrary.INSTANCE.BASS_ErrorGetCode() != BassLibrary.BASS_ERROR_HANDLE) {
this.checkError("Could not free audio channel");
}
}
}

Expand All @@ -119,12 +121,14 @@ public void playSound(final String sound, final float pitch, final float volume,
if (!BassLibrary.INSTANCE.BASS_ChannelSetAttribute(channel, BassLibrary.BASS_ATTRIB_FREQ, freq.getValue() * pitch)) {
this.checkError("Could not set audio channel frequency");
}
final int sync = BassLibrary.INSTANCE.BASS_ChannelSetSync(channel, BassLibrary.BASS_SYNC_END | BassLibrary.BASS_SYNC_ONETIME, 0, this.channelEndSync, null);
final int sync = BassLibrary.INSTANCE.BASS_ChannelSetSync(channel, BassLibrary.BASS_SYNC_FREE, 0, this.channelFreeSync, null);
if (sync == 0) {
this.checkError("Could not set audio channel end sync");
}
if (!BassLibrary.INSTANCE.BASS_ChannelStart(channel)) {
this.checkError("Could not play audio channel");
if (BassLibrary.INSTANCE.BASS_ErrorGetCode() != BassLibrary.BASS_ERROR_START) {
this.checkError("Could not play audio channel");
}
}
this.playingChannels.add(channel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void close() {
private final ListFrame.LoadedSong song;
private final MonitoringSongPlayer songPlayer;
private final Timer updateTimer;
private final JComboBox<String> soundSystemComboBox = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (better system compatibility, laggier)", "Javax multithreaded (experimental)", "Un4seen BASS (experimental)"});
private final JComboBox<String> soundSystemComboBox = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (better system compatibility, laggier)", "Javax multithreaded (experimental)", "Un4seen BASS"});
private final JSpinner maxSoundsSpinner = new JSpinner(new SpinnerNumberModel(256, 64, 8192, 64));
private final JSlider volumeSlider = new JSlider(0, 100, 50);
private final JButton playStopButton = new JButton("Play");
Expand Down

0 comments on commit 99fb64a

Please sign in to comment.