Update genericDriver.py

This commit is contained in:
chrys87 2017-03-03 14:03:22 +01:00 committed by GitHub
parent b89753fce8
commit d4d1cb2dca

View File

@ -10,7 +10,6 @@ from threading import Thread
from queue import Queue, Empty from queue import Queue, Empty
import time import time
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import subprocess
class speakQueue(Queue): class speakQueue(Queue):
def clear(self): def clear(self):
@ -25,12 +24,12 @@ class driver():
self.proc = None self.proc = None
self.speechThread = Thread(target=self.worker) self.speechThread = Thread(target=self.worker)
self.textQueue = speakQueue() self.textQueue = speakQueue()
self.volume = None self.volume = ''
self.rate = None self.rate = ''
self.pitch = None self.pitch = ''
self.module = None self.module = ''
self.language = None self.language = ''
self.voice = None self.voice = ''
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.minVolume = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMinVolume') self.minVolume = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMinVolume')
@ -76,9 +75,12 @@ class driver():
return return
self.clear_buffer() self.clear_buffer()
if self.proc: if self.proc:
self.proc.terminate() try:
self.proc.kill() self.proc.terminate()
self.proc = None except:
self.proc.kill()
finally:
self.proc = None
def setCallback(self, callback): def setCallback(self, callback):
print('SpeechDummyDriver: setCallback') print('SpeechDummyDriver: setCallback')
@ -87,6 +89,7 @@ class driver():
if not self._isInitialized: if not self._isInitialized:
return return
self.textQueue.clear() self.textQueue.clear()
def setVoice(self, voice): def setVoice(self, voice):
if not self._isInitialized: if not self._isInitialized:
return return
@ -95,12 +98,12 @@ class driver():
def setPitch(self, pitch): def setPitch(self, pitch):
if not self._isInitialized: if not self._isInitialized:
return return
self.pitch = self.minPitch + pitch * (self.maxPitch - self.minPitch ) self.pitch = str(self.minPitch + pitch * (self.maxPitch - self.minPitch ))
def setRate(self, rate): def setRate(self, rate):
if not self._isInitialized: if not self._isInitialized:
return return
self.rate = self.minRate + rate * (self.maxRate - self.minRate ) self.rate = str(self.minRate + rate * (self.maxRate - self.minRate ))
def setModule(self, module): def setModule(self, module):
if not self._isInitialized: if not self._isInitialized:
@ -115,7 +118,7 @@ class driver():
def setVolume(self, volume): def setVolume(self, volume):
if not self._isInitialized: if not self._isInitialized:
return return
self.volume = self.minVolume + volume * (self.maxVolume - self.minVolume ) self.volume = str(self.minVolume + volume * (self.maxVolume - self.minVolume ))
def worker(self): def worker(self):
while True: while True:
@ -130,9 +133,12 @@ class driver():
for key in ['volume','module','language','voice','pitch','rate','text']: for key in ['volume','module','language','voice','pitch','rate','text']:
if not key in utterance: if not key in utterance:
utterance[key] = '' utterance[key] = ''
if not utterance[key]: if not isinstance(utterance[key],str):
utterance[key] = '' utterance[key] = ''
utterance = utterance.copy() if key == 'text':
if utterance[key] == '':
continue
popenSpeechCommand = self.speechCommand popenSpeechCommand = self.speechCommand
popenSpeechCommand = popenSpeechCommand.replace('fenrirVolume', str(utterance['volume'] ).replace('"','')) popenSpeechCommand = popenSpeechCommand.replace('fenrirVolume', str(utterance['volume'] ).replace('"',''))
popenSpeechCommand = popenSpeechCommand.replace('fenrirModule', str(utterance['module']).replace('"','')) popenSpeechCommand = popenSpeechCommand.replace('fenrirModule', str(utterance['module']).replace('"',''))
@ -147,9 +153,9 @@ class driver():
#subprocess.check_call(popenSpeechCommand,shell=True) #subprocess.check_call(popenSpeechCommand,shell=True)
self.proc = Popen(popenSpeechCommand , stdout=PIPE, stderr=PIPE, shell=True) self.proc = Popen(popenSpeechCommand , stdout=PIPE, stderr=PIPE, shell=True)
self.proc.wait() self.proc.wait()
self.proc = None
print(popenSpeechCommand) print(popenSpeechCommand)
print('run',time.time() -s) print('run',time.time() -s)
except Exception as e: except Exception as e:
print('except' + str(e)) print('except' + str(e))
self.proc = None