@kuralle-syrinx/recorder 4.4.1 → 4.5.0

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.
@@ -0,0 +1,42 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ import { describe, expect, it } from "vitest";
4
+ import { interleaveStereoPcm16, pcm16ToWav } from "./wav.js";
5
+
6
+ describe("pcm16ToWav", () => {
7
+ it("writes a canonical 44-byte RIFF/PCM header for the given rate and channels", () => {
8
+ const pcm = new Uint8Array([1, 2, 3, 4]); // 2 mono samples
9
+ const wav = pcm16ToWav(pcm, 16000, 1);
10
+ expect(wav.byteLength).toBe(44 + 4);
11
+ const view = new DataView(wav.buffer);
12
+ expect(String.fromCharCode(wav[0]!, wav[1]!, wav[2]!, wav[3]!)).toBe("RIFF");
13
+ expect(view.getUint32(4, true)).toBe(36 + 4); // chunk size
14
+ expect(String.fromCharCode(wav[8]!, wav[9]!, wav[10]!, wav[11]!)).toBe("WAVE");
15
+ expect(view.getUint16(20, true)).toBe(1); // PCM
16
+ expect(view.getUint16(22, true)).toBe(1); // channels
17
+ expect(view.getUint32(24, true)).toBe(16000); // sample rate
18
+ expect(view.getUint16(32, true)).toBe(2); // blockAlign = channels * 2
19
+ expect(view.getUint32(40, true)).toBe(4); // data size
20
+ expect([...wav.subarray(44)]).toEqual([1, 2, 3, 4]);
21
+ });
22
+
23
+ it("sets stereo blockAlign and byteRate", () => {
24
+ const view = new DataView(pcm16ToWav(new Uint8Array(8), 24000, 2).buffer);
25
+ expect(view.getUint16(32, true)).toBe(4); // 2ch * 2 bytes
26
+ expect(view.getUint32(28, true)).toBe(24000 * 4); // byteRate
27
+ });
28
+ });
29
+
30
+ describe("interleaveStereoPcm16", () => {
31
+ it("interleaves L/R frames and pads the shorter stream with silence", () => {
32
+ const left = new Uint8Array([0x10, 0x00, 0x20, 0x00]); // samples 16, 32
33
+ const right = new Uint8Array([0x01, 0x00]); // sample 1, then padded
34
+ const out = interleaveStereoPcm16(left, right);
35
+ const view = new DataView(out.buffer);
36
+ expect(out.byteLength).toBe(2 * 4); // 2 frames × (2ch × 2 bytes)
37
+ expect(view.getInt16(0, true)).toBe(16); // L0
38
+ expect(view.getInt16(2, true)).toBe(1); // R0
39
+ expect(view.getInt16(4, true)).toBe(32); // L1
40
+ expect(view.getInt16(6, true)).toBe(0); // R1 padded
41
+ });
42
+ });