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

Commit

Permalink
Add speaker identification option. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioga authored Oct 30, 2020
1 parent 1dc6b4e commit e232883
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A client for Amazon Transcribe using the websocket interface

## Getting Started

With NPM install the module with: `npm install aws-transcribe --save`
With NPM install the module with: `npm install aws-transcribe --save`
With YARN install the module with: `yarn add aws-transcribe`

## Example
Expand Down Expand Up @@ -68,6 +68,10 @@ The `transcribeStreamConfig` is required and must have the following properties:
- `languageCode` must be one of "en-US", "en-AU", "en-GB", "fr-CA", "fr-FR", "es-US"
- `sampleRate` must be between 8000 and 44100 - the supported sample rate differs depending on the language code being used. For more information, go [here](https://docs.aws.amazon.com/transcribe/latest/dg/streaming.html)

It may also optionally include:

- `showSpeakerLabel` - when `true`, [speaker identification](https://docs.aws.amazon.com/transcribe/latest/dg/diarization-streaming.html) will be enabled

### StreamingClient EVENTS

- `open` - when the socket to aws is opened
Expand Down
16 changes: 5 additions & 11 deletions src/AwsTranscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ export class AwsTranscribe {
private accessKeyId!: string
private secretAccessKey!: string
private sessionToken: string | undefined
private showSpeakerLabel?: boolean

constructor(config?: ClientConfig) {
// get from environment if config not provided
this.setAccessKeyId(config?.accessKeyId || process.env.AWS_ACCESS_KEY_ID)
this.setSecretAccessKey(config?.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY)
this.setSessionToken(config?.sessionToken || process.env.AWS_SESSION_TOKEN)
this.setShowSpeakerLabel(config?.showSpeakerLabel || false)
}

private createPreSignedUrl(config: TranscribeStreamConfig) {
const { region, languageCode, sampleRate, showSpeakerLabel } = config
const endpoint = "transcribestreaming." + region + ".amazonaws.com:8443"
let query = "language-code=" + languageCode + "&media-encoding=pcm&sample-rate=" + sampleRate
if (showSpeakerLabel) {
query += '&show-speaker-label=true'
}
let query = "language-code=" + languageCode + "&media-encoding=pcm&sample-rate=" + sampleRate
if (showSpeakerLabel) {
query += '&show-speaker-label=' + showSpeakerLabel
}

return createPresignedURL(
"GET",
Expand All @@ -39,7 +37,7 @@ export class AwsTranscribe {
protocol: "wss",
expires: 15,
region: region,
query: query
query: query,
}
)
}
Expand All @@ -64,8 +62,4 @@ export class AwsTranscribe {
setSessionToken(sessionToken: string | undefined) {
this.sessionToken = sessionToken
}

setShowSpeakerLabel(showSpeakerLabel: boolean | false) {
this.showSpeakerLabel = showSpeakerLabel
}
}
21 changes: 21 additions & 0 deletions src/__tests__/AwsTranscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ describe("AwsTranscribe", () => {
})
})

it(`should include show-speaker-label parameter when given`, () => {
const region = "us-east-1"
const sampleRate = 8000
const languageCode = "en-GB"
const showSpeakerLabel = true

client.createStreamingClient({
region,
sampleRate,
languageCode,
showSpeakerLabel,
})

expect(mockedCreatePresignedURL).toBeCalled()
const args = mockedCreatePresignedURL.mock.calls[0]
const options = args[5]
const query = options.query

expect(query).toBe(`language-code=${languageCode}&media-encoding=pcm&sample-rate=${sampleRate}&show-speaker-label=true`)
})

it(`should create and return an instance of Streaming client with the pre signed url`, () => {
const region = "us-east-1"
const sampleRate = 8000
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface ClientConfig {
accessKeyId?: string
secretAccessKey?: string
sessionToken?: string
showSpeakerLabel?: boolean
}

export interface TranscribeStreamConfig {
Expand Down Expand Up @@ -79,6 +78,7 @@ interface TranscribeItem {
EndTime: number
StartTime: number
Type: "pronunciation" | "punctuation"
Speaker: string
}

interface TranscribeAlternative {
Expand Down

0 comments on commit e232883

Please sign in to comment.