音頻只是一條曲線 - 所以要建立你的振盪器,你會出現這個算法來輸出一條曲線。數字不是模擬的軟件要求將曲線定義爲一系列時間點(樣本),其值是音頻曲線的瞬時高度。通常這些樣本每秒發生44100次,即。赫茲。
結帳Web音頻API - 其驚人的強大和非常支持。只是爲了得到它的靈活性結帳這個演示由谷歌職員內幕
Web Audio Playground
http://webaudioplayground.appspot.com/
除其他音頻控件編寫的升值,它提供了黑盒振盪器,但可以讓你滾你自己,甚至使合成的或基於文件的音頻數據實時。它的模塊化設計,因此每個組件都稱爲一個節點 - 您可以通過連接這些節點
這裏建立是用來合成音(振盪器)回調的定義
function setup_onaudioprocess_callback(given_node) {
given_node.onaudioprocess = (function() {
return function(event) {
if (allow_synth) {
// console.log('inside main_glob callback onaudioprocess BUFF_SIZE ', BUFF_SIZE);
var synthesized_output_buffer;
// stens TODO - how to pass in own buffer instead of being given object: out so I can do a circular ring of such buffers
synthesized_output_buffer = event.outputBuffer.getChannelData(0); // stens TODO - do both channels not just left
var phi = 0,
dphi = 2.0 * Math.PI * given_node.sample_freq/
given_node.sample_rate;
for (var curr_sample = 0; curr_sample < given_node.BUFF_SIZE; curr_sample++, phi += dphi) {
synthesized_output_buffer[curr_sample] = Math.sin(phi);
}
given_node.sample_freq *= given_node.freq_factor;
if (given_node.sample_freq <
given_node.MIN_FREQ) {
given_node.freq_factor = given_node.increasing_freq_factor;
} else if (given_node.sample_freq > given_node.MAX_FREQ) {
given_node.freq_factor = given_node.decreasing_freq_factor;
}
// ---
audio_display_obj.pipeline_buffer_for_time_domain_cylinder(synthesized_output_buffer,
BUFF_SIZE, "providence_2");
}
};
}());
}
,將在相關地使用節點使用createScriptProcessor
function init_synth_settings(given_node, g_MIN_FREQ, g_MAX_FREQ, g_BUFF_SIZE, g_decreasing_freq_factor, g_increasing_freq_factor) {
given_node.MIN_FREQ = g_MIN_FREQ;
given_node.MAX_FREQ = g_MAX_FREQ;
given_node.sample_freq = given_node.MIN_FREQ; // Hertz
given_node.BUFF_SIZE = g_BUFF_SIZE;
given_node.decreasing_freq_factor = g_decreasing_freq_factor;
given_node.increasing_freq_factor = g_increasing_freq_factor;
given_node.freq_factor = g_increasing_freq_factor;
}
var this_glob_01 = audio_context.createScriptProcessor(BUFF_SIZE, 1, 1);
init_synth_settings(this_glob_01, 20, 300, BUFF_SIZE, 0.98, 1.01);
setup_onaudioprocess_callback(this_glob_01);
產生這應該讓你渡過了難關
感謝您連接到操場。雖然這篇文章的其餘部分基本上是我的問題的重演。也許我不清楚?我正在尋找關於實際實施方法的提示! :) – simme 2014-10-31 19:40:06
感謝您的代碼!但你在哪裏找到這個?在https://github.com/cwilso/WebAudio/?我是否因爲找不到而失明? :P – simme 2014-10-31 20:18:47
上面的代碼是從我的第一個JavaScript項目https://github.com/scottstensland/webgl-3d-animation/blob/master/src/webaudio_tooling.js – 2014-11-01 14:10:13