-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstreamingLiveMicStream.js
210 lines (182 loc) · 6.64 KB
/
streamingLiveMicStream.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const {sdk} = require("@symblai/symbl-js");
const APP_ID = "<your App ID>";
const APP_SECRET = '<your App Secret>';
const USER_ID = '<your Email address/userId>';
const FULL_NAME = '<your name>';
const uuid = require("uuid").v4;
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
// For demo purposes, we're using mic to simply get audio from the microphone and pass it on to the WebSocket connection
const mic = require("mic");
const sampleRateHertz = 16000;
const micInstance = mic({
"rate": sampleRateHertz,
"channels": "1",
"debug": false,
"exitOnSilence": 6
});
let muted = true;
let muteUnmuteProgress = false;
let terminationInProgress = false;
(async () => {
try {
// Initialize the SDK
await sdk.init({
"appId": APP_ID,
"appSecret": APP_SECRET,
"basePath": "https://api-labs.symbl.ai"
});
// Need unique Id
const id = uuid();
console.log(`Establishing new stream with Symbl -- Connection ID: ${id}`);
// Start Real-time Streaming Request (Uses Streaming API behind the scenes)
const stream = await sdk.createStream({
id,
"disconnectOnStopRequest": false,
"disconnectOnStopRequestTimeout": 300,
"noConnectionTimeout": 900,
"insightTypes": [
"action_item",
"question"
],
"config": {
"meetingTitle": "My Test Meeting", // Set name for meeting
"confidenceThreshold": 0.7,
"timezoneOffset": 480, // Offset in minutes from UTC
"languageCode": "en-US",
sampleRateHertz
},
"speaker": {
// Optional, if not specified, will simply not send an email in the end.
"userId": USER_ID, // Update with valid email
"name": FULL_NAME
},
"handlers": {
// This will return live speech-to-text transcription of the call.
"onSpeechDetected": (data) => {
if (data) {
const {punctuated} = data;
console.log(
"Live: ",
punctuated && punctuated.transcript
);
console.log("");
}
},
// When processed messages are available, this callback will be called.
"onMessageResponse": (data) => {
console.log(
"onMessageResponse",
JSON.stringify(
data,
null,
2
)
);
},
// When Symbl detects an insight, this callback will be called.
"onInsightResponse": (data) => {
console.log(
"onInsightResponse",
JSON.stringify(
data,
null,
2
)
);
},
// When Symbl detects a topic, this callback will be called.
"onTopicResponse": (data) => {
console.log(
"onTopicResponse",
JSON.stringify(
data,
null,
2
)
);
}
}
});
console.log("Successfully initialized stream with Symbl -- Connection ID: ", stream.connectionId);
const micInputStream = micInstance.getAudioStream();
// Raw audio stream
micInputStream.on(
"data",
(data) => {
// Push audio from Microphone to websocket stream
stream.sendAudio(data);
}
);
// Logging errors from the mic instance
micInputStream.on(
"error",
(err) => {
console.log(`Error in Input Stream: ${err}`);
}
);
// Logging when the mic instance connects and begins listening to microphone
micInputStream.on(
"startComplete",
() => {
console.log("Started listening to Microphone.");
}
);
// Logging when the mic is only picking up silence.
micInputStream.on(
"silence",
() => {
console.log("Silence detected...");
}
);
micInstance.start();
console.log(`Mic initialization complete. You're muted. Please press 'm' to unmute and wait for the console log to start speaking...`);
process.stdin.on('keypress', async (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit();
} else if (key.name === 'm') {
if (!muted && !muteUnmuteProgress) {
muteUnmuteProgress = true;
console.log('\r\nMuting...');
await stream.stop();
muted = true;
console.log('Muted.');
muteUnmuteProgress = false;
} else if (muted && !muteUnmuteProgress) {
muteUnmuteProgress = true;
console.log('\r\nUn-muting...');
await stream.start();
muted = false;
console.log('Unmuted.');
muteUnmuteProgress = false;
}
} else if (key.name === 't' && !terminationInProgress) {
terminationInProgress = true;
console.log('Terminating stream');
micInstance.stop();
console.log("Stopped listening to Microphone.");
try {
// Stop stream
await stream.stop();
console.log("Connection Stopped.");
} catch (e) {
console.error(
"Error while stopping the stream.",
e
);
}
terminationInProgress = false;
}
});
(async () => {
const conversationId = await stream.conversationId;
console.log(`Conversation ID: ${conversationId}`);
})();
} catch (e) {
console.error(
"Error: ",
e
);
}
})();