aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorFernando Jiménez Moreno <ferjmoreno@gmail.com>2018-06-30 10:45:40 +0200
committerFernando Jiménez Moreno <ferjmoreno@gmail.com>2018-07-30 14:21:42 +0200
commit3fe38a99ad39769ae676bec16ffd5b1063acae39 (patch)
tree73367cab1644e308975800e88833f54eb918a6f7 /components/script/dom
parent93990d437e013c42a991e68d082c7d2c0f018ca6 (diff)
downloadservo-3fe38a99ad39769ae676bec16ffd5b1063acae39.tar.gz
servo-3fe38a99ad39769ae676bec16ffd5b1063acae39.zip
AudioBuffer stub
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/audiobuffer.rs82
-rw-r--r--components/script/dom/audionode.rs2
-rw-r--r--components/script/dom/mod.rs1
-rw-r--r--components/script/dom/webidls/AudioBuffer.webidl29
4 files changed, 113 insertions, 1 deletions
diff --git a/components/script/dom/audiobuffer.rs b/components/script/dom/audiobuffer.rs
new file mode 100644
index 00000000000..b5a8f43ca9a
--- /dev/null
+++ b/components/script/dom/audiobuffer.rs
@@ -0,0 +1,82 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+use dom::audionode::MAX_CHANNEL_COUNT;
+use dom::bindings::codegen::Bindings::AudioBufferBinding::{self, AudioBufferMethods, AudioBufferOptions};
+use dom::bindings::error::{Error, Fallible};
+use dom::bindings::num::Finite;
+use dom::bindings::reflector::{Reflector, reflect_dom_object};
+use dom::bindings::root::DomRoot;
+use dom::window::Window;
+use dom_struct::dom_struct;
+use smallvec::SmallVec;
+
+#[derive(JSTraceable, MallocSizeOf)]
+struct AudioChannel(pub Vec<f32>);
+
+impl AudioChannel {
+ pub fn new(capacity: usize) -> AudioChannel {
+ AudioChannel(Vec::with_capacity(capacity))
+ }
+}
+
+#[dom_struct]
+pub struct AudioBuffer {
+ reflector_: Reflector,
+ internal_data: SmallVec<[AudioChannel; MAX_CHANNEL_COUNT as usize]>,
+ sample_rate: f32,
+ length: u32,
+ duration: f64,
+ number_of_channels: u32,
+}
+
+impl AudioBuffer {
+ #[allow(unrooted_must_root)]
+ #[allow(unsafe_code)]
+ pub fn new_inherited(options: &AudioBufferOptions) -> AudioBuffer {
+ let mut internal_data = SmallVec::new();
+ unsafe { internal_data.set_len(options.numberOfChannels as usize); }
+ AudioBuffer {
+ reflector_: Reflector::new(),
+ internal_data,
+ sample_rate: *options.sampleRate,
+ length: options.length,
+ duration: options.length as f64 / *options.sampleRate as f64,
+ number_of_channels: options.numberOfChannels,
+ }
+ }
+
+ #[allow(unrooted_must_root)]
+ pub fn new(global: &Window,
+ options: &AudioBufferOptions) -> DomRoot<AudioBuffer> {
+ let buffer = AudioBuffer::new_inherited(options);
+ reflect_dom_object(Box::new(buffer), global, AudioBufferBinding::Wrap)
+ }
+
+ pub fn Constructor(window: &Window,
+ options: &AudioBufferOptions) -> Fallible<DomRoot<AudioBuffer>> {
+ if options.numberOfChannels > MAX_CHANNEL_COUNT {
+ return Err(Error::NotSupported);
+ }
+ Ok(AudioBuffer::new(window, options))
+ }
+}
+
+impl AudioBufferMethods for AudioBuffer {
+ fn SampleRate(&self) -> Finite<f32> {
+ Finite::wrap(self.sample_rate)
+ }
+
+ fn Length(&self) -> u32 {
+ self.length
+ }
+
+ fn Duration(&self) -> Finite<f64> {
+ Finite::wrap(self.duration)
+ }
+
+ fn NumberOfChannels(&self) -> u32 {
+ self.number_of_channels
+ }
+}
diff --git a/components/script/dom/audionode.rs b/components/script/dom/audionode.rs
index e6eea52d5f5..f3b26937497 100644
--- a/components/script/dom/audionode.rs
+++ b/components/script/dom/audionode.rs
@@ -17,7 +17,7 @@ use std::cell::Cell;
// 32 is the minimum required by the spec for createBuffer() and the deprecated
// createScriptProcessor() and matches what is used by Blink and Gecko.
// The limit protects against large memory allocations.
-pub static MAX_CHANNEL_COUNT: u32 = 32;
+pub const MAX_CHANNEL_COUNT: u32 = 32;
#[dom_struct]
pub struct AudioNode {
diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs
index 794d0c65a4d..f616922fce6 100644
--- a/components/script/dom/mod.rs
+++ b/components/script/dom/mod.rs
@@ -216,6 +216,7 @@ pub mod abstractworker;
pub mod abstractworkerglobalscope;
pub mod activation;
pub mod attr;
+pub mod audiobuffer;
pub mod audiocontext;
pub mod audiodestinationnode;
pub mod audionode;
diff --git a/components/script/dom/webidls/AudioBuffer.webidl b/components/script/dom/webidls/AudioBuffer.webidl
new file mode 100644
index 00000000000..7ec015ecb2f
--- /dev/null
+++ b/components/script/dom/webidls/AudioBuffer.webidl
@@ -0,0 +1,29 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+/*
+ * The origin of this IDL file is
+ * https://webaudio.github.io/web-audio-api/#audiobuffer
+ */
+
+dictionary AudioBufferOptions {
+ unsigned long numberOfChannels = 1;
+ required unsigned long length;
+ required float sampleRate;
+};
+
+[Exposed=Window,
+ Constructor (AudioBufferOptions options)]
+interface AudioBuffer {
+ readonly attribute float sampleRate;
+ readonly attribute unsigned long length;
+ readonly attribute double duration;
+ readonly attribute unsigned long numberOfChannels;
+// Float32Array getChannelData(unsigned long channel);
+// void copyFromChannel(Float32Array destination,
+// unsigned long channelNumber,
+// optional unsigned long startInChannel = 0);
+// void copyToChannel (Float32Array source,
+// unsigned long channelNumber,
+// optional unsigned long startInChannel = 0);
+};