Skip to content

Commit 32508ea

Browse files
committed
Fix #46
1 parent 9a4d1c7 commit 32508ea

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎packages/decode-aiff/decode-aiff.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,14 @@ function scanAiffHdr(b) {
526526
let form = str4(b, 8)
527527
if (form !== 'AIFF' && form !== 'AIFC') throw Error('Not an AIFF file')
528528
let isAIFC = form === 'AIFC'
529-
let nCh = 0, bps = 0, sr = 0, comp = 'NONE', commFound = false
529+
let nCh = 0, bps = 0, sr = 0, comp = 'NONE', commFound = false, numSampleFrames = 0
530530
let pos = 12, end = Math.min(8 + r32(b, 4), b.length)
531531
while (pos + 8 <= end) {
532532
let ckId = str4(b, pos), ckSize = r32(b, pos + 4), ckData = pos + 8
533533
if (ckId === 'COMM') {
534534
if (ckData + 18 > b.length) return null
535535
nCh = r16(b, ckData)
536+
numSampleFrames = r32(b, ckData + 2)
536537
bps = r16(b, ckData + 6)
537538
sr = readF80(b, ckData + 8)
538539
if (isAIFC && ckSize >= 22 && ckData + 22 <= b.length) comp = str4(b, ckData + 18)
@@ -542,6 +543,7 @@ function scanAiffHdr(b) {
542543
if (ckData + 8 > b.length) return null
543544
let dataOff = r32(b, ckData)
544545
let dataStart = ckData + 8 + dataOff
546+
let ssndDataSize = ckSize - 8 - dataOff
545547
let cUpper = comp.toUpperCase()
546548
let byteDepth = Math.ceil(bps / 8)
547549
let frameBytes
@@ -550,16 +552,18 @@ function scanAiffHdr(b) {
550552
else if (comp === 'alaw' || comp === 'ulaw' || comp === 'ULAW') frameBytes = nCh
551553
else if (comp === 'ima4' || comp === 'GSM ' || comp === 'gsm ') frameBytes = 1 // block-based, handled via flush
552554
else frameBytes = nCh * byteDepth
553-
return { nCh, bps, sr, comp, byteDepth, frameBytes, dataStart, isAIFC }
555+
let maxFrames = numSampleFrames ? Math.min(numSampleFrames, Math.floor(ssndDataSize / frameBytes)) : 0
556+
return { nCh, bps, sr, comp, byteDepth, frameBytes, dataStart, isAIFC, maxFrames, ssndDataSize }
554557
}
555558
pos = ckData + ckSize + (ckSize & 1)
556559
}
557560
return null
558561
}
559562

560563
function decodeAiffRaw(raw, hdr) {
561-
let { nCh, bps, sr, comp, byteDepth, frameBytes } = hdr
564+
let { nCh, bps, sr, comp, byteDepth, frameBytes, maxFrames } = hdr
562565
let frames = Math.floor(raw.length / frameBytes)
566+
if (maxFrames) frames = Math.min(frames, maxFrames)
563567
if (!frames) return EMPTY
564568
let channelData = Array.from({ length: nCh }, () => new Float32Array(frames))
565569
let cUpper = comp.toUpperCase()

‎packages/decode-aiff/test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,37 @@ console.log('sample rate parsing')
322322
}
323323
}
324324

325+
// ---- Trailing chunk after SSND (issue #46) ----
326+
console.log('trailing chunk after SSND')
327+
{
328+
// Build AIFF with 100 frames of silence, then an ID3 chunk with garbage after SSND
329+
let nFrames = 100
330+
let samples = new Array(nFrames).fill(0)
331+
let base = buildAIFF({ sr: 44100, nCh: 1, bps: 16, samples })
332+
333+
// Append a fake ID3 chunk (4 id + 4 size + 128 bytes of non-zero garbage)
334+
let trailingSize = 128
335+
let trailing = new Uint8Array(8 + trailingSize)
336+
writeStr(trailing, 0, 'ID3 ')
337+
w32(trailing, 4, trailingSize)
338+
trailing.fill(0xFF, 8)
339+
340+
let aiff = new Uint8Array(base.length + trailing.length)
341+
aiff.set(base)
342+
aiff.set(trailing, base.length)
343+
// Update FORM size
344+
w32(aiff, 4, r32(base, 4) + trailing.length)
345+
346+
function r32(b, o) { return ((b[o] << 24) | (b[o + 1] << 16) | (b[o + 2] << 8) | b[o + 3]) >>> 0 }
347+
348+
let r = await decode(aiff)
349+
ok(r.channelData[0].length === nFrames, 'no extra frames from trailing chunk (got ' + r.channelData[0].length + ', expected ' + nFrames + ')')
350+
// All samples should be silence (0), not noise
351+
let maxAbs = 0
352+
for (let v of r.channelData[0]) maxAbs = Math.max(maxAbs, Math.abs(v))
353+
ok(maxAbs === 0, 'trailing garbage not decoded as audio (max=' + maxAbs + ')')
354+
}
355+
325356
// ---- Edge cases ----
326357
console.log('edge cases')
327358
{

0 commit comments

Comments
 (0)