You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除Google Text-To-Speech MULAW音频WAV头解决Twilio播放杂音问题

解决Google Text-to-Speech MULAW音频WAV头导致Twilio播放杂音的问题

核心解决方案

Google返回的MULAW格式音频实际包含标准WAV文件头(44字节),而Twilio需要的是裸MULAW音频流,直接移除前44字节即可解决开头杂音问题。

修改后的PHP代码

use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

$textToSpeechClient = new TextToSpeechClient();

$input = new SynthesisInput();
$input->setText('This is my test audio');
$voice = new VoiceSelectionParams();
$voice->setLanguageCode('en-US');
$audioConfig = new AudioConfig();
$audioConfig->setAudioEncoding(AudioEncoding::MULAW);
$audioConfig->setSampleRateHertz(8000);

$resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);

$audioContent = $resp->getAudioContent();
// 移除44字节的WAV文件头
$rawMulawAudio = substr($audioContent, 44);
$TheBase64Audio = base64_encode($rawMulawAudio);

原理说明

标准WAV文件头包含文件标识、格式信息、声道数等元数据,共44字节。当Twilio接收到带WAV头的MULAW数据时,会将头字节当作音频数据解析,从而产生开头的点击杂音。截取掉这部分头后,剩下的就是纯MULAW编码的音频流,符合Twilio的要求。

内容的提问来源于stack exchange,提问作者HotsyncUK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:32:46