Cut and Paste audio using web audio api and wavesurfer.js

前端 未结 2 1507
小鲜肉
小鲜肉 2021-02-01 11:14

I am currently trying to make a web editor allowing users to easily adjust basic settings to their audio files, as a plugin I\'ve integrated wavesurfer.js as it has a very neat

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 11:38

    Reading this answer suggests you can create an empty AudioBuffer of the size of the audio segment you want to copy (size = length in seconds ⨉ sample rate), then fill its channel data with the data from the segment.

    So the code might be like this:

    var originalBuffer = wavesurfer.backend.buffer;
    var emptySegment = wavesurfer.backend.ac.createBuffer(
        originalBuffer.numberOfChannels,
        segmentDuration * originalBuffer.sampleRate,
        originalBuffer.sampleRate
    );
    for (var i = 0; i < originalBuffer.numberOfChannels; i++) {
        var chanData = originalBuffer.getChannelData(i);
        var segmentChanData = emptySegment.getChannelData(i);
        for (var j = 0, len = chanData.length; j < len; j++) {
            segmentChanData[j] = chanData[j];
        }
    }
    
    emptySegment; // Here you go!
                  // Not empty anymore, contains a copy of the segment!
    

提交回复
热议问题