@audio/decode-aiff 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/decode-aiff.js +9 -5
  2. package/package.json +1 -1
package/decode-aiff.js CHANGED
@@ -19,12 +19,12 @@ const ULAW_TBL = new Int16Array(256)
19
19
  if (seg) val = (val + 256) << (seg - 1)
20
20
  ALAW_TBL[i] = (ax & 0x80) ? val : -val
21
21
 
22
- // mu-law
22
+ // mu-law (ITU-T G.711) — sign bit set ⇒ negative
23
23
  let ux = ~i & 0xFF
24
24
  seg = (ux >> 4) & 7
25
25
  val = ((ux & 0x0F) << 3) + 132
26
26
  val <<= seg
27
- ULAW_TBL[i] = (ux & 0x80) ? (val - 132) : -(val - 132)
27
+ ULAW_TBL[i] = (ux & 0x80) ? (132 - val) : (val - 132)
28
28
  }
29
29
  })()
30
30
 
@@ -526,13 +526,14 @@ function scanAiffHdr(b) {
526
526
  let form = str4(b, 8)
527
527
  if (form !== 'AIFF' && form !== 'AIFC') throw Error('Not an AIFF file')
528
528
  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
530
530
  let pos = 12, end = Math.min(8 + r32(b, 4), b.length)
531
531
  while (pos + 8 <= end) {
532
532
  let ckId = str4(b, pos), ckSize = r32(b, pos + 4), ckData = pos + 8
533
533
  if (ckId === 'COMM') {
534
534
  if (ckData + 18 > b.length) return null
535
535
  nCh = r16(b, ckData)
536
+ numSampleFrames = r32(b, ckData + 2)
536
537
  bps = r16(b, ckData + 6)
537
538
  sr = readF80(b, ckData + 8)
538
539
  if (isAIFC && ckSize >= 22 && ckData + 22 <= b.length) comp = str4(b, ckData + 18)
@@ -542,6 +543,7 @@ function scanAiffHdr(b) {
542
543
  if (ckData + 8 > b.length) return null
543
544
  let dataOff = r32(b, ckData)
544
545
  let dataStart = ckData + 8 + dataOff
546
+ let ssndDataSize = ckSize - 8 - dataOff
545
547
  let cUpper = comp.toUpperCase()
546
548
  let byteDepth = Math.ceil(bps / 8)
547
549
  let frameBytes
@@ -550,7 +552,8 @@ function scanAiffHdr(b) {
550
552
  else if (comp === 'alaw' || comp === 'ulaw' || comp === 'ULAW') frameBytes = nCh
551
553
  else if (comp === 'ima4' || comp === 'GSM ' || comp === 'gsm ') frameBytes = 1 // block-based, handled via flush
552
554
  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 }
554
557
  }
555
558
  pos = ckData + ckSize + (ckSize & 1)
556
559
  }
@@ -558,8 +561,9 @@ function scanAiffHdr(b) {
558
561
  }
559
562
 
560
563
  function decodeAiffRaw(raw, hdr) {
561
- let { nCh, bps, sr, comp, byteDepth, frameBytes } = hdr
564
+ let { nCh, bps, sr, comp, byteDepth, frameBytes, maxFrames } = hdr
562
565
  let frames = Math.floor(raw.length / frameBytes)
566
+ if (maxFrames) frames = Math.min(frames, maxFrames)
563
567
  if (!frames) return EMPTY
564
568
  let channelData = Array.from({ length: nCh }, () => new Float32Array(frames))
565
569
  let cUpper = comp.toUpperCase()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/decode-aiff",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Decode AIFF/AIFF-C audio to PCM samples",
5
5
  "type": "module",
6
6
  "main": "decode-aiff.js",