From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/audiobuffer/audiobuffer/index.html | 59 +++++++++ .../web/api/audiobuffer/copyfromchannel/index.html | 137 +++++++++++++++++++++ .../zh-cn/web/api/audiobuffer/duration/index.html | 128 +++++++++++++++++++ .../web/api/audiobuffer/getchanneldata/index.html | 102 +++++++++++++++ files/zh-cn/web/api/audiobuffer/index.html | 111 +++++++++++++++++ files/zh-cn/web/api/audiobuffer/length/index.html | 129 +++++++++++++++++++ .../api/audiobuffer/numberofchannels/index.html | 132 ++++++++++++++++++++ .../web/api/audiobuffer/samplerate/index.html | 81 ++++++++++++ 8 files changed, 879 insertions(+) create mode 100644 files/zh-cn/web/api/audiobuffer/audiobuffer/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/copyfromchannel/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/duration/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/getchanneldata/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/length/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/numberofchannels/index.html create mode 100644 files/zh-cn/web/api/audiobuffer/samplerate/index.html (limited to 'files/zh-cn/web/api/audiobuffer') diff --git a/files/zh-cn/web/api/audiobuffer/audiobuffer/index.html b/files/zh-cn/web/api/audiobuffer/audiobuffer/index.html new file mode 100644 index 0000000000..0b207a8d4c --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/audiobuffer/index.html @@ -0,0 +1,59 @@ +--- +title: AudioBuffer() +slug: Web/API/AudioBuffer/AudioBuffer +translation_of: Web/API/AudioBuffer/AudioBuffer +--- +

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

+ +

Web Audio API 的 AudioBuffer 构造函数将创建一个新的 {{domxref("AudioBuffer")}} 对象.

+ +

语法

+ +
var audioBuffer = new AudioBuffer([options]);
+var audioBuffer = new AudioBuffer(context[, options]);
+ +

参数

+ +

继承参数自 {{domxref("AudioNodeOptions")}} 字典.

+ +
+
context {{obsolete_inline("")}}
+
一个 {{domxref("AudioContext")}} 对象. (这个参数已经被标准移除, 详细信息请参阅浏览器兼容性部分.)
+
options {{optional_inline}}
+
Options are as follows: +
    +
  • length: buffer中采样帧的长度.
  • +
  • numberOfChannels: buffer的通道数. 默认值为1. 
  • +
  • sampleRate: buffer的采样率 (Hz). 默认值为构造此对象时使用的 context 的采样率.
  • +
+
+
+ +

返回值

+ +

一个新的 {{domxref("AudioBuffer")}} 对象实例.

+ +

规范

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

浏览器兼容性

+ +
+ + +

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

+
diff --git a/files/zh-cn/web/api/audiobuffer/copyfromchannel/index.html b/files/zh-cn/web/api/audiobuffer/copyfromchannel/index.html new file mode 100644 index 0000000000..2cbcf3a57b --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/copyfromchannel/index.html @@ -0,0 +1,137 @@ +--- +title: AudioBuffer.copyFromChannel() +slug: Web/API/AudioBuffer/copyFromChannel +translation_of: Web/API/AudioBuffer/copyFromChannel +--- +

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

+ +
+

{{ domxref("AudioBuffer") }}接口的copyFromChannel方法将样本从AudioBuffer的指定通道复制到目标数组中

+
+ +

语法

+ +
myArrayBuffer.copyFromChannel(destination,channelNumber,startInChannel);
+ +

参数

+ +
+
destination
+
将通道数据复制到的{{domxref("Float32Array")}}
+
channelNumber
+
当前AudioBuffer的通道号,用于复制通道数据。键入channelNumber大于或等于{{domxref("AudioBuffer.numberOfChannels")}},将会抛出INDEX_SIZE_ERR 的错误
+
startInChannel {{optional_inline}}
+
用于复制数据的可选偏移量。假如startInChannel比{{domxref("AudioBuffer.length")}}大,将会抛出INDEX_SIZE_ERR 的错误
+
+ +

例子

+ +
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+var anotherArray = new Float32Array;
+myArrayBuffer.copyFromChannel(anotherArray,1,0);
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioBuffer-copyFromChannel-void-Float32Array-destination-long-channelNumber-unsigned-long-startInChannel', 'copyFromChannel')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(14.0)}} {{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(27)}}{{CompatNo}}15 {{property_prefix("webkit")}}
+ 22 (unprefixed)
{{CompatNo}}
Unprefixed{{CompatChrome(43.0)}}{{CompatVersionUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile(27)}}1.2{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(28.0)}} {{property_prefix("webkit")}}
Unprefixed{{CompatNo}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatNo}}{{CompatChrome(43.0)}}
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/audiobuffer/duration/index.html b/files/zh-cn/web/api/audiobuffer/duration/index.html new file mode 100644 index 0000000000..42b804569e --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/duration/index.html @@ -0,0 +1,128 @@ +--- +title: AudioBuffer.duration +slug: Web/API/AudioBuffer/duration +translation_of: Web/API/AudioBuffer/duration +--- +

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

