如何使用网络音频将音频从合成器渲染到缓冲区(PCM 值数组)API
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 数据。
谢谢!
最终我在这里找到了答案:http://www.pp4s.co.uk/main/tu-sms-audio-recording.html#co-tu-sms-audio-recording__js 但你不会喜欢的。显然,音频 API 还不够标准化,无法在所有浏览器上运行。所以我已经能够在 Firefox 中 运行 上面的代码,但不能 Chrome.
基本思路:
- 使用 dest = ac.createMediaStreamDestination(); 获取目的地
声音
- 使用 new MediaRecorder(dest.stream); 获取录音机
- 使用 MediaRecorder ondataavailable and stop events to get the data and combine it into a Blob
你说你想使用离线上下文,但实际上你并没有使用离线上下文。所以你应该做
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
})
(我不确定所有浏览器都支持来自离线上下文的承诺。如果不支持,请使用 offlineCtx.oncomplete 获取数据。请参阅规范。)
我有一个简单的合成器,可以播放一段时间的音符:
// 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 数据。
谢谢!
最终我在这里找到了答案:http://www.pp4s.co.uk/main/tu-sms-audio-recording.html#co-tu-sms-audio-recording__js 但你不会喜欢的。显然,音频 API 还不够标准化,无法在所有浏览器上运行。所以我已经能够在 Firefox 中 运行 上面的代码,但不能 Chrome.
基本思路:
- 使用 dest = ac.createMediaStreamDestination(); 获取目的地 声音
- 使用 new MediaRecorder(dest.stream); 获取录音机
- 使用 MediaRecorder ondataavailable and stop events to get the data and combine it into a Blob
你说你想使用离线上下文,但实际上你并没有使用离线上下文。所以你应该做
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
})
(我不确定所有浏览器都支持来自离线上下文的承诺。如果不支持,请使用 offlineCtx.oncomplete 获取数据。请参阅规范。)