BaseAudioContext: createScriptProcessor() ã¡ã½ãã
Deprecated
Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers. >
createScriptProcessor() 㯠BaseAudioContext ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã¡ã½ããã§ãç´æ¥é³å£°å¦çã«ç¨ãããã ScriptProcessorNode ãçæãã¾ãã
ã¡ã¢:
ãã®æ©è½ã¯ AudioWorklet 㨠AudioWorkletNode ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ç½®ãæãããã¾ããã
æ§æ
createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels)
弿°
bufferSize-
ãµã³ãã«ãã¬ã¼ã åä½ã§ã®ãããã¡ã¼ãµã¤ãºãæå®ããå ´åã bufferSize 㯠256, 512, 1024, 2048, 4096, 8192, 16384 ã®å¤ã®ããããã§ãªããã°ãªãã¾ããããããæ¸¡ãããªãã£ãå ´åããããã¯å¤ã 0 ã®å ´åãå®è£ ã¯ä¸ããããç°å¢ã«æé©ãªãããã¡ã¼ãµã¤ãºã鏿ããããã¯ãã¼ãã®å¯¿å½ãéã㦠2 ã®ä¸å®ä¹ã¨ãªãã¾ãã
ãã®å¤ã¯ã
audioprocessã¤ãã³ããé ä¿¡ãããé »åº¦ã¨ãåå¼ã³åºãã§å¦çãããå¿ è¦ããããµã³ãã«ãã¬ã¼ã ã®æ°ãå¶å¾¡ãã¾ããbufferSizeã®å¤ãå°ããããã¨ãé å»¶ã¯å°ããï¼è¯ãï¼ãªãã¾ããé³å£°ã䏿ãããã°ãªãããé¿ããããã«ã¯ãããé«ãå¤ãå¿ è¦ã§ããä½è ã¯ãã®ãããã¡ã¼ãµã¤ãºãæå®ãããé å»¶ã¨é³è³ªã®ãã©ã³ã¹ãã¨ãããã«ãå®è£ ãé©åãªãããã¡ã¼ãµã¤ãºã鏿ã§ããããã«ãããã¨ããå§ããã¾ãã numberOfInputChannels-
æ´æ°ã§ããã®ãã¼ãã®å ¥åã®ãã£ã³ãã«æ°ãæå®ãã¾ããæ¢å®å¤ã¯ 2 ã§ãã32 ã¾ã§ã®å¤ã«å¯¾å¿ãã¦ãã¾ãã
numberOfOutputChannels-
æ´æ°ã§ããã®ãã¼ãã®åºåã®ãã£ã³ãã«æ°ãæå®ãã¾ããæ¢å®å¤ã¯ 2 ã§ãã32 ã¾ã§ã®å¤ã«å¯¾å¿ãã¦ãã¾ãã
è¦å:
Webkit ã¯ç¾å¨ï¼ãã¼ã¸ã§ã³ 31ï¼ããã®ã¡ã½ãããå¼ã³åºãã¨ãã«æå¹ãª bufferSize ãæ¸¡ããã¨ãè¦æ±ãã¦ãã¾ãã
ã¡ã¢:
numberOfInputChannelsã¨numberOfOutputChannels` ã®ä¸¡æ¹ã 0 ã«ããã®ã¯ç¡å¹ã§ãã
è¿å¤
ScriptProcessorNode ã§ãã
ä¾
>ã¹ã¯ãªããããã»ããµã¼ã使ç¨ãã¦ãã¯ã¤ããã¤ãºã追å
次ã®ä¾ã¯ã AudioContext.decodeAudioData() ã«ãã£ã¦èªã¿è¾¼ãã ãã©ãã¯ããå
¥åãã©ãã¯ï¼ãããã¡ã¼ï¼ã®ããããã®é³å£°ãµã³ãã«ã«ãã¯ã¤ããã¤ãºãå ãã¦å¦çã AudioDestinationNode ã«ãã£ã¦åçãã ScriptProcessorNode ã®åºæ¬çãªä½¿ç¨æ¹æ³ã示ãã¦ãã¾ãã
åãã£ã³ãã«ã¨åãµã³ãã«ãã¬ã¼ã ã«å¯¾ãã¦ãã¹ã¯ãªãããã¼ãã® audioprocess ã¤ãã³ããã³ãã©ã¼ãé¢é£ãã audioProcessingEvent ã使ç¨ããå
¥åãããã¡ã¼ã®åãã£ã³ãã«ã¨åãã£ã³ãã«ã®åãµã³ãã«ãéãã¦ã«ã¼ãããå°éã®ãã¯ã¤ããã¤ãºã追å ãã¦ãããã®çµæãåã±ã¼ã¹ã§åºåãµã³ãã«ã¨ãã¦ã»ãããã¾ãã
ã¡ã¢: å®å ¨ãªä¾ãã©ã¤ãã§å®è¡ããããã½ã¼ã¹ã表示ããããããã¨ãã§ãã¾ãã
const myScript = document.querySelector("script");
const myPre = document.querySelector("pre");
const playButton = document.querySelector("button");
// Create AudioContext and buffer source
let audioCtx;
async function init() {
audioCtx = new AudioContext();
const source = audioCtx.createBufferSource();
// Create a ScriptProcessorNode with a bufferSize of 4096 and
// a single input and output channel
const scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
// Load in an audio track using fetch() and decodeAudioData()
try {
const response = await fetch("viper.ogg");
const arrayBuffer = await response.arrayBuffer();
source.buffer = await audioCtx.decodeAudioData(arrayBuffer);
} catch (err) {
console.error(
`Unable to fetch the audio file: ${name} Error: ${err.message}`,
);
}
// Give the node a function to process audio events
scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
// The input buffer is the song we loaded earlier
let inputBuffer = audioProcessingEvent.inputBuffer;
// The output buffer contains the samples that will be modified and played
let outputBuffer = audioProcessingEvent.outputBuffer;
// Loop through the output channels (in this case there is only one)
for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
let inputData = inputBuffer.getChannelData(channel);
let outputData = outputBuffer.getChannelData(channel);
// Loop through the 4096 samples
for (let sample = 0; sample < inputBuffer.length; sample++) {
// make output equal to the same as the input
outputData[sample] = inputData[sample];
// add noise to each output sample
outputData[sample] += (Math.random() * 2 - 1) * 0.1;
}
}
});
source.connect(scriptNode);
scriptNode.connect(audioCtx.destination);
source.start();
// When the buffer source stops playing, disconnect everything
source.addEventListener("ended", () => {
source.disconnect(scriptNode);
scriptNode.disconnect(audioCtx.destination);
});
}
// wire up play button
playButton.addEventListener("click", () => {
if (!audioCtx) {
init();
}
});
仿§æ¸
2014 å¹´ 8 æ 29 æ¥ã®ã¦ã§ããªã¼ãã£ãª API 仿§æ¸å ¬é以éããã®æ©è½ã¯éæ¨å¥¨ã¨ãªãã¾ãããæ¨æºåã®ç®å¦ãç«ããªããªãã¾ããã
ãã㯠AudioWorklet 㨠AudioWorkletNode ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ç½®ãæãããã¾ããã