fix: mp2 player would not play the very first frame

This commit is contained in:
minjaesong
2023-01-16 23:29:41 +09:00
parent 5b86e88779
commit dfe5bf3390
5 changed files with 17 additions and 12 deletions

View File

@@ -67,7 +67,7 @@ class SequentialFileBuffer {
let filebuf = new SequentialFileBuffer(_G.shell.resolvePathInput(exec_args[1]).full)
const FILE_SIZE = filebuf.length - 100
const FILE_SIZE = filebuf.length// - 100
let FRAME_SIZE = audio.mp2GetInitialFrameSize(filebuf.fileHeader)
@@ -182,7 +182,7 @@ let bufRealTimeLen = 36
let stopPlay = false
let errorlevel = 0
try {
while (bytes_left >= 0 && !stopPlay) {
while (bytes_left > 0 && !stopPlay) {
if (interactive) {
sys.poke(-40, 1)
@@ -194,9 +194,6 @@ try {
printPlayBar()
filebuf.readBytes(FRAME_SIZE, frame)
bytes_left -= FRAME_SIZE
decodedLength += FRAME_SIZE
let [frameSize, samples] = audio.mp2DecodeFrame(mp2context, frame, true, samplePtrL, samplePtrR)
if (frameSize) {
// println(samples)
@@ -204,6 +201,9 @@ try {
decodeEvent(frameSize, samples)
FRAME_SIZE = frameSize // JUST IN CASE when a vbr mp2 is not filtered and played thru
}
bytes_left -= FRAME_SIZE
decodedLength += FRAME_SIZE
}
}
catch (e) {

View File

@@ -30,10 +30,7 @@ function s16Tou8(i) {
function u16Tos16(i) { return (i > 32767) ? i - 65536 : i }
function randomRound(k) {
let rnd = (Math.random() + Math.random()) / 2.0 // this produces triangular distribution
if (rnd < (k - (k|0)))
return Math.ceil(k)
else
return Math.floor(k)
return (rnd < (k - (k|0))) ? Math.ceil(k) : Math.floor(k)
}
function lerp(start, end, x) {
return (1 - x) * start + x * end

View File

@@ -777,7 +777,7 @@ class AudioJSR223Delegate(private val vm: VM) {
};
// check for valid header: syncword OK, MPEG-Audio Layer 2
if ((syspeek(mp2_frame!!) != 0xFF) || ((syspeek(mp2_frame!! +1) and 0xFE) != 0xFC)){
throw Error("Invalid header")
throw Error("Invalid header at $mp2_frame: ${syspeek(mp2_frame!!).toString(16)} ${syspeek(mp2_frame!! +1).toString(16)}")
};
// set up the bitstream reader

View File

@@ -1,8 +1,9 @@
package net.torvald.tsvm
import com.badlogic.gdx.utils.Base64Coder
import net.torvald.UnsafeHelper
object Base64Delegate {
class Base64Delegate(val vm: VM) {
fun atob(inputstr: String): ByteArray {
return Base64Coder.decode(inputstr)
@@ -18,4 +19,11 @@ object Base64Delegate {
return sb.toString()
}
fun atoptr(inputstr: String): Int {
val b = atob(inputstr)
val ptr = vm.malloc(b.size)
UnsafeHelper.memcpyRaw(b, UnsafeHelper.getArrayOffset(b), null, vm.usermem.ptr + ptr, b.size.toLong())
return ptr
}
}

View File

@@ -72,7 +72,7 @@ object VMRunnerFactory {
bind.putMember("graphics", GraphicsJSR223Delegate(vm))
bind.putMember("serial", VMSerialDebugger(vm))
bind.putMember("gzip", CompressorDelegate(vm))
bind.putMember("base64", Base64Delegate)
bind.putMember("base64", Base64Delegate(vm))
bind.putMember("com", SerialHelperDelegate(vm))
bind.putMember("dma", DMADelegate(vm))
bind.putMember("audio", AudioJSR223Delegate(vm))