wav direct upload with bugfixes

This commit is contained in:
minjaesong
2026-06-20 17:27:27 +09:00
parent 04aa651ff1
commit 646af9452c
11 changed files with 228 additions and 68 deletions

View File

@@ -1238,6 +1238,15 @@ function audioSetProgress(progress, elapsedSec, totalSec) {
ag_drawProgress(progress, elapsedSec | 0, totalSec | 0)
}
// Pre-build the mini-AAlib glyph tables (which loads the 7x14 font ROM from disk). MUST be
// called BEFORE the media file is opened: the wavescope's first render would otherwise
// files.open() the font ROM mid-playback, and a disk drive is single-file-open per drive —
// opening the font while the audio file is being streamed off the same drive corrupts the
// stream (short noise burst, then silence). After this, audioRender does no disk I/O.
function preloadAssets() {
aa_mktable()
}
function audioRender() {
const now = sys.nanoTime()
if (now - ag_lastRenderNs < AG_RENDER_INTERVAL_NS) return
@@ -1274,5 +1283,6 @@ exports = {
audioSetProgress,
audioRender,
audioClose,
audioIsExitRequested
audioIsExitRequested,
preloadAssets
}

View File

@@ -5,12 +5,17 @@
let readCount = 0
let port = undefined
let fileHeader = new Uint8Array(4096)
// Valid byte count of the block currently sitting in the read buffer. The disk's last block is
// usually shorter than 4096; without tracking this the reuse path read 4096-padding bytes of stale
// buffer past EOF (white-noise burst / the old OOB crash).
let curBlockLen = 0
function prepare(fullPath) {
if (fullPath[2] != '/' && fullPath[2] != '\\') throw Error("Expected full path with drive letter, got " + fullPath)
readCount = 0
curBlockLen = 0
let driveLetter = fullPath[0].toUpperCase()
let diskPath = fullPath.substring(2).replaceAll("\\",'/')
@@ -68,12 +73,18 @@ function readBytes(length, ptrToDecode) {
let blockTransferStatus = ((sys.peek(-4085 - port*2) & 255) | ((sys.peek(-4086 - port*2) & 255) << 8))
let thisBlockLen = blockTransferStatus & 4095
if (thisBlockLen == 0) thisBlockLen = 4096 // [1, 4096]
let hasMore = (blockTransferStatus & 0x8000 != 0)
// bit 12 (0x1000) of the status = "the disk's block size is exactly 0" — the EOF marker.
// Without it a 0-length terminating block is indistinguishable from a full 4096-byte
// block (4096 & 4095 == 0 too), so the old code read 4096 bytes of stale buffer past EOF.
let blockIsEmpty = (blockTransferStatus & 0x1000) != 0
if (thisBlockLen == 0 && !blockIsEmpty) thisBlockLen = 4096 // [1, 4096]
curBlockLen = thisBlockLen
// serial.println(`block: (${thisBlockLen})[${[...Array(thisBlockLen).keys()].map(k => (sys.peek(-4097 - k) & 255).toString(16).padStart(2,'0')).join()}]`)
if (thisBlockLen == 0) break // EOF: nothing more to read (zero-filled below)
let remaining = Math.min(thisBlockLen, length - completedReads)
// serial.println(`Pulled a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}, remaining = ${remaining}`)
@@ -87,11 +98,13 @@ function readBytes(length, ptrToDecode) {
}
else {
let padding = readCount % 4096
let remaining = length - completedReads
let thisBlockLen = Math.min(4096 - padding, length - completedReads)
// Only `curBlockLen - padding` bytes of the buffered block are real; the rest is stale.
// A short final block leaves padding >= curBlockLen, i.e. we are past EOF.
let avail = curBlockLen - padding
if (avail <= 0) break // past the short last block: EOF (zero-filled below)
let thisBlockLen = Math.min(avail, length - completedReads)
// serial.println(`padding = ${padding}; remaining = ${remaining}`)
// serial.println(`block: (${thisBlockLen})[${[...Array(thisBlockLen).keys()].map(k => (sys.peek(-4097 - padding - k) & 255).toString(16).padStart(2,'0')).join()}]`)
// serial.println(`padding = ${padding}; avail = ${avail}`)
// serial.println(`Reusing a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}`)
// copy from read buffer to designated position
@@ -103,6 +116,15 @@ function readBytes(length, ptrToDecode) {
}
}
// Reached EOF before satisfying the request: zero-fill the remainder so callers get defined
// bytes (silence, for audio) instead of stale garbage, and advance readCount so the caller's
// read loop still terminates (it was relying on readCount reaching the requested position).
while (completedReads < length) {
sys.poke(ptr + completedReads * destVector, 0)
completedReads += 1
readCount += 1
}
//serial.println(`END readBytes(${length}); readCount = ${readCount}\n`)
return ptr