#!/bin/bash

# Your Google Cloud API key
API_KEY="<YOUR KEY>"

# Google TTS API endpoint with API key as query param
TTS_API_URL="https://texttospeech.googleapis.com/v1/text:synthesize?key=$API_KEY"

# Request payload (set audioEncoding to LINEAR16 for PCM)
read -r -d '' PAYLOAD <<EOF
{
  "input": {
    "text": "Hello, I am jerry who are you."
  },
  "voice": {
    "languageCode": "en-US",
    "name": "en-US-Wavenet-D"
  },
  "audioConfig": {
    "audioEncoding": "LINEAR16"
  }
}
EOF

# Make the request and extract audioContent
AUDIO_BASE64=$(curl -s -X POST "$TTS_API_URL" \
  -H "Content-Type: application/json" \
  -d "$PAYLOAD" | jq -r '.audioContent')

# Decode base64 and save to output.pcm
echo "$AUDIO_BASE64" | base64 --decode > output.pcm

echo "✅ PCM audio saved to: output.pcm"
