mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 12:21:52 +09:00
audiocodex: no shared music instances
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
package net.torvald.terrarum.audio
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.audio.Music
|
||||
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.tryDispose
|
||||
|
||||
@@ -13,22 +10,25 @@ typealias MaterialID = String
|
||||
/**
|
||||
* 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()
|
||||
|
||||
fun addToFootstepPool(materialID: MaterialID, music: FileHandle) {
|
||||
if (footsteps[materialID] == null) footsteps[materialID] = HashSet()
|
||||
footsteps[materialID]!!.add(
|
||||
MusicContainer(music.nameWithoutExtension(), music.file(), Gdx.audio.newMusic(music)) {}
|
||||
)
|
||||
footsteps[materialID]!!.add(music)
|
||||
}
|
||||
|
||||
fun getRandomFootstep(materialID: MaterialID) = footsteps[materialID]?.random()
|
||||
|
||||
override fun dispose() {
|
||||
footsteps.values.forEach { it.forEach { it.gdxMusic.tryDispose() } }
|
||||
fun getRandomFootstep(materialID: MaterialID): MusicContainer? {
|
||||
val file = footsteps[materialID]?.random()
|
||||
return if (file != null) {
|
||||
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
|
||||
if (track.trackType != TrackType.MASTER && track.trackType != TrackType.BUS && track.streamPlaying) {
|
||||
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 {
|
||||
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
|
||||
|
||||
@@ -2047,7 +2047,7 @@ open class ActorWithBody : Actor {
|
||||
|
||||
private fun makeNoise(collisionDamage: Double) {
|
||||
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 volumeMax = collisionDamage / DIVIDER
|
||||
val feetTileMats = feetTiles.slice(feetTiles.indices).map { BlockCodex[it.second].material }
|
||||
|
||||
@@ -1587,7 +1587,6 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
|
||||
}
|
||||
|
||||
musicGovernor.dispose()
|
||||
audioCodex.dispose()
|
||||
super.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user