From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../audiobuffersourcenode/index.html | 64 ++++++ .../api/audiobuffersourcenode/buffer/index.html | 81 ++++++++ .../zh-cn/web/api/audiobuffersourcenode/index.html | 225 +++++++++++++++++++++ .../web/api/audiobuffersourcenode/start/index.html | 84 ++++++++ 4 files changed, 454 insertions(+) create mode 100644 files/zh-cn/web/api/audiobuffersourcenode/audiobuffersourcenode/index.html create mode 100644 files/zh-cn/web/api/audiobuffersourcenode/buffer/index.html create mode 100644 files/zh-cn/web/api/audiobuffersourcenode/index.html create mode 100644 files/zh-cn/web/api/audiobuffersourcenode/start/index.html (limited to 'files/zh-cn/web/api/audiobuffersourcenode') diff --git a/files/zh-cn/web/api/audiobuffersourcenode/audiobuffersourcenode/index.html b/files/zh-cn/web/api/audiobuffersourcenode/audiobuffersourcenode/index.html new file mode 100644 index 0000000000..62202a1370 --- /dev/null +++ b/files/zh-cn/web/api/audiobuffersourcenode/audiobuffersourcenode/index.html @@ -0,0 +1,64 @@ +--- +title: AudioBufferSourceNode.AudioBufferSourceNode() +slug: Web/API/AudioBufferSourceNode/AudioBufferSourceNode +translation_of: Web/API/AudioBufferSourceNode/AudioBufferSourceNode +--- +

{{APIRef("Web Audio API")}}

+ +

 AudioBufferSourceNode() 构造器创建一个新的 {{domxref("AudioBufferSourceNode")}} 实例.

+ +

Syntax

+ +
var audioBufferSourceNode = new AudioBufferSourceNode(context, options)
+ +

Parameters

+ +

从 {{domxref("AudioNodeOptions")}} 字典中继承变量.

+ +
+
内容
+
指向 {{domxref("AudioContext")}}.
+
选项 {{optional_inline}}
+
如下: +
    +
  • buffer: An instance of {{domxref("AudioBuffer")}} to be played.
  • +
  • detune: A value in cents to modulate the speed of audio stream rendering. Its nominal range is (-∞ to +∞). The default is 0.
  • +
  • loop: A boolean indicating whether the audio should play in a loop. The default is false. If the loop is dynamically modified during playback, the new value will take effect on the next processing block of audio.
  • +
  • loopEnd: An optional value, in seconds, where looping should end if the loop attribute is true. The default is 0. Its value is exclusive to the content of the loop. The sample frames, comprising the loop, run from the values loopStart to loopEnd-(1/sampleRate). It's sensible to set this to a value between 0 and the duration of the buffer. If loopEnd is less than 0, looping will end at 0. If loopEnd is greater than the duration of the buffer, looping will end at the end of the buffer. This attribute is converted to an exact sample frame offset within the buffer, by multiplying by the buffer's sample rate and rounding to the nearest integer value. Thus, its behavior is independent of the value of the playbackRate parameter.
  • +
+ +
    +
  • loopStart: An optional value in seconds, where looping should end if the loop attribute is true. The default is 0. It's sensible to set this to a value between 0 and the duration of the buffer. If loopStart is less than 0, looping will begin at 0. If loopStart is greater than the duration of the buffer, looping will begin at the end of the buffer. This attribute is converted to an exact sample frame offset within the buffer, by multiplying by the buffer's sample rate and rounding to the nearest integer value. Thus, its behavior is independent of the value of the playbackRate parameter.
  • +
  • playbackRate: The speed at which to render the audio stream. Its default value is 1. This parameter is k-rate. This is a compound parameter with detune. Its nominal range is (-∞ to +∞).
  • +
+
+
+ +

Return value

+ +

A new {{domxref("AudioBufferSourceNode")}} object instance.

+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API','#AudioBufferSourceNode','AudioBufferSourceNode()')}}{{Spec2('Web Audio API')}}Initial definition.
+ +

Browser Compatibility

+ +
+ + +

{{Compat("api.AudioBufferSourceNode.AudioBufferSourceNode")}}

+
diff --git a/files/zh-cn/web/api/audiobuffersourcenode/buffer/index.html b/files/zh-cn/web/api/audiobuffersourcenode/buffer/index.html new file mode 100644 index 0000000000..39591c82ba --- /dev/null +++ b/files/zh-cn/web/api/audiobuffersourcenode/buffer/index.html @@ -0,0 +1,81 @@ +--- +title: AudioBufferSourceNode.buffer +slug: Web/API/AudioBufferSourceNode/buffer +tags: + - API + - AudioBufferSourceNode + - Web Audio API +translation_of: Web/API/AudioBufferSourceNode/buffer +--- +

{{ APIRef("Web Audio API") }}

+ +

{{ domxref("AudioBufferSourceNode") }} 接口的 buffer 属性提供了重复播放音频的能力,该音频使用 {{domxref("AudioBuffer")}} 作为声音文件的来源。

