简体   繁体   English

如何使用 javascript 将 mp3 转换为声波图像

[英]How to convert mp3 to the sound wave image using javascript

I have a requirement in my project to convert mp3 audio to an image containing the waves for that mp3.我的项目需要将 mp3 音频转换为包含该 mp3 波形的图像。 Something like就像是

在此处输入图像描述

Can I do this using javascript(specially nodejs)?我可以使用javascript(特别是nodejs)来做到这一点吗? Kindly help请帮助

Here is a short JavaScript program I wrote.这是我编写的一个简短的 JavaScript 程序。 When you input a mp3 file, it decodes it, and displays the amplitudes of the waveform on a canvas.当您输入 mp3 文件时,它会对其进行解码,并在 canvas 上显示波形的幅度。 The canvas looks a bit squished for longer files, but that could be changed later with css. canvas 对于更长的文件看起来有点挤压,但稍后可以使用 css 进行更改。 Try a smaller file for best results (like 5-10 second duration audio file).尝试使用较小的文件以获得最佳效果(例如 5-10 秒持续时间的音频文件)。

Note: This works in the browser, and I don't know about Node.注意:这在浏览器中有效,我不了解 Node.js。 This uses the built-in browser features HTML Canvas Element, and the Web Audio API.这使用内置浏览器功能 HTML Canvas 元素和 Web 音频 ZDB974238714CA8DE64FZACE. I expect Node has comparable features, or you could find a library that makes an image file with commands similar to the HTML Canvas 2d Context.我希望 Node 具有类似的功能,或者您可以找到一个使用类似于 HTML Canvas 2d 上下文的命令制作图像文件的库。 Good luck!祝你好运!

 const margin = 10; const chunkSize = 50; const input = document.querySelector("input"); const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); const ac = new AudioContext(); const {width, height} = canvas; const centerHeight = Math.ceil(height / 2); const scaleFactor = (height - margin * 2) / 2; async function drawToCanvas() { const buffer = await input.files[0].arrayBuffer(); const audioBuffer = await ac.decodeAudioData(buffer); const float32Array = audioBuffer.getChannelData(0); const array = []; let i = 0; const length = float32Array.length; while (i < length) { array.push( float32Array.slice(i, i += chunkSize).reduce(function (total, value) { return Math.max(total, Math.abs(value)); }) ); } canvas.width = Math.ceil(float32Array.length / chunkSize + margin * 2); for (let index in array) { ctx.strokeStyle = "black"; ctx.beginPath(); ctx.moveTo(margin + Number(index), centerHeight - array[index] * scaleFactor); ctx.lineTo(margin + Number(index), centerHeight + array[index] * scaleFactor); ctx.stroke(); } } input.addEventListener("input", drawToCanvas);
 canvas { width: 90%; height: 400px; }
 <input type="file" accept="audio/mp3"> <br> <canvas height="1000"></canvas>

You could use wavesurfer.js library that makes waves out of all different kinds of audio formats.您可以使用wavesurfer.js库来制作各种不同类型的音频格式的波形。 I added an example with an mp3 file of how to implement it with HTML and JS :我添加了一个带有mp3文件的示例,说明如何使用HTMLJS实现它:

If you want to add it with npm have a look at this documentation: wavesurfer.js npm doc .如果您想使用npm添加它,请查看此文档: wavesurfer.js npm 文档

 var wavesurfer = WaveSurfer.create({ container: '#waveform', waveColor: 'pink', progressColor: 'lightgreen' }); wavesurfer.load('https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3');
 <div id="waveform"></div> <script src="https://unpkg.com/wavesurfer.js"></script>

Check this npm package waveplayer .检查此 npm package 波形播放器。

Load an audio file and play it加载音频文件并播放

Create a waveplayer.js instance and pass in some (optional) options:创建一个 waveplayer.js 实例并传入一些(可选)选项:

import WavePlayer from 'waveplayer';
 
var wavePlayer = new WavePlayer({
    container: '#waveform',
    barWidth: 4,
    barGap: 1,
    height: 128
});

Load an audio file from a URL and start playback when loading has finished:从 URL 加载音频文件并在加载完成后开始播放:

wavePlayer.load('url-to-some-audio-file.mp3')
    .then(() => waveplayer.play());

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

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