@acpfx/mic-file 0.2.2 → 0.2.4
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.
- package/LICENSE +15 -0
- package/README.md +34 -0
- package/dist/index.js +248 -0
- package/dist/manifest.json +1 -0
- package/package.json +6 -2
- package/CHANGELOG.md +0 -38
- package/src/index.ts +0 -189
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 acpfx contributors
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @acpfx/mic-file
|
|
2
|
+
|
|
3
|
+
Plays back a WAV file as if it were microphone input. Useful for testing and development without a live microphone.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
This package is a pipeline node for [@acpfx/cli](../orchestrator/README.md). See the CLI package for installation and usage.
|
|
8
|
+
|
|
9
|
+
## Manifest
|
|
10
|
+
|
|
11
|
+
- **Consumes:** `control.interrupt`
|
|
12
|
+
- **Emits:** `audio.chunk`, `audio.level`, `lifecycle.ready`, `lifecycle.done`
|
|
13
|
+
|
|
14
|
+
## Settings
|
|
15
|
+
|
|
16
|
+
| Name | Type | Default | Description |
|
|
17
|
+
|------|------|---------|-------------|
|
|
18
|
+
| `path` | string | **(required)** | Path to WAV file to play back |
|
|
19
|
+
| `realtime` | boolean | | Play back at real-time speed |
|
|
20
|
+
| `chunkMs` | number | | Chunk duration in milliseconds |
|
|
21
|
+
|
|
22
|
+
## Pipeline Example
|
|
23
|
+
|
|
24
|
+
```yaml
|
|
25
|
+
nodes:
|
|
26
|
+
mic:
|
|
27
|
+
use: "@acpfx/mic-file"
|
|
28
|
+
settings: { path: ./test-audio.wav, realtime: true }
|
|
29
|
+
outputs: [stt]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
ISC
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
|
|
7
|
+
// ../node-sdk/src/index.ts
|
|
8
|
+
import { createInterface } from "node:readline";
|
|
9
|
+
|
|
10
|
+
// ../core/src/config.ts
|
|
11
|
+
import { parse as parseYaml } from "yaml";
|
|
12
|
+
|
|
13
|
+
// ../core/src/manifest.ts
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { join, dirname } from "node:path";
|
|
16
|
+
import { z as z2 } from "zod";
|
|
17
|
+
|
|
18
|
+
// ../core/src/acpfx-flags.ts
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
var SetupCheckResponseSchema = z.object({
|
|
21
|
+
needed: z.boolean(),
|
|
22
|
+
description: z.string().optional()
|
|
23
|
+
});
|
|
24
|
+
var SetupProgressSchema = z.discriminatedUnion("type", [
|
|
25
|
+
z.object({
|
|
26
|
+
type: z.literal("progress"),
|
|
27
|
+
message: z.string(),
|
|
28
|
+
pct: z.number().optional()
|
|
29
|
+
}),
|
|
30
|
+
z.object({ type: z.literal("complete"), message: z.string() }),
|
|
31
|
+
z.object({ type: z.literal("error"), message: z.string() })
|
|
32
|
+
]);
|
|
33
|
+
var UnsupportedFlagResponseSchema = z.object({
|
|
34
|
+
unsupported: z.boolean(),
|
|
35
|
+
flag: z.string()
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// ../core/src/manifest.ts
|
|
39
|
+
var ArgumentTypeSchema = z2.enum(["string", "number", "boolean"]);
|
|
40
|
+
var ManifestArgumentSchema = z2.object({
|
|
41
|
+
type: ArgumentTypeSchema,
|
|
42
|
+
default: z2.unknown().optional(),
|
|
43
|
+
description: z2.string().optional(),
|
|
44
|
+
required: z2.boolean().optional(),
|
|
45
|
+
enum: z2.array(z2.unknown()).optional()
|
|
46
|
+
});
|
|
47
|
+
var ManifestEnvFieldSchema = z2.object({
|
|
48
|
+
required: z2.boolean().optional(),
|
|
49
|
+
description: z2.string().optional()
|
|
50
|
+
});
|
|
51
|
+
var NodeManifestSchema = z2.object({
|
|
52
|
+
name: z2.string(),
|
|
53
|
+
description: z2.string().optional(),
|
|
54
|
+
consumes: z2.array(z2.string()),
|
|
55
|
+
emits: z2.array(z2.string()),
|
|
56
|
+
arguments: z2.record(z2.string(), ManifestArgumentSchema).optional(),
|
|
57
|
+
additional_arguments: z2.boolean().optional(),
|
|
58
|
+
env: z2.record(z2.string(), ManifestEnvFieldSchema).optional()
|
|
59
|
+
});
|
|
60
|
+
function handleAcpfxFlags(manifestPath) {
|
|
61
|
+
const acpfxFlag = process.argv.find((a) => a.startsWith("--acpfx-"));
|
|
62
|
+
const legacyManifest = process.argv.includes("--manifest");
|
|
63
|
+
if (!acpfxFlag && !legacyManifest) return;
|
|
64
|
+
const flag = acpfxFlag ?? "--acpfx-manifest";
|
|
65
|
+
switch (flag) {
|
|
66
|
+
case "--acpfx-manifest":
|
|
67
|
+
printManifest(manifestPath);
|
|
68
|
+
break;
|
|
69
|
+
case "--acpfx-setup-check":
|
|
70
|
+
process.stdout.write(JSON.stringify({ needed: false }) + "\n");
|
|
71
|
+
process.exit(0);
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
process.stdout.write(
|
|
75
|
+
JSON.stringify({ unsupported: true, flag }) + "\n"
|
|
76
|
+
);
|
|
77
|
+
process.exit(0);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function handleManifestFlag(manifestPath) {
|
|
81
|
+
handleAcpfxFlags(manifestPath);
|
|
82
|
+
}
|
|
83
|
+
function printManifest(manifestPath) {
|
|
84
|
+
if (!manifestPath) {
|
|
85
|
+
const script = process.argv[1];
|
|
86
|
+
const scriptDir = dirname(script);
|
|
87
|
+
const scriptBase = script.replace(/\.[^.]+$/, "");
|
|
88
|
+
const colocated = `${scriptBase}.manifest.json`;
|
|
89
|
+
try {
|
|
90
|
+
readFileSync(colocated);
|
|
91
|
+
manifestPath = colocated;
|
|
92
|
+
} catch {
|
|
93
|
+
manifestPath = join(scriptDir, "manifest.json");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const content = readFileSync(manifestPath, "utf8");
|
|
98
|
+
process.stdout.write(content.trim() + "\n");
|
|
99
|
+
process.exit(0);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
process.stderr.write(`Failed to read manifest: ${err}
|
|
102
|
+
`);
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ../node-sdk/src/index.ts
|
|
108
|
+
var NODE_NAME = process.env.ACPFX_NODE_NAME ?? "unknown";
|
|
109
|
+
function emit(event) {
|
|
110
|
+
process.stdout.write(JSON.stringify(event) + "\n");
|
|
111
|
+
}
|
|
112
|
+
function log(level, message) {
|
|
113
|
+
emit({ type: "log", level, component: NODE_NAME, message });
|
|
114
|
+
}
|
|
115
|
+
log.info = (message) => log("info", message);
|
|
116
|
+
log.warn = (message) => log("warn", message);
|
|
117
|
+
log.error = (message) => log("error", message);
|
|
118
|
+
log.debug = (message) => log("debug", message);
|
|
119
|
+
function onEvent(handler) {
|
|
120
|
+
const rl2 = createInterface({ input: process.stdin });
|
|
121
|
+
rl2.on("line", (line) => {
|
|
122
|
+
if (!line.trim()) return;
|
|
123
|
+
try {
|
|
124
|
+
const event = JSON.parse(line);
|
|
125
|
+
handler(event);
|
|
126
|
+
} catch {
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return rl2;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/index.ts
|
|
133
|
+
handleManifestFlag();
|
|
134
|
+
var settings = JSON.parse(
|
|
135
|
+
process.env.ACPFX_SETTINGS || "{}"
|
|
136
|
+
);
|
|
137
|
+
if (!settings.path) {
|
|
138
|
+
log.error("settings.path is required");
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
141
|
+
var CHUNK_MS = settings.chunkMs ?? 100;
|
|
142
|
+
var REALTIME = settings.realtime ?? true;
|
|
143
|
+
var BYTES_PER_SAMPLE = 2;
|
|
144
|
+
var TRACK_ID = "mic";
|
|
145
|
+
var interrupted = false;
|
|
146
|
+
var rl = onEvent((event) => {
|
|
147
|
+
if (event.type === "control.interrupt") {
|
|
148
|
+
interrupted = true;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
rl.on("close", () => {
|
|
152
|
+
process.exit(0);
|
|
153
|
+
});
|
|
154
|
+
process.on("SIGTERM", () => {
|
|
155
|
+
interrupted = true;
|
|
156
|
+
process.exit(0);
|
|
157
|
+
});
|
|
158
|
+
function parseWavHeader(data) {
|
|
159
|
+
let offset = 12;
|
|
160
|
+
let sampleRate = 16e3;
|
|
161
|
+
let channels = 1;
|
|
162
|
+
let bitsPerSample = 16;
|
|
163
|
+
let dataOffset = 44;
|
|
164
|
+
while (offset + 8 <= data.length) {
|
|
165
|
+
const chunkId = data.toString("ascii", offset, offset + 4);
|
|
166
|
+
const chunkSize = data.readUInt32LE(offset + 4);
|
|
167
|
+
if (chunkId === "fmt ") {
|
|
168
|
+
channels = data.readUInt16LE(offset + 10);
|
|
169
|
+
sampleRate = data.readUInt32LE(offset + 12);
|
|
170
|
+
bitsPerSample = data.readUInt16LE(offset + 22);
|
|
171
|
+
} else if (chunkId === "data") {
|
|
172
|
+
dataOffset = offset + 8;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
offset += 8 + chunkSize;
|
|
176
|
+
if (chunkSize % 2 !== 0) offset += 1;
|
|
177
|
+
}
|
|
178
|
+
return { sampleRate, channels, bitsPerSample, dataOffset };
|
|
179
|
+
}
|
|
180
|
+
function computeLevel(pcm) {
|
|
181
|
+
const samples = pcm.length / BYTES_PER_SAMPLE;
|
|
182
|
+
if (samples === 0) return { rms: 0, peak: 0, dbfs: -Infinity };
|
|
183
|
+
let sumSq = 0;
|
|
184
|
+
let peak = 0;
|
|
185
|
+
for (let i = 0; i < pcm.length; i += BYTES_PER_SAMPLE) {
|
|
186
|
+
const sample = pcm.readInt16LE(i);
|
|
187
|
+
sumSq += sample * sample;
|
|
188
|
+
const abs = Math.abs(sample);
|
|
189
|
+
if (abs > peak) peak = abs;
|
|
190
|
+
}
|
|
191
|
+
const rms = Math.sqrt(sumSq / samples);
|
|
192
|
+
const dbfs = rms > 0 ? 20 * Math.log10(rms / 32768) : -Infinity;
|
|
193
|
+
return { rms: Math.round(rms), peak, dbfs: Math.round(dbfs * 10) / 10 };
|
|
194
|
+
}
|
|
195
|
+
function sleep(ms) {
|
|
196
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
197
|
+
}
|
|
198
|
+
async function main() {
|
|
199
|
+
const filePath = resolve(settings.path);
|
|
200
|
+
const fileData = readFileSync2(filePath);
|
|
201
|
+
let sampleRate = 16e3;
|
|
202
|
+
let channels = 1;
|
|
203
|
+
let pcmData;
|
|
204
|
+
if (fileData.length > 44 && fileData.toString("ascii", 0, 4) === "RIFF" && fileData.toString("ascii", 8, 12) === "WAVE") {
|
|
205
|
+
const wavInfo = parseWavHeader(fileData);
|
|
206
|
+
sampleRate = wavInfo.sampleRate;
|
|
207
|
+
channels = wavInfo.channels;
|
|
208
|
+
pcmData = fileData.subarray(wavInfo.dataOffset);
|
|
209
|
+
} else {
|
|
210
|
+
pcmData = fileData;
|
|
211
|
+
}
|
|
212
|
+
const bytesPerFrame = channels * BYTES_PER_SAMPLE;
|
|
213
|
+
const chunkSize = Math.floor(sampleRate * channels * BYTES_PER_SAMPLE * CHUNK_MS / 1e3);
|
|
214
|
+
emit({ type: "lifecycle.ready", component: "mic-file" });
|
|
215
|
+
let offset = 0;
|
|
216
|
+
while (offset < pcmData.length && !interrupted) {
|
|
217
|
+
const end = Math.min(offset + chunkSize, pcmData.length);
|
|
218
|
+
const chunk = pcmData.subarray(offset, end);
|
|
219
|
+
const durationMs = Math.round(chunk.length / (sampleRate * bytesPerFrame) * 1e3);
|
|
220
|
+
emit({
|
|
221
|
+
type: "audio.chunk",
|
|
222
|
+
trackId: TRACK_ID,
|
|
223
|
+
format: "pcm_s16le",
|
|
224
|
+
sampleRate,
|
|
225
|
+
channels,
|
|
226
|
+
data: Buffer.from(chunk).toString("base64"),
|
|
227
|
+
durationMs
|
|
228
|
+
});
|
|
229
|
+
const level = computeLevel(chunk);
|
|
230
|
+
emit({
|
|
231
|
+
type: "audio.level",
|
|
232
|
+
trackId: TRACK_ID,
|
|
233
|
+
rms: level.rms,
|
|
234
|
+
peak: level.peak,
|
|
235
|
+
dbfs: level.dbfs
|
|
236
|
+
});
|
|
237
|
+
offset = end;
|
|
238
|
+
if (REALTIME && offset < pcmData.length) {
|
|
239
|
+
await sleep(durationMs);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
emit({ type: "lifecycle.done", component: "mic-file" });
|
|
243
|
+
process.exit(0);
|
|
244
|
+
}
|
|
245
|
+
main().catch((err) => {
|
|
246
|
+
log.error(`Fatal: ${err.message}`);
|
|
247
|
+
process.exit(1);
|
|
248
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"mic-file","description":"Plays back a WAV file as if it were mic input","consumes":["control.interrupt"],"emits":["audio.chunk","audio.level","lifecycle.ready","lifecycle.done"],"arguments":{"path":{"type":"string","required":true,"description":"Path to WAV file to play back"},"realtime":{"type":"boolean","description":"Whether to play back at real-time speed"},"chunkMs":{"type":"number","description":"Chunk duration in milliseconds"}}}
|
package/package.json
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acpfx/mic-file",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"acpfx-mic-file": "./dist/index.js"
|
|
7
7
|
},
|
|
8
8
|
"main": "./dist/index.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"manifest.yaml"
|
|
12
|
+
],
|
|
9
13
|
"dependencies": {
|
|
10
14
|
"@acpfx/core": "0.4.0",
|
|
11
15
|
"@acpfx/node-sdk": "0.3.0"
|
|
12
16
|
},
|
|
13
17
|
"scripts": {
|
|
14
|
-
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js --packages=external"
|
|
18
|
+
"build": "esbuild src/index.ts --bundle --banner:js=\"#!/usr/bin/env node\" --platform=node --format=esm --outfile=dist/index.js --packages=external && node ../../scripts/copy-manifest.js"
|
|
15
19
|
}
|
|
16
20
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# @acpfx/mic-file
|
|
2
|
-
|
|
3
|
-
## 0.2.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- Updated dependencies [0e6838e]
|
|
8
|
-
- @acpfx/core@0.4.0
|
|
9
|
-
- @acpfx/node-sdk@0.3.0
|
|
10
|
-
|
|
11
|
-
## 0.2.1
|
|
12
|
-
|
|
13
|
-
### Patch Changes
|
|
14
|
-
|
|
15
|
-
- Updated dependencies [79c6694]
|
|
16
|
-
- Updated dependencies [a0320a1]
|
|
17
|
-
- @acpfx/core@0.3.0
|
|
18
|
-
- @acpfx/node-sdk@0.2.1
|
|
19
|
-
|
|
20
|
-
## 0.2.0
|
|
21
|
-
|
|
22
|
-
### Minor Changes
|
|
23
|
-
|
|
24
|
-
- d757640: Initial release: type-safe contracts, Rust orchestrator, manifest-driven event filtering
|
|
25
|
-
|
|
26
|
-
- Rust schema crate as canonical event type source of truth with codegen to TypeScript + Zod
|
|
27
|
-
- Node manifests (manifest.yaml) declaring consumes/emits contracts
|
|
28
|
-
- Orchestrator event filtering: nodes only receive declared events
|
|
29
|
-
- Rust orchestrator with ratatui TUI (--ui flag)
|
|
30
|
-
- node-sdk with structured logging helpers
|
|
31
|
-
- CI/CD with GitHub Actions and changesets
|
|
32
|
-
- Platform-specific npm packages for Rust binaries (esbuild-style distribution)
|
|
33
|
-
|
|
34
|
-
### Patch Changes
|
|
35
|
-
|
|
36
|
-
- Updated dependencies [d757640]
|
|
37
|
-
- @acpfx/core@0.2.0
|
|
38
|
-
- @acpfx/node-sdk@0.2.0
|
package/src/index.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mic-file node — reads a WAV file and emits audio.chunk events with real-time pacing.
|
|
3
|
-
* Also emits audio.level events with computed RMS energy.
|
|
4
|
-
*
|
|
5
|
-
* Settings (via ACPFX_SETTINGS):
|
|
6
|
-
* path: string — path to WAV file
|
|
7
|
-
* realtime?: boolean — pace at real-time rate (default: true)
|
|
8
|
-
* chunkMs?: number — chunk duration in ms (default: 100)
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { readFileSync } from "node:fs";
|
|
12
|
-
import { resolve } from "node:path";
|
|
13
|
-
import { emit, log, onEvent, handleManifestFlag } from "@acpfx/node-sdk";
|
|
14
|
-
|
|
15
|
-
handleManifestFlag();
|
|
16
|
-
|
|
17
|
-
type Settings = {
|
|
18
|
-
path: string;
|
|
19
|
-
realtime?: boolean;
|
|
20
|
-
chunkMs?: number;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const settings: Settings = JSON.parse(
|
|
24
|
-
process.env.ACPFX_SETTINGS || "{}",
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
if (!settings.path) {
|
|
28
|
-
log.error("settings.path is required");
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const CHUNK_MS = settings.chunkMs ?? 100;
|
|
33
|
-
const REALTIME = settings.realtime ?? true;
|
|
34
|
-
const BYTES_PER_SAMPLE = 2; // 16-bit PCM
|
|
35
|
-
const TRACK_ID = "mic";
|
|
36
|
-
|
|
37
|
-
let interrupted = false;
|
|
38
|
-
|
|
39
|
-
// Handle control.interrupt from stdin
|
|
40
|
-
const rl = onEvent((event) => {
|
|
41
|
-
if (event.type === "control.interrupt") {
|
|
42
|
-
interrupted = true;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
rl.on("close", () => {
|
|
47
|
-
process.exit(0);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
process.on("SIGTERM", () => {
|
|
51
|
-
interrupted = true;
|
|
52
|
-
process.exit(0);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
// --- WAV parsing ---
|
|
56
|
-
|
|
57
|
-
type WavInfo = {
|
|
58
|
-
sampleRate: number;
|
|
59
|
-
channels: number;
|
|
60
|
-
bitsPerSample: number;
|
|
61
|
-
dataOffset: number;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
function parseWavHeader(data: Buffer): WavInfo {
|
|
65
|
-
let offset = 12;
|
|
66
|
-
let sampleRate = 16000;
|
|
67
|
-
let channels = 1;
|
|
68
|
-
let bitsPerSample = 16;
|
|
69
|
-
let dataOffset = 44;
|
|
70
|
-
|
|
71
|
-
while (offset + 8 <= data.length) {
|
|
72
|
-
const chunkId = data.toString("ascii", offset, offset + 4);
|
|
73
|
-
const chunkSize = data.readUInt32LE(offset + 4);
|
|
74
|
-
|
|
75
|
-
if (chunkId === "fmt ") {
|
|
76
|
-
channels = data.readUInt16LE(offset + 10);
|
|
77
|
-
sampleRate = data.readUInt32LE(offset + 12);
|
|
78
|
-
bitsPerSample = data.readUInt16LE(offset + 22);
|
|
79
|
-
} else if (chunkId === "data") {
|
|
80
|
-
dataOffset = offset + 8;
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
offset += 8 + chunkSize;
|
|
85
|
-
if (chunkSize % 2 !== 0) offset += 1;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return { sampleRate, channels, bitsPerSample, dataOffset };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// --- Audio level computation ---
|
|
92
|
-
|
|
93
|
-
function computeLevel(pcm: Buffer): { rms: number; peak: number; dbfs: number } {
|
|
94
|
-
const samples = pcm.length / BYTES_PER_SAMPLE;
|
|
95
|
-
if (samples === 0) return { rms: 0, peak: 0, dbfs: -Infinity };
|
|
96
|
-
|
|
97
|
-
let sumSq = 0;
|
|
98
|
-
let peak = 0;
|
|
99
|
-
for (let i = 0; i < pcm.length; i += BYTES_PER_SAMPLE) {
|
|
100
|
-
const sample = pcm.readInt16LE(i);
|
|
101
|
-
sumSq += sample * sample;
|
|
102
|
-
const abs = Math.abs(sample);
|
|
103
|
-
if (abs > peak) peak = abs;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const rms = Math.sqrt(sumSq / samples);
|
|
107
|
-
const dbfs = rms > 0 ? 20 * Math.log10(rms / 32768) : -Infinity;
|
|
108
|
-
|
|
109
|
-
return { rms: Math.round(rms), peak, dbfs: Math.round(dbfs * 10) / 10 };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function sleep(ms: number): Promise<void> {
|
|
113
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// --- Main ---
|
|
117
|
-
|
|
118
|
-
async function main() {
|
|
119
|
-
const filePath = resolve(settings.path);
|
|
120
|
-
const fileData = readFileSync(filePath);
|
|
121
|
-
|
|
122
|
-
let sampleRate = 16000;
|
|
123
|
-
let channels = 1;
|
|
124
|
-
let pcmData: Buffer;
|
|
125
|
-
|
|
126
|
-
// Parse WAV header if present
|
|
127
|
-
if (
|
|
128
|
-
fileData.length > 44 &&
|
|
129
|
-
fileData.toString("ascii", 0, 4) === "RIFF" &&
|
|
130
|
-
fileData.toString("ascii", 8, 12) === "WAVE"
|
|
131
|
-
) {
|
|
132
|
-
const wavInfo = parseWavHeader(fileData);
|
|
133
|
-
sampleRate = wavInfo.sampleRate;
|
|
134
|
-
channels = wavInfo.channels;
|
|
135
|
-
pcmData = fileData.subarray(wavInfo.dataOffset);
|
|
136
|
-
} else {
|
|
137
|
-
pcmData = fileData;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const bytesPerFrame = channels * BYTES_PER_SAMPLE;
|
|
141
|
-
const chunkSize = Math.floor((sampleRate * channels * BYTES_PER_SAMPLE * CHUNK_MS) / 1000);
|
|
142
|
-
|
|
143
|
-
// Emit lifecycle.ready
|
|
144
|
-
emit({ type: "lifecycle.ready", component: "mic-file" });
|
|
145
|
-
|
|
146
|
-
let offset = 0;
|
|
147
|
-
while (offset < pcmData.length && !interrupted) {
|
|
148
|
-
const end = Math.min(offset + chunkSize, pcmData.length);
|
|
149
|
-
const chunk = pcmData.subarray(offset, end);
|
|
150
|
-
const durationMs = Math.round((chunk.length / (sampleRate * bytesPerFrame)) * 1000);
|
|
151
|
-
|
|
152
|
-
// Emit audio.chunk
|
|
153
|
-
emit({
|
|
154
|
-
type: "audio.chunk",
|
|
155
|
-
trackId: TRACK_ID,
|
|
156
|
-
format: "pcm_s16le",
|
|
157
|
-
sampleRate,
|
|
158
|
-
channels,
|
|
159
|
-
data: Buffer.from(chunk).toString("base64"),
|
|
160
|
-
durationMs,
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// Emit audio.level
|
|
164
|
-
const level = computeLevel(chunk);
|
|
165
|
-
emit({
|
|
166
|
-
type: "audio.level",
|
|
167
|
-
trackId: TRACK_ID,
|
|
168
|
-
rms: level.rms,
|
|
169
|
-
peak: level.peak,
|
|
170
|
-
dbfs: level.dbfs,
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
offset = end;
|
|
174
|
-
|
|
175
|
-
// Pace at real-time rate
|
|
176
|
-
if (REALTIME && offset < pcmData.length) {
|
|
177
|
-
await sleep(durationMs);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Emit lifecycle.done
|
|
182
|
-
emit({ type: "lifecycle.done", component: "mic-file" });
|
|
183
|
-
process.exit(0);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
main().catch((err) => {
|
|
187
|
-
log.error(`Fatal: ${err.message}`);
|
|
188
|
-
process.exit(1);
|
|
189
|
-
});
|