+ +
+

{{domxref("AudioBuffer")}}接口的duration属性返回一个双精度数,表示缓冲区中存储的PCM数据的持续时间(以秒为单位)

+
+ +

语法

+ +
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+myArrayBuffer.duration;
+ +

+ +

双精度值

+ +

例子

+ +
// 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;
+    }
+  }
+
+  console.log(myArrayBuffer.duration);
+}
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioBuffer-duration', 'duration')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support14 {{property_prefix("webkit")}}{{CompatVersionUnknown}}23{{CompatNo}}15 {{property_prefix("webkit")}}
+ 22 (unprefixed)
6 {{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChromeEdgeFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}28 {{property_prefix("webkit")}}{{CompatVersionUnknown}}251.2{{CompatNo}}{{CompatNo}}6 {{property_prefix("webkit")}}
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/audiobuffer/getchanneldata/index.html b/files/zh-cn/web/api/audiobuffer/getchanneldata/index.html new file mode 100644 index 0000000000..2c0d75cb1b --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/getchanneldata/index.html @@ -0,0 +1,102 @@ +--- +title: AudioBuffer.getChannelData() +slug: Web/API/AudioBuffer/getChannelData +translation_of: Web/API/AudioBuffer/getChannelData +--- +

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

+ +

 {{ domxref("AudioBuffer") }} 接口的getChannelData()方法返回一{{domxref("Float32Array")}} ,其中包含与通道关联的PCM数据,通道参数定义(0表示第一个通道)。

+ +

语法

+ +
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+var nowBuffering = myArrayBuffer.getChannelData(channel);
+ +

参数

+ +
+
channel
+
channel属性是要获取特定通道数据的索引。0代表第一个通道。 如果索引值大于或等于{{domxref("AudioBuffer.numberOfChannels")}}, 会抛出一个索引大小异常(INDEX_SIZE_ERR )的错误。
+
+ +

返回值

+ +

 {{domxref("Float32Array")}}.

+ +
+
+ +

例子

+ +

在下例中,我们创建一个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();
+}
+ +

Specification

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#dom-audiobuffer-getchanneldata', 'getChannelData')}}{{Spec2('Web Audio API')}} 
+ +

Browser compatibility

+ +
+ + +

{{Compat("api.AudioBuffer.getChannelData")}}

+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/audiobuffer/index.html b/files/zh-cn/web/api/audiobuffer/index.html new file mode 100644 index 0000000000..2922de46d6 --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/index.html @@ -0,0 +1,111 @@ +--- +title: AudioBuffer +slug: Web/API/AudioBuffer +translation_of: Web/API/AudioBuffer +--- +

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

+ +
+

AudioBuffer接口表示存在内存里的一段短小的音频资源,利用{{ domxref("AudioContext.decodeAudioData()") }}方法从一个音频文件构建,或者利用 {{ domxref("AudioContext.createBuffer()") }}从原始数据构建。把音频放入AudioBuffer后,可以传入到一个 {{ domxref("AudioBufferSourceNode") }}进行播放。

+
+ +

这些类型对象被设计来控制小音频片段,往往短于45秒。对于更长的声音,通过 {{domxref("MediaElementAudioSourceNode")}}来实现更为合适。缓存区(buffer)包含以下数据:不间断的IEEE75432位线性PCM,从-1到1的范围额定,就是说,32位的浮点缓存区的每个样本在-1.0到1.0之间。如果{{domxref("AudioBuffer")}}有不同的频道,他们通常被保存在独立的缓存区。

+ +

属性

+ +
+
{{domxref("AudioBuffer.sampleRate")}} {{readonlyInline}}
+
存储在缓存区的PCM数据的采样率:浮点数,单位为 sample/s。
+
{{domxref("AudioBuffer.length")}} {{readonlyInline}}
+
返回存储在缓存区的PCM数据的采样帧率:整形。
+
{{domxref("AudioBuffer.duration")}} {{readonlyInline}}
+
返回存储在缓存区的PCM数据的时长:双精度型(单位为秒),。
+
{{domxref("AudioBuffer.numberOfChannels")}} {{readonlyInline}}
+
返回存储在缓存区的PCM数据的通道数:整形。
+
+ +

方法

+ +
+
{{domxref("AudioBuffer.getChannelData()")}}
+
返回一个 {{jsxref("Float32Array")}},包含了带有频道的PCM数据,由频道参数定义(有0代表第一个频道)
+
{{domxref("AudioBuffer.copyFromChannel()")}}
+
从AudioBuffer的指定频道复制到数组终端。
+
{{domxref("AudioBuffer.copyToChannel()")}}
+
复制样品到原数组的AudioBuffer的指定频道
+
+ +

