簡體   English   中英

如何使用Web Audio API將音頻從合成器渲染到緩沖區(PCM值數組)

[英]How to render the audio from a synth to a buffer (array of PCM values) with the Web Audio API

我有一個簡單的合成器,可以播放一段時間的音符:

// Creating audio graph
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);

// Setting parameters
oscillator.type = "sine";
oscillator.frequency.value = 2500;

// Run audio graph
var currentTime = offlineCtx.currentTime;
oscillator.start(currentTime);
oscillator.stop(currentTime + 1);

如何獲得合成器發出的聲音的PCM數據? 我已經設法通過使用decodeAudioData對音頻樣本執行此操作 ,但我無法找到不基於加載樣本的音頻圖形的等效項。

我特別想用OfflineAudioContext渲染音頻圖,因為我只關心盡可能快地檢索PCM數據。

謝謝!

您說您想要使用脫機上下文,然后您實際上不使用脫機上下文。 所以你應該這樣做

var offlineCtx = new OfflineAudioContext(nc, length, rate)

其中nc =通道數, length是樣本數, rate是您要使用的采樣率。

創建圖表,啟動所有內容,然后執行

offlineCtx.startRendering().then(function (buffer) {
  // buffer has the PCM data you want. Save it somewhere, 
  // or whatever
})

(我不確定所有瀏覽器是否支持離線上下文中的promise。如果沒有,請使用offlineCtx.oncomplete獲取數據。請參閱規范。)

最后我在這里找到了答案: http//www.pp4s.co.uk/main/tu-sms-audio-recording.html#co-tu-sms-audio-recording__js但你不會喜歡它。 顯然,Audio API不夠標准化,因此無法在所有瀏覽器上運行。 所以我能夠在Firefox中運行上面的代碼,但不能運行Chrome。

基本思路:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM