简体   繁体   English

如何使用 javascript 中的 pcm 文件(.pcm)绘制波形?

[英]how to draw an waveform using pcm file(.pcm) in javascript?

Do I convert pcm to mp3?我可以将 pcm 转换为 mp3 吗? I do test using wavesurfer.js to draw an waveform using mp3 and it's done.我使用 wavesurfer.js 进行测试,使用 mp3 绘制波形并完成。 But I don't know how to use pcm.但我不知道如何使用 pcm。 Here is code.这是代码。

//javascript
    var wavesurfer = WaveSurfer.create({
    container: '#waveform',
    scrollParent: true,
    waveColor: 'violet',
    progressColor: 'purple'
});

wavesurfer.load('/audio/1.mp3'); // I want to use .pcm insted of .mp3

wavesurfer.on('ready', function () {
    wavesurfer.play();
});

// html
  <div id="waveform">waveform</div>
  <button onclick="wavesurfer.play()">Play</button>
  <button onclick="wavesurfer.pause()">Pause</button>
  <button onclick="wavesurfer.stop()">Stop</button>
  <button onclick="wavesurfer.playPause()">Play/Pause</button>

For anyone landing here from Google:对于从谷歌登陆这里的任何人:

Once your file is loaded (inside the wavesurfer.on('ready'.. function) you can call the wavesurfer.exportPCM() , something like this:加载文件后(在wavesurfer.on('ready'..函数内),您可以调用wavesurfer.exportPCM() ,如下所示:

wavesurfer.on('ready', () => {
     wavesurfer.exportPCM().then(PCMData => {
          console.log(PCMData);
     });
});

In the log you will get that data.在日志中,您将获得该数据。 Once you get that data you can save it on your server, and the next time your page loads, you can pass it as the second argument to your wavesufer.load(url, PCMData) , and you will immediately see the waveform.获得该数据后,您可以将其保存在服务器上,下次加载页面时,您可以将其作为第二个参数传递给您的wavesufer.load(url, PCMData) ,您将立即看到波形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM