-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (72 loc) · 2.19 KB
/
app.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
const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const revai = require("revai-node-sdk");
require("dotenv").config();
if (!process.env.REVAI_ACCESS_TOKEN) {
console.log("Access token not found. Add token to .env");
process.exit();
}
const token = process.env.REVAI_ACCESS_TOKEN;
// initialize client with audio configuration and access token
const audioConfig = new revai.AudioConfig(
"audio/x-raw", // contentType
"interleaved", // layout
16000, // sample rate
"S16LE", // format
1 // channels
);
var client = new revai.RevAiStreamingClient(token, audioConfig);
// initialize express app
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
// serve static files from 'public' directory
app.use(express.static("public"));
// create event responses
client.on("close", (code, reason) => {
console.log(`Connection closed, ${code}: ${reason}`);
});
client.on("httpResponse", (code) => {
console.log(`Streaming client received http response with code: ${code}`);
});
client.on("connectFailed", (error) => {
console.log(`Connection failed with error: ${error}`);
});
client.on("connect", (connectionMessage) => {
console.log(`Connected with message: ${JSON.stringify(connectionMessage)}`);
});
// begin streaming session
var stream = client.start();
// create event responses
//this is the audio stream in JSON
stream.on("data", (data) => {
// emit data to all connected clients
// console.log(data);
io.emit("caption", data);
});
stream.on("end", function () {
console.log("End of Stream");
});
// microphone
const mic = require("mic");
// initialize microphone configuration
const micConfig = {
rate: 16000, // sample rate
channels: 1, // channels
device: "hw:0,0", // device id
};
var micInstance = mic(micConfig);
// create microphone stream
var micStream = micInstance.getAudioStream();
micStream.on("error", (error) => {
console.log(`Microphone input stream error: ${error}`);
});
// pipe the microphone audio to Rev AI client
micStream.pipe(stream);
// start the microphone
micInstance.start();
// start server
server.listen(3000, () => {
console.log("Server is running on port 3000");
});