Skip to content

Commit f3ec709

Browse files
committed
Replace AL_SOFT_events with polling again
AL_SOFT_events is currently not well suited for use in NoteBlockTool. See kcat/openal-soft#1003
1 parent a72326a commit f3ec709

File tree

6 files changed

+19
-23
lines changed

6 files changed

+19
-23
lines changed

src/main/java/net/raphimc/noteblocktool/audio/export/impl/OpenALAudioExporter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected void processSound(final String sound, final float pitch, final float v
4141
@Override
4242
protected void writeSamples() {
4343
this.soundSystem.renderSamples(this.sampleOutputStream, this.samplesPerTick);
44+
this.soundSystem.tick();
4445
}
4546

4647
@Override

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/SoundSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public SoundSystem(final int maxSounds) {
2727

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

30-
public void writeSamples() {
30+
public void tick() {
3131
}
3232

3333
public abstract void stopSounds();

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/impl/JavaxSoundSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public synchronized void playSound(final String sound, final float pitch, final
6969
}
7070

7171
@Override
72-
public synchronized void writeSamples() {
72+
public synchronized void tick() {
7373
final long[] samples = new long[this.samplesPerTick];
7474
final int[] outputBuffer = new int[this.samplesPerTick];
7575
final int[] mutationBuffer = new int[this.samplesPerTick * 2];

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/impl/MultithreadedJavaxSoundSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public MultithreadedJavaxSoundSystem(final int maxSounds, final float playbackSp
8989
}
9090

9191
@Override
92-
public synchronized void writeSamples() {
92+
public synchronized void tick() {
9393
this.soundsToRender.addAll(this.playingSounds);
9494
this.syncLock.set(this.playingSounds.size());
9595
while (this.syncLock.get() != 0 && !Thread.currentThread().isInterrupted()) {

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/impl/OpenALSoundSystem.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ public static OpenALSoundSystem createCapture(final int maxSounds, final AudioFo
6464
private Thread shutdownHook;
6565
private ByteBuffer captureBuffer;
6666

67-
@SuppressWarnings("FieldCanBeLocal")
68-
private final SOFTEventProcI eventCallback = (eventType, object, param, length, message, userParam) -> {
69-
if (eventType == SOFTEvents.AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT && param == AL10.AL_STOPPED) {
70-
synchronized (this) {
71-
this.playingSources.remove((Integer) object);
72-
AL10.alDeleteSources(object);
73-
this.checkALError("Could not delete audio source", AL10.AL_INVALID_NAME);
74-
}
75-
}
76-
};
77-
7867
private OpenALSoundSystem(final int maxSounds) {
7968
this(maxSounds, null);
8069
}
@@ -130,9 +119,6 @@ private OpenALSoundSystem(final int maxSounds, final AudioFormat captureAudioFor
130119
if (!alCapabilities.OpenAL11) {
131120
throw new RuntimeException("OpenAL 1.1 is not supported");
132121
}
133-
if (!alCapabilities.AL_SOFT_events) {
134-
throw new RuntimeException("AL_SOFT_events is not supported");
135-
}
136122

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

152-
SOFTEvents.alEventCallbackSOFT(this.eventCallback, null);
153-
this.checkALError("Could not set event callback");
154-
SOFTEvents.alEventControlSOFT(new int[]{SOFTEvents.AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT}, true);
155-
this.checkALError("Could not configure event control");
156-
157138
Runtime.getRuntime().addShutdownHook(this.shutdownHook = new Thread(() -> {
158139
this.shutdownHook = null;
159140
this.close();
@@ -190,6 +171,20 @@ public synchronized void playSound(final String sound, final float pitch, final
190171
this.playingSources.add(source);
191172
}
192173

174+
@Override
175+
public synchronized void tick() {
176+
this.playingSources.removeIf(source -> {
177+
final int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
178+
this.checkALError("Could not get audio source state");
179+
if (state == AL10.AL_STOPPED) {
180+
AL10.alDeleteSources(source);
181+
this.checkALError("Could not delete audio source");
182+
return true;
183+
}
184+
return false;
185+
});
186+
}
187+
193188
public synchronized void renderSamples(final SampleOutputStream outputStream, final int sampleCount) {
194189
final int samplesLength = sampleCount * this.captureAudioFormat.getChannels();
195190
if (samplesLength * this.captureAudioFormat.getSampleSizeInBits() / 8 > this.captureBuffer.capacity()) {

src/main/java/net/raphimc/noteblocktool/frames/SongPlayerFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public void playCustomNote(NbsCustomInstrument customInstrument, float pitch, fl
348348
public void playNotes(java.util.List<? extends Note> notes) {
349349
for (Note note : notes) this.playNote(note);
350350

351-
this.soundSystem.writeSamples();
351+
this.soundSystem.tick();
352352
}
353353

354354
}

0 commit comments

Comments
 (0)