reactjs如何将ulaw格式文件转为wav格式 [英] How to convert ulaw format file to wav format reactjs

查看:44
本文介绍了reactjs如何将ulaw格式文件转为wav格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 reactjs 中播放 .ulaw 格式,我没有找到任何直接播放 ulaw 文件的方法.所以我尝试通过使用 wavfile 插件将 ulaw 格式转换为 wav.但是转换后它播放不同的噪音,我无法确定实际错误在哪里.

I am trying to play .ulaw format in reactjs, I didn't find any straight forward way to play the ulaw file. So I tried to convert ulaw format to wav, by using the wavfile plugin. But after conversion it playing different noise, I am not able to identify where is the actual error.

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% 的匹配声音.

Below changes worked for me, still some noise is there, but it can produce 80% matching sound.

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格式文件转为wav格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