@audio/decode-caf 1.4.1 → 1.4.2

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-caf.js +19 -28
  2. package/package.json +1 -1
package/decode-caf.js CHANGED
@@ -12,6 +12,21 @@ const FORMATS = new Set(['lpcm', 'alaw', 'ulaw', 'ima4'])
12
12
  const IMA_STEP = new Int16Array([7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767])
13
13
  const IMA_IDX = new Int8Array([-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8])
14
14
 
15
+ // G.711 expansion tables (ITU-T), as decode-aiff and decode-wav: int16 PCM per encoded byte
16
+ const ALAW_TBL = new Int16Array(256)
17
+ const ULAW_TBL = new Int16Array(256)
18
+ for (let i = 0; i < 256; i++) {
19
+ let ax = i ^ 0x55, seg = (ax >> 4) & 7, val = ((ax & 0x0F) << 4) + 8
20
+ if (seg) val = (val + 256) << (seg - 1)
21
+ ALAW_TBL[i] = (ax & 0x80) ? val : -val
22
+
23
+ let ux = ~i & 0xFF
24
+ seg = (ux >> 4) & 7
25
+ val = ((ux & 0x0F) << 3) + 132
26
+ val <<= seg
27
+ ULAW_TBL[i] = (ux & 0x80) ? (132 - val) : (val - 132)
28
+ }
29
+
15
30
  /** Decode a complete CAF file synchronously. */
16
31
  export default function decode(src) {
17
32
  let dec = decoder()
@@ -107,8 +122,8 @@ function decodeCafRaw(raw, hdr) {
107
122
  if (!frames) return EMPTY
108
123
  let samples
109
124
  if (formatID === 'lpcm') samples = decodeLPCM(raw, formatFlags, bits, ch)
110
- else if (formatID === 'alaw') samples = decodeAlaw(raw, ch)
111
- else if (formatID === 'ulaw') samples = decodeUlaw(raw, ch)
125
+ else if (formatID === 'alaw') samples = decodeLaw(raw, ch, ALAW_TBL)
126
+ else if (formatID === 'ulaw') samples = decodeLaw(raw, ch, ULAW_TBL)
112
127
  else if (formatID === 'ima4') samples = decodeIma4(raw, hdr)
113
128
  else throw Error('CAF: unsupported format ' + formatID)
114
129
  return { channelData: samples, sampleRate }
@@ -206,36 +221,12 @@ function decodeLPCM(data, flags, bits, ch) {
206
221
  return channelData
207
222
  }
208
223
 
209
- function alawDecode(val) {
210
- val ^= 0x55
211
- let sign = val & 0x80, seg = (val >> 4) & 7, quant = val & 0x0F
212
- let sample = seg ? ((quant << 1) | 0x21) << (seg - 1) : (quant << 1) | 1
213
- return (sign ? -sample : sample) / 32768
214
- }
215
-
216
- function ulawDecode(val) {
217
- val = ~val & 0xFF
218
- let sign = val & 0x80, exp = (val >> 4) & 7, mant = val & 0x0F
219
- let sample = ((mant << 1) | 0x21) << exp
220
- return (sign ? -(sample - 33) : (sample - 33)) / 32768
221
- }
222
-
223
- function decodeAlaw(data, ch) {
224
- let framesCount = (data.length / ch) | 0
225
- if (!framesCount) return []
226
- let channelData = Array.from({ length: ch }, () => new Float32Array(framesCount))
227
- for (let i = 0, off = 0; i < framesCount; i++)
228
- for (let c = 0; c < ch; c++, off++)
229
- channelData[c][i] = alawDecode(data[off])
230
- return channelData
231
- }
232
-
233
- function decodeUlaw(data, ch) {
224
+ function decodeLaw(data, ch, tbl) {
234
225
  let framesCount = (data.length / ch) | 0
235
226
  if (!framesCount) return []
236
227
  let channelData = Array.from({ length: ch }, () => new Float32Array(framesCount))
237
228
  for (let i = 0, off = 0; i < framesCount; i++)
238
229
  for (let c = 0; c < ch; c++, off++)
239
- channelData[c][i] = ulawDecode(data[off])
230
+ channelData[c][i] = tbl[data[off]] / 32768
240
231
  return channelData
241
232
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/decode-caf",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "Decode CAF (Core Audio Format) audio to PCM samples",
5
5
  "type": "module",
6
6
  "main": "decode-caf.js",