[英]Can I create an audio source node from a fragment of an MP3 file?
我想創建幾個源音頻節點,每個節點包含一個不同的MP3片段。 有沒有辦法,提取文件,從文件的給定部分創建源節點? 目前我有:
var source1 = audioCtx.createBufferSource();
return fetch('speech.mp3')
.then(function (response) {
if (!response.ok) {
throw new Error("HTTP error, status = " + response.status);
}
return response.arrayBuffer();
})
.then(function (buffer) {
return audioCtx.decodeAudioData(buffer);
})
.then(function (decodedData) {
source1.buffer = decodedData;
source1.connect(audioCtx.destination);
});
不過,我想source1
(再source2
和source3
)包含的不同片段speech.mp3
。
由於您閱讀了所有“speech.mp3”,因此可以為mp3文件的各個部分創建多個AudioBufferSourceNode
對象。 我至少可以想到兩種方式。 也許是最簡單的(未經測試):
for (k = 0; k < n; ++k) {
src[k] = new AudioBufferSourceNode(context, decodedData);
src[k].connect(context.destination);
src[k].start(time[k], offset[k], duration[k]);
}
每個src使用相同的AudioBuffer,但是當你調用start()
,你可以從你想要播放的decodeData中指定偏移量, offset[k]
和持續duration[k]
。
或者,您可以將原始decodeData
為許多包含要使用的片段的較小AudioBuffer
對象。 然后每個AudioBufferSourceNode
可以使用該小緩沖區作為其源。 但是,這會浪費空間來創建所有的小緩沖區。
像這樣(未經測試):
for (k = 0; k < n; ++k) {
// Grab the part of the mp3 that we want.
d[k] = decodedData.slice(start[k], end[k]);
b[k] = new AudioBuffer({length: d[k].length, sampleRate: context.sampleRate});
b[k].copyToChannel(d[k], 0, 0);
s[k] = new AudioBufferSourceNode(context, {buffer: b[k]);
// Play back this piece of the mp3 at the appropriate time.
s[k].start(time[k]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.