Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3D sound API (not implemented yet) #2161

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions korge-core/src/korlibs/audio/sound/PlatformAudioOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import korlibs.datastructure.thread.*
import korlibs.io.async.*
import korlibs.io.lang.*
import korlibs.math.*
import korlibs.math.geom.*
import korlibs.time.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
Expand Down Expand Up @@ -33,6 +34,7 @@ open class NewPlatformAudioOutput(
override var pitch: Double = 1.0
override var volume: Double = 1.0
override var panning: Double = 0.0
override var position: Vector3 = Vector3.ZERO

protected open fun internalStart() = Unit
protected open fun internalStop() = Unit
Expand Down Expand Up @@ -83,6 +85,7 @@ open class PlatformAudioOutput(
override var pitch: Double = 1.0
override var volume: Double = 1.0
override var panning: Double = 0.0
override var position: Vector3 = Vector3.ZERO
open suspend fun add(samples: AudioSamples, offset: Int = 0, size: Int = samples.totalSamples) {
delay(100.milliseconds)
}
Expand Down Expand Up @@ -196,6 +199,7 @@ open class DequeBasedPlatformAudioOutput(
override var pitch: Double = 1.0 ; set(value) { field = value; updateProps() }
override var volume: Double = 1.0 ; set(value) { field = value; updateProps() }
override var panning: Double = 0.0 ; set(value) { field = value; updateProps() }
override var position: Vector3 = Vector3.ZERO; set(value) { field = value; updateProps() }

var volumes = FloatArray(2) { 1f }

Expand Down
18 changes: 18 additions & 0 deletions korge-core/src/korlibs/audio/sound/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import korlibs.io.file.*
import korlibs.io.lang.*
import korlibs.io.stream.*
import korlibs.math.*
import korlibs.math.geom.*
import korlibs.time.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
Expand Down Expand Up @@ -50,6 +51,12 @@ open class NativeSoundProvider() : Disposable, Pauseable {

override var paused: Boolean = false

open var listenerPosition: Vector3 = Vector3.ZERO
open var listenerOrientationAt: Vector3 = Vector3.FORWARD // Look vector
open var listenerOrientationUp: Vector3 = Vector3.UP
// @TODO: Should this be estimated automatically from position samples?
open var listenerSpeed: Vector3 = Vector3.ZERO

@Deprecated("")
open fun createPlatformAudioOutput(coroutineContext: CoroutineContext, freq: Int = 44100): PlatformAudioOutput = PlatformAudioOutput(coroutineContext, freq)
@Deprecated("")
Expand Down Expand Up @@ -164,12 +171,14 @@ interface ReadonlySoundProps {
val volume: Double
val pitch: Double
val panning: Double
val position: Vector3
}

interface SoundProps : ReadonlySoundProps {
override var volume: Double
override var pitch: Double
override var panning: Double
override var position: Vector3
}

object DummySoundProps : SoundProps {
Expand All @@ -182,6 +191,7 @@ object DummySoundProps : SoundProps {
override var panning: Double
get() = 0.0
set(v) = Unit
override var position: Vector3 = Vector3.ZERO
}

fun SoundProps.copySoundPropsFrom(other: ReadonlySoundProps) {
Expand Down Expand Up @@ -250,6 +260,11 @@ class SoundChannelGroup(volume: Double = 1.0, pitch: Double = 1.0, panning: Doub
field = value
all { it.panning = value }
}
override var position: Vector3 = Vector3.ZERO
set(value) {
field = value
all { it.position = value }
}

init {
this.volume = volume
Expand Down Expand Up @@ -315,6 +330,7 @@ abstract class SoundChannel(val sound: Sound) : SoundChannelBase, Extra by Extra
override var volume = 1.0
override var pitch = 1.0
override var panning = 0.0 // -1.0 left, +1.0 right
override var position: Vector3 = Vector3.ZERO
// @TODO: Rename to position
open var current: TimeSpan
get() = DateTime.now() - startTime
Expand Down Expand Up @@ -352,6 +368,7 @@ abstract class Sound(val creationCoroutineContext: CoroutineContext) : SoundProp
override var volume: Double = 1.0
override var panning: Double = 0.0
override var pitch: Double = 1.0
override var position: Vector3 = Vector3.ZERO
open val length: TimeSpan = 0.seconds
open val nchannels: Int get() = 1

Expand Down Expand Up @@ -382,6 +399,7 @@ data class PlaybackParameters(
override val volume: Double = 1.0,
override val pitch: Double = 1.0,
override val panning: Double = 0.0,
override val position: Vector3 = Vector3.ZERO,
val onCancel: (() -> Unit)? = null,
val onFinish: (() -> Unit)? = null,
) : ReadonlySoundProps {
Expand Down
Loading