我目前正在尝试制作一个网络编辑器,使用户可以轻松地调整其音频文件的基本设置,作为一个插件,我已经集成了waveurfer.js,因为它具有非常整洁的跨浏览器波形解决方案。
在为功能性列出了必不可少的 list 之后,我决定剪切和粘贴对于使此产品正常运行至关重要,但是在花费了数小时的时间试图弄清楚如何在现有库中实现这一点之后,甚至开始重建从头开始wavesurfer.js功能,以了解我尚未成功的逻辑。
我的问题是,是否有人可以给我一些有关如何开始构建剪切和粘贴功能的提示,甚至是一个将不胜感激的示例。
提前致谢!
waveurfer插件:
http://wavesurfer-js.org
采摘网页编辑器
http://plucked.de
编辑解决方案(实例是waveurfer对象。):
function cut(instance){
var selection = instance.getSelection();
if(selection){
var original_buffer = instance.backend.buffer;
var new_buffer = instance.backend.ac.createBuffer(original_buffer.numberOfChannels, original_buffer.length, original_buffer.sampleRate);
var first_list_index = (selection.startPosition * original_buffer.sampleRate);
var second_list_index = (selection.endPosition * original_buffer.sampleRate);
var second_list_mem_alloc = (original_buffer.length - (selection.endPosition * original_buffer.sampleRate));
var new_list = new Float32Array( parseInt( first_list_index ));
var second_list = new Float32Array( parseInt( second_list_mem_alloc ));
var combined = new Float32Array( original_buffer.length );
original_buffer.copyFromChannel(new_list, 0);
original_buffer.copyFromChannel(second_list, 0, second_list_index)
combined.set(new_list)
combined.set(second_list, first_list_index)
new_buffer.copyToChannel(combined, 0);
instance.loadDecodedBuffer(new_buffer);
}else{
console.log('did not find selection')
}
}
最佳答案
阅读此answer建议您可以创建一个要复制的音频片段大小的空AudioBuffer(大小=以秒为单位的长度⨉采样率),然后用片段中的数据填充其 channel 数据。
所以代码可能像这样:
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!