-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
140 lines (129 loc) · 3.91 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
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
// Copyright 2022 Symbl.ai SDK contributors. All Rights Reserved.
// SPDX-License-Identifier: MIT
/*
* This example shows how to pass in different Audio Codecs. The ones currently supported are
* OPUS:
* * Supported Sample Rates -- 16000Hz, 24000Hz, 48000Hz
* * Both CBR (Constant Bitrate) and VBR (Variable Bitrate) are supported
* * Support for in-band FEC
*
* SPEEX:
* * Supported Sample Rates -- 16000Hz
* * VBR is not supported
*
* LINEAR16:
* * Supported Sample Rates -- 44100Hz
*
* MULAW:
* * Supported Sample Rates -- 8000Hz
*
* NOTE: We recommend using OPUS as compared to other codecs because it provides the most flexibility in terms of
* audio transportation and also has packet retransmission mechanisms like FEC which work well especially
* in low-bandwidth scenarios.
*
* If you have a requirement to use a codec not included in the ones above or have any other queries,
* please drop an e-mail to [email protected]
*/
require('dotenv').config()
const {sdk, SpeakerEvent} = require('symbl-node')
const getScheduleEvent = (sdk, connectionId) => {
return (eventType, user, time) => {
setTimeout(() => {
const speakerEvent = new SpeakerEvent({
type: eventType,
user,
})
speakerEvent.timestamp = new Date().toISOString()
console.log(
`Pushing event [${speakerEvent.timestamp}] ${speakerEvent.type} : ${speakerEvent.user.name}`,
)
sdk.pushEventOnConnection(connectionId, speakerEvent.toJSON(), (err) => {
if (err) {
console.error('Error during push event.', err)
} else {
console.log('Event pushed!')
}
})
}, time * 1000)
}
}
const users = {
john: {
userId: '[email protected]',
name: 'John',
},
mary: {
userId: '[email protected]',
name: 'Mary',
},
tim: {
userId: '[email protected]',
name: 'Tim',
},
jennifer: {
userId: '[email protected]',
name: 'Jennifer',
},
}
;(async () => {
try {
// Initialize the SDK
await sdk.init({
appId: process.env.APP_ID,
appSecret: process.env.APP_SECRET,
basePath: 'https://api.symbl.ai',
})
const connection = await sdk.startEndpoint({
endpoint: {
//*****************Custom Audio Config******************
audioConfig: {
encoding: 'OPUS',
sampleRate: 16000,
},
//******************************************************
type: 'pstn',
phoneNumber: process.env.DEFAULT_PHONE_NUMBER,
},
actions: [
{
invokeOn: 'stop',
name: 'sendSummaryEmail',
parameters: {
emails: ['[email protected]'],
},
},
],
data: {
session: {
name: 'Ship-wide nanomachines, to the center.',
},
},
})
const connectionId = connection.connectionId
console.log('Successfully connected.', connectionId)
const scheduleEvent = getScheduleEvent(sdk, connectionId)
setTimeout(() => {
// This is just for interactive purposeas to show the elapsed time.
scheduleEvent(SpeakerEvent.types.startedSpeaking, users.john, 0)
scheduleEvent(SpeakerEvent.types.stoppedSpeaking, users.john, 4)
scheduleEvent(SpeakerEvent.types.startedSpeaking, users.mary, 4)
scheduleEvent(SpeakerEvent.types.stoppedSpeaking, users.mary, 9)
// Scheduling stop endpoint call after 60 seconds
setTimeout(() => {
console.log('stopping connection ' + connection.connectionId)
sdk
.stopEndpoint({
connectionId,
})
.then(() => {
console.log('Stopped the connection')
})
.catch((err) =>
console.error('Error while stopping the connection.', err),
)
}, 10000)
}, 1000)
} catch (err) {
console.error('Error in SDK initialization.', err)
}
})()