Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Add an example for streaming from file #11

Merged
merged 1 commit into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
71 changes: 71 additions & 0 deletions src/examples/stream-from-file.ts
Original file line number Diff line number Diff line change
@@ -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)
})