From 29ddffd78ab9f7815af4e7715658445c494b0530 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Thu, 4 Jun 2020 23:10:47 +0300 Subject: [PATCH] Add an example for streaming from file --- package-lock.json | 45 ++++++++++++++++++++ package.json | 2 + src/examples/stream-from-file.ts | 71 ++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 src/examples/stream-from-file.ts diff --git a/package-lock.json b/package-lock.json index a3d6fd5..bdb7e96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1042,6 +1042,15 @@ "integrity": "sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==", "dev": true }, + "@types/throttle": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@types/throttle/-/throttle-1.0.0.tgz", + "integrity": "sha512-TT7+u+0JrKMfrw4IwQqL1K2MmS6DwknQlC5Lsf90mgWEVDurNWqZ/DQ82/hmxE0gm2HyUCHpbD2XaLmWRPBWhA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/ws": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.2.3.tgz", @@ -9725,6 +9734,32 @@ "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=", "dev": true }, + "stream-parser": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/stream-parser/-/stream-parser-0.3.1.tgz", + "integrity": "sha1-FhhUhpRCACGhGC/wrxkRwSl2F3M=", + "dev": true, + "requires": { + "debug": "2" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + } + } + }, "strict-uri-encode": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz", @@ -9974,6 +10009,16 @@ "integrity": "sha512-fcwX4mndzpLQKBS1DVYhGAcYaYt7vsHNIvQV+WXMvnow5cgjPphq5CaayLaGsjRdSCKZFNGt7/GYAuXaNOiYCA==", "dev": true }, + "throttle": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/throttle/-/throttle-1.0.3.tgz", + "integrity": "sha1-ijLkoV8XY9mXlIMXxevjrYpB5Lc=", + "dev": true, + "requires": { + "readable-stream": ">= 0.3.0", + "stream-parser": ">= 0.0.2" + } + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", diff --git a/package.json b/package.json index 5bb29ac..d2927d9 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@types/debug": "^4.1.5", "@types/jest": "^25.1.4", "@types/node": "^13.9.5", + "@types/throttle": "^1.0.0", "@types/ws": "^7.2.3", "@typescript-eslint/eslint-plugin": "^2.25.0", "@typescript-eslint/parser": "^2.25.0", @@ -62,6 +63,7 @@ "prettier": "^2.0.2", "shx": "^0.3.2", "standard-version": "^7.1.0", + "throttle": "^1.0.3", "ts-jest": "^25.2.1", "ts-node": "^8.8.1", "typedoc": "^0.17.3", diff --git a/src/examples/stream-from-file.ts b/src/examples/stream-from-file.ts new file mode 100644 index 0000000..180ac8b --- /dev/null +++ b/src/examples/stream-from-file.ts @@ -0,0 +1,71 @@ +import * as fs from 'fs' +import * as Throttle from 'throttle' +import { AwsTranscribe, StreamingClient, TranscriptEvent } from "../index" + +const sampleRate = 16000 +/** + * You can run this file by running the npm script called example-stream-from-microphone + * npm run example-stream-from-microphone + * Ensure sox is installed and accessible from the cli + * http://sox.sourceforge.net + */ + +/****************************************************************************** + * Create AWS transcribe client + *******************************************************************************/ + +const client = new AwsTranscribe({ + // if these aren't provided, they will be taken from the environment + accessKeyId: "ACCESS KEY HERE", + secretAccessKey: "SECRET KEY HERE", +}) + +/** + * Note - currently, transcribe supports the following sample rate with the given languages + * 8 KHz and 16 KHz + * US English (en-US) + * US Spanish (es-US) + * 8 KHz only + * Australian English (en-AU) + * British English (en-GB) + * French (fr-FR) + * Canadian French (fr-CA) + */ +const transcribeStream = client + .createStreamingClient({ + region: "eu-west-1", + sampleRate, + languageCode: "en-US", + }) + // enums for returning the event names which the stream will emit + .on(StreamingClient.EVENTS.OPEN, () => console.log(`transcribe connection opened`)) + .on(StreamingClient.EVENTS.ERROR, console.error) + .on(StreamingClient.EVENTS.CLOSE, () => console.log(`transcribe connection closed`)) + .on(StreamingClient.EVENTS.DATA, (data: TranscriptEvent) => { + const results = data.Transcript.Results + + if (!results || results.length === 0) { + return + } + + const result = results[0] + const final = !result.IsPartial + const prefix = final ? "recognized" : "recognizing" + const text = result.Alternatives[0].Transcript + console.log(`${prefix} text: ${text}`) + }) + +/****************************************************************************** + * Stream the file. It must be LINEAR16 with the given sample rate + *******************************************************************************/ +fs.createReadStream('test.wav').pipe(new Throttle(sampleRate * 2)).pipe(transcribeStream) + +/****************************************************************************** + * For closing the transcribe stream + *******************************************************************************/ +process.on("SIGINT", function () { + // call the destroy method on the transcribe stream to close the socket and end the stream + console.log("Goodbye") + transcribeStream.destroy() + process.exit(0) +})