Skip to content

Commit

Permalink
Fixes base volume in a music Sound not being used when playing it (#1751
Browse files Browse the repository at this point in the history
)
  • Loading branch information
soywiz authored Jul 3, 2023
1 parent d530d16 commit 3fbb9c8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import korlibs.time.seconds
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext

class AudioChannel {
class AudioChannel(
val nativeSoundProvider: NativeSoundProvider = korlibs.audio.sound.nativeSoundProvider
) {
private var channel: SoundChannel? = null

val state get() = channel?.state ?: SoundChannelState.INITIAL
Expand Down
3 changes: 1 addition & 2 deletions korau/src/commonMain/kotlin/korlibs/audio/sound/AudioData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ class AudioDataStream(val data: AudioData) : AudioStream(data.rate, data.channel
override suspend fun clone(): AudioStream = AudioDataStream(data)
}


suspend fun AudioData.toSound(): Sound = nativeSoundProvider.createSound(this)
suspend fun AudioData.toSound(soundProvider: NativeSoundProvider = nativeSoundProvider): Sound = soundProvider.createSound(this)

suspend fun VfsFile.readAudioData(formats: AudioFormat = defaultAudioFormats, props: AudioDecodingProps = AudioDecodingProps.DEFAULT): AudioData =
this.openUse { formats.decode(this, props) ?: invalidOp("Can't decode audio file ${this@readAudioData}") }
16 changes: 15 additions & 1 deletion korau/src/commonMain/kotlin/korlibs/audio/sound/AudioSamples.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package korlibs.audio.sound

import korlibs.datastructure.iterators.fastForEach
import korlibs.memory.FastShortTransfer
import korlibs.memory.arraycopy
import korlibs.memory.arrayinterleave
import korlibs.memory.clamp01
import korlibs.audio.internal.SampleConvert
import korlibs.audio.internal.coerceToShort
import korlibs.datastructure.iterators.*
import korlibs.io.lang.assert
import kotlin.math.absoluteValue
import kotlin.math.max
Expand Down Expand Up @@ -104,6 +104,7 @@ class AudioSamples(override val channels: Int, override val totalSamples: Int, v
}

fun scaleVolume(scale: Double): AudioSamples = scaleVolume(scale.toFloat())
fun scaleVolume(channelScales: DoubleArray): AudioSamples = scaleVolume(FloatArray(channelScales.size) { channelScales[it].toFloat() })

fun scaleVolume(scale: Float): AudioSamples {
data.fastForEach { channel ->
Expand All @@ -113,6 +114,14 @@ class AudioSamples(override val channels: Int, override val totalSamples: Int, v
}
return this
}
fun scaleVolume(channelScales: FloatArray): AudioSamples {
data.fastForEachWithIndex { ch, channel ->
for (n in channel.indices) {
channel[n] = (channel[n] * channelScales[ch]).toInt().coerceToShort()
}
}
return this
}

fun setTo(that: AudioSamples): AudioSamples {
that.copyTo(this)
Expand All @@ -125,6 +134,11 @@ class AudioSamples(override val channels: Int, override val totalSamples: Int, v
}
}

fun clone(out: AudioSamples = AudioSamples(channels, totalSamples, Array(data.size) { ShortArray(data[0].size) })) : AudioSamples {
this.copyTo(out)
return out
}

override fun hashCode(): Int = channels + totalSamples * 32 + data.contentDeepHashCode() * 64
override fun equals(other: Any?): Boolean = (other is AudioSamples) && this.channels == other.channels && this.totalSamples == other.totalSamples && this.data.contentDeepEquals(other.data)

Expand Down
6 changes: 4 additions & 2 deletions korau/src/commonMain/kotlin/korlibs/audio/sound/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ open class LogNativeSoundProvider : NativeSoundProvider() {
) : PlatformAudioOutput(coroutineContext, frequency) {
val data = AudioSamplesDeque(2)
override suspend fun add(samples: AudioSamples, offset: Int, size: Int) {
val addInfo = AddInfo(samples, offset, size)
val out = samples.clone()
out.scaleVolume(volume)
val addInfo = AddInfo(out, offset, size)
onBeforeAdd(addInfo)
data.write(samples, offset, size)
data.write(out, offset, size)
onAfterAdd(addInfo)
}
fun consumeToData(): AudioData = data.consumeToData(frequency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SoundAudioStream(
@OptIn(ExperimentalStdlibApi::class)
override fun play(coroutineContext: CoroutineContext, params: PlaybackParameters): SoundChannel {
val nas: PlatformAudioOutput = soundProvider.createPlatformAudioOutput(coroutineContext, stream.rate)
nas.copySoundPropsFrom(params)
nas.copySoundPropsFromCombined(params, this)
var playing = true
var paused = false
var newStream: AudioStream? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package korlibs.audio.sound

import korlibs.io.async.*
import korlibs.io.file.std.*
import kotlin.test.*

class NativeSoundProviderTest {
class DequeNativeSoundProvider : LogNativeSoundProvider() {
}

@Test
fun test() = suspendTest {
val values = listOf(null, 1.0, 0.1).map { volume ->
val nativeSoundProvider = DequeNativeSoundProvider()
val sound = nativeSoundProvider
.createSound(resourcesVfs["wav8bit.wav"], streaming = true)
.also { if (volume != null) it.volume = volume }
sound.playAndWait()
val data = nativeSoundProvider.streams.first().data
(0 until 10).map { data.read(0).toInt() }
}
assertEquals(
listOf(
listOf(0, 1020, 2295, 3315, 4335, 5610, 6630, 7650, 8670, 9690),
listOf(0, 1020, 2295, 3315, 4335, 5610, 6630, 7650, 8670, 9690),
listOf(0, 102, 229, 331, 433, 561, 663, 765, 867, 969),
),
values
)
}
}
2 changes: 1 addition & 1 deletion korge-sandbox/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import korlibs.audio.sound.*
import korlibs.event.*
import korlibs.image.color.*
import korlibs.image.font.*
Expand Down Expand Up @@ -73,7 +74,6 @@ suspend fun main() = Korge(
debug = false,
forceRenderEveryFrame = false
) {
//return@Korge
//sceneContainer().changeTo({MainSprites10k()}); return@start
//sceneContainer().changeTo({MainGraphicsText()}); return@start
//sceneContainer().changeTo({MainUI()}); return@start
Expand Down

0 comments on commit 3fbb9c8

Please sign in to comment.