3636public 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
0 commit comments