mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-14 04:24:05 +09:00
music track resampling
This commit is contained in:
@@ -309,10 +309,11 @@ object AudioMixer: Disposable {
|
||||
|
||||
// stop streaming if fadeBus is muted
|
||||
if (req.fadeTarget == 0.0 && track == fadeBus) {
|
||||
musicTrack.stop()
|
||||
musicTrack.currentTrack = null
|
||||
musicTrack.streamPlaying = false
|
||||
|
||||
ambientTrack.stop()
|
||||
ambientTrack.currentTrack = null
|
||||
ambientTrack.streamPlaying = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.torvald.terrarum.audio
|
||||
|
||||
import com.jme3.math.FastMath
|
||||
import net.torvald.terrarum.App.printdbg
|
||||
import net.torvald.terrarum.audio.TerrarumAudioMixerTrack.Companion.BUFFER_SIZE
|
||||
import net.torvald.terrarum.audio.TerrarumAudioMixerTrack.Companion.SAMPLING_RATE
|
||||
import net.torvald.terrarum.ceilToInt
|
||||
@@ -40,7 +41,7 @@ class AudioProcessBuf(inputSamplingRate: Int, val audioReadFun: (ByteArray) -> I
|
||||
private val samplesIn = inputSamplingRate / gcd // 147 for 44100
|
||||
private val samplesOut = SAMPLING_RATE / gcd // 160 for 48000
|
||||
|
||||
private val internalBufferSize = samplesOut * ((BS.toFloat()) / samplesIn).ceilToInt() // (512 / 160) -> 640 for 44100, 48000
|
||||
private val internalBufferSize = if (doResample) (BS.toFloat() / samplesOut).ceilToInt().plus(1) * samplesOut else BS // (512 / 160) -> 640 for 44100, 48000
|
||||
|
||||
|
||||
private fun resampleBlock(inn: FloatArray, out: FloatArray) {
|
||||
@@ -75,7 +76,11 @@ class AudioProcessBuf(inputSamplingRate: Int, val audioReadFun: (ByteArray) -> I
|
||||
try {
|
||||
val bytesRead = audioReadFun(readBuf)
|
||||
|
||||
if (bytesRead == null || bytesRead <= 0) onAudioFinished()
|
||||
if (bytesRead == null || bytesRead <= 0) {
|
||||
printdbg(this, "Music finished; bytesRead = $bytesRead")
|
||||
|
||||
onAudioFinished()
|
||||
}
|
||||
else {
|
||||
for(c in 0 until readCount) {
|
||||
val sl = (getFromReadBuf(4 * c + 0, bytesRead) or getFromReadBuf(4 * c + 1, bytesRead).shl(8)).toShort()
|
||||
@@ -119,6 +124,10 @@ class AudioProcessBuf(inputSamplingRate: Int, val audioReadFun: (ByteArray) -> I
|
||||
// shift bytes in the fout
|
||||
System.arraycopy(foutL, BS, foutL, 0, validSamplesInBuf - BS)
|
||||
System.arraycopy(foutR, BS, foutR, 0, validSamplesInBuf - BS)
|
||||
for (i in validSamplesInBuf until BS) {
|
||||
foutL[i] = 0f
|
||||
foutR[i] = 0f
|
||||
}
|
||||
// decrement necessary variables
|
||||
validSamplesInBuf -= BS
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
package net.torvald.terrarum.audio
|
||||
|
||||
import com.badlogic.gdx.backends.lwjgl3.audio.Mp3
|
||||
import com.badlogic.gdx.utils.Queue
|
||||
import javazoom.jl.decoder.Bitstream
|
||||
import javazoom.jl.decoder.MP3Decoder
|
||||
import net.torvald.reflection.extortField
|
||||
import net.torvald.reflection.forceInvoke
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.audio.dsp.BinoPan
|
||||
import net.torvald.terrarum.audio.dsp.NullFilter
|
||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
||||
import net.torvald.terrarum.printStackTrace
|
||||
import net.torvald.terrarum.relativeXposition
|
||||
import net.torvald.terrarum.sqr
|
||||
import kotlin.math.*
|
||||
@@ -44,11 +49,17 @@ class MixerTrackProcessor(val bufferSize: Int, val rate: Int, val track: Terraru
|
||||
}
|
||||
|
||||
private fun allocateStreamBuf(track: TerrarumAudioMixerTrack) {
|
||||
printdbg("Allocating a StreamBuf with rate ${track.currentTrack!!.samplingRate}")
|
||||
streamBuf = AudioProcessBuf(track.currentTrack!!.samplingRate, {
|
||||
track.currentTrack?.gdxMusic?.forceInvoke<Int>("read", arrayOf(it))
|
||||
val l = track.currentTrack?.gdxMusic?.forceInvoke<Int>("read", arrayOf(it))
|
||||
track.currentTrack?.let {
|
||||
it.samplesRead += l!! / 4
|
||||
}
|
||||
l
|
||||
}, {
|
||||
track.stop()
|
||||
this.streamBuf = null
|
||||
printdbg("StreamBuf is now null")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -139,6 +139,7 @@ class TerrarumAudioMixerTrack(val name: String, val trackType: TrackType, var ma
|
||||
override fun equals(other: Any?) = this.hash == (other as TerrarumAudioMixerTrack).hash
|
||||
|
||||
fun stop() {
|
||||
currentTrack?.samplesRead = 0L
|
||||
currentTrack?.gdxMusic?.forceInvoke<Int>("reset", arrayOf())
|
||||
streamPlaying = false
|
||||
// playStartedTime = 0L
|
||||
@@ -151,6 +152,7 @@ class TerrarumAudioMixerTrack(val name: String, val trackType: TrackType, var ma
|
||||
// fireSoundFinishHook()
|
||||
|
||||
trackingTarget = null
|
||||
processor.streamBuf = null
|
||||
}
|
||||
|
||||
fun fireSongFinishHook() {
|
||||
|
||||
Reference in New Issue
Block a user