/
index.js
172 lines (138 loc) · 5 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
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
require('dotenv').config()
const { spawn } = require('child_process');
const fs = require('fs')
const speech = require('@google-cloud/speech');
const { Client, Intents, VoiceChannel } = require('discord.js');
const VOICE_CHANNEL_ID = '845384132388192306'
class Broadcaster {
constructor() {
this.client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
this.client.login(process.env.DISCORD_BOT_TOKEN_BROADCASTER);
this.client.on('ready', () => {
console.log(`Logged in as ${this.client.user.tag}!`);
this.joinVoiceChannel(VOICE_CHANNEL_ID)
.catch(console.error)
});
this.client.on('message', message => {
if (message.content == 'play') {
this.playStory()
message.channel.send(':speech_balloon: Playing stotry')
}
if (message.content == 'stop') {
this.stopStory()
message.channel.send(':zipper_mouth: Stop playing')
}
});
}
joinVoiceChannel(voiceChannelId) {
return this.client.channels.fetch(voiceChannelId)
.then(channel => channel.join())
.then(connection => this.voiceConnection = connection)
}
playStory() {
this.broadcast = this.voiceConnection.play('audio/katie-weather.mp3')
return this.broadcast
}
stopStory() {
if (this.broadcast) {
this.broadcast.end()
this.broadcast = null
}
}
}
class Transcriber {
constructor() {
this.client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
this.client.login(process.env.DISCORD_BOT_TOKEN_TRANSCRIBER);
this.client.on('ready', () => {
console.log(`Logged in as ${this.client.user.tag}!`);
this.joinVoiceChannel(VOICE_CHANNEL_ID)
.catch(console.error)
});
this.client.on('message', message => {
if (message.content == 'listen') {
this.listen(message.author, message.channel)
}
});
this.client.on('message', message => {
if (message.content == 'trans') {
this.transcribe()
}
});
}
joinVoiceChannel(voiceChannelId) {
return this.client.channels.fetch(voiceChannelId)
.then(channel => channel.join())
.then(connection => this.voiceConnection = connection)
}
listen(user, channel) {
channel.send(':ear: I\'m listening')
this.getStream(user).on('close', () => {
channel.send(':pencil2: Transcribing...')
const conversion = spawn('ffmpeg',
[
'-f', 's16le',
'-vn',
'-ar', '48k',
'-ac', '2',
'-i', 'audio/audio_s16le.raw',
'-acodec', 'pcm_s16le',
'-ar', '16k',
'-ac', '1',
'audio/audio_LINEAR16.wav',
'-y'
])
conversion.on('close', () => {
this.transcribe(transcription => {
channel.send(transcription)
})
})
})
}
getStream(user) {
// Create a ReadableStream of s16le PCM audio
const audio = this.voiceConnection.receiver.createStream(user, { mode: 'pcm' });
const file = fs.createWriteStream('audio/audio_s16le.raw')
audio.pipe(file);
return file
}
transcribe(callback) {
// Creates a client
const client = new speech.SpeechClient();
const streamingConfig = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
}
/* For streaming transcription (WIP)
const streamRequest = {
config: streamingConfig,
singleUtterance: true,
interimResults: true, //Get interim results from stream
};
// Stream the audio to the Google Cloud Speech API
const recognizeStream = client
.streamingRecognize(request)
.on('error', console.error)
.on('data', data => {
console.log(
`Transcription: ${data.results[0].alternatives[0].transcript}`
);
});
// Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw"
fs.createReadStream(filename).pipe(recognizeStream);
*/
client.recognize({
config: streamingConfig,
audio: {
content: fs.readFileSync('audio/audio_LINEAR16.wav').toString('base64')
}
}).then(([response]) => {
const transcription = response.results.map(result => result.alternatives[0].transcript).join('\n');
console.log(transcription)
callback(transcription)
})
}
}
new Broadcaster()
new Transcriber()