1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* 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 https://mozilla.org/MPL/2.0/. */

use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcReceiver};
use ipc_channel::router::ROUTER;
use js::rust::{CustomAutoRooterGuard, HandleObject};
use js::typedarray::{Float32Array, Uint8Array};
use servo_media::audio::analyser_node::AnalysisEngine;
use servo_media::audio::block::Block;
use servo_media::audio::node::AudioNodeInit;

use crate::dom::audionode::AudioNode;
use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AnalyserNodeBinding::{
    AnalyserNodeMethods, AnalyserOptions,
};
use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::{
    ChannelCountMode, ChannelInterpretation,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::window::Window;
use crate::task_source::TaskSource;

#[dom_struct]
pub struct AnalyserNode {
    node: AudioNode,
    #[ignore_malloc_size_of = "Defined in servo-media"]
    #[no_trace]
    engine: DomRefCell<AnalysisEngine>,
}

impl AnalyserNode {
    #[allow(crown::unrooted_must_root)]
    pub fn new_inherited(
        _: &Window,
        context: &BaseAudioContext,
        options: &AnalyserOptions,
    ) -> Fallible<(AnalyserNode, IpcReceiver<Block>)> {
        let node_options =
            options
                .parent
                .unwrap_or(2, ChannelCountMode::Max, ChannelInterpretation::Speakers);

        if options.fftSize > 32768 ||
            options.fftSize < 32 ||
            (options.fftSize & (options.fftSize - 1) != 0)
        {
            return Err(Error::IndexSize);
        }

        if *options.maxDecibels <= *options.minDecibels {
            return Err(Error::IndexSize);
        }

        if *options.smoothingTimeConstant < 0. || *options.smoothingTimeConstant > 1. {
            return Err(Error::IndexSize);
        }

        let (send, rcv) = ipc::channel().unwrap();
        let callback = move |block| {
            send.send(block).unwrap();
        };

        let node = AudioNode::new_inherited(
            AudioNodeInit::AnalyserNode(Box::new(callback)),
            context,
            node_options,
            1, // inputs
            1, // outputs
        )?;

        let engine = AnalysisEngine::new(
            options.fftSize as usize,
            *options.smoothingTimeConstant,
            *options.minDecibels,
            *options.maxDecibels,
        );
        Ok((
            AnalyserNode {
                node,
                engine: DomRefCell::new(engine),
            },
            rcv,
        ))
    }

    pub fn new(
        window: &Window,
        context: &BaseAudioContext,
        options: &AnalyserOptions,
    ) -> Fallible<DomRoot<AnalyserNode>> {
        Self::new_with_proto(window, None, context, options)
    }

    #[allow(crown::unrooted_must_root)]
    pub fn new_with_proto(
        window: &Window,
        proto: Option<HandleObject>,
        context: &BaseAudioContext,
        options: &AnalyserOptions,
    ) -> Fallible<DomRoot<AnalyserNode>> {
        let (node, recv) = AnalyserNode::new_inherited(window, context, options)?;
        let object = reflect_dom_object_with_proto(Box::new(node), window, proto);
        let (source, canceller) = window
            .task_manager()
            .dom_manipulation_task_source_with_canceller();
        let this = Trusted::new(&*object);

        ROUTER.add_route(
            recv.to_opaque(),
            Box::new(move |block| {
                let this = this.clone();
                let _ = source.queue_with_canceller(
                    task!(append_analysis_block: move || {
                        let this = this.root();
                        this.push_block(block.to().unwrap())
                    }),
                    &canceller,
                );
            }),
        );
        Ok(object)
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-analysernode>
    #[allow(non_snake_case)]
    pub fn Constructor(
        window: &Window,
        proto: Option<HandleObject>,
        context: &BaseAudioContext,
        options: &AnalyserOptions,
    ) -> Fallible<DomRoot<AnalyserNode>> {
        AnalyserNode::new_with_proto(window, proto, context, options)
    }

    pub fn push_block(&self, block: Block) {
        self.engine.borrow_mut().push(block)
    }
}

impl AnalyserNodeMethods for AnalyserNode {
    #[allow(unsafe_code)]
    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-getfloatfrequencydata>
    fn GetFloatFrequencyData(&self, mut array: CustomAutoRooterGuard<Float32Array>) {
        // Invariant to maintain: No JS code that may touch the array should
        // run whilst we're writing to it
        let dest = unsafe { array.as_mut_slice() };
        self.engine.borrow_mut().fill_frequency_data(dest);
    }

    #[allow(unsafe_code)]
    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-getbytefrequencydata>
    fn GetByteFrequencyData(&self, mut array: CustomAutoRooterGuard<Uint8Array>) {
        // Invariant to maintain: No JS code that may touch the array should
        // run whilst we're writing to it
        let dest = unsafe { array.as_mut_slice() };
        self.engine.borrow_mut().fill_byte_frequency_data(dest);
    }

    #[allow(unsafe_code)]
    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-getfloattimedomaindata>
    fn GetFloatTimeDomainData(&self, mut array: CustomAutoRooterGuard<Float32Array>) {
        // Invariant to maintain: No JS code that may touch the array should
        // run whilst we're writing to it
        let dest = unsafe { array.as_mut_slice() };
        self.engine.borrow().fill_time_domain_data(dest);
    }

    #[allow(unsafe_code)]
    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-getbytetimedomaindata>
    fn GetByteTimeDomainData(&self, mut array: CustomAutoRooterGuard<Uint8Array>) {
        // Invariant to maintain: No JS code that may touch the array should
        // run whilst we're writing to it
        let dest = unsafe { array.as_mut_slice() };
        self.engine.borrow().fill_byte_time_domain_data(dest);
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-fftsize>
    fn SetFftSize(&self, value: u32) -> Fallible<()> {
        if !(32..=32768).contains(&value) || (value & (value - 1) != 0) {
            return Err(Error::IndexSize);
        }
        self.engine.borrow_mut().set_fft_size(value as usize);
        Ok(())
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-fftsize>
    fn FftSize(&self) -> u32 {
        self.engine.borrow().get_fft_size() as u32
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-frequencybincount>
    fn FrequencyBinCount(&self) -> u32 {
        self.FftSize() / 2
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-mindecibels>
    fn MinDecibels(&self) -> Finite<f64> {
        Finite::wrap(self.engine.borrow().get_min_decibels())
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-mindecibels>
    fn SetMinDecibels(&self, value: Finite<f64>) -> Fallible<()> {
        if *value >= self.engine.borrow().get_max_decibels() {
            return Err(Error::IndexSize);
        }
        self.engine.borrow_mut().set_min_decibels(*value);
        Ok(())
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-maxdecibels>
    fn MaxDecibels(&self) -> Finite<f64> {
        Finite::wrap(self.engine.borrow().get_max_decibels())
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-maxdecibels>
    fn SetMaxDecibels(&self, value: Finite<f64>) -> Fallible<()> {
        if *value <= self.engine.borrow().get_min_decibels() {
            return Err(Error::IndexSize);
        }
        self.engine.borrow_mut().set_max_decibels(*value);
        Ok(())
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-smoothingtimeconstant>
    fn SmoothingTimeConstant(&self) -> Finite<f64> {
        Finite::wrap(self.engine.borrow().get_smoothing_constant())
    }

    /// <https://webaudio.github.io/web-audio-api/#dom-analysernode-smoothingtimeconstant>
    fn SetSmoothingTimeConstant(&self, value: Finite<f64>) -> Fallible<()> {
        if *value < 0. || *value > 1. {
            return Err(Error::IndexSize);
        }
        self.engine.borrow_mut().set_smoothing_constant(*value);
        Ok(())
    }
}