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