+ +

如果 buffer 属性的值为 null, 节点会自动生成一个单声道的无声文件(所有采样均为0)。

+ +

语法

+ +
AudioBufferSourceNode.buffer = soundBuffer;
+
+ +

返回值

+ +

{{domxref("AudioBuffer")}},包含了节点将要播放的声音数据

+ +

示例

+ +
+

完整的示例请查看  this code running live,或  view the source。

+
+ +
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;
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Web Audio API", "#widl-AudioBufferSourceNode-buffer", "buffer")}}{{Spec2("Web Audio API")}}Initial definition
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.AudioBufferSourceNode.buffer")}}

+
+ +

相关链接

+ +

{{page("/en-US/docs/Web/API/AudioBufferSourceNode","See_also")}}

diff --git a/files/zh-cn/web/api/audiobuffersourcenode/index.html b/files/zh-cn/web/api/audiobuffersourcenode/index.html new file mode 100644 index 0000000000..2d6ef36dae --- /dev/null +++ b/files/zh-cn/web/api/audiobuffersourcenode/index.html @@ -0,0 +1,225 @@ +--- +title: AudioBufferSourceNode +slug: Web/API/AudioBufferSourceNode +translation_of: Web/API/AudioBufferSourceNode +--- +

{{APIRef("Web Audio API")}}

+ +
+

AudioBufferSourceNode 接口继承自{{domxref("AudioScheduledSourceNode")}} ,表现为一个音频源,它包含了一些写在内存中的音频数据,通常储存在一个ArrayBuffer对象中。在处理有严格的时间精确度要求的回放的情形下它尤其有用。比如播放那些需要满足一个指定节奏的声音或者那些储存在内存而不是硬盘或者来自网络的声音。为了播放那些有时间精确度需求但来自网络的流文件或者来自硬盘,则使用{{domxref("AudioWorkletNode")}}来实现回放。

+ +

{{InheritanceDiagram}}

+
+ +

AudioBufferSourceNode 没有输入却有一个输出,其通道数与其 {{domxref("AudioBufferSourceNode.buffer", "buffer")}} 属性所指定的 AudioBuffer 相同。如果没有设置 buffer,也就是说 buffer 属性是 null 的话,输出将包含一个无声的单通道(每个采样点均为0)。

+ +

一个 {{domxref("AudioBufferSourceNode")}} 只能被播放一次,也就是说,每次调用 {{domxref("AudioScheduledSourceNode.start", "start()")}} 之后,如果还想再播放一遍同样的声音,那么就需要再创建一个 AudioBufferSourceNode。庆幸的是,创建该节点的代价并不大,并且想要多次播放声音的话,实际上 AudioBuffer 也可以被重用。事实上,你可以用一种“阅后即焚”的方式来使用这些节点:创建节点,调用 start() 开始播放声音,然后,你甚至不需要再保留这个节点的引用了。声音播放完成之后,垃圾收集器会找个恰当的时机回收资源。

+ +

多次调用 AudioBufferSourceNode.stop() 是允许的。如果这时候 AudioBufferSourceNode 还没有到达缓冲区末尾的话,最近一次的调用将替换上一次的调用。

+ +


+ The AudioBufferSourceNode takes the content of an AudioBuffer and m

+ + + + + + + + + + + + + + + + +
输入数量0
输出数量1
频道数量由相关的 {{domxref("AudioBuffer")}} 定义
+ +

属性

+ +

从父级的 {{domxref("AudioNode")}} 继承属性.

+ +
+
{{domxref("AudioBufferSourceNode.buffer")}}
+
是一个 {{domxref("AudioBuffer")}} 它定义了要播放的音频,当设置它的值为0时,它会定义一个静默的单通道。
+
{{domxref("AudioBufferSourceNode.detune")}}
+
Is a k-rate {{domxref("AudioParam")}} representing detuning of oscillation in cents. Its default value is 0.
+
{{domxref("AudioBufferSourceNode.loop")}}
+
Is a Boolean attribute indicating if the audio asset must be replayed when the end of the {{domxref("AudioBuffer")}} is reached. Its default value is false.
+
{{domxref("AudioBufferSourceNode.loopStart")}}
+
Is a double value indicating, in seconds, where in the {{domxref("AudioBuffer")}} the restart of the play must happen. Its default value is 0.
+
{{domxref("AudioBufferSourceNode.loopEnd")}}
+
Is a double value indicating, in seconds, where in the {{domxref("AudioBuffer")}} the replay of the play must stop (and eventually loop again). Its default value is 0.
+
{{domxref("AudioBufferSourceNode.playbackRate")}}
+
Is an a-rate {{domxref("AudioParam")}} that 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.
+
+ +

事件

+ +
+
{{domxref("AudioBufferSourceNode.onended")}}
+
是一个 {{domxref("EventHandler")}} 类型,包含了与 {{event("ended_(Web_Audio)", "ended")}} 相关联的结束事件。
+
+ +

