audio: use doubles internally

This commit is contained in:
Bixilon 2023-05-25 14:29:18 +02:00
parent 050fbea65c
commit 3d8ba64e12
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 30 additions and 29 deletions

View File

@ -13,21 +13,21 @@
package de.bixilon.minosoft.data.world.audio
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.gui.rendering.util.VecUtil.centerf
import de.bixilon.minosoft.gui.rendering.util.VecUtil.center
interface AbstractAudioPlayer {
fun playSoundEvent(sound: ResourceLocation, position: Vec3i? = null, volume: Float = 1.0f, pitch: Float = 1.0f) {
playSound(sound, position?.centerf, volume, pitch)
playSound(sound, position?.center, volume, pitch)
}
fun playSound(sound: ResourceLocation, position: Vec3? = null, volume: Float = 1.0f, pitch: Float = 1.0f)
fun playSound(sound: ResourceLocation, position: Vec3d? = null, volume: Float = 1.0f, pitch: Float = 1.0f)
fun play2DSound(sound: ResourceLocation, volume: Float = 1.0f, pitch: Float = 1.0f) {
playSound(sound, null as Vec3?, volume, pitch)
playSound(sound, null as Vec3d?, volume, pitch)
}
fun stopAllSounds()

View File

@ -13,14 +13,14 @@
package de.bixilon.minosoft.data.world.audio
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
@Deprecated("")
interface WorldAudioPlayer : AbstractAudioPlayer {
val audioPlayer: AbstractAudioPlayer?
override fun playSound(sound: ResourceLocation, position: Vec3?, volume: Float, pitch: Float) {
override fun playSound(sound: ResourceLocation, position: Vec3d?, volume: Float, pitch: Float) {
audioPlayer?.playSound(sound, position, volume, pitch)
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.rendering.sound
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kutil.collections.CollectionUtil.synchronizedListOf
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedList
import de.bixilon.kutil.concurrent.queue.Queue
@ -25,6 +26,7 @@ import de.bixilon.minosoft.gui.rendering.Rendering
import de.bixilon.minosoft.gui.rendering.camera.CameraDefinition.CAMERA_UP_VEC3
import de.bixilon.minosoft.gui.rendering.events.CameraPositionChangeEvent
import de.bixilon.minosoft.gui.rendering.sound.sounds.Sound
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec3
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@ -121,7 +123,7 @@ class AudioPlayer(
latch.dec()
}
override fun playSound(sound: ResourceLocation, position: Vec3?, volume: Float, pitch: Float) {
override fun playSound(sound: ResourceLocation, position: Vec3d?, volume: Float, pitch: Float) {
if (!initialized) {
return
}
@ -180,9 +182,9 @@ class AudioPlayer(
return source
}
private fun shouldPlay(sound: Sound, position: Vec3?): Boolean {
private fun shouldPlay(sound: Sound, position: Vec3d?): Boolean {
if (position == null) return true
val distance = (this.listener.position - position).length2()
val distance = (this.listener.position - position.toVec3).length2()
if (distance >= sound.attenuationDistance * sound.attenuationDistance) {
return false
}
@ -190,7 +192,7 @@ class AudioPlayer(
return true
}
private fun playSound(sound: Sound, position: Vec3? = null, volume: Float = 1.0f, pitch: Float = 1.0f) {
private fun playSound(sound: Sound, position: Vec3d? = null, volume: Float = 1.0f, pitch: Float = 1.0f) {
if (!profile.enabled) {
return
}
@ -205,7 +207,7 @@ class AudioPlayer(
}
position?.let {
source.relative = false
source.position = it
source.position = it.toVec3
} ?: let {
source.position = Vec3.EMPTY
source.relative = true

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -13,7 +13,6 @@
package de.bixilon.minosoft.gui.rendering.sound
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.minosoft.modding.event.events.ExplosionEvent
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener
@ -29,9 +28,9 @@ object DefaultAudioBehavior {
val world = connection.world
val invokers = listOf(
CallbackEventListener.of<PlaySoundEvent> { world.playSound(it.soundEvent, it.position, it.volume, it.pitch) },
CallbackEventListener.of<ExplosionEvent> { world.playSound(ENTITY_GENERIC_EXPLODE, Vec3(it.position), 4.0f, (1.0f + (random.nextFloat() - random.nextFloat()) * 0.2f) * 0.7f) },
CallbackEventListener.of<ExplosionEvent> { world.playSound(ENTITY_GENERIC_EXPLODE, it.position, 4.0f, (1.0f + (random.nextFloat() - random.nextFloat()) * 0.2f) * 0.7f) },
)
connection.register(*invokers.toTypedArray())
connection.events.register(*invokers.toTypedArray())
}
}

View File

@ -13,7 +13,7 @@
package de.bixilon.minosoft.modding.event.events
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.SoundCategories
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.modding.event.events.connection.play.PlayConnectionEvent
@ -24,15 +24,15 @@ import de.bixilon.minosoft.protocol.packets.s2c.play.sound.SoundEventS2CP
class PlaySoundEvent(
connection: PlayConnection,
val category: SoundCategories?,
position: Vec3,
position: Vec3d,
val soundEvent: ResourceLocation,
val volume: Float,
val pitch: Float,
) : PlayConnectionEvent(connection), CancelableEvent {
val position: Vec3 = position
get() = Vec3(field)
val position: Vec3d = position
get() = Vec3d(field)
constructor(connection: PlayConnection, packet: SoundEventS2CP) : this(connection, packet.category, Vec3(packet.position), packet.sound, packet.volume, packet.pitch.toFloat())
constructor(connection: PlayConnection, packet: SoundEventS2CP) : this(connection, packet.category, packet.position, packet.sound, packet.volume, packet.pitch)
constructor(connection: PlayConnection, packet: NamedSoundS2CP) : this(connection, packet.category, packet.position, packet.soundEvent!!, packet.volume, packet.pitch.toFloat())
constructor(connection: PlayConnection, packet: NamedSoundS2CP) : this(connection, packet.category, packet.position, packet.soundEvent!!, packet.volume, packet.pitch)
}

View File

@ -12,7 +12,7 @@
*/
package de.bixilon.minosoft.protocol.packets.s2c.play.sound
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.SoundCategories
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
@ -33,7 +33,7 @@ class NamedSoundS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val soundEvent: ResourceLocation?
val volume: Float
val pitch: Float
lateinit var position: Vec3
lateinit var position: Vec3d
private set
lateinit var category: SoundCategories
private set
@ -49,13 +49,13 @@ class NamedSoundS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
buffer.readString() // parrot entity type
}
if (buffer.versionId < V_16W02A) {
position = Vec3(buffer.readInt() * 8, buffer.readInt() * 8, buffer.readInt() * 8) // ToDo: check if it is not * 4
position = Vec3d(buffer.readInt() * 4, buffer.readInt() * 4, buffer.readInt() * 4)
}
if (buffer.versionId >= V_16W02A && (buffer.versionId !in V_17W15A until V_17W18A)) {
this.category = SoundCategories[buffer.readVarInt()]
}
if (buffer.versionId >= V_16W02A) {
position = Vec3(buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4)
position = Vec3d(buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4)
}
volume = buffer.readFloat()
pitch = buffer.readSoundPitch()

View File

@ -12,7 +12,7 @@
*/
package de.bixilon.minosoft.protocol.packets.s2c.play.sound
import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.SoundCategories
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
@ -33,7 +33,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
class SoundEventS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
var category: SoundCategories? = null
private set
val position: Vec3i
val position: Vec3d
val sound: ResourceLocation
val volume: Float
val pitch: Float
@ -65,7 +65,7 @@ class SoundEventS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
if (buffer.versionId >= V_16W02A && (buffer.versionId !in V_17W15A until V_17W18A)) {
this.category = SoundCategories[buffer.readVarInt()]
}
position = Vec3i(buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4)
position = Vec3d(buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4)
volume = buffer.readFloat()
pitch = buffer.readSoundPitch()