我正在尝试在给定的浏览器上播放Google Cloud TTS的音频输出。我可以成功地将TTS输出保存为wav文件,但我想要做的是从客户端播放字节数组。现在,当我播放音频字节数组时,我得到的只是静音。
根据谷歌云文档,我需要在播放音频之前将base64编码的文本转换为二进制文件(https://cloud.google.com/text-to-speech/docs/base64-decoding),所以我在下面做了:
为了将base64转换为二进制,我提到:Python converting from base64 to binary
from google.cloud import texttospeech
import base64
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
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.LINEAR16)
response = client.synthesize_speech(input_text, voice, audio_config)
print(type(response.audio_content))
# The response's audio_content is binary.
audio = response.audio_content
decoded = base64.decodebytes(audio)
decoded_audio = "".join(["{:08b}".format(x) for x in decoded])
with open('static/playback.wav', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
return decoded_audio
我通过flask_socketio连接传递了“decoding_audio”二进制音频数据,然后转到我的javascript:
socket.on('audio', function(msg) {
playWave(msg);
})
然后我试图通过playWave函数播放音频(我从中得到了这个:Play wav file as bytes received from server
function playWave(byteArray) {
console.log(byteArray.length)
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var myAudioBuffer = audioCtx.createBuffer(1, byteArray.length, 8000);
var nowBuffering = myAudioBuffer.getChannelData(0);
for (var i = 0; i < byteArray.length; i++) {
nowBuffering[i] = byteArray[i];
}
var source = audioCtx.createBufferSource();
source.buffer = myAudioBuffer;
source.connect(audioCtx.destination);
source.start();
}
我不确定为什么我得到的唯一音频输出是静音。我不确定我是否正在解码Base64编码的文本(我将其转换为LINEAR16,应该是wav),然后我将其转换为二进制字节数组。
或者我不确定我的采样率或playWave功能是否正确。有没有人有过如何从客户端浏览器端播放Base64编码音频的经验?