Exemple #1
0
def dhvanify(content):
    translator = Translator(os.environ['BING_SPEECH_ACCESS_KEY'])
    audioOutputs = []
    finalText = []
    for idx, val in enumerate(content.newsItems):
        val.body = ("Story #%s: " %(idx)) + val.body + "       "
        finalText.append(val.body)
    for idx, text in enumerate(finalText):
        output = translator.speak(text, "en-US", "Female", "riff-16khz-16bit-mono-pcm")
        time.sleep(1)
        with open("tmp/file%s.wav"%(idx), "wb") as f:
            f.write(output)
        audioOutputs.append(AudioSegment.from_wav("tmp/file%s.wav"%(idx)))
    
    for idx, snippet in enumerate(audioOutputs):
        if(idx == 0):
            finalAudio = snippet
        else:
            finalAudio = finalAudio + snippet
    finalAudio.export("tmp/final.wav", format="wav")
    return "tmp/final.wav"

    
    
    
Exemple #2
0
def tts(text="Hi there"):
    translator = Translator(os.getenv('BING_KEY', ''))
    output = translator.speak(text, "en-US", "JessaRUS", "riff-16khz-16bit-mono-pcm")

    with open('output.wav', 'wb') as f:
        f.write(output)

    CHUNK = 1024

    wf = wave.open('output.wav', 'rb')

    # instantiate PyAudio (1)
    p = pyaudio.PyAudio()

    # open stream (2)
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)

    # read data
    data = wf.readframes(CHUNK)

    # play stream (3)
    while len(data) > 0:
        stream.write(data)
        data = wf.readframes(CHUNK)

    # stop stream (4)
    stream.stop_stream()
    stream.close()

    # close PyAudio (5)
    p.terminate()
Exemple #3
0
def synthesize_speech(recipe_id, steps_json, ing_pos):
    # TODO: Do unhardcode path, it is relative to calling PWD
    wav_root = "./website/static/wav/"

    if not os.path.exists(wav_root + recipe_id):
        tts = Translator('50dbceaa8a424c1c89ea9c33d3a4eec0')
        speech_text = [step_raw['step'] for step_raw in steps_json]
        # TODO: optimize speech
        speech_bin = [
            tts.speak(step_raw, "en-US", "Female", "riff-16khz-16bit-mono-pcm")
            for step_raw in speech_text
        ]
        ing_bin = [
            tts.speak(ing_raw, "en-US", "Female", "riff-16khz-16bit-mono-pcm")
            for ing_raw in ing_pos
        ]

        # create the recipe ID holding the recipe wave files

        try:
            print('creating folder ' + wav_root + recipe_id)
            os.makedirs(wav_root + recipe_id)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        wav_list = []

        for ing_num in xrange(len(ing_pos)):
            fname = 'ing_%d' % ing_num
            wav_list.append('%s' % fname)
            with open("%s%s/%s.wav" % (wav_root, recipe_id, fname), 'w') as f:
                f.write(ing_bin[ing_num])

        for step in xrange(len(speech_text)):
            fname = 'step_%d' % step
            wav_list.append('%s' % fname)
            with open("%s%s/%s.wav" % (wav_root, recipe_id, fname), 'w') as f:
                f.write(speech_bin[step])

        return wav_list
    else:
        return [
            file[:-4] for file in os.listdir('%s%s' % (wav_root, recipe_id))
            if file.endswith(".wav")
        ]
def renderResult(detected):
    objectDetected = detected['description']['captions'][0]['text']
    print(objectDetected)
    translator = Translator('[tts key]')
    output = translator.speak(objectDetected, "en-US", "Female",
                              "riff-16khz-16bit-mono-pcm")
    with open("file.wav", "w") as f:
        f.write(output)
    i = 15
    time.sleep(0.3)
Exemple #5
0
def main():

    # recognized_text = recognize()
    # print(recognized_text)

    # bing = BingSpeechAPI()
    # bing.text_to_speech(text='Can I have some coffee?')

    from bingtts import Translator
    translator = Translator(os.getenv('BING_KEY', ''))
    output = translator.speak("This is a text to speech translation", "en-US",
                              "Female", "riff-16khz-16bit-mono-pcm")
    with open("file.wav", "w") as f:
        f.write(output)
class BingTTS(TTS):
    def __init__(self, lang, config):
        super(BingTTS, self).__init__(lang, config, BingTTSValidator(self))
        self.type = 'wav'
        from bingtts import Translator
        api = self.config.get("api_key")
        self.bing = Translator(api)
        self.gender = self.config.get("gender", "Male")
        self.format = self.config.get("format", "riff-16khz-16bit-mono-pcm")

    def get_tts(self, sentence, wav_file):
        output = self.bing.speak(sentence, self.lang, self.gender, self.format)
        with open(wav_file, "w") as f:
            f.write(output)
        return (wav_file, None)  # No phonemes
