I'm trying to play the audio output of Google Cloud TTS on a given browser. I can successfully save the TTS output as a wav file, but what I want to do is play the byte array from the client side. Right now when I play my audio byte array, all I get is loud static.
According to the google cloud documentation, I need to convert the base64 encoded text to binary before audio can be played (https://cloud.google.com/text-to-speech/docs/base64-decoding), so I've done that below:
For converting base64 to binary, I referred to: 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
I've passed the "decoded_audio" binary audio data through a flask_socketio connection, which then goes to my javascript like so:
socket.on('audio', function(msg) {
playWave(msg);
})
And then I'm trying to play the audio through the function playWave (which I got from this: 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();
}
I'm not really sure why the only audio output I'm getting is loud static. I'm not sure if I'm decoding the Base64 encoded text incorrectly (I'm converting it to LINEAR16, which should be for wav), and then I'm converting it to a binary byte array.
Or I'm not sure if my sampling rate or playWave function isn't right. Does anyone have experience with how to play Base64 encoded audio from the client browser side?