mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 11:24:56 -04:00
audio: use doubles internally
This commit is contained in:
parent
050fbea65c
commit
3d8ba64e12
@ -13,21 +13,21 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.data.world.audio
|
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.kotlinglm.vec3.Vec3i
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
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 {
|
interface AbstractAudioPlayer {
|
||||||
|
|
||||||
fun playSoundEvent(sound: ResourceLocation, position: Vec3i? = null, volume: Float = 1.0f, pitch: Float = 1.0f) {
|
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) {
|
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()
|
fun stopAllSounds()
|
||||||
|
@ -13,14 +13,14 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.data.world.audio
|
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
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||||
|
|
||||||
@Deprecated("")
|
@Deprecated("")
|
||||||
interface WorldAudioPlayer : AbstractAudioPlayer {
|
interface WorldAudioPlayer : AbstractAudioPlayer {
|
||||||
val audioPlayer: 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)
|
audioPlayer?.playSound(sound, position, volume, pitch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
package de.bixilon.minosoft.gui.rendering.sound
|
package de.bixilon.minosoft.gui.rendering.sound
|
||||||
|
|
||||||
import de.bixilon.kotlinglm.vec3.Vec3
|
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.synchronizedListOf
|
||||||
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedList
|
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedList
|
||||||
import de.bixilon.kutil.concurrent.queue.Queue
|
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.camera.CameraDefinition.CAMERA_UP_VEC3
|
||||||
import de.bixilon.minosoft.gui.rendering.events.CameraPositionChangeEvent
|
import de.bixilon.minosoft.gui.rendering.events.CameraPositionChangeEvent
|
||||||
import de.bixilon.minosoft.gui.rendering.sound.sounds.Sound
|
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.gui.rendering.util.vec.vec3.Vec3Util.EMPTY
|
||||||
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen
|
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen
|
||||||
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
|
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
|
||||||
@ -121,7 +123,7 @@ class AudioPlayer(
|
|||||||
latch.dec()
|
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) {
|
if (!initialized) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -180,9 +182,9 @@ class AudioPlayer(
|
|||||||
return source
|
return source
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldPlay(sound: Sound, position: Vec3?): Boolean {
|
private fun shouldPlay(sound: Sound, position: Vec3d?): Boolean {
|
||||||
if (position == null) return true
|
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) {
|
if (distance >= sound.attenuationDistance * sound.attenuationDistance) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -190,7 +192,7 @@ class AudioPlayer(
|
|||||||
return true
|
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) {
|
if (!profile.enabled) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -205,7 +207,7 @@ class AudioPlayer(
|
|||||||
}
|
}
|
||||||
position?.let {
|
position?.let {
|
||||||
source.relative = false
|
source.relative = false
|
||||||
source.position = it
|
source.position = it.toVec3
|
||||||
} ?: let {
|
} ?: let {
|
||||||
source.position = Vec3.EMPTY
|
source.position = Vec3.EMPTY
|
||||||
source.relative = true
|
source.relative = true
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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
|
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.ExplosionEvent
|
||||||
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
|
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
|
||||||
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener
|
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener
|
||||||
@ -29,9 +28,9 @@ object DefaultAudioBehavior {
|
|||||||
val world = connection.world
|
val world = connection.world
|
||||||
val invokers = listOf(
|
val invokers = listOf(
|
||||||
CallbackEventListener.of<PlaySoundEvent> { world.playSound(it.soundEvent, it.position, it.volume, it.pitch) },
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.modding.event.events
|
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.SoundCategories
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||||
import de.bixilon.minosoft.modding.event.events.connection.play.PlayConnectionEvent
|
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(
|
class PlaySoundEvent(
|
||||||
connection: PlayConnection,
|
connection: PlayConnection,
|
||||||
val category: SoundCategories?,
|
val category: SoundCategories?,
|
||||||
position: Vec3,
|
position: Vec3d,
|
||||||
val soundEvent: ResourceLocation,
|
val soundEvent: ResourceLocation,
|
||||||
val volume: Float,
|
val volume: Float,
|
||||||
val pitch: Float,
|
val pitch: Float,
|
||||||
) : PlayConnectionEvent(connection), CancelableEvent {
|
) : PlayConnectionEvent(connection), CancelableEvent {
|
||||||
val position: Vec3 = position
|
val position: Vec3d = position
|
||||||
get() = Vec3(field)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.bixilon.minosoft.protocol.packets.s2c.play.sound
|
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.SoundCategories
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||||
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
|
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
|
||||||
@ -33,7 +33,7 @@ class NamedSoundS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
|
|||||||
val soundEvent: ResourceLocation?
|
val soundEvent: ResourceLocation?
|
||||||
val volume: Float
|
val volume: Float
|
||||||
val pitch: Float
|
val pitch: Float
|
||||||
lateinit var position: Vec3
|
lateinit var position: Vec3d
|
||||||
private set
|
private set
|
||||||
lateinit var category: SoundCategories
|
lateinit var category: SoundCategories
|
||||||
private set
|
private set
|
||||||
@ -49,13 +49,13 @@ class NamedSoundS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
|
|||||||
buffer.readString() // parrot entity type
|
buffer.readString() // parrot entity type
|
||||||
}
|
}
|
||||||
if (buffer.versionId < V_16W02A) {
|
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)) {
|
if (buffer.versionId >= V_16W02A && (buffer.versionId !in V_17W15A until V_17W18A)) {
|
||||||
this.category = SoundCategories[buffer.readVarInt()]
|
this.category = SoundCategories[buffer.readVarInt()]
|
||||||
}
|
}
|
||||||
if (buffer.versionId >= V_16W02A) {
|
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()
|
volume = buffer.readFloat()
|
||||||
pitch = buffer.readSoundPitch()
|
pitch = buffer.readSoundPitch()
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.bixilon.minosoft.protocol.packets.s2c.play.sound
|
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.SoundCategories
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||||
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
|
import de.bixilon.minosoft.modding.event.events.PlaySoundEvent
|
||||||
@ -33,7 +33,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
|
|||||||
class SoundEventS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
|
class SoundEventS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
|
||||||
var category: SoundCategories? = null
|
var category: SoundCategories? = null
|
||||||
private set
|
private set
|
||||||
val position: Vec3i
|
val position: Vec3d
|
||||||
val sound: ResourceLocation
|
val sound: ResourceLocation
|
||||||
val volume: Float
|
val volume: Float
|
||||||
val pitch: 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)) {
|
if (buffer.versionId >= V_16W02A && (buffer.versionId !in V_17W15A until V_17W18A)) {
|
||||||
this.category = SoundCategories[buffer.readVarInt()]
|
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()
|
volume = buffer.readFloat()
|
||||||
pitch = buffer.readSoundPitch()
|
pitch = buffer.readSoundPitch()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user