例子

+ +

以下的例子展示了如何构建一个AudioBuffer以及随机用白噪音填充。你可以在 audio-buffer demo库发现完整的源代码;一个running live 的版本也可获得。

+ +
// 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(channels, 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 array 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();
+
+}
+
+ +

规格参数

+ + + + + + + + + + + + + + +
规格参数状态注释
{{SpecName('Web Audio API', '#the-audiobuffer-interface', 'AudioBuffer')}}{{Spec2('Web Audio API')}}Initial definition.
+ +

浏览器兼容性

+ + + +

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

+ +
+ +

可查看

+ + diff --git a/files/zh-cn/web/api/audiobuffer/length/index.html b/files/zh-cn/web/api/audiobuffer/length/index.html new file mode 100644 index 0000000000..054584f83b --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/length/index.html @@ -0,0 +1,129 @@ +--- +title: AudioBuffer.length +slug: Web/API/AudioBuffer/length +translation_of: Web/API/AudioBuffer/length +--- +

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

+ +
+

The length property of the {{ domxref("AudioBuffer") }} interface returns an integer representing the length, in sample-frames, of the PCM data stored in the buffer.

+ +

{{ domxref("AudioBuffer") }} 的length属性接口返回整数,该整数代表采样帧中,存贮在缓冲区中的PCM数据的长度

+
+ +

语法

+ +
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+myArrayBuffer.length;
+ +

+ +

浮点数

+ +

例子

+ +
// 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;
+    }
+  }
+
+  console.log(myArrayBuffer.length);
+}
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioBuffer-length', 'length')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support14 {{property_prefix("webkit")}}{{CompatVersionUnknown}}23{{CompatNo}}15 {{property_prefix("webkit")}}
+ 22 (unprefixed)
6 {{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChromeEdgeFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}28 {{property_prefix("webkit")}}{{CompatVersionUnknown}}251.2{{CompatNo}}{{CompatNo}}6 {{property_prefix("webkit")}}
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/audiobuffer/numberofchannels/index.html b/files/zh-cn/web/api/audiobuffer/numberofchannels/index.html new file mode 100644 index 0000000000..5ea379d53a --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/numberofchannels/index.html @@ -0,0 +1,132 @@ +--- +title: AudioBuffer.numberOfChannels +slug: Web/API/AudioBuffer/numberOfChannels +tags: + - API + - AudioBuffer + - Web Audio API + - 属性 +translation_of: Web/API/AudioBuffer/numberOfChannels +--- +

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

+ +
+

{{ domxref("AudioBuffer") }}接口的numberOfChannels属性返回一个整数,该整数表示由缓冲区中存储的PCM数据描述的离散音频通道的数量

+
+ +

语法

+ +
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+myArrayBuffer.numberOfChannels;
+ +

返回值

+ +

一个整数。

+ +

示例

+ +
// 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;
+    }
+  }
+
+  console.log(myArrayBuffer.numberOfChannels);
+}
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioBuffer-numberOfChannels', 'numberOfChannels')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support14 {{property_prefix("webkit")}}{{CompatVersionUnknown}}23{{CompatNo}}15 {{property_prefix("webkit")}}
+ 22 (unprefixed)
6 {{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChromeEdgeFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}28 {{property_prefix("webkit")}}{{CompatVersionUnknown}}251.2{{CompatNo}}{{CompatNo}}6 {{property_prefix("webkit")}}
+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/audiobuffer/samplerate/index.html b/files/zh-cn/web/api/audiobuffer/samplerate/index.html new file mode 100644 index 0000000000..1de2c66fb5 --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/samplerate/index.html @@ -0,0 +1,81 @@ +--- +title: AudioBuffer.sampleRate +slug: Web/API/AudioBuffer/sampleRate +tags: + - API + - AudioBuffer + - Web Audio API + - sampleRate +translation_of: Web/API/AudioBuffer/sampleRate +--- +

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

+ +
+

{{ domxref("AudioBuffer") }} 接口的 sampleRate 属性返回一个以浮点数表示的采样率。该采样率是存储在缓冲区的PCM数据每秒钟的采样。

+
+ +

语法

+ +
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+myArrayBuffer.sampleRate;
+ +

返回值

+ +

一个浮点数,表示缓冲区数据的当前采样率。

+ +

示例

+ +
// 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;
+    }
+  }
+
+  console.log(myArrayBuffer.sampleRate);
+}
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioBuffer-sampleRate', 'sampleRate')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.AudioBuffer.sampleRate")}}

+
+ +

相关链接

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