Exemple #7
0
class BingTTS(TTS):
    def __init__(self, lang, config):
        super(BingTTS, self).__init__(lang, config, BingTTSValidator(self))
        self.type = 'wav'
        from bingtts import Translator
        self.config = Configuration.get().get("tts", {}).get("bing", {})
        api = self.config.get("api_key")
        self.bing = Translator(api)
        self.gender = self.config.get("gender", "Male")
        self.format = self.config.get("format", "riff-16khz-16bit-mono-pcm")

    def get_tts(self, sentence, wav_file):
        output = self.bing.speak(sentence, self.lang, self.gender,
                                 self.format)
        with open(wav_file, "w") as f:
            f.write(output)
        return (wav_file, None)  # No phonemes
Exemple #8
0
def tts(text):
    translator = Translator('b2efca95d4b74c3ca0d1e5b0729963fe')
    output = translator.speak(text, "en-US", "Male",
                              "riff-16khz-16bit-mono-pcm")
    # print(type(output))
    with open("file.wav", "w") as f:
        f.write(output)
    chunk = 1024
    wf = wave.open(r"file.wav", 'rb')

    p = pyaudio.PyAudio()
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)
    while True:
        data = wf.readframes(chunk)
        if data == "": break
        stream.write(data)
    stream.close()
    p.terminate()
class TextToSpeech():
    def __init__(self, bingTtsKey):
        self.translator = Translator(bingTtsKey)
        self.ttsAudio = {}

    def playAudio(self, audio):
        CHUNK = 1024

        f = io.BytesIO()
        f.write(audio)
        f.seek(0)
        wf = wave.Wave_read(f)

        p = pyaudio.PyAudio()

        stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=wf.getframerate(),
                        output=True)

        data = wf.readframes(CHUNK)

        while data != b'':
            stream.write(data)
            data = wf.readframes(CHUNK)

        stream.stop_stream()
        stream.close()
        p.terminate()

    def Text2Speech(self, text):
        text = text.lower()
        audio = self.ttsAudio.get(text)
        if audio == None:
            print('audio not found')
            audio = self.translator.speak(
                text, "en-AU", "Catherine", "riff-16khz-16bit-mono-pcm")
            self.ttsAudio[text] = audio
        self.playAudio(audio)
Exemple #10
0
def synthesize_speech_one_item(input_string, input_language, voice,
                               output_name, output_dir):
    from bingtts import Translator
    #translator = Translator(BING_SUBSCRIPTION_KEY)
    bing_key = BING_SUBSCRIPTION_KEY
    # bing_key = keyring.get_password("bing_api", "obi")
    if bing_key == None:
        print("Please set BING_SUBSCRIPTION_KEY")
        return
    translator = Translator(bing_key)

    output = translator.speak(input_string, input_language, voice,
                              "audio-16khz-64kbitrate-mono-mp3 ")

    output_filename = output_name.strip().replace(" ", "_")
    output_filename = '{}/{}_{}.mp3'.format(output_dir, output_filename,
                                            input_language)

    with open(output_filename, "wb") as f:
        f.write(output)

    print('Audio content written to file "{}"'.format(output_filename))
Exemple #11
0
    # If our file already exists, just return it so we don't have to do an API call...
    if os.path.isfile(os.path.join(ttsCache, filename)):
        try:
            shutil.copy2(os.path.join(ttsCache, filename), destLoc)
            sys.exit(0)
        except (Exception) as e:
            print("Could not copy cached file: {}".format(e))
            print("Exiting...")
            sys.exit(1)

# Wait to import this until after we check cache to keep things as speedy as possible...
from bingtts import Translator
translator = Translator(ttsApiKey)
try:
    data = translator.speak(
        ttsText.encode('utf-8').decode('latin-1'), ttsLang, ttsVoice,
        ttsFormat)
except (Exception) as e:
    print("Error retrieving speech file: {}".format(e))
    sys.exit(1)

if ttsCache:
    try:
        with open(destLoc, 'wb') as f:
            f.write(data)
    except (Exception) as e:
        print("Couldn't write to file {}: {}".format(destLoc, e))
        sys.exit(1)
    try:
        with open(os.path.join(ttsCache, filename), 'w') as f:
            f.write(data)
if ttsCache:
    # If our file already exists, just return it so we don't have to do an API call...
    if os.path.isfile(ttsCache + '/' + filename):
        try:
            shutil.copy2(ttsCache + '/' + filename, destLoc)
            sys.exit(0)
        except (Exception) as e:
            print "Could not copy cached file:", e
            print "Exiting..."
            sys.exit(1)

# Wait to import this until after we check cache to keep things as speedy as possible...
from bingtts import Translator
translator = Translator(ttsApiKey)
try:
    data = translator.speak(ttsText, ttsLang, ttsGender, ttsFormat)
except (Exception) as e:
    print "Error retrieving speech file:", e
    sys.exit(1)

if ttsCache:
    try:
        with open(destLoc, 'w') as f:
            f.write(data)
    except (Exception) as e:
        print "Couldn't write to file", destLoc, ":", e
        sys.exit(1)
    try:
        with open(ttsCache + '/' + filename, 'w') as f:
            f.write(data)
    except (Exception) as e: