def textToSpeech(text): wdc = WDCService('TS') text_to_speech = TextToSpeech(username=wdc.service.user, password=wdc.service.password) #print text_to_speech.voices() module_dir = os.path.dirname(__file__) filename = 'tts.wav' file_path = os.path.join(module_dir, '../static/', filename) with open(file_path, 'wb+') as audio_file: audio_file.write(text_to_speech.synthesize(text))
class SpeechText(object): def __init__(self): self.STT = SpeechToText( username='******', password='******') self.TTS = TextToSpeech( username='******', password='******') def transcribe_audio(self, audio_file): return self.STT.recognize( audio_file, content_type='audio/wav' )['results'][0]['alternatives'][0]['transcript'] def speak_text(self, text): return self.TTS.synthesize(text, accept='audio/wav')
def textToSpeech(results): #Init the service cred = json.load( open('credentials' + os.sep + 'watson_credentials.json', 'r'))['tts'] text_to_speech = TextToSpeech(url=cred['url'], iam_apikey=cred['iam_apikey']) #Translate the results to text #{'coins': {}, 'unknown': 0, 'total': 0.0} textToSay = "" if len(results['coins']) == 0: textToSay = "No coins have been found" else: textToSay = "Total Money is: " + str(results['total']) + "." if results['total'] > 0: textToSay += "The coins found have been:" for k, v in results['coins'].iteritems(): textToSay += str(v) if v > 1: textToSay += " coins " else: textToSay += " currency " if k == 'euro1': textToSay += "un euro, " elif k == 'euro2': textToSay += "dos euros, " elif k == 'cent1': textToSay += "un céntimo, " elif k == 'cent2': textToSay += "dos céntimos, " elif k == 'cent10': textToSay += "diez céntimos, " elif k == 'cent5': textToSay += "cinco céntimos, " elif k == 'cent50': textToSay += "cincuenta céntimos, " if results['unknown'] != 0: textToSay += "In addition, They have not been identified " + str( results['unknown']) if results['unknown'] % 2 == 0: textToSay += " monedas." else: textToSay += " moneda." #Save and play the audio speaker = pyaudio.PyAudio() data = text_to_speech.synthesize(text=textToSay, voice='en-GB_KateVoice', accept='audio/wav') wf = wave.open('.tempAudio.wav', 'wb') wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(22050) wf.writeframes(data) wf.close() wf = wave.open('.tempAudio.wav', 'rb') data = wf.readframes(1024) stream = speaker.open(format=pyaudio.get_format_from_width( wf.getsampwidth()), channels=1, rate=wf.getframerate(), output=True) while len(data) > 0: stream.write(data) data = wf.readframes(1024) # stop stream (4) stream.stop_stream() stream.close() wf.close() os.remove('.tempAudio.wav') return None
import json from os.path import join, dirname from watson_developer_cloud import TextToSpeechV1 as TextToSpeech text_to_speech = TextToSpeech(username='******', password='******') print(json.dumps(text_to_speech.voices(), indent=2)) with open(join(dirname(__file__), '../resources/output.wav'), 'wb') as audio_file: audio_file.write(text_to_speech.synthesize('Hello world!'))
def textToSpeech(results): #Init the service cred = json.load(open('credentials'+os.sep+'watson_credentials.json', 'r'))['tts'] text_to_speech = TextToSpeech(url=cred['url'], username=cred['username'], password=cred['password']) #Translate the results to text #{'coins': {}, 'unknown': 0, 'total': 0.0} textToSay = "" if len(results['coins']) == 0: textToSay = "No se han encontrado monedas." else: textToSay = "El dinero total es: " + str(results['total']) + "." if results['total'] > 0: textToSay += "Las monedas encontradas han sido:" for k,v in results['coins'].iteritems(): textToSay += str(v) if v > 1: textToSay += " monedas de " else: textToSay += " moneda de " if k == 'euro1': textToSay += "un euro, " elif k == 'euro2': textToSay += "dos euros, " elif k == 'cent1': textToSay += "un céntimo, " elif k == 'cent2': textToSay += "dos céntimos, " elif k == 'cent10': textToSay += "diez céntimos, " elif k == 'cent5': textToSay += "cinco céntimos, " elif k == 'cent50': textToSay += "cincuenta céntimos, " if results['unknown'] != 0: textToSay += "Además, no se han podido identificar " + str(results['unknown']) if results['unknown'] % 2 == 0: textToSay += " monedas." else: textToSay += " moneda." #Save and play the audio speaker = pyaudio.PyAudio() data = text_to_speech.synthesize( text=textToSay, voice='es-ES_EnriqueVoice', accept='audio/wav') wf = wave.open('.tempAudio.wav', 'wb') wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(22050) wf.writeframes(data) wf.close() wf = wave.open('.tempAudio.wav', 'rb') data = wf.readframes(1024) stream = speaker.open(format=pyaudio.get_format_from_width(wf.getsampwidth()), channels=1, rate=wf.getframerate(), output=True) while len(data) > 0: stream.write(data) data = wf.readframes(1024) # stop stream (4) stream.stop_stream() stream.close() wf.close() os.remove('.tempAudio.wav') return None
import json from os.path import join, dirname from watson_developer_cloud import TextToSpeechV1 as TextToSpeech text_to_speech = TextToSpeech(username='******', password='******') print(json.dumps(text_to_speech.voices(), indent=2)) with open(join(dirname(__file__), '../resources/output.wav'), 'wb') as audio_file: audio_file.write(text_to_speech.synthesize('Hello world!'))