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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
<!--
- @copyright Copyright (c) 2021 Marco Ambrosini <marcoambrosini@pm.me>
-
- @author Marco Ambrosini <marcoambrosini@pm.me>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<div
class="audio-recorder">
<button
v-if="!isRecording"
v-tooltip.auto="{
content: startRecordingTooltip,
delay: tooltipDelay,
}"
:disabled="!canStartRecording"
class="audio-recorder__trigger nc-button nc-button__main"
:class="{'audio-recorder__trigger--disabled': !canStartRecording}"
@click="start">
<Microphone
:size="16"
title=""
decorative />
</button>
<div v-else class="wrapper">
<button
v-tooltip.auto="{
content: abortRecordingTooltip,
delay: tooltipDelay,
}"
class="audio-recorder__stop nc-button nc-button__main"
@click="abortRecording">
<Close
:size="16"
title=""
decorative />
</button>
<div class="audio-recorder__info">
<div class="recording-indicator fadeOutIn" />
<span
class="time">
{{ parsedRecordTime }}</span>
</div>
<button
v-tooltip.auto="{
content: stopRecordingTooltip,
delay: tooltipDelay,
}"
class="audio-recorder__trigger nc-button nc-button__main"
:class="{'audio-recorder__trigger--recording': isRecording}"
@click="stop">
<Check
:size="16"
title=""
decorative />
</button>
</div>
</div>
</template>
<script>
import Microphone from 'vue-material-design-icons/Microphone'
import Close from 'vue-material-design-icons/Close'
import Check from 'vue-material-design-icons/Check'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
import { mediaDevicesManager } from '../../../utils/webrtc/index'
import { showError } from '@nextcloud/dialogs'
import { MediaRecorder } from 'extendable-media-recorder'
export default {
name: 'AudioRecorder',
components: {
Microphone,
Close,
Check,
},
directives: {
tooltip: Tooltip,
},
props: {
disabled: {
type: Boolean,
default: false,
},
},
data() {
return {
// The audio stream object
audioStream: null,
// The media recorder which generate the recorded chunks
mediaRecorder: null,
// The chunks array
chunks: [],
// The final audio file blob
blob: null,
// Switched to true if the recording is aborted
aborted: false,
// recordTimer
recordTimer: null,
// the record timer
recordTime: {
minutes: 0,
seconds: 0,
},
}
},
computed: {
// Recording state of the mediaRecorder
isRecording() {
if (this.mediaRecorder) {
return this.mediaRecorder.state === 'recording'
} else {
return false
}
},
parsedRecordTime() {
const seconds = this.recordTime.seconds.toString().length === 2 ? this.recordTime.seconds : `0${this.recordTime.seconds}`
const minutes = this.recordTime.minutes.toString().length === 2 ? this.recordTime.minutes : `0${this.recordTime.minutes}`
return `${minutes}:${seconds}`
},
tooltipDelay() {
return { show: 500, hide: 200 }
},
startRecordingTooltip() {
return t('spreed', 'Record voice message')
},
stopRecordingTooltip() {
return t('spreed', 'End recording and send')
},
abortRecordingTooltip() {
return t('spreed', 'Dismiss recording')
},
encoderReady() {
return this.$store.getters.encoderReady
},
canStartRecording() {
if (this.disabled) {
return false
} else {
return this.encoderReady
}
},
},
watch: {
isRecording(newValue) {
console.debug('isRecording', newValue)
},
},
mounted() {
this.$store.dispatch('initializeAudioEncoder')
},
beforeDestroy() {
this.killStreams()
},
methods: {
/**
* Initialize the media stream and start capturing the audio
*/
async start() {
if (!this.canStartRecording) {
return
}
// Create new audio stream
try {
this.audioStream = await mediaDevicesManager.getUserMedia({
audio: true,
video: false,
})
} catch (exception) {
console.debug(exception)
this.killStreams()
if (exception.name === 'NotAllowedError') {
showError(t('spreed', 'Access to the microphone was denied'))
} else {
showError(t('spreed', 'Microphone either not available or disabled in settings'))
}
return
}
// Create a mediarecorder to capture the stream
try {
this.mediaRecorder = new MediaRecorder(this.audioStream, {
mimeType: 'audio/wav',
})
} catch (exception) {
console.debug(exception)
this.killStreams()
this.audioStream = null
showError(t('spreed', 'Error while recording audio'))
return
}
// Add event handler to onstop
this.mediaRecorder.onstop = this.generateFile
// Add event handler to ondataavailable
this.mediaRecorder.ondataavailable = (e) => {
this.chunks.push(e.data)
}
try {
// Start the recording
this.mediaRecorder.start()
} catch (exception) {
console.debug(exception)
this.aborted = true
this.stop()
this.killStreams()
this.resetComponentData()
showError(t('spreed', 'Error while recording audio'))
return
}
console.debug(this.mediaRecorder.state)
// Start the timer
this.recordTimer = setInterval(() => {
if (this.recordTime.seconds === 59) {
this.recordTime.minutes++
this.recordTime.seconds = 0
}
this.recordTime.seconds++
}, 1000)
// Forward an event to let the parent NewMessageForm component
// that there's an undergoing recording operation
this.$emit('recording', true)
},
/**
* Stop the mediaRecorder
*/
stop() {
this.mediaRecorder.stop()
clearInterval(this.recordTimer)
this.$emit('recording', false)
},
/**
* Generate the file
*/
generateFile() {
this.killStreams()
if (!this.aborted) {
this.blob = new Blob(this.chunks, { type: 'audio/wav' })
// Generate file name
const fileName = this.generateFileName()
// Convert blob to file
const audioFile = new File([this.blob], fileName)
audioFile.localURL = window.URL.createObjectURL(this.blob)
this.$emit('audioFile', audioFile)
this.$emit('recording', false)
}
this.resetComponentData()
},
/**
* Aborts the recording operation.
*/
abortRecording() {
this.aborted = true
this.stop()
},
/**
* Resets this component to its initial state
*/
resetComponentData() {
this.audioStream = null
this.mediaRecorder = null
this.chunks = []
this.blob = null
this.aborted = false
this.isAudiorecorderActive = false
this.recordTime = {
minutes: 0,
seconds: 0,
}
},
generateFileName() {
const token = this.$store.getters.getToken()
const conversation = this.$store.getters.conversation(token).name
const today = new Date()
let time = today.getFullYear() + '-' + ('0' + today.getMonth()).slice(-2) + '-' + ('0' + today.getDay()).slice(-2)
time += ' ' + ('0' + today.getHours()).slice(-2) + '-' + ('0' + today.getMinutes()).slice(-2) + '-' + ('0' + today.getSeconds()).slice(-2)
return t('spreed', 'Talk recording from {time} ({conversation})', { time, conversation }) + '.wav'
},
/**
* Stop the audio streams
*/
killStreams() {
this.audioStream?.getTracks().forEach(track => track.stop())
},
},
}
</script>
<style lang="scss" scoped>
@import '../../../assets/buttons';
.audio-recorder {
display: flex;
// Audio record button
&__trigger {
&--recording {
background-color: var(--color-success) !important;
opacity: .8;
color: white;
&:hover,
&:focus {
opacity: 1;
}
}
&--disabled {
opacity: .5;
cursor: default;
&:hover,
&:focus {
background-color: transparent !important;
}
}
&--disabled * {
cursor: default;
}
}
&__stop {
margin-left: 4px;
background-color: var(--color-error) !important;
color: white;
opacity: .8;
&:hover,
&:focus {
opacity: 1;
}
}
&__info {
width: 86px;
display: flex;
justify-content: center;
align-items: center;
.time {
flex: 0 0 50px;
}
.recording-indicator {
width: 16px;
height: 16px;
flex: 0 0 16px;
border-radius: 8px;
background-color: var(--color-error);
margin: 8px;
}
}
}
.wrapper {
display: flex;
}
@keyframes fadeOutIn {
0% { opacity:1; }
50% { opacity:.3; }
100% { opacity:1; }
}
.fadeOutIn {
animation: fadeOutIn 3s infinite;
}
</style>
|