Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Try to fix tests by not being able to open an OpenAL context
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Feb 17, 2021
1 parent 270ee73 commit f83dfc4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 4 additions & 2 deletions korau/src/commonMain/kotlin/com/soywiz/korau/sound/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ open class NativeSoundProvider {
suspend fun playAndWait(stream: AudioStream, params: PlaybackParameters = PlaybackParameters.DEFAULT) = createStreamingSound(stream).playAndWait(params)
}

open class DummyNativeSoundProvider : NativeSoundProvider() {
companion object : DummyNativeSoundProvider()
open class DummyNativeSoundProvider(
override val audioFormats: AudioFormats
) : NativeSoundProvider() {
companion object : DummyNativeSoundProvider(AudioFormats(WAV))
}

class DummySoundChannel(sound: Sound, val data: AudioData? = null) : SoundChannel(sound) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.soywiz.korau.sound

import com.soywiz.klogger.*
import com.soywiz.korau.format.*
import com.soywiz.korau.format.mp3.*
import com.soywiz.korau.sound.impl.jna.*
Expand All @@ -13,16 +14,21 @@ internal val nativeAudioFormats = AudioFormats(WAV, MP3Decoder, OGG) + AudioForm
listOf<AudioFormat>()
})

private val dummyNativeSoundProvider by lazy { DummyNativeSoundProvider(nativeAudioFormats) }

actual val nativeSoundProvider: NativeSoundProvider by lazy {
try {
traceTime("OpenAL") {
JnaOpenALNativeSoundProvider()
}
} catch (e: UnsatisfiedLinkError) {
DummyNativeSoundProvider
dummyNativeSoundProvider
} catch (e: OpenALException) {
Console.error("OpenALException", e.message)
dummyNativeSoundProvider
} catch (e: Throwable) {
e.printStackTrace()
DummyNativeSoundProvider
dummyNativeSoundProvider
}
}
//actual val nativeSoundProvider: NativeSoundProvider by lazy { JogampNativeSoundProvider() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import com.soywiz.korau.sound.*
import com.soywiz.korio.async.*
import com.soywiz.korio.util.*
import kotlinx.coroutines.*
import java.lang.RuntimeException
import java.nio.*
import kotlin.coroutines.*
import kotlin.math.*

class OpenALException(message: String) : RuntimeException(message)

class JnaOpenALNativeSoundProvider : NativeSoundProvider() {
val device = (AL.alcOpenDevice(null) ?: error("Can't open OpenAL device")).also { device ->
val device = (AL.alcOpenDevice(null) ?: throw OpenALException("Can't open OpenAL device")).also { device ->
Runtime.getRuntime().addShutdownHook(Thread {
AL.alcCloseDevice(device)
})
}
val context = (AL.alcCreateContext(device, null) ?: error("Can't get OpenAL context")).also { context ->
val context = (AL.alcCreateContext(device, null) ?: throw OpenALException("Can't get OpenAL context")).also { context ->
Runtime.getRuntime().addShutdownHook(Thread {
//alc?.alcDestroyContext(context) // Crashes on mac!
})
Expand Down

0 comments on commit f83dfc4

Please sign in to comment.