audio:short function to enqueue decoded mp2; playmov:no more choppy audio

This commit is contained in:
minjaesong
2023-01-21 19:43:11 +09:00
parent b60b5bada6
commit 99212adea3
7 changed files with 18 additions and 48 deletions

View File

@@ -83,7 +83,7 @@ function getRepeatCount(fnum) {
return (fnum == 1) ? 2 : 1
}
else if ("MP2fr" == AUDIOFORMAT) {
let r = Math.ceil((AUDIO_SAMPLE_SIZE*2 - audioSamplesWrote) / AUDIO_SAMPLE_SIZE) + ((fnum == 1) ? 1 : 0)
let r = Math.ceil((AUDIO_SAMPLE_SIZE - audioSamplesWrote) / AUDIO_SAMPLE_SIZE) + ((fnum == 1) ? 1 : 0)
return (fnum > TOTAL_FRAMES) ? Math.ceil(audioRemaining / MP2_PACKETSIZE) : r
}
}

View File

@@ -236,7 +236,10 @@ while (!stopPlay && seqread.getReadCount() < FILE_LENGTH) {
AUDIO_QUEUE_LENGTH += 1
audioQueue.push(sys.malloc(AUDIO_QUEUE_BYTES))
}
if (!mp2Initialised) audio.mp2Init()
if (!mp2Initialised) {
mp2Initialised = true
audio.mp2Init()
}
seqread.readBytes(readLength, SND_BASE_ADDR - 2368)
audio.mp2Decode()

View File

@@ -79,47 +79,7 @@ let bytes_left = FILE_SIZE
let decodedLength = 0
serial.println(`Frame size: ${FRAME_SIZE}`)
function decodeAndResample(inPtrL, inPtrR, outPtr, inputLen) {
// TODO resample
for (let k = 0; k < inputLen; k+=2) {
let sample1 = pcm.u16Tos16(sys.peek(inPtrL + k + 0) | (sys.peek(inPtrL + k + 1) << 8))
let sample2 = pcm.u16Tos16(sys.peek(inPtrR + k + 0) | (sys.peek(inPtrR + k + 1) << 8))
sys.poke(outPtr + k, pcm.s16Tou8(sample1))
sys.poke(outPtr + k + 1, pcm.s16Tou8(sample2))
}
}
function decodeEvent(frameSize, len) {
let t2 = sys.nanoTime()
// printdbg(`Audio queue size: ${audio.getPosition(0)}/${QUEUE_MAX}`)
if (audio.getPosition(0) >= QUEUE_MAX) {
while (audio.getPosition(0) >= (QUEUE_MAX >>> 1)) {
printdbg(`Queue full, waiting until the queue has some space (${audio.getPosition(0)}/${QUEUE_MAX})`)
sys.sleep(bufRealTimeLen)
}
}
// decodeAndResample(samplePtrL, samplePtrR, decodePtr, len)
audio.putPcmDataByPtr(decodePtr, len, 0)
audio.setSampleUploadLength(0, len)
audio.startSampleUpload(0)
sys.sleep(10)
let decodingTime = (t2 - t1) / 1000000.0
bufRealTimeLen = (len) / 64000.0 * 1000
t1 = t2
// println(`Decoded ${decodedLength} bytes; target: ${bufRealTimeLen} ms, lag: ${decodingTime - bufRealTimeLen} ms`)
}
//serial.println(`Frame size: ${FRAME_SIZE}`)
con.curs_set(0)
@@ -203,9 +163,7 @@ try {
sys.sleep(bufRealTimeLen)
}
}
audio.putPcmDataByPtr(SND_BASE_ADDR - 64, 2304, 0)
audio.setSampleUploadLength(0, 2304)
audio.startSampleUpload(0)
audio.mp2UploadDecoded(0)
sys.sleep(10)

View File

@@ -1,5 +1,6 @@
package net.torvald.tsvm
import net.torvald.UnsafeHelper
import net.torvald.tsvm.peripheral.AudioAdapter
import net.torvald.tsvm.peripheral.MP2Env
@@ -84,6 +85,13 @@ class AudioJSR223Delegate(private val vm: VM) {
fun mp2Init() = getFirstSnd()?.mmio_write(40L, 16)
fun mp2Decode() = getFirstSnd()?.mmio_write(40L, 1)
fun mp2InitThenDecode() = getFirstSnd()?.mmio_write(40L, 17)
fun mp2UploadDecoded(playhead: Int) {
getFirstSnd()?.let { snd ->
val ba = ByteArray(2304)
UnsafeHelper.memcpyRaw(null, snd.mediaDecodedBin.ptr, ba, UnsafeHelper.getArrayOffset(ba), 2304)
snd.playheads[playhead].pcmQueue.addLast(ba)
}
}

View File

@@ -82,7 +82,7 @@ class VMJSR223Delegate(private val vm: VM) {
val from = from.toLong()
val to = to.toLong()
val len = len.toLong()
val fromVector = if (from >= 0) 1 else -1
val toVector = if (to >= 0) 1 else -1
val fromDev = getDev(from, len, false)

View File

@@ -69,7 +69,7 @@ private class WriteQueueingRunnable(val playhead: AudioAdapter.Playhead, val pcm
while (!exit) {
playhead.let {
if (it.pcmQueue.size < it.getPcmQueueCapacity() && it.pcmUpload && it.pcmUploadLength > 0) {
if (/*it.pcmQueue.size < it.getPcmQueueCapacity() &&*/ it.pcmUpload && it.pcmUploadLength > 0) {
printdbg("Downloading samples ${it.pcmUploadLength}")
val samples = ByteArray(it.pcmUploadLength)

View File

@@ -47,6 +47,7 @@ import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.roundToInt
class MP2Env(val vm: VM) {
private var mp2_frame: Long? = null; // ptr