如何将ulaw格式文件转换为wav格式reactjs
How to convert ulaw format file to wav format reactjs
我正在尝试在 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();
})
}
我正在尝试在 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();
})
}