read sound effects from RAM

This commit is contained in:
minjaesong
2024-04-02 14:32:13 +09:00
parent 53f54a450d
commit 918276a1be
12 changed files with 25 additions and 27 deletions

View File

@@ -554,9 +554,9 @@ public class App implements ApplicationListener {
CommonResourcePool.INSTANCE.addToLoadingList("title_health1", () -> new Texture(Gdx.files.internal("./assets/graphics/gui/health_take_a_break.tga")));
CommonResourcePool.INSTANCE.addToLoadingList("title_health2", () -> new Texture(Gdx.files.internal("./assets/graphics/gui/health_distance.tga")));
CommonResourcePool.INSTANCE.addToLoadingList("sound:haptic_bop", () -> new MusicContainer(true, "haptic_bop", Gdx.files.internal("./assets/audio/effects/haptic_bop.ogg").file(), false, (Music m) -> { return null; }));
CommonResourcePool.INSTANCE.addToLoadingList("sound:haptic_bup", () -> new MusicContainer(true, "haptic_bup", Gdx.files.internal("./assets/audio/effects/haptic_bup.ogg").file(), false, (Music m) -> { return null; }));
CommonResourcePool.INSTANCE.addToLoadingList("sound:haptic_bip", () -> new MusicContainer(true, "haptic_bip", Gdx.files.internal("./assets/audio/effects/haptic_bip.ogg").file(), false, (Music m) -> { highPrioritySoundPlaying = false; return null; }));
CommonResourcePool.INSTANCE.addToLoadingList("sound:haptic_bop", () -> new MusicContainer("haptic_bop", Gdx.files.internal("./assets/audio/effects/haptic_bop.ogg").file(), false, true, (MusicContainer m) -> { return null; }));
CommonResourcePool.INSTANCE.addToLoadingList("sound:haptic_bup", () -> new MusicContainer("haptic_bup", Gdx.files.internal("./assets/audio/effects/haptic_bup.ogg").file(), false, true, (MusicContainer m) -> { return null; }));
CommonResourcePool.INSTANCE.addToLoadingList("sound:haptic_bip", () -> new MusicContainer("haptic_bip", Gdx.files.internal("./assets/audio/effects/haptic_bip.ogg").file(), false, true, (MusicContainer m) -> { highPrioritySoundPlaying = false; return null; }));
// make loading list
CommonResourcePool.INSTANCE.loadAll();

View File

@@ -124,7 +124,6 @@ class AudioMixer : Disposable {
fun getFreeGuiTrackNoMatterWhat(): TerrarumAudioMixerTrack {
synchronized(this) {
val it = getFreeGuiTrack() ?: guiTracks.minBy { it.playStartedTime }.also { it.checkedOutTime = System.nanoTime() }
println("GuiTrack ${it.name}")
return it
}
}

View File

@@ -88,10 +88,7 @@ class MixerTrackProcessor(bufferSize: Int, val rate: Int, val track: TerrarumAud
private fun allocateStreamBuf(track: TerrarumAudioMixerTrack) {
printdbg("Allocating a StreamBuf with rate ${track.currentTrack!!.samplingRate}")
streamBuf = AudioProcessBuf(track.currentTrack!!.samplingRate, { buffer ->
var bytesRead = track.currentTrack?.gdxMusic?.forceInvoke<Int>("read", arrayOf(buffer)) ?: 0
// increment samplesRead of the current track
track.currentTrack?.let { it.samplesRead += bytesRead / 4 }
var bytesRead = track.currentTrack?.readBytes(buffer) ?: 0
// do gapless fetch if there is space in the buffer
if (track.doGaplessPlayback && bytesRead < buffer.size) {
@@ -116,9 +113,8 @@ class MixerTrackProcessor(bufferSize: Int, val rate: Int, val track: TerrarumAud
private fun read0(buffer: ByteArray, bytesRead: Int): Int {
val tmpBuf = ByteArray(buffer.size - bytesRead)
val newRead = track.currentTrack?.gdxMusic?.forceInvoke<Int>("read", arrayOf(tmpBuf)) ?: 0
val newRead = track.currentTrack?.readBytes(tmpBuf) ?: 0
track.currentTrack?.let { it.samplesRead += newRead / 4 }
System.arraycopy(tmpBuf, 0, buffer, bytesRead, tmpBuf.size)
return newRead

View File

@@ -19,11 +19,11 @@ import java.io.FileInputStream
import javax.sound.sampled.AudioSystem
data class MusicContainer(
val toRAM: Boolean = false,
val name: String,
val file: File,
val looping: Boolean = false,
internal var songFinishedHook: (Music) -> Unit = {}
val toRAM: Boolean = false,
internal var songFinishedHook: (MusicContainer) -> Unit = {}
): Disposable {
val samplingRate: Int
val codec: String
@@ -35,11 +35,12 @@ data class MusicContainer(
private var soundBuf: UnsafePtr? = null; private set
private val hash = System.nanoTime()
init {
gdxMusic.isLooping = looping
gdxMusic.setOnCompletionListener(songFinishedHook)
// gdxMusic.setOnCompletionListener(songFinishedHook)
samplingRate = when (gdxMusic) {
is Wav.Music -> {
@@ -114,8 +115,9 @@ data class MusicContainer(
}
else {
val bytesToRead = minOf(buffer.size.toLong(), 4 * (samplesTotal - samplesReadCount))
if (bytesToRead <= 0) return bytesToRead.toInt()
UnsafeHelper.memcpyRaw(null, soundBuf!!.ptr, buffer, UnsafeHelper.getArrayOffset(buffer), bytesToRead)
UnsafeHelper.memcpyRaw(null, soundBuf!!.ptr + samplesReadCount * 4, buffer, UnsafeHelper.getArrayOffset(buffer), bytesToRead)
samplesReadCount += bytesToRead / 4
return bytesToRead.toInt()
@@ -177,6 +179,7 @@ data class MusicContainer(
override fun toString() = if (name.isEmpty()) file.nameWithoutExtension else name
override fun equals(other: Any?) = this.file.path == (other as MusicContainer).file.path
fun equalInstance(other: Any?) = this.file.path == (other as MusicContainer).file.path && this.hash == (other as MusicContainer).hash
override fun dispose() {
gdxMusic.dispose()

View File

@@ -49,7 +49,6 @@ class TerrarumAudioMixerTrack(
get() = field
set(value) {
field = value
currentTrack?.gdxMusic?.volume = volume.toFloat()
}
val maxVolume: Double
@@ -182,7 +181,7 @@ class TerrarumAudioMixerTrack(
}
fun fireSongFinishHook() {
currentTrack?.songFinishedHook?.invoke(currentTrack!!.gdxMusic)
currentTrack?.songFinishedHook?.invoke(currentTrack!!)
}

View File

@@ -217,7 +217,7 @@ abstract class Actor : Comparable<Actor>, Runnable {
}*/
open @Event fun onAudioInterrupt(music: MusicContainer) {
music.songFinishedHook(music.gdxMusic)
music.songFinishedHook(music)
}
abstract fun dispose()

View File

@@ -51,7 +51,7 @@ class TerrarumMusicGovernor : MusicGovernor() {
printdbg(this, "MusicTitle: ${muscon.name}")
muscon.songFinishedHook = {
if (it == App.audioMixer.musicTrack.currentTrack?.gdxMusic) {
if (App.audioMixer.musicTrack.currentTrack == it) {
stopMusic(this, true, getRandomMusicInterval())
}
}
@@ -79,7 +79,7 @@ class TerrarumMusicGovernor : MusicGovernor() {
stopMusic0(App.audioMixer.musicTrack.currentTrack)
}
songs.forEach { it.gdxMusic.tryDispose() }
songs.forEach { it.tryDispose() }
registerSongsFromDir(musicDir, fileToName)
this.shuffled = shuffled
@@ -174,13 +174,13 @@ class TerrarumMusicGovernor : MusicGovernor() {
init {
songs.forEach {
App.disposables.add(it.gdxMusic)
App.disposables.add(it)
}
ambients.forEach { (k, v) ->
printdbg(this, "Ambients: $k -> $v")
v.forEach {
App.disposables.add(it.gdxMusic)
App.disposables.add(it)
}
}
}

View File

@@ -34,17 +34,17 @@ open class ActorPrimedBomb(
private var explosionCalled = false
@Transient private val boomSound = MusicContainer(
"boom", ModMgr.getFile("basegame", "audio/effects/explosion/bang_bomb.ogg")
"boom", ModMgr.getFile("basegame", "audio/effects/explosion/bang_bomb.ogg"), toRAM = true
) {
this.flagDespawn()
}
@Transient private val fuseSound = MusicContainer(
"fuse", ModMgr.getFile("basegame", "audio/effects/explosion/fuse.ogg")
"fuse", ModMgr.getFile("basegame", "audio/effects/explosion/fuse.ogg"), toRAM = true
) {
this.flagDespawn()
}
@Transient private val fuseSoundCont = MusicContainer(
"fuse_continue", ModMgr.getFile("basegame", "audio/effects/explosion/fuse_continue.ogg")
"fuse_continue", ModMgr.getFile("basegame", "audio/effects/explosion/fuse_continue.ogg"), toRAM = true
) {
this.flagDespawn()
}

View File

@@ -130,7 +130,7 @@ class FixtureJukebox : Electric, PlaysMusic {
musicNowPlaying = MusicContainer(title, musicFile.file()) {
unloadEffector(musicNowPlaying)
discCurrentlyPlaying = null
musicNowPlaying?.gdxMusic?.tryDispose()
musicNowPlaying?.tryDispose()
musicNowPlaying = null
printdbg(this, "Stop music $title - $artist")

View File

@@ -115,7 +115,7 @@ class FixtureMusicalTurntable : Electric, PlaysMusic {
musicNowPlaying = MusicContainer(title, musicFile.file()) {
unloadEffector(musicNowPlaying)
musicNowPlaying?.gdxMusic?.tryDispose()
musicNowPlaying?.tryDispose()
musicNowPlaying = null
App.printdbg(this, "Stop music $title - $artist")

View File

@@ -267,6 +267,7 @@ object PickaxeCore {
private val soundCue = MusicContainer(
"pickaxe_sound_cue",
ModMgr.getFile("basegame", "audio/effects/accessibility/pickaxe_valuable.ogg"),
toRAM = true
).also {
App.disposables.add(it)
}

View File

@@ -673,7 +673,7 @@ class BasicDebugInfoWindow : UICanvas() {
// fill the song title line with a progress bar
if (i == 0 && track.currentTrack != null) {
val perc = (track.currentTrack!!.samplesRead.toFloat() / track.currentTrack!!.samplesTotal).coerceAtMost(1f)
val perc = (track.currentTrack!!.samplesReadCount.toFloat() / track.currentTrack!!.samplesTotal).coerceAtMost(1f)
batch.color = COL_PROGRESS_GRAD2
Toolkit.fillArea(batch, x.toFloat(), faderY - (i + 1) * 16f, STRIP_W * perc, 14f)
batch.color = COL_PROGRESS_GRAD