AudioBuffer stub

This commit is contained in:
Fernando Jiménez Moreno 2018-06-30 10:45:40 +02:00
parent 93990d437e
commit 3fe38a99ad
4 changed files with 113 additions and 1 deletions

View file

@ -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
}
}

View file

@ -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 {

View file

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

View file

@ -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);
};