This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asr.ts
134 lines (131 loc) · 3.56 KB
/
asr.ts
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
import spokestackService, { SpokestackASRConfig } from './spokestackASRService'
import { SpeechClient } from '@google-cloud/speech'
/**
* A one-off method for processing speech to text
* using Spokestack ASR.
*
*
* ```js
* import fileUpload from 'express-fileupload'
* import { asr } from 'spokestack'
* import express from 'express'
*
* const expressApp = express()
*
* expressApp.post('/asr', fileUpload(), (req, res) => {
* const sampleRate = Number(req.body.sampleRate)
* const audio = req.files.audio
* if (isNaN(sampleRate)) {
* res.status(400)
* res.send('Parameter required: "sampleRate"')
* return
* }
* if (!audio) {
* res.status(400)
* res.send('Parameter required: "audio"')
* return
* }
* asr(Buffer.from(audio.data.buffer), sampleRate)
* .then((text) => {
* res.status(200)
* res.json({ text })
* })
* .catch((error: Error) => {
* console.error(error)
* res.status(500)
* res.send('Unknown error during speech recognition. Check server logs.')
* })
* })
*
* ```
*/
export function asr(
content: string | Uint8Array,
config: SpokestackASRConfig
): Promise<string | null> {
return new Promise((resolve, reject) => {
spokestackService(config, (response) => {
// console.log('[asr] response', response)
if (response.status === 'ok' && response.final) {
resolve(
response.hypotheses
.map((value) => value && value.transcript)
.filter(Boolean)
.join('\n')
)
} else if (response.status === 'error') {
console.error('Unexpected ASR error', response.error)
reject(new Error(response.error))
}
})
.then((spokestackSocket) => {
spokestackSocket.on('error', reject)
spokestackSocket.send(content)
// Send an empty buffer to signal that the transaction is done
spokestackSocket.send(Buffer.from(''))
})
.catch(reject)
})
}
/**
* A one-off method for processing speech to text
* using Google Speech.
*
*
* ```js
* import fileUpload from 'express-fileupload'
* import { googleASR } from 'spokestack'
* import express from 'express'
*
* const expressApp = express()
*
* expressApp.post('/asr', fileUpload(), (req, res) => {
* const sampleRate = Number(req.body.sampleRate)
* const audio = req.files.audio
* if (isNaN(sampleRate)) {
* res.status(400)
* res.send('Parameter required: "sampleRate"')
* return
* }
* if (!audio) {
* res.status(400)
* res.send('Parameter required: "audio"')
* return
* }
* googleASR(Buffer.from(audio.data.buffer), sampleRate)
* .then((text) => {
* res.status(200)
* res.json({ text })
* })
* .catch((error: Error) => {
* console.error(error)
* res.status(500)
* res.send('Unknown error during speech recognition. Check server logs.')
* })
* })
*
* ```
*/
export async function googleASR(
content: string | Uint8Array,
sampleRate: number
): Promise<string | null> {
const client = new SpeechClient()
const fullResponse = await client.recognize({
audio: { content },
config: {
sampleRateHertz: sampleRate,
encoding: 'LINEAR16',
languageCode: 'en-US'
}
})
// console.log(fullResponse)
const response = fullResponse[0]
if (!response || !response.results || !response.results.length) {
return null
}
return response.results
.map((result) => (result.alternatives ? result.alternatives[0].transcript : undefined))
.filter(Boolean)
.join('\n')
}