Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. AudioBufferSourceNode

AudioBufferSourceNode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheAudioBufferSourceNode interface is anAudioScheduledSourceNode which represents an audio source consisting of in-memory audio data, stored in anAudioBuffer.

This interface is especially useful for playing back audio which has particularly stringent timing accuracy requirements, such as for sounds that must match a specific rhythm and can be kept in memory rather than being played from disk or the network. To play sounds which require accurate timing but must be streamed from the network or played from disk, use aAudioWorkletNode to implement its playback.

EventTarget AudioNode AudioScheduledSourceNode AudioBufferSourceNode

AnAudioBufferSourceNode has no inputs and exactly one output, which has the same number of channels as theAudioBuffer indicated by itsbuffer property. If there's no buffer set—that is, ifbuffer isnull—the output contains a single channel of silence (every sample is 0).

AnAudioBufferSourceNode can only be played once; after each call tostart(), you have to create a new node if you want to play the same sound again. Fortunately, these nodes are very inexpensive to create, and the actualAudioBuffers can be reused for multiple plays of the sound. Indeed, you can use these nodes in a "fire and forget" manner: create the node, callstart() to begin playing the sound, and don't even bother to hold a reference to it. It will automatically be garbage-collected at an appropriate time, which won't be until sometime after the sound has finished playing.

Multiple calls tostop() are allowed. The most recent call replaces the previous one, if theAudioBufferSourceNode has not already reached the end of the buffer.

The AudioBufferSourceNode takes the content of an AudioBuffer and m

Number of inputs0
Number of outputs1
Channel countdefined by the associatedAudioBuffer

Constructor

AudioBufferSourceNode()

Creates and returns a newAudioBufferSourceNode object. As an alternative, you can use theBaseAudioContext.createBufferSource() factory method; seeCreating an AudioNode.

Instance properties

Inherits properties from its parent,AudioScheduledSourceNode.

AudioBufferSourceNode.buffer

AnAudioBuffer that defines the audio asset to be played, or when set to the valuenull, defines a single channel of silence (in which every sample is 0.0).

AudioBufferSourceNode.detune

Ak-rateAudioParam representing detuning of playback incents. This value is compounded withplaybackRate to determine the speed at which the sound is played. Its default value is0 (meaning no detuning), and its nominal range is -∞ to ∞.

AudioBufferSourceNode.loop

A Boolean attribute indicating if the audio asset must be replayed when the end of theAudioBuffer is reached. Its default value isfalse.

AudioBufferSourceNode.loopStartOptional

A floating-point value indicating the time, in seconds, at which playback of theAudioBuffer must begin whenloop istrue. Its default value is0 (meaning that at the beginning of each loop, playback begins at the start of the audio buffer).

AudioBufferSourceNode.loopEndOptional

A floating-point number indicating the time, in seconds, at which playback of theAudioBuffer stops and loops back to the time indicated byloopStart, ifloop istrue. The default value is0.

AudioBufferSourceNode.playbackRate

Ak-rateAudioParam that defines the speed factor at which the audio asset will be played, where a value of 1.0 is the sound's natural sampling rate. Since no pitch correction is applied on the output, this can be used to change the pitch of the sample. This value is compounded withdetune to determine the final playback rate.

Instance methods

Inherits methods from its parent,AudioScheduledSourceNode, and overrides the following method:.

start()

Schedules playback of the audio data contained in the buffer, or begins playback immediately. Additionally allows the start offset and play duration to be set.

Examples

In this example, we create a two-second buffer, fill it with white noise, and then play it using anAudioBufferSourceNode. The comments should clearly explain what is going on.

Note:You can alsorun the code live, orview the source.

js
const audioCtx = new AudioContext();// Create an empty three-second stereo buffer at the sample rate of the AudioContextconst myArrayBuffer = audioCtx.createBuffer(  2,  audioCtx.sampleRate * 3,  audioCtx.sampleRate,);// Fill the buffer with white noise;// just random values between -1.0 and 1.0for (let channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) {  // This gives us the actual ArrayBuffer that contains the data  const nowBuffering = myArrayBuffer.getChannelData(channel);  for (let i = 0; i < myArrayBuffer.length; 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 AudioBufferconst source = audioCtx.createBufferSource();// set the buffer in the AudioBufferSourceNodesource.buffer = myArrayBuffer;// connect the AudioBufferSourceNode to the// destination so we can hear the soundsource.connect(audioCtx.destination);// start the source playingsource.start();

Note:For adecodeAudioData() example, see theAudioContext.decodeAudioData() page.

Specifications

Specification
Web Audio API
# AudioBufferSourceNode

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp