This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
108 lines (95 loc) · 2.94 KB
/
index.js
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
var EventEmitter = require('events').EventEmitter
var getUserMedia = require('getusermedia')
var Speech = require('@google-cloud/speech')
var audio = require('audio-stream')
var pcm = require('pcm-stream')
var assert = require('assert')
var pumpify = require('pumpify')
var writer = require('flush-write-stream')
var pump = require('pump')
module.exports = GetUserMediaToText
function GetUserMediaToText (opts) {
if (!(this instanceof GetUserMediaToText)) return new GetUserMediaToText()
if (!opts) opts = {}
// assert.ok(opts.projectId, 'GetUserMediaToText: Missing projectId in options')
assert.ok(opts.keyFilename, 'GetUserMediaToText: Missing keyFilename path in options')
this._opts = Object.assign({
projectId: 'getusermedia-to-text',
request: {
config: {
encoding: 'LINEAR16',
sampleRateHertz: 44100,
languageCode: 'en-US'
},
singleUtterance: false,
interimResults: false,
verbose: true
}
}, opts)
this.speech = Speech({
projectId: this._opts.projectId,
keyFilename: this._opts.keyFilename
})
this.mediaStream = null
this.audioStream = null
this.sinkStream = null
this.pipeline = null
this.listening = false
this.waiting = false
var self = this
getUserMedia({video: false, audio: true}, function (err, ms) {
if (err) throw err
self.mediaStream = ms
self.emit('mediaStream', ms)
})
EventEmitter.call(this)
}
GetUserMediaToText.prototype = Object.create(EventEmitter.prototype)
GetUserMediaToText.prototype.start = function () {
var self = this
if (this.listening) return this.emit('status', 'Already Listening')
if (!this.mediaStream) {
if (this.waiting) return this.emit('status', 'Waiting for userMedia')
this.waiting = true
return this.once('mediaStream', function () {
self.waiting = false
self.start()
})
}
if (!this.sinkStream) {
var recognizeStream = this.speech.createRecognizeStream(this._opts.request)
var emitter = writer.obj(function (data, enc, cb) {
self.emit('data', data)
cb()
})
this.sinkStream = pumpify(pcm(), recognizeStream, emitter)
}
if (!this.audioStream) {
this.audioStream = audio(this.mediaStream, {
channels: 1,
volume: 0.8
})
}
this.listening = true
this.emit('listening', true)
this.emit('status', 'Started listening')
this.pipeline = pump(this.audioStream, this.sinkStream, function (err) {
if (err) self.emit('error', err)
self.clearPipeline()
})
}
GetUserMediaToText.prototype.clearPipeline = function () {
this.listening = false
this.emit('listening', false)
this.audioStream = null
this.sinkStream = null
this.pipeline = null
// this.mediaStream.getAudioTracks().forEach(function (track) {
// track.stop()
// })
this.emit('status', 'Stopped listening')
}
GetUserMediaToText.prototype.stop = function () {
if (this.listening) this.audioStream.destroy()
else this.emit('status', 'Already Stopped')
}