简体   繁体   English

用js获取mp3文件频率数据

[英]Getting mp3 file frequency data with js

Is there a way to get mp3 data (its frequencies) before playing it?有没有办法在播放之前获取 mp3 数据(它的频率)? I don't have to get it all, mind you - it would be enough if I could get it to give me just the frame data for 5 seconds "into the future" (a frame that hasn't been played yet).请注意,我不必全部掌握 - 如果我能得到它就足以给我“进入未来”5 秒的帧数据(尚未播放的帧)。 I also need this to be client/browser-side, and not server-side.我还需要它是客户端/浏览器端,而不是服务器端。 Is there a way to do this?有没有办法做到这一点?

You don't have to stream the audio data from the WebAudioAPI to the output source.您不必 stream 将音频数据从 WebAudioAPI 传输到 output 源。 This is the node.network they usually use in tutorials:这是他们通常在教程中使用的 node.network:

    context = new AudioContext();
    src = context.createMediaElementSource(source);
    analyser = context.createAnalyser();
    var listen = context.createGain();

    src.connect(listen);
    listen.connect(analyser);
    analyser.connect(context.destination);

if you disconnect the context.destination, you should get the data, but muted.如果你断开 context.destination,你应该得到数据,但被静音了。 For a code like this:对于这样的代码:

    context = new AudioContext();
    src = context.createMediaElementSource(source);
    analyser = context.createAnalyser();
    var listen = context.createGain();

    src.connect(listen);
    listen.connect(analyser);

then you can run this code on two same audio files, each with a different timestamp, and save the data from the silent one.然后您可以在两个相同的音频文件上运行此代码,每个文件具有不同的时间戳,并保存无声文件中的数据。

As you mentioned already the AnalyzerNode that is part of the Web Audio API is meant for real-time usage.正如您已经提到的, AnalyzerNode是 Web 音频 API 的一部分,用于实时使用。

Therefore I would recommend using Meyda .因此我建议使用Meyda Meyda itself relies on fftjs for computing the spectrum when used in a offline scenario. Meyda 本身在离线场景中使用时依赖于fftjs来计算频谱。

Here is a little snippet which would log the amplitudespectrum for every block of 512 samples without any overlap for the first 5 seconds of an mp3.这是一个小片段,它将记录 512 个样本的每个块的振幅频谱,在 mp3 的前 5 秒内没有任何重叠。

const Meyda = require('meyda');

fetch('/my.mp3')
    .then((response) => response.arrayBuffer())
    .then((arrayBuffer) => {
        // It's of course also possible to re-use an existing
        // AudioContext to decode the mp3 instead of creating
        // a new one here.
        offlineAudioContext = new OfflineAudioContext({
            length: 1,
            sampleRate: 44100
        });

        return offlineAudioContext.decodeAudioData(arrayBuffer);
    })
    .then((audioBuffer) => {
        const signal = new Float32Array(512);

        for (let i = 0; i < audioBuffer.sampleRate * 5; i += 512) {
            audioBuffer.copyFromChannel(signal, 0, i);

            console.log(Meyda.extract('amplitudeSpectrum', signal));
        }
    });

Is there a way to get mp3 data (its frequencies) before playing it?有没有办法在播放之前获取 mp3 数据(它的频率)? I don't have to get it all, mind you - it would be enough if I could get it to give me just the frame data for 5 seconds "into the future" (a frame that hasn't been played yet).请注意,我不必全部获取 - 如果我能得到它,只为我提供“未来”5 秒的帧数据(尚未播放的帧)就足够了。 I also need this to be client/browser-side, and not server-side.我还需要它是客户端/浏览器端,而不是服务器端。 Is there a way to do this?有没有办法做到这一点?

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

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