Django中的Google TTS:从base64字符串以JavaScript创建音频文件

时间:2018-08-30 23:55:24

标签: javascript django google-cloud-platform base64 google-text-to-speech

我目前在我的Django视图之一中使用Google's TTS Python API“ synthesize_text”函数。

def synthesize_text(text):
    """Synthesizes speech from the input string of text."""
    from google.cloud import texttospeech
    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.types.VoiceSelectionParams(
        language_code='en-US',
        ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)

    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    # Removing this because I do not care about writing the audio file
    # ----------------------------------------------------
    '''
    with open('output.mp3', 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "output.mp3"')
    '''
    # ----------------------------------------------------
    # instead return the encoded audio_content to decode and play in Javascript
    return response.audio_content


def my_view(request):
    test_audio_content = synthesize_text('Test audio.')
    return render('my_template.html', {'test_audio_content': test_audio_content})

我对“ synthesize_text”函数所做的唯一更改是,我返回了audio_content而不是将其写到音频文件中。这是因为我不在乎存储文件,而是只想使用Javascript在我的模板中播放它。 Google声称他们在base64中对audio_content进行了编码:“ Cloud Text-to-Speech API允许您将单词和句子转换为以base64编码的自然人类语音音频数据。然后,您可以将音频数据转换为可播放的音频文件,例如MP3,解码base64数据。”因此,我尝试按照建议的here使用以下代码创建和播放音频文件:

<!-- my_template.html -->

<script>
var audio_content = "{{ test_audio_content }}";
var snd = new Audio("data:audio/mp3;base64," + audio_content);
console.log(snd);
snd.play();
</script>

但是出现以下错误:

Uncaught (in promise) DOMException: Failed to load because no supported source was found.

我注销了audio_content,它以b&#39;ÿóDÄH开头。.不确定那是否是base64。 我也尝试通过以下方式解码audio_content:

var decoded_content = window.atob(audio_content);

这也给了我一个错误,声称它不是base64。

1 个答案:

答案 0 :(得分:1)

根据您的示例:

  

The response's audio_content is binary

这意味着您需要先将结果编码为base64,然后才能使用它:

import base64
...
return base64.b64encode(response.audio_content).decode('ascii'))

然后,这应该可以完全按照您的意图使用JS代码段。