First iteration of live speech recognition
authorCedric Godin <cedric.godin@usherbrooke.ca>
Fri, 2 Feb 2018 15:47:39 +0000 (10:47 -0500)
committerCedric Godin <cedric.godin@usherbrooke.ca>
Fri, 2 Feb 2018 15:47:39 +0000 (10:47 -0500)
recordings.js
resources/js/recordings_model.js
stream-to-text.js [new file with mode: 0644]
views/recordings.html

index 7b6fb3d3897e1f779344f4b20ba561f5a74737d2..fad75790bceadb8ab0db8bee938a675dd13a59e3 100644 (file)
@@ -4,6 +4,7 @@ const path = require('path')
 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...
@@ -79,6 +80,16 @@ class AudioRecorder {
     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) {
@@ -101,17 +112,22 @@ class AudioRecorder {
 
     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()
     }
   }
 
@@ -121,6 +137,8 @@ class AudioRecorder {
     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)
index 2b5f2305a192cdb21b64234443616fc9e62f71e0..bec840aa2d67a831676f394a4ea2d75dcab674d1 100644 (file)
@@ -13,13 +13,15 @@ class FuzzyRecording {
     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
@@ -30,9 +32,21 @@ class Recording {
     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();
+    }
 
   }
 
@@ -118,6 +132,13 @@ class Recording {
     }
   }
 
+  saveTranscription() {
+      const txtPath = this.path.slice(0, -4)+'.txt'
+      fs.writeFile(txtPath, this.transcription + this.potentialTranscription, (err) => {
+          if(err) console.log(err);
+      })
+  }
+
 }
 
 // Recording model
@@ -132,7 +153,7 @@ const RecordingsModel = new Vue({
   },
   computed: {
       transcription: function() {
-          if(this.hovering) return this.hovering.transcription;
+          if(this.hovering) return this.hovering.transcription + this.hovering.potentialTranscription;
           else return '';
       }
   },
@@ -144,6 +165,7 @@ const RecordingsModel = new Vue({
       fs.unlinkSync(filepath.slice(0, -4)+'.txt')
 
       this.recordings = this.recordings.filter((rec) => {return rec.path !== filepath})
+      this.hovering = null
     }
   }
 })
@@ -204,12 +226,16 @@ const recordControl = function() {
   }
 }
 
+// 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) => {
@@ -217,6 +243,54 @@ 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')
diff --git a/stream-to-text.js b/stream-to-text.js
new file mode 100644 (file)
index 0000000..04dbf0a
--- /dev/null
@@ -0,0 +1,55 @@
+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);
+        }
+    }
+}
index bcbdf7571dc459ebbb75c693bd2904f2f2372d43..e3ffa5b40aa02b19f2ce5a90d6a741138c71f7f2 100644 (file)
@@ -64,7 +64,7 @@
                   <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>