const url = require('url')
const net = require('net')
const wav = require('wav')
+const streamToText = require('./stream-to-text.js')
/* Some hardcoded parameters for now
Should be dynamic...
this.index = index
this.active = false
this.writer = undefined
+ this.path = undefined
+
+ this.transcripter = new streamToText.Transcripter()
+ this.transcripter.on('data', data => {
+ console.log(data)
+ odasStudio.recordingsWindow.webContents.send('fuzzy-transcript', this.path, data)
+ })
+ this.transcripter.on('error', err => {
+ console.error(err)
+ })
ipcMain.on('new-recording', (event, index, id) => {
if(index == this.index) {
if(typeof(this.writer)!=='undefined') {
this.writer.write(data)
+ this.transcripter.putData(data)
}
}
startRecording(id) {
- console.log('recorder started')
if(typeof(this.writer) == 'undefined' && record) {
+ console.log('recorder started')
+
let filename = path.join(workspacePath, `ODAS_${id}_${new Date().toLocaleString()}.wav`)
+ this.path = filename
this.writer = new wav.FileWriter(filename,{channels:1, sampleRate:sampleRate, bitDepth:bitNumber})
odasStudio.recordingsWindow.webContents.send('fuzzy-recording', filename)
+
+ this.transcripter.start()
}
}
if(typeof(this.writer) !== 'undefined') {
this.writer.end()
+ this.transcripter.stop()
+
this.writer.on('header',(header) => {
if(typeof(odasStudio.recordingsWindow)!='undefined') {
odasStudio.recordingsWindow.webContents.send('add-recording', this.writer.path)
this.filename = path.basename(fullPath)
this.path = fullPath
this.timestamp = new Date()
+ this.transcription = ''
+ this.potentialTranscription = ''
}
}
// Single recording class
class Recording {
- constructor(fullPath) {
+ constructor(fullPath, transcription) {
// Init audio recording
this.isPlaying = false
this.audio = undefined
this.deleting = 'hidden'
this.transcription = 'Not available'
+ this.potentialTranscription = ''
this.readInfo(true)
- this.createTranscript();
+
+ if(typeof(transcription) !== 'undefined') {
+ this.transcription = transcription
+ const txtPath = this.path.slice(0, -4)+'.txt';
+ fs.writeFile(txtPath, transcription, (err) => {
+ if(err) console.log(err);
+ })
+ }
+
+ else {
+ this.createTranscript();
+ }
}
}
}
+ saveTranscription() {
+ const txtPath = this.path.slice(0, -4)+'.txt'
+ fs.writeFile(txtPath, this.transcription + this.potentialTranscription, (err) => {
+ if(err) console.log(err);
+ })
+ }
+
}
// Recording model
},
computed: {
transcription: function() {
- if(this.hovering) return this.hovering.transcription;
+ if(this.hovering) return this.hovering.transcription + this.hovering.potentialTranscription;
else return '';
}
},
fs.unlinkSync(filepath.slice(0, -4)+'.txt')
this.recordings = this.recordings.filter((rec) => {return rec.path !== filepath})
+ this.hovering = null
}
}
})
}
}
+// Receive recordings from main process
+
ipcRenderer.on('add-recording', (event, filename) => {
- RecordingsModel.recordings.unshift(new Recording(filename))
- RecordingsModel.fuzzyRecordings = RecordingsModel.fuzzyRecordings.filter((recording) => {
- return recording.path !== filename
- })
+ const fuzzy = RecordingsModel.fuzzyRecordings.filter(recording => recording.path === filename)[0]
+ RecordingsModel.recordings.unshift(new Recording(filename, fuzzy.transcription + fuzzy.potentialTranscription))
+
+ RecordingsModel.fuzzyRecordings = RecordingsModel.fuzzyRecordings.filter((recording) => {
+ return recording.path !== filename
+ })
})
ipcRenderer.on('fuzzy-recording', (event, filename) => {
RecordingsModel.fuzzyRecordings.unshift(new FuzzyRecording(filename))
})
+ipcRenderer.on('fuzzy-transcript', (event, filename, data) => {
+
+ let isFuzzy = true
+
+ console.log('fuzzy data ' + data + ' at ' + filename)
+ let target = RecordingsModel.fuzzyRecordings.filter(recording => recording.path === filename)
+
+ console.log(target)
+ if(target.length < 1) {
+ target = RecordingsModel.recordings.filter(recording => recording.path === filename)
+ isFuzzy = false
+ }
+
+ console.log(target[0])
+
+ if(isFuzzy) {
+
+ let transcript = target[0].transcription
+ let potentialTranscript = ''
+
+ data.map(result => {
+
+ if(result.is_final) {
+ transcript += result.alternatives[0].transcript
+ }
+
+ else {
+ potentialTranscript += result.alternatives[0].transcript
+ }
+ })
+
+ target[0].transcription += transcript
+ target[0].potentialTranscription = potentialTranscript
+ }
+
+ else {
+
+ data.map(result => {
+
+ if(result.is_final) {
+ target[0].transcription += result.alternatives[0].transcript
+ }
+ })
+
+ target[0].saveTranscription()
+ }
+})
+
// Close window and stop recording
const quit = function() {
ipcRenderer.send('stop-recording')
--- /dev/null
+const EventEmitter = require('events');
+const speech = require('@google-cloud/speech');
+
+const client = new speech.SpeechClient();
+
+const languageCode = "fr-CA";
+
+exports.setLanguage = function(code) {
+ languageCode = code;
+}
+
+const request = {
+ config: {
+ encoding: 'LINEAR16',
+ sampleRateHertz: 44100,
+ languageCode: languageCode
+ },
+ interimResults: true
+};
+
+exports.Transcripter = class Transcripter extends EventEmitter {
+ constructor() {
+ super()
+ this.stream = null;
+ this.dataBuffer = Buffer.alloc(0);
+ }
+
+ start() {
+ this.stream = client.streamingRecognize(request)
+ .on('error', (err) => {
+ this.emit('error', err);
+ })
+ .on('data', (data) => {
+ if(data.error)
+ this.emit('error', data.error);
+ else if(data.results)
+ this.emit('data', data.results);
+ });
+ }
+
+ stop() {
+ this.stream.end();
+ this.stream = null;
+ this.dataBuffer = Buffer.alloc(0);
+ }
+
+ putData(data) {
+ this.dataBuffer = Buffer.concat([this.dataBuffer, data]);
+
+ if(this.dataBuffer.length > 882) {
+ this.stream.write(this.dataBuffer);
+ this.dataBuffer = Buffer.alloc(0);
+ }
+ }
+}
<div style="overflow: auto; padding-top: 36px; z-index:-10;">
<table class="table table-hover">
<tbody>
- <tr v-for = "recording in fuzzyRecordings">
+ <tr v-for = "recording in fuzzyRecordings" v-on:mouseenter = "hovering = recording;" v-on:mouseleave = "hovering = null;">
<td><span class="glyphicon glyphicon-record text-danger"></span></td>
<td>{{recording.timestamp.toLocaleString()}}</td>
<td></td>