Skip to main content

script/dom/audio/
audiobuffer.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cmp::min;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use js::rust::{CustomAutoRooterGuard, HandleObject};
10use js::typedarray::{Float32, Float32Array, HeapFloat32Array};
11use script_bindings::cell::{DomRefCell, Ref};
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
13use script_bindings::trace::RootedTraceableBox;
14use servo_media::audio::buffer_source_node::AudioBuffer as ServoMediaAudioBuffer;
15
16use crate::dom::audio::audionode::MAX_CHANNEL_COUNT;
17use crate::dom::bindings::buffer_source::HeapBufferSource;
18use crate::dom::bindings::codegen::Bindings::AudioBufferBinding::{
19    AudioBufferMethods, AudioBufferOptions,
20};
21use crate::dom::bindings::error::{Error, Fallible};
22use crate::dom::bindings::num::Finite;
23use crate::dom::bindings::root::DomRoot;
24use crate::dom::globalscope::GlobalScope;
25use crate::dom::window::Window;
26use crate::realms::enter_realm;
27
28// Spec mandates at least [8000, 96000], we use [8000, 192000] to match Firefox
29// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
30pub(crate) const MIN_SAMPLE_RATE: f32 = 8000.;
31pub(crate) const MAX_SAMPLE_RATE: f32 = 192000.;
32
33/// The AudioBuffer keeps its data either in js_channels
34/// or in shared_channels if js_channels buffers are detached.
35///
36/// js_channels buffers are (re)attached right before calling GetChannelData
37/// and remain attached until its contents are needed by some other API
38/// implementation. Follow <https://webaudio.github.io/web-audio-api/#acquire-the-content>
39/// to know in which situations js_channels buffers must be detached.
40///
41#[dom_struct]
42pub(crate) struct AudioBuffer {
43    reflector_: Reflector,
44    /// Float32Arrays returned by calls to GetChannelData.
45    #[ignore_malloc_size_of = "mozjs"]
46    js_channels: DomRefCell<Vec<HeapBufferSource<Float32>>>,
47    /// Aggregates the data from js_channels.
48    /// This is `Some<T>` iff the buffers in js_channels are detached.
49    #[ignore_malloc_size_of = "servo_media"]
50    #[no_trace]
51    shared_channels: DomRefCell<Option<ServoMediaAudioBuffer>>,
52    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-samplerate>
53    sample_rate: f32,
54    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-length>
55    length: u32,
56    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-duration>
57    duration: f64,
58    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-numberofchannels>
59    number_of_channels: u32,
60}
61
62impl AudioBuffer {
63    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
64    pub(crate) fn new_inherited(
65        number_of_channels: u32,
66        length: u32,
67        sample_rate: f32,
68    ) -> AudioBuffer {
69        let vec = (0..number_of_channels)
70            .map(|_| HeapBufferSource::default())
71            .collect();
72        AudioBuffer {
73            reflector_: Reflector::new(),
74            js_channels: DomRefCell::new(vec),
75            shared_channels: DomRefCell::new(None),
76            sample_rate,
77            length,
78            duration: length as f64 / sample_rate as f64,
79            number_of_channels,
80        }
81    }
82
83    pub(crate) fn new(
84        cx: &mut JSContext,
85        global: &Window,
86        number_of_channels: u32,
87        length: u32,
88        sample_rate: f32,
89        initial_data: Option<&[Vec<f32>]>,
90    ) -> DomRoot<AudioBuffer> {
91        Self::new_with_proto(
92            cx,
93            global,
94            None,
95            number_of_channels,
96            length,
97            sample_rate,
98            initial_data,
99        )
100    }
101
102    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
103    fn new_with_proto(
104        cx: &mut JSContext,
105        global: &Window,
106        proto: Option<HandleObject>,
107        number_of_channels: u32,
108        length: u32,
109        sample_rate: f32,
110        initial_data: Option<&[Vec<f32>]>,
111    ) -> DomRoot<AudioBuffer> {
112        let buffer = AudioBuffer::new_inherited(number_of_channels, length, sample_rate);
113        let buffer = reflect_dom_object_with_proto_and_cx(Box::new(buffer), global, proto, cx);
114        buffer.set_initial_data(initial_data);
115        buffer
116    }
117
118    // Initialize the underlying channels data with initial data provided by
119    // the user or silence otherwise.
120    fn set_initial_data(&self, initial_data: Option<&[Vec<f32>]>) {
121        let mut channels = ServoMediaAudioBuffer::new(
122            self.number_of_channels as u8,
123            self.length as usize,
124            self.sample_rate,
125        );
126        for channel in 0..self.number_of_channels {
127            channels.buffers[channel as usize] = match initial_data {
128                Some(data) => data[channel as usize].clone(),
129                None => vec![0.; self.length as usize],
130            };
131        }
132        *self.shared_channels.borrow_mut() = Some(channels);
133    }
134
135    fn restore_js_channel_data(&self, cx: &mut JSContext) -> bool {
136        let _ac = enter_realm(self);
137        for (i, channel) in self.js_channels.borrow().iter().enumerate() {
138            if channel.is_initialized() {
139                // Already have data in JS array.
140                continue;
141            }
142
143            if let Some(ref shared_channels) = *self.shared_channels.borrow() {
144                // Step 4. of
145                // https://webaudio.github.io/web-audio-api/#acquire-the-content
146                // "Attach ArrayBuffers containing copies of the data to the AudioBuffer,
147                // to be returned by the next call to getChannelData()".
148                if channel.set_data(cx, &shared_channels.buffers[i]).is_err() {
149                    return false;
150                }
151            }
152        }
153
154        *self.shared_channels.borrow_mut() = None;
155
156        true
157    }
158
159    /// <https://webaudio.github.io/web-audio-api/#acquire-the-content>
160    fn acquire_contents(&self) -> Option<ServoMediaAudioBuffer> {
161        let mut result = ServoMediaAudioBuffer::new(
162            self.number_of_channels as u8,
163            self.length as usize,
164            self.sample_rate,
165        );
166        let cx = GlobalScope::get_cx();
167        for (i, channel) in self.js_channels.borrow_mut().iter().enumerate() {
168            // Step 1.
169            if !channel.is_initialized() {
170                return None;
171            }
172
173            // Step 3.
174            result.buffers[i] = channel.acquire_data(cx).ok()?;
175        }
176
177        Some(result)
178    }
179
180    pub(crate) fn get_channels(&self) -> Ref<'_, Option<ServoMediaAudioBuffer>> {
181        if self.shared_channels.borrow().is_none() {
182            let channels = self.acquire_contents();
183            if channels.is_some() {
184                *self.shared_channels.borrow_mut() = channels;
185            }
186        }
187        self.shared_channels.borrow()
188    }
189}
190
191impl AudioBufferMethods<crate::DomTypeHolder> for AudioBuffer {
192    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-audiobuffer>
193    fn Constructor(
194        cx: &mut JSContext,
195        window: &Window,
196        proto: Option<HandleObject>,
197        options: &AudioBufferOptions,
198    ) -> Fallible<DomRoot<AudioBuffer>> {
199        if options.length == 0 ||
200            options.numberOfChannels == 0 ||
201            options.numberOfChannels > MAX_CHANNEL_COUNT ||
202            *options.sampleRate < MIN_SAMPLE_RATE ||
203            *options.sampleRate > MAX_SAMPLE_RATE
204        {
205            return Err(Error::NotSupported(None));
206        }
207        Ok(AudioBuffer::new_with_proto(
208            cx,
209            window,
210            proto,
211            options.numberOfChannels,
212            options.length,
213            *options.sampleRate,
214            None,
215        ))
216    }
217
218    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-samplerate>
219    fn SampleRate(&self) -> Finite<f32> {
220        Finite::wrap(self.sample_rate)
221    }
222
223    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-length>
224    fn Length(&self) -> u32 {
225        self.length
226    }
227
228    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-duration>
229    fn Duration(&self) -> Finite<f64> {
230        Finite::wrap(self.duration)
231    }
232
233    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-numberofchannels>
234    fn NumberOfChannels(&self) -> u32 {
235        self.number_of_channels
236    }
237
238    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-getchanneldata>
239    fn GetChannelData(
240        &self,
241        cx: &mut JSContext,
242        channel: u32,
243    ) -> Fallible<RootedTraceableBox<HeapFloat32Array>> {
244        if channel >= self.number_of_channels {
245            return Err(Error::IndexSize(None));
246        }
247
248        if !self.restore_js_channel_data(cx) {
249            return Err(Error::JSFailed);
250        }
251
252        self.js_channels.borrow()[channel as usize]
253            .get_typed_array()
254            .map_err(|_| Error::JSFailed)
255    }
256
257    // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copyfromchannel
258    fn CopyFromChannel(
259        &self,
260        mut destination: CustomAutoRooterGuard<Float32Array>,
261        channel_number: u32,
262        start_in_channel: u32,
263    ) -> Fallible<()> {
264        if destination.is_shared() {
265            return Err(Error::Type(c"Cannot copy to shared buffer".to_owned()));
266        }
267
268        if channel_number >= self.number_of_channels || start_in_channel >= self.length {
269            return Err(Error::IndexSize(None));
270        }
271
272        let bytes_to_copy = min(self.length - start_in_channel, destination.len() as u32) as usize;
273        let cx = GlobalScope::get_cx();
274        let channel_number = channel_number as usize;
275        let offset = start_in_channel as usize;
276        let mut dest = vec![0.0_f32; bytes_to_copy];
277
278        // We either copy form js_channels or shared_channels.
279        let js_channel = &self.js_channels.borrow()[channel_number];
280        if js_channel.is_initialized() {
281            if js_channel
282                .copy_data_to(cx, &mut dest, offset, offset + bytes_to_copy)
283                .is_err()
284            {
285                return Err(Error::IndexSize(None));
286            }
287        } else if let Some(ref shared_channels) = *self.shared_channels.borrow() &&
288            let Some(shared_channel) = shared_channels.buffers.get(channel_number)
289        {
290            dest.extend_from_slice(&shared_channel.as_slice()[offset..offset + bytes_to_copy]);
291        }
292
293        destination.update(&dest);
294
295        Ok(())
296    }
297
298    /// <https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copytochannel>
299    fn CopyToChannel(
300        &self,
301        cx: &mut JSContext,
302        source: CustomAutoRooterGuard<Float32Array>,
303        channel_number: u32,
304        start_in_channel: u32,
305    ) -> Fallible<()> {
306        if source.is_shared() {
307            return Err(Error::Type(c"Cannot copy from shared buffer".to_owned()));
308        }
309
310        if channel_number >= self.number_of_channels || start_in_channel > (source.len() as u32) {
311            return Err(Error::IndexSize(None));
312        }
313
314        if !self.restore_js_channel_data(cx) {
315            return Err(Error::JSFailed);
316        }
317
318        let js_channel = &self.js_channels.borrow()[channel_number as usize];
319        if !js_channel.is_initialized() {
320            // The array buffer was detached.
321            return Err(Error::IndexSize(None));
322        }
323
324        let bytes_to_copy = min(self.length - start_in_channel, source.len() as u32) as usize;
325        js_channel
326            .copy_data_from(cx.into(), source, start_in_channel as usize, bytes_to_copy)
327            .map_err(|_| Error::IndexSize(None))
328    }
329}