Skip to content

Commit

Permalink
Replace AL_SOFT_events with polling again
Browse files Browse the repository at this point in the history
AL_SOFT_events is currently not well suited for use in NoteBlockTool. See kcat/openal-soft#1003
  • Loading branch information
RaphiMC committed Jun 16, 2024
1 parent a72326a commit f3ec709
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected void processSound(final String sound, final float pitch, final float v
@Override
protected void writeSamples() {
this.soundSystem.renderSamples(this.sampleOutputStream, this.samplesPerTick);
this.soundSystem.tick();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SoundSystem(final int maxSounds) {

public abstract void playSound(final String sound, final float pitch, final float volume, final float panning);

public void writeSamples() {
public void tick() {
}

public abstract void stopSounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public synchronized void playSound(final String sound, final float pitch, final
}

@Override
public synchronized void writeSamples() {
public synchronized void tick() {
final long[] samples = new long[this.samplesPerTick];
final int[] outputBuffer = new int[this.samplesPerTick];
final int[] mutationBuffer = new int[this.samplesPerTick * 2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public MultithreadedJavaxSoundSystem(final int maxSounds, final float playbackSp
}

@Override
public synchronized void writeSamples() {
public synchronized void tick() {
this.soundsToRender.addAll(this.playingSounds);
this.syncLock.set(this.playingSounds.size());
while (this.syncLock.get() != 0 && !Thread.currentThread().isInterrupted()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ public static OpenALSoundSystem createCapture(final int maxSounds, final AudioFo
private Thread shutdownHook;
private ByteBuffer captureBuffer;

@SuppressWarnings("FieldCanBeLocal")
private final SOFTEventProcI eventCallback = (eventType, object, param, length, message, userParam) -> {
if (eventType == SOFTEvents.AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT && param == AL10.AL_STOPPED) {
synchronized (this) {
this.playingSources.remove((Integer) object);
AL10.alDeleteSources(object);
this.checkALError("Could not delete audio source", AL10.AL_INVALID_NAME);
}
}
};

private OpenALSoundSystem(final int maxSounds) {
this(maxSounds, null);
}
Expand Down Expand Up @@ -130,9 +119,6 @@ private OpenALSoundSystem(final int maxSounds, final AudioFormat captureAudioFor
if (!alCapabilities.OpenAL11) {
throw new RuntimeException("OpenAL 1.1 is not supported");
}
if (!alCapabilities.AL_SOFT_events) {
throw new RuntimeException("AL_SOFT_events is not supported");
}

AL10.alDistanceModel(AL10.AL_NONE);
this.checkALError("Could not set distance model");
Expand All @@ -149,11 +135,6 @@ private OpenALSoundSystem(final int maxSounds, final AudioFormat captureAudioFor
throw new RuntimeException("Could not load sound samples", e);
}

SOFTEvents.alEventCallbackSOFT(this.eventCallback, null);
this.checkALError("Could not set event callback");
SOFTEvents.alEventControlSOFT(new int[]{SOFTEvents.AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT}, true);
this.checkALError("Could not configure event control");

Runtime.getRuntime().addShutdownHook(this.shutdownHook = new Thread(() -> {
this.shutdownHook = null;
this.close();
Expand Down Expand Up @@ -190,6 +171,20 @@ public synchronized void playSound(final String sound, final float pitch, final
this.playingSources.add(source);
}

@Override
public synchronized void tick() {
this.playingSources.removeIf(source -> {
final int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
this.checkALError("Could not get audio source state");
if (state == AL10.AL_STOPPED) {
AL10.alDeleteSources(source);
this.checkALError("Could not delete audio source");
return true;
}
return false;
});
}

public synchronized void renderSamples(final SampleOutputStream outputStream, final int sampleCount) {
final int samplesLength = sampleCount * this.captureAudioFormat.getChannels();
if (samplesLength * this.captureAudioFormat.getSampleSizeInBits() / 8 > this.captureBuffer.capacity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void playCustomNote(NbsCustomInstrument customInstrument, float pitch, fl
public void playNotes(java.util.List<? extends Note> notes) {
for (Note note : notes) this.playNote(note);

this.soundSystem.writeSamples();
this.soundSystem.tick();
}

}

0 comments on commit f3ec709

Please sign in to comment.