Skip to content

Commit 1a7a062

Browse files
committed
Made OpenAL audio exporting multithreaded
1 parent 36bd918 commit 1a7a062

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
public class OpenALSoundSystem extends SoundSystem {
3737

3838
private static OpenALSoundSystem instance;
39+
private static final ThreadLocal<OpenALSoundSystem> tlsInstance = ThreadLocal.withInitial(() -> null);
3940

4041
public static OpenALSoundSystem createPlayback(final Map<String, byte[]> soundData, final int maxSounds) {
4142
if (instance != null) {
@@ -46,11 +47,11 @@ public static OpenALSoundSystem createPlayback(final Map<String, byte[]> soundDa
4647
}
4748

4849
public static OpenALSoundSystem createCapture(final Map<String, byte[]> soundData, final int maxSounds, final AudioFormat captureAudioFormat) {
49-
if (instance != null) {
50+
if (tlsInstance.get() != null) {
5051
throw new IllegalStateException("OpenAL sound system already initialized");
5152
}
52-
instance = new OpenALSoundSystem(soundData, maxSounds, captureAudioFormat);
53-
return instance;
53+
tlsInstance.set(new OpenALSoundSystem(soundData, maxSounds, captureAudioFormat));
54+
return tlsInstance.get();
5455
}
5556

5657

@@ -105,10 +106,13 @@ private OpenALSoundSystem(final Map<String, byte[]> soundData, final int maxSoun
105106
if (captureAudioFormat != null && !alcCapabilities.ALC_SOFT_loopback) {
106107
throw new RuntimeException("ALC_SOFT_loopback is not supported");
107108
}
109+
if (captureAudioFormat != null && !alcCapabilities.ALC_EXT_thread_local_context) {
110+
throw new RuntimeException("ALC_EXT_thread_local_context is not supported");
111+
}
108112

109113
this.context = ALC10.alcCreateContext(this.device, attributes);
110114
this.checkALCError("Failed to create context");
111-
if (!ALC10.alcMakeContextCurrent(this.context)) {
115+
if (captureAudioFormat != null ? !EXTThreadLocalContext.alcSetThreadContext(this.context) : !ALC10.alcMakeContextCurrent(this.context)) {
112116
throw new RuntimeException("Failed to make context current");
113117
}
114118

@@ -223,7 +227,11 @@ public synchronized void close() {
223227
this.soundBuffers.clear();
224228
this.playingSources.clear();
225229
if (this.context != 0L) {
226-
ALC10.alcMakeContextCurrent(0);
230+
if (this.captureAudioFormat != null) {
231+
EXTThreadLocalContext.alcSetThreadContext(0);
232+
} else {
233+
ALC10.alcMakeContextCurrent(0);
234+
}
227235
ALC10.alcDestroyContext(this.context);
228236
this.context = 0L;
229237
}
@@ -235,7 +243,11 @@ public synchronized void close() {
235243
MemoryUtil.memFree(this.captureBuffer);
236244
this.captureBuffer = null;
237245
}
238-
instance = null;
246+
if (this.captureAudioFormat != null) {
247+
tlsInstance.remove();
248+
} else {
249+
instance = null;
250+
}
239251
}
240252

241253
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class ExportFrame extends JFrame {
6666
private final List<ListFrame.LoadedSong> loadedSongs;
6767
private final JComboBox<String> format = new JComboBox<>(new String[]{"NBS", "MP3 (Using LAME encoder)", "WAV", "AIF"});
6868
private final JLabel soundSystemLabel = new JLabel("Sound System:");
69-
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (parallel capable, normalized)"});
69+
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (faster, better sound quality)", "Javax (normalized)"});
7070
private final JLabel sampleRateLabel = new JLabel("Sample Rate:");
7171
private final JSpinner sampleRate = new JSpinner(new SpinnerNumberModel(48000, 8000, 192000, 8000));
7272
private final JLabel bitDepthLabel = new JLabel("PCM Bit Depth:");
@@ -325,7 +325,7 @@ private void doExport(final File outFile) {
325325
}
326326
} else {
327327
final int threadCount;
328-
if (!isAudioFile || this.soundSystem.getSelectedIndex() == 0) threadCount = 1;
328+
if (!isAudioFile) threadCount = 1;
329329
else threadCount = Math.min(this.loadedSongs.size(), Runtime.getRuntime().availableProcessors());
330330
ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount);
331331
Queue<Runnable> uiQueue = new ConcurrentLinkedQueue<>();

0 commit comments

Comments
 (0)