import { exec } from 'child_process'
import kill from 'tree-kill'

export default class Speech {

  constructor(opts) {
    console.log('Init Speech Plugin.')
    this.speechProc = null
    // configure
    this.configureVoice({
      processor: 'say',
      speed: 130,
      voice: 'Anna'
    })
  }

  mqttInterface() {
    return ['say','stopTalking','configureVoice']
  }

  configureVoice(opts) {
    this.voice = (opts.voice) ? opts.voice : 'Anna'
    this.processor = (opts.processor) ? opts.processor : 'say'
    this.language = (opts.language) ? opts.language : 'en'
    this.speed = (opts.speed) ? opts.speed : 130
    this.call = this.processor +
                ' -v ' + this.voice +
                ((this.processor === 'say') ? ' -r ' : ' -s ') + this.speed
  }

  say(txt) {
    this.speechProc = exec(this.call + ' "' + txt + '"')
  }

  stopTalking() {
    kill(this.speechProc)
  }
}

@@ -780,24 +780,47 @@
  say(txt) %7B%0A
+    this.stopTalking()%0A
     this.spe
@@ -883,24 +883,75 @@
 Talking() %7B%0A
+    if(this.speechProc && this.speechProc.pid) %7B%0A  
     kill(thi
@@ -962,16 +962,26 @@
 eechProc
-)
+.pid)%0A    %7D
 %0A  %7D%0A%7D%0A

Fix stopTalking method.