方法

+ +

从父级的 {{domxref("AudioNode")}} 继承方法

+ +
+
{{domxref("AudioBufferSourceNode.start()")}}
+
Schedules the start of the playback of the audio asset.
+
{{domxref("AudioBufferSourceNode.stop()")}}
+
Schedules the end of the playback of an audio asset.
+
+ +

例子

+ +

在这个例子中, 我们将会创建一个2秒的缓冲器,并用白噪音填充它, 然后通过{{domxref("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();
+}
+ +
+

注意: 音频数据解码的例子请查看 {{domxref("AudioContext.decodeAudioData")}} 页面.

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#the-audiobuffersourcenode-interface', 'AudioBufferSourceNode')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support14 {{property_prefix("webkit")}}[1]{{CompatGeckoDesktop("23.0")}}{{CompatNo}}15 {{property_prefix("webkit")}}
+ 22
6 {{property_prefix("webkit")}}
detune property{{CompatVersionUnknown}}{{CompatGeckoDesktop("40.0")}}{{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChromeFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}28 {{property_prefix("webkit")}}[1]{{CompatGeckoMobile("25.0")}}1.2{{CompatNo}}{{CompatNo}}6 {{property_prefix("webkit")}}
detune property{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatUnknown}}
+
+ +

[1] As of Chrome 42.0 setting AudioBufferSourceNode.buffer more than once is deprecated. A deprecation message is displayed if the buffer attribute is assigned more than once.

+ +

相关页面

+ + diff --git a/files/zh-cn/web/api/audiobuffersourcenode/start/index.html b/files/zh-cn/web/api/audiobuffersourcenode/start/index.html new file mode 100644 index 0000000000..0975fa5c24 --- /dev/null +++ b/files/zh-cn/web/api/audiobuffersourcenode/start/index.html @@ -0,0 +1,84 @@ +--- +title: AudioBufferSourceNode.start() +slug: Web/API/AudioBufferSourceNode/start +translation_of: Web/API/AudioBufferSourceNode/start +--- +

{{ APIRef("Web Audio API") }}

+ +
+

 {{ domxref("AudioBufferSourceNode") }} 接口的start()方法用于计划对缓冲区中包含的音频数据的回放,或者立即开始回放。

+
+ +

语法

+ +
AudioBufferSourceNode.start([when][, offset][, duration]);
+
+ +

参数

+ +
+
when {{optional_inline}}
+
The time声音开始播放的时间,单位是秒,与 {{domxref("AudioContext")}}使用相同的时间坐标系统. 如果 when 小于 ({{domxref("AudioContext.currentTime")}}, 或者是0,声音立即被播放。 默认值是0。
+
offset {{optional_inline}}
+
An offset, specified as the number of seconds in the same time coordinate system as the AudioContext, to the time within the audio buffer that playback should begin. For example, to start playback halfway through a 10-second audio clip, offset should be 5. The default value, 0, will begin playback at the beginning of the audio buffer, and offsets past the end of the audio which will be played (based on the audio buffer's {{domxref("AudioBuffer.duration", "duration")}} and/or the {{domxref("AudioBufferSourceNode.loopEnd", "loopEnd")}} property) are silently clamped to the maximum value allowed. The computation of the offset into the sound is performed using the sound buffer's natural sample rate, rather than the current playback rate, so even if the sound is playing at twice its normal speed, the midway point through a 10-second audio buffer is still 5.
+
duration {{optional_inline}}
+
将要播放的声音的持续时间,指定单位为秒。如果这个参数没有被指定,声音播放到自然结束或者使用{{domxref("AudioScheduledSourceNode.stop", "stop()")}} 方法结束。使用这个参数的功能与调用 start(when, offset) 和调用 stop(when+duration)完全相同。
+
+ +

返回值

+ +

{{jsxref("undefined")}}.

+ +

异常

+ +
+
TypeError
+
A negative value was specified for one or more of the three time parameters. Please don't attempt to tamper with the laws of temporal physics.
+
InvalidStateError
+
start() 已经被调用。在一个AudioBufferSourceNode的生命周期内只能调用一次这个函数。
+
+ +

例子

+ +

The most simple example just starts the audio buffer playing from the beginning — you don't need to specify any parameters in this case:

+ +
source.start();
+ +

The following more complex example will, 1 second from now, start playing 10 seconds worth of sound starting 3 seconds into the audio buffer.

+ +
source.start(audioCtx.currentTime + 1,3,10);
+ +
+

For a more complete example showing start() in use, check out our {{domxref("AudioContext.decodeAudioData()")}} example, You can also run the code example live, or view the source.

+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioBufferSourceNode-start-void-double-when-double-offset-double-duration', 'start()')}}{{Spec2('Web Audio API')}} 
+ +

Browser compatibility

+ +
+ + +

{{Compat("api.AudioBufferSourceNode.start")}}

+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf