Skip to content

Commit

Permalink
update demos
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed Feb 2, 2024
1 parent d5fbbeb commit df4ad02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
8 changes: 8 additions & 0 deletions demo/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ cobra-file-demo --access_key ${ACCESS_KEY} --input_audio_file_path ${AUDIO_PATH}
Replace `${ACCESS_KEY}` with yours obtained from Picovoice Console and `${AUDIO_PATH}` with a path to an audio file you
wish to use for voice activity detection.

The threshold of the engine can be tuned using the `threshold` input argument:

```console
cobra-file-demo --access_key ${ACCESS_KEY} --input_audio_file_path ${AUDIO_PATH} --threshold ${DETECTION_THRESHOLD}
```

Threshold is a floating point number within `[0, 1]`. A higher threshold reduces the miss rate at the cost of increased false alarm rate.

### Microphone Demo

You need a working microphone connected to your machine for this demo. Run the following in the terminal:
Expand Down
22 changes: 17 additions & 5 deletions demo/nodejs/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ program
.option(
"-l, --library_file_path <string>",
"absolute path to cobra dynamic library"
)
.option(
"-t --threshold <string>",
"Threshold for the probability of voice activity"
);

if (process.argv.length < 2) {
Expand All @@ -42,6 +46,7 @@ function fileDemo() {
let audioPath = program["input_audio_file_path"];
let accessKey = program["access_key"];
let libraryFilePath = program["library_file_path"];
let threshold = program["threshold"] ?? 0.8;

let engineInstance = new Cobra(accessKey, {
libraryPath: libraryFilePath,
Expand Down Expand Up @@ -70,13 +75,20 @@ function fileDemo() {

let frames = getInt16Frames(inputWaveFile, engineInstance.frameLength);

let voiceProbabilities = [];
for (let frame of frames) {
const voiceProbability = engineInstance.process(frame);
voiceProbabilities.push(voiceProbability);
const printedNums = new Set();
for (let i = 0; i < frames.length; i++) {
const result = engineInstance.process(frames[i]);
const timestamp = (
(i * engineInstance.frameLength) /
engineInstance.sampleRate
).toFixed(1);

if (result >= threshold && !printedNums.has(timestamp)) {
console.log(`Detected voice activity at ${timestamp} sec`);
printedNums.add(timestamp);
}
}

console.log(voiceProbabilities);
engineInstance.release();
}

Expand Down
12 changes: 11 additions & 1 deletion demo/nodejs/mic.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ async function micDemo() {
const pcm = await recorder.read();
try {
const voiceProbability = engineInstance.process(pcm);
console.log(voiceProbability);
const percentage = voiceProbability * 100;
const barLength = Math.floor((percentage / 10) * 3);
const emptyLength = 30 - barLength;
const spacer = ` `.repeat(3 - percentage.toFixed(0).length);

process.stdout.write(
`\r[${spacer}${percentage.toFixed(0)}]|${"█".repeat(
barLength
)}${" ".repeat(emptyLength)}|`
);
} catch (err) {
if (err instanceof CobraActivationLimitReachedError) {
console.error(
Expand All @@ -106,6 +115,7 @@ async function micDemo() {
recorder.stop();
recorder.release();
engineInstance.release();
process.stdout.write("\n");
process.exit();
}

Expand Down

0 comments on commit df4ad02

Please sign in to comment.