问题描述
我正在尝试在 reactjs 中播放 .ulaw 格式,我没有找到任何直接播放 ulaw 文件的方法。所以我尝试通过使用 wavfile 插件将 ulaw 格式转换为 wav。但是转换后它播放不同的噪音,我无法确定实际错误在哪里。
ulaw 文件详情:
采样率 8000
比特率 64
单声道
import voice from '../src/app/components/voice.ulaw'
const WaveFile = require('wavefile').WaveFile;
let wav = new WaveFile();
const playUlawFile = () => {
fetch(voice)
.then(r => r.text())
.then(text => Buffer.from(text,"utf-8").toString("base64"))
.then(result =>{
wav.bitDepth = '128000'
wav.fromScratch(1,8000,'64',result);
wav.fromMuLaw();
wav.toSampleRate(64000);
return wav;
}).then(wav => {
audio = new Audio(wav.toDataURI())
return audio;
}).then(audio => {
audio.play();
})
}
解决方法
以下更改对我有用,仍然存在一些噪音,但可以产生 80% 匹配的声音。
const readFile = () => {
fetch(voice)
.then(r => r.text())
.then(text => btoa(unescape(encodeURIComponent(text))) )
.then(result =>{
wav.fromScratch(1,18000,'8m',Buffer.from(result,"base64"));
wav.fromMuLaw(32);
wav.toSampleRate(64000,{method: "sinc"});
return wav;
}).then(wav => {
audio = new Audio(wav.toDataURI())
return audio;
}).then(audio => {
audio.play();
})
}