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
/* 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 std::f32;

use dom_struct::dom_struct;
use servo_media::audio::node::AudioNodeType;
use servo_media::audio::param::{ParamDir, ParamType};

use crate::dom::audioparam::AudioParam;
use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::codegen::Bindings::AudioListenerBinding::AudioListenerMethods;
use crate::dom::bindings::codegen::Bindings::AudioParamBinding::{
    AudioParamMethods, AutomationRate,
};
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::window::Window;
use crate::script_runtime::CanGc;

#[dom_struct]
pub struct AudioListener {
    reflector_: Reflector,
    position_x: Dom<AudioParam>,
    position_y: Dom<AudioParam>,
    position_z: Dom<AudioParam>,
    forward_x: Dom<AudioParam>,
    forward_y: Dom<AudioParam>,
    forward_z: Dom<AudioParam>,
    up_x: Dom<AudioParam>,
    up_y: Dom<AudioParam>,
    up_z: Dom<AudioParam>,
}

impl AudioListener {
    fn new_inherited(window: &Window, context: &BaseAudioContext) -> AudioListener {
        let node = context.listener();

        let position_x = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Position(ParamDir::X),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let position_y = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Position(ParamDir::Y),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let position_z = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Position(ParamDir::Z),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let forward_x = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Forward(ParamDir::X),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let forward_y = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Forward(ParamDir::Y),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let forward_z = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Forward(ParamDir::Z),
            AutomationRate::A_rate,
            -1.,      // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let up_x = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Up(ParamDir::X),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let up_y = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Up(ParamDir::Y),
            AutomationRate::A_rate,
            1.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );
        let up_z = AudioParam::new(
            window,
            context,
            node,
            AudioNodeType::AudioListenerNode,
            ParamType::Up(ParamDir::Z),
            AutomationRate::A_rate,
            0.,       // default value
            f32::MIN, // min value
            f32::MAX, // max value
        );

        AudioListener {
            reflector_: Reflector::new(),
            position_x: Dom::from_ref(&position_x),
            position_y: Dom::from_ref(&position_y),
            position_z: Dom::from_ref(&position_z),
            forward_x: Dom::from_ref(&forward_x),
            forward_y: Dom::from_ref(&forward_y),
            forward_z: Dom::from_ref(&forward_z),
            up_x: Dom::from_ref(&up_x),
            up_y: Dom::from_ref(&up_y),
            up_z: Dom::from_ref(&up_z),
        }
    }

    #[allow(crown::unrooted_must_root)]
    pub fn new(
        window: &Window,
        context: &BaseAudioContext,
        can_gc: CanGc,
    ) -> DomRoot<AudioListener> {
        let node = AudioListener::new_inherited(window, context);
        reflect_dom_object(Box::new(node), window, can_gc)
    }
}

#[allow(non_snake_case)]
impl AudioListenerMethods<crate::DomTypeHolder> for AudioListener {
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-positionx
    fn PositionX(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.position_x)
    }
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-positiony
    fn PositionY(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.position_y)
    }
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-positionz
    fn PositionZ(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.position_z)
    }

    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-forwardx
    fn ForwardX(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.forward_x)
    }
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-forwardy
    fn ForwardY(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.forward_y)
    }
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-forwardz
    fn ForwardZ(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.forward_z)
    }

    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-upx
    fn UpX(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.up_x)
    }
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-upy
    fn UpY(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.up_y)
    }
    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-upz
    fn UpZ(&self) -> DomRoot<AudioParam> {
        DomRoot::from_ref(&self.up_z)
    }

    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-setorientation
    fn SetOrientation(
        &self,
        x: Finite<f32>,
        y: Finite<f32>,
        z: Finite<f32>,
        xUp: Finite<f32>,
        yUp: Finite<f32>,
        zUp: Finite<f32>,
    ) -> Fallible<DomRoot<AudioListener>> {
        self.forward_x.SetValue(x);
        self.forward_y.SetValue(y);
        self.forward_z.SetValue(z);
        self.up_x.SetValue(xUp);
        self.up_y.SetValue(yUp);
        self.up_z.SetValue(zUp);
        Ok(DomRoot::from_ref(self))
    }

    // https://webaudio.github.io/web-audio-api/#dom-audiolistener-setposition
    fn SetPosition(
        &self,
        x: Finite<f32>,
        y: Finite<f32>,
        z: Finite<f32>,
    ) -> Fallible<DomRoot<AudioListener>> {
        self.position_x.SetValue(x);
        self.position_y.SetValue(y);
        self.position_z.SetValue(z);
        Ok(DomRoot::from_ref(self))
    }
}