mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 11:04:05 +09:00
audiocodex: no shared music instances
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
package net.torvald.terrarum.audio
|
package net.torvald.terrarum.audio
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx
|
import com.badlogic.gdx.Gdx
|
||||||
import com.badlogic.gdx.audio.Music
|
|
||||||
import com.badlogic.gdx.files.FileHandle
|
import com.badlogic.gdx.files.FileHandle
|
||||||
import com.badlogic.gdx.utils.Disposable
|
|
||||||
import net.torvald.terrarum.Terrarum
|
|
||||||
import net.torvald.terrarum.modulebasegame.MusicContainer
|
import net.torvald.terrarum.modulebasegame.MusicContainer
|
||||||
import net.torvald.terrarum.tryDispose
|
import net.torvald.terrarum.tryDispose
|
||||||
|
|
||||||
@@ -13,22 +10,25 @@ typealias MaterialID = String
|
|||||||
/**
|
/**
|
||||||
* Created by minjaesong on 2023-12-02.
|
* Created by minjaesong on 2023-12-02.
|
||||||
*/
|
*/
|
||||||
class AudioCodex: Disposable {
|
class AudioCodex {
|
||||||
|
|
||||||
@Transient val footsteps = HashMap<MaterialID, HashSet<MusicContainer>>()
|
@Transient val footsteps = HashMap<MaterialID, HashSet<FileHandle>>()
|
||||||
|
|
||||||
internal constructor()
|
internal constructor()
|
||||||
|
|
||||||
fun addToFootstepPool(materialID: MaterialID, music: FileHandle) {
|
fun addToFootstepPool(materialID: MaterialID, music: FileHandle) {
|
||||||
if (footsteps[materialID] == null) footsteps[materialID] = HashSet()
|
if (footsteps[materialID] == null) footsteps[materialID] = HashSet()
|
||||||
footsteps[materialID]!!.add(
|
footsteps[materialID]!!.add(music)
|
||||||
MusicContainer(music.nameWithoutExtension(), music.file(), Gdx.audio.newMusic(music)) {}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getRandomFootstep(materialID: MaterialID) = footsteps[materialID]?.random()
|
fun getRandomFootstep(materialID: MaterialID): MusicContainer? {
|
||||||
|
val file = footsteps[materialID]?.random()
|
||||||
override fun dispose() {
|
return if (file != null) {
|
||||||
footsteps.values.forEach { it.forEach { it.gdxMusic.tryDispose() } }
|
MusicContainer(file.nameWithoutExtension(), file.file(), Gdx.audio.newMusic(file)) {
|
||||||
|
it.tryDispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else null
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -92,8 +92,6 @@ class MixerTrackProcessor(val bufferSize: Int, val rate: Int, val track: Terraru
|
|||||||
// fetch deviceBufferSize amount of sample from the disk
|
// fetch deviceBufferSize amount of sample from the disk
|
||||||
if (track.trackType != TrackType.MASTER && track.trackType != TrackType.BUS && track.streamPlaying) {
|
if (track.trackType != TrackType.MASTER && track.trackType != TrackType.BUS && track.streamPlaying) {
|
||||||
streamBuf.fetchBytes {
|
streamBuf.fetchBytes {
|
||||||
// FIXME THIS IS NOT THREAD SAFE: trying to play different audio too fast too much, shits happen
|
|
||||||
// OR somehow make sfx memory-streamable
|
|
||||||
try {
|
try {
|
||||||
val bytesRead = track.currentTrack?.gdxMusic?.forceInvoke<Int>("read", arrayOf(it))
|
val bytesRead = track.currentTrack?.gdxMusic?.forceInvoke<Int>("read", arrayOf(it))
|
||||||
if (bytesRead == null || bytesRead <= 0) { // some class (namely Mp3) may return 0 instead of negative value
|
if (bytesRead == null || bytesRead <= 0) { // some class (namely Mp3) may return 0 instead of negative value
|
||||||
|
|||||||
@@ -2047,7 +2047,7 @@ open class ActorWithBody : Actor {
|
|||||||
|
|
||||||
private fun makeNoise(collisionDamage: Double) {
|
private fun makeNoise(collisionDamage: Double) {
|
||||||
val DIVIDER = 108.0
|
val DIVIDER = 108.0
|
||||||
if (collisionDamage / DIVIDER >= 0.1) { // only make noise when the expected volume is at least -20dBfs
|
if (collisionDamage / DIVIDER >= 0.05) { // only make noise when the expected volume is at least -26dBfs
|
||||||
val feetTiles = getFeetTiles()
|
val feetTiles = getFeetTiles()
|
||||||
val volumeMax = collisionDamage / DIVIDER
|
val volumeMax = collisionDamage / DIVIDER
|
||||||
val feetTileMats = feetTiles.slice(feetTiles.indices).map { BlockCodex[it.second].material }
|
val feetTileMats = feetTiles.slice(feetTiles.indices).map { BlockCodex[it.second].material }
|
||||||
|
|||||||
@@ -1587,7 +1587,6 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
musicGovernor.dispose()
|
musicGovernor.dispose()
|
||||||
audioCodex.dispose()
|
|
||||||
super.dispose()
|
super.dispose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user