aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/baseaudiocontext.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/baseaudiocontext.rs')
-rw-r--r--components/script/dom/baseaudiocontext.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/components/script/dom/baseaudiocontext.rs b/components/script/dom/baseaudiocontext.rs
new file mode 100644
index 00000000000..7e23b2a3274
--- /dev/null
+++ b/components/script/dom/baseaudiocontext.rs
@@ -0,0 +1,79 @@
+/* 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::audiodestinationnode::AudioDestinationNode;
+use dom::bindings::codegen::Bindings::AudioNodeBinding::AudioNodeOptions;
+use dom::bindings::codegen::Bindings::AudioNodeBinding::{ChannelCountMode, ChannelInterpretation};
+use dom::bindings::codegen::Bindings::BaseAudioContextBinding::BaseAudioContextMethods;
+use dom::bindings::codegen::Bindings::BaseAudioContextBinding::AudioContextState;
+use dom::bindings::num::Finite;
+use dom::bindings::reflector::{DomObject, Reflector};
+use dom::bindings::root::DomRoot;
+use dom::globalscope::GlobalScope;
+use dom::promise::Promise;
+use dom_struct::dom_struct;
+use std::rc::Rc;
+
+#[dom_struct]
+pub struct BaseAudioContext {
+ reflector_: Reflector,
+ destination: Option<DomRoot<AudioDestinationNode>>,
+ sample_rate: f32,
+ current_time: f64,
+ state: AudioContextState,
+}
+
+impl BaseAudioContext {
+ #[allow(unrooted_must_root)]
+ #[allow(unsafe_code)]
+ pub fn new_inherited(
+ global: &GlobalScope,
+ channel_count: u32,
+ sample_rate: f32,
+ ) -> BaseAudioContext {
+ let mut context = BaseAudioContext {
+ reflector_: Reflector::new(),
+ destination: None,
+ current_time: 0.,
+ sample_rate,
+ state: AudioContextState::Suspended,
+ };
+
+ let mut options = unsafe { AudioNodeOptions::empty(global.get_cx()) };
+ options.channelCount = Some(channel_count);
+ options.channelCountMode = Some(ChannelCountMode::Explicit);
+ options.channelInterpretation = Some(ChannelInterpretation::Speakers);
+
+ context.destination = Some(AudioDestinationNode::new(global, &context, &options));
+
+ context
+ }
+}
+
+impl BaseAudioContextMethods for BaseAudioContext {
+ // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-samplerate
+ fn SampleRate(&self) -> Finite<f32> {
+ Finite::wrap(self.sample_rate)
+ }
+
+ // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-currenttime
+ fn CurrentTime(&self) -> Finite<f64> {
+ Finite::wrap(self.current_time)
+ }
+
+ // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-resume
+ #[allow(unrooted_must_root)]
+ fn Resume(&self) -> Rc<Promise> {
+ // TODO
+ Promise::new(&self.global())
+ }
+
+ // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-destination
+ fn Destination(&self) -> DomRoot<AudioDestinationNode> {
+ DomRoot::from_ref(self.destination.as_ref().unwrap())
+ }
+
+ // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-onstatechange
+ event_handler!(statechange, GetOnstatechange, SetOnstatechange);
+}