import { spawn } from "node:child_process";
const API_KEY = "<YOUR_API_KEY>";
const URL = "https://dictation.assemblyai.com/v1/transcribe/live";
const BOUNDARY = "----dictation-boundary";
const encoder = new TextEncoder();
// One upload to the live endpoint, fed frame by frame. Needs Node.js 18+.
function liveRequest(config) {
let body;
const stream = new ReadableStream({ start: (controller) => (body = controller) });
body.enqueue(encoder.encode(
`--${BOUNDARY}\r\n` +
'Content-Disposition: form-data; name="config"\r\n' +
"Content-Type: application/json\r\n\r\n" +
`${JSON.stringify(config)}\r\n` +
`--${BOUNDARY}\r\n` +
'Content-Disposition: form-data; name="audio"; filename="audio"\r\n' +
"Content-Type: audio/pcm\r\n\r\n",
));
const aborter = new AbortController();
const response = fetch(URL, {
method: "POST",
headers: {
Authorization: API_KEY,
"Content-Type": `multipart/form-data; boundary=${BOUNDARY}`,
},
body: stream,
duplex: "half",
signal: aborter.signal,
});
response.catch(() => {}); // an aborted request rejects; nothing to keep
return {
write(frame) {
body.enqueue(frame);
},
// Drop the request without a transcript. An aborted request is not billed.
abort() {
aborter.abort();
},
// End the audio and wait for the transcript.
async result() {
body.enqueue(encoder.encode(`\r\n--${BOUNDARY}--\r\n`));
body.close();
const res = await response;
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
return res.json();
},
};
}
// A live dictation whose config can change while the user is talking.
function restartableDictation(config) {
const frames = [];
let request = liveRequest(config);
return {
write(frame) {
frames.push(frame);
request.write(frame);
},
updateConfig(newConfig) {
const oldRequest = request;
request = liveRequest(newConfig);
for (const frame of frames) request.write(frame);
oldRequest.abort();
},
result() {
return request.result();
},
};
}
const config = { sample_rate: 16000, channels: 1, rewrite_mode: "smart" };
const dictation = restartableDictation(config);
// SoX writes 16 kHz, 16-bit mono PCM from the default microphone to stdout.
const microphone = spawn("sox", [
"--default-device", "--no-show-progress",
"--rate", "16000", "--channels", "1",
"--encoding", "signed-integer", "--bits", "16", "--type", "raw", "-",
]);
microphone.on("error", () => console.error("Install SoX to record audio."));
microphone.stdout.on("data", (chunk) => dictation.write(new Uint8Array(chunk)));
// Your screen-context lookup calls updateConfig when it's ready.
setTimeout(() => {
dictation.updateConfig({ ...config, keyterms_prompt: ["Priya Shah", "Q3 roadmap"] });
}, 3000);
console.log("Dictating, press Enter to stop...");
process.stdin.once("data", () => {
microphone.kill();
process.stdin.pause();
});
await new Promise((resolve) => microphone.on("close", resolve));
const result = await dictation.result();
console.log(result.llm_response);