-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
136 lines (116 loc) · 4.08 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/env node
// Copyright 2024 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
"use strict";
const fs = require("fs");
const { program } = require("commander");
const { PvSpeaker } = require("@picovoice/pvspeaker-node");
program
.option(
"-s, --show_audio_devices",
"List of audio devices currently available for use."
).option(
"-d, --audio_device_index <number>",
"Index of input audio device.",
Number,
-1
).option(
"-i, --input_wav_path <string>",
"Path to PCM WAV file to be played."
).option(
"-b, --buffer_size_secs <number>",
"Size of internal PCM buffer in seconds.",
Number,
20
).option(
"-o, --output_wav_path <string>",
"Path to the output WAV file where the PCM data passed to PvSpeaker will be written."
);
if (process.argv.length < 2) {
program.help();
}
program.parse(process.argv);
function splitArrayBuffer(arrBuf, maxSize) {
const inputArray = new Uint8Array(arrBuf);
const result = [];
for (let i = 0; i < inputArray.length; i += maxSize) {
let endIndex = Math.min(i + maxSize, inputArray.length);
const chunk = new Uint8Array(inputArray.slice(i, endIndex));
result.push(chunk.buffer);
}
return result;
}
async function runDemo() {
let showAudioDevices = program["show_audio_devices"];
let deviceIndex = program["audio_device_index"];
let inputWavPath = program["input_wav_path"];
let bufferSizeSecs = program["buffer_size_secs"];
let outputPath = program["output_wav_path"];
if (showAudioDevices) {
const devices = PvSpeaker.getAvailableDevices();
for (let i = 0; i < devices.length; i++) {
console.log(`index: ${i}, device name: ${devices[i]}`)
}
} else {
if (!inputWavPath) {
program.help();
return;
}
const wavBuffer = fs.readFileSync(inputWavPath);
if (wavBuffer.toString('utf8', 0, 4) !== 'RIFF' || wavBuffer.toString('utf8', 8, 12) !== 'WAVE') {
throw new Error('Invalid WAV file');
}
const formatChunkOffset = wavBuffer.indexOf('fmt ', 12);
if (formatChunkOffset === -1) {
throw new Error('Invalid WAV file: fmt chunk not found');
}
const numChannels = wavBuffer.readUInt16LE(formatChunkOffset + 10);
const sampleRate = wavBuffer.readUInt32LE(formatChunkOffset + 12);
const bitsPerSample = wavBuffer.readUInt16LE(formatChunkOffset + 22);
if (numChannels !== 1) {
throw new Error('WAV file must have a single channel (MONO)');
}
const headerSize = 44;
const pcmBuffer = wavBuffer.buffer.slice(headerSize);
try {
const speaker = new PvSpeaker(sampleRate, bitsPerSample, { bufferSizeSecs, deviceIndex });
console.log(`Using PvSpeaker version: ${speaker.version}`);
console.log(`Using device: ${speaker.getSelectedDevice()}`);
speaker.start();
if (outputPath) {
speaker.writeToFile(outputPath);
}
console.log("Playing audio...");
const bytesPerSample = bitsPerSample / 8;
const pcmChunks = splitArrayBuffer(pcmBuffer, sampleRate * bytesPerSample);
for (const pcmChunk of pcmChunks) {
let totalWrittenByteLength = 0;
while (totalWrittenByteLength < pcmChunk.byteLength) {
const writtenLength = speaker.write(pcmChunk.slice(totalWrittenByteLength));
totalWrittenByteLength += (writtenLength * bytesPerSample);
}
}
console.log("Waiting for audio to finish...");
speaker.flush();
console.log("Finished playing audio...");
speaker.stop();
speaker.release();
} catch (e) {
console.log(e.message);
}
}
}
(async function () {
try {
await runDemo();
} catch (e) {
console.error(e.toString());
}
})();