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

@@ -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!!)
}