AudioBufferSourceNode
åºçº¿
广æ³å¯ç¨
èª 2015å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
AudioBufferSourceNode æ¥å£ç»§æ¿èª AudioScheduledSourceNodeï¼è¡¨ç°ä¸ºä¸ä¸ªé³é¢æºï¼å®å
å«äºä¸äºåå¨å
åä¸çé³é¢æ°æ®ï¼é常å¨åå¨ä¸ä¸ª ArrayBuffer 对象ä¸ãå¨å¤çæä¸¥æ ¼çæ¶é´ç²¾ç¡®åº¦è¦æ±çåæ¾çæ
å½¢ä¸å®å°¤å
¶æç¨ãæ¯å¦ææ¾é£äºéè¦æ»¡è¶³ä¸ä¸ªæå®èå¥ç声鳿è
é£äºå¨åå¨å
åè䏿¯ç¡¬çæè
æ¥èªç½ç»ç声é³ãä¸ºäºææ¾é£äºææ¶é´ç²¾ç¡®åº¦éæ±ä½æ¥èªç½ç»çæµæä»¶æè
æ¥èªç¡¬çï¼åä½¿ç¨ AudioWorkletNode æ¥å®ç°åæ¾ã
AudioBufferSourceNode 没æè¾å
¥å´æä¸ä¸ªè¾åºï¼å
¶ééæ°ä¸å
¶ buffer 屿§ææå®ç AudioBuffer ç¸åãå¦ææ²¡æè®¾ç½® bufferï¼ä¹å°±æ¯è¯´ buffer 屿§æ¯ null çè¯ï¼è¾åºå°å
å«ä¸ä¸ªæ 声çåééï¼æ¯ä¸ªéæ ·ç¹å为 0ï¼ã
ä¸ä¸ª AudioBufferSourceNode åªè½è¢«ææ¾ä¸æ¬¡ï¼ä¹å°±æ¯è¯´ï¼æ¯æ¬¡è°ç¨ start() ä¹åï¼å¦æè¿æ³åææ¾ä¸éåæ ·ç声é³ï¼é£ä¹å°±éè¦åå建ä¸ä¸ª AudioBufferSourceNodeãåºå¹¸çæ¯ï¼å建该èç¹ç代价并ä¸å¤§ï¼å¹¶ä¸æ³è¦å¤æ¬¡ææ¾å£°é³çè¯ï¼å®é
ä¸ AudioBuffer ä¹å¯ä»¥è¢«éç¨ãäºå®ä¸ï¼ä½ å¯ä»¥ç¨ä¸ç§âé
åå³çâçæ¹å¼æ¥ä½¿ç¨è¿äºèç¹ï¼å建èç¹ï¼è°ç¨ start() å¼å§ææ¾å£°é³ï¼ç¶åï¼ä½ çè³ä¸éè¦åä¿çè¿ä¸ªèç¹çå¼ç¨äºã声鳿æ¾å®æä¹åï¼å徿¶éå¨ä¼æ¾ä¸ªæ°å½çæ¶æºåæ¶èµæºã
夿¬¡è°ç¨ AudioBufferSourceNode.stop() æ¯å
许çãå¦æè¿æ¶å AudioBufferSourceNode è¿æ²¡æå°è¾¾ç¼å²åºæ«å°¾çè¯ï¼æè¿ä¸æ¬¡çè°ç¨å°æ¿æ¢ä¸ä¸æ¬¡çè°ç¨ã

| è¾å ¥æ°é | 0 |
|---|---|
| è¾åºæ°é | 1 |
| é¢éæ°é | ç±ç¸å
³ç AudioBuffer å®ä¹ |
屿§
ä»ç¶çº§ç AudioNode ç»§æ¿å±æ§.
AudioBufferSourceNode.buffer-
æ¯ä¸ä¸ª
AudioBufferå®å®ä¹äºè¦ææ¾çé³é¢ï¼å½è®¾ç½®å®çå¼ä¸º 0 æ¶ï¼å®ä¼å®ä¹ä¸ä¸ªéé»çåééã AudioBufferSourceNode.detune-
Is a k-rate
AudioParamrepresenting detuning of oscillation in cents. Its default value is0. AudioBufferSourceNode.loop-
Is a Boolean attribute indicating if the audio asset must be replayed when the end of the
AudioBufferis reached. Its default value isfalse. AudioBufferSourceNode.loopStart-
Is a double value indicating, in seconds, where in the
AudioBufferthe restart of the play must happen. Its default value is0. AudioBufferSourceNode.loopEnd-
Is a double value indicating, in seconds, where in the
AudioBufferthe replay of the play must stop (and eventually loop again). Its default value is0. AudioBufferSourceNode.playbackRate-
Is an a-rate
AudioParamthat defines the speed factor at which the audio asset will be played. Since no pitch correction is applied on the output, this can be used to change the pitch of the sample.
äºä»¶
AudioBufferSourceNode.onended-
æ¯ä¸ä¸ªäºä»¶å¤çå¨ç±»åï¼å å«äºä¸
endedç¸å ³èçç»æäºä»¶ã
æ¹æ³
ä»ç¶çº§ç AudioNode ç»§æ¿æ¹æ³ã
AudioBufferSourceNode.start()-
Schedules the start of the playback of the audio asset.
AudioBufferSourceNode.stop()-
Schedules the end of the playback of an audio asset.
ä¾å
å¨è¿ä¸ªä¾åä¸ï¼æä»¬å°ä¼å建ä¸ä¸ª 2 ç§çç¼å²å¨ï¼å¹¶ç¨ç½åªé³å¡«å
å®ï¼ç¶åéè¿AudioBufferSourceNodeæ¥ææ¾å®ã注éé说æäºå®çåè½ã
夿³¨ï¼ä½ å¯ä»¥ æ¥çå¨çº¿æ¼ç¤º æ æ¥çæºä»£ç .
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var button = document.querySelector("button");
var pre = document.querySelector("pre");
var myScript = document.querySelector("script");
pre.innerHTML = myScript.innerHTML;
// Stereo
var channels = 2;
// Create an empty two-second stereo buffer at the
// sample rate of the AudioContext
var frameCount = audioCtx.sampleRate * 2.0;
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
button.onclick = function () {
// Fill the buffer with white noise;
//just random values between -1.0 and 1.0
for (var channel = 0; channel < channels; channel++) {
// This gives us the actual ArrayBuffer that contains the data
var nowBuffering = myArrayBuffer.getChannelData(channel);
for (var i = 0; i < frameCount; i++) {
// Math.random() is in [0; 1.0]
// audio needs to be in [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
var source = audioCtx.createBufferSource();
// set the buffer in the AudioBufferSourceNode
source.buffer = myArrayBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the source playing
source.start();
};
夿³¨ï¼é³é¢æ°æ®è§£ç çä¾å请æ¥ç AudioContext.decodeAudioData 页é¢ã
è§è
| è§è |
|---|
| Web Audio API > # AudioBufferSourceNode > |