fix bug in audio system

This commit is contained in:
Bixilon 2021-07-15 16:12:16 +02:00
parent 27ba498f40
commit 28e28288cd
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 7 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import glm_.vec3.Vec3
import org.lwjgl.openal.AL10.*
class SoundSource {
private var playTime = -1L
private val source: Int = alGenSources()
var loop: Boolean = false
@ -72,9 +73,10 @@ class SoundSource {
get() = alGetSourcei(source, AL_SOURCE_STATE) == AL_PLAYING
val available: Boolean
get() = !isPlaying
get() = !isPlaying || System.currentTimeMillis() - playTime > sound?.length ?: 0L // ToDo: Allow pause
fun play() {
playTime = System.currentTimeMillis()
alSourcePlay(source)
}
@ -83,6 +85,7 @@ class SoundSource {
}
fun stop() {
playTime = -1L
alSourceStop(source)
}

View File

@ -36,6 +36,8 @@ data class Sound(
val preload: Boolean = false,
// ToDo: type
) {
var length: Long = -1L
private set
var loaded: Boolean = false
private set
var loadFailed: Boolean = false
@ -79,6 +81,7 @@ data class Sound(
samplesLength = stb_vorbis_stream_length_in_samples(vorbis)
sampleSeconds = stb_vorbis_stream_length_in_seconds(vorbis)
length = (sampleSeconds * 1000).toLong()
val pcm = BufferUtils.createShortBuffer(samplesLength)