reactjs如何将ulaw格式文件转为wav格式

问题描述

我正在尝试在 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();
        })
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...