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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Distinct audio playback channels for music and sound effects
//!
//! Voxygen's audio system uses a limited number of channels to play multiple
//! sounds simultaneously. Each additional channel used decreases performance
//! in-game, so the amount of channels utilized should be kept to a minimum.
//!
//! When constructing a new [`AudioFrontend`](../struct.AudioFrontend.html), the
//! number of sfx channels are determined by the `num_sfx_channels` value
//! defined in the client
//! [`AudioSettings`](../../settings/struct.AudioSettings.html)

use kira::{
    effect::filter::{FilterBuilder, FilterHandle},
    manager::AudioManager,
    sound::PlaybackState,
    spatial::emitter::EmitterHandle,
    track::{TrackBuilder, TrackHandle, TrackId, TrackRoutes},
    tween::{Easing, Tween, Value},
    StartTime, Volume,
};
use serde::Deserialize;
use std::time::Duration;
use strum::EnumIter;
use tracing::warn;
use vek::*;

use super::soundcache::AnySoundHandle;

/// Each `MusicChannel` has a `MusicChannelTag` which help us determine when we
/// should transition between two types of in-game music. For example, we
/// transition between `TitleMusic` and `Exploration` when a player enters the
/// world by crossfading over a slow duration. In the future, transitions in the
/// world such as `Exploration` -> `BossBattle` would transition more rapidly.
#[derive(PartialEq, Clone, Copy, Hash, Eq, Deserialize, Debug)]
pub enum MusicChannelTag {
    TitleMusic,
    Exploration,
    Combat,
}

/// A MusicChannel is designed to play music which
/// is always heard at the player's position.
pub struct MusicChannel {
    tag: MusicChannelTag,
    track: Option<TrackHandle>,
    source: Option<AnySoundHandle>,
    length: f32,
    loop_data: (bool, LoopPoint, LoopPoint), // Loops?, Start, End
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LoopPoint {
    Start,
    End,
    Point(f64),
}

impl MusicChannel {
    pub fn new(manager: &mut AudioManager, parent_track: TrackId) -> Self {
        let new_track = manager.add_sub_track(
            TrackBuilder::new()
                .volume(Volume::Amplitude(0.0))
                .routes(TrackRoutes::parent(parent_track)),
        );
        match new_track {
            Ok(track) => Self {
                tag: MusicChannelTag::TitleMusic,
                track: Some(track),
                source: None,
                length: 0.0,
                loop_data: (false, LoopPoint::Start, LoopPoint::End),
            },
            Err(_) => {
                warn!(?new_track, "Failed to create track. May not play music.");
                Self {
                    tag: MusicChannelTag::TitleMusic,
                    track: None,
                    source: None,
                    length: 0.0,
                    loop_data: (false, LoopPoint::Start, LoopPoint::End),
                }
            },
        }
    }

    pub fn set_tag(&mut self, tag: MusicChannelTag) { self.tag = tag; }

    pub fn set_source(&mut self, source_handle: Option<AnySoundHandle>) {
        self.source = source_handle;
    }

    pub fn set_length(&mut self, length: f32) { self.length = length; }

    // Gets the currently set loop data
    pub fn get_loop_data(&self) -> (bool, LoopPoint, LoopPoint) { self.loop_data }

    /// Sets whether the sound loops, and the start and end points of the loop
    pub fn set_loop_data(&mut self, loops: bool, start: LoopPoint, end: LoopPoint) {
        if let Some(source) = self.source.as_mut() {
            self.loop_data = (loops, start, end);
            if loops {
                match (start, end) {
                    (LoopPoint::Start, LoopPoint::End) => {
                        source.set_loop_region(0.0..);
                    },
                    (LoopPoint::Start, LoopPoint::Point(end)) => {
                        source.set_loop_region(..end);
                    },
                    (LoopPoint::Point(start), LoopPoint::End) => {
                        source.set_loop_region(start..);
                    },
                    (LoopPoint::Point(start), LoopPoint::Point(end)) => {
                        source.set_loop_region(start..end);
                    },
                    _ => {
                        warn!("Invalid loop points given")
                    },
                }
            } else {
                source.set_loop_region(None);
            }
        }
    }

    /// Stop whatever is playing on this channel with an optional fadeout and
    /// delay
    pub fn stop(&mut self, duration: Option<f32>, delay: Option<f32>) {
        if let Some(source) = self.source.as_mut() {
            let tween = Tween {
                duration: Duration::from_secs_f32(duration.unwrap_or(0.1)),
                start_time: StartTime::Delayed(Duration::from_secs_f32(delay.unwrap_or(0.0))),
                ..Default::default()
            };
            source.stop(tween)
        };
    }

    /// Set the volume of the current channel.
    pub fn set_volume(&mut self, volume: f32) {
        if let Some(track) = self.track.as_mut() {
            track.set_volume(Volume::Amplitude(volume as f64), Tween::default());
            // } else {
            //     warn!("Music track not present; cannot set volume")
        }
    }

    /// Fade to a given amplitude over a given duration, optionally after a
    /// delay
    pub fn fade_to(&mut self, volume: f32, duration: f32, delay: Option<f32>) {
        let mut start_time = StartTime::Immediate;
        if let Some(delay) = delay {
            start_time = StartTime::Delayed(Duration::from_secs_f32(delay))
        }
        let tween = Tween {
            start_time,
            duration: Duration::from_secs_f32(duration),
            easing: Easing::Linear,
        };
        if let Some(track) = self.track.as_mut() {
            track.set_volume(Volume::Amplitude(volume as f64), tween);
        }
    }

    /// Fade to silence over a given duration and stop, optionally after a delay
    /// Use fade_to() if this fade is temporary
    pub fn fade_out(&mut self, duration: f32, delay: Option<f32>) {
        self.stop(Some(duration), delay);
    }

    /// Returns true if the sound has stopped playing (whether by fading out or
    /// by finishing)
    pub fn is_done(&self) -> bool {
        self.source
            .as_ref()
            .map_or(true, |source| source.state() == PlaybackState::Stopped)
    }

    pub fn get_tag(&self) -> MusicChannelTag { self.tag }

    /// Get an immutable reference to the channel's track for purposes of
    /// setting the output destination of a sound
    pub fn get_track(&self) -> Option<&TrackHandle> { self.track.as_ref() }

    /// Get a mutable reference to the channel's track
    pub fn get_track_mut(&mut self) -> Option<&mut TrackHandle> { self.track.as_mut() }

    pub fn get_source(&mut self) -> Option<&mut AnySoundHandle> { self.source.as_mut() }

    pub fn get_length(&self) -> f32 { self.length }
}

/// AmbienceChannelTags are used for non-positional sfx. Currently the only use
/// is for wind.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, EnumIter)]
pub enum AmbienceChannelTag {
    Wind,
    Rain,
    ThunderRumbling,
    Leaves,
    Cave,
    Thunder,
}

/// An AmbienceChannel uses a non-positional audio sink designed to play sounds
/// which are always heard at the camera's position.
#[derive(Debug)]
pub struct AmbienceChannel {
    tag: AmbienceChannelTag,
    target_volume: f32,
    track: TrackHandle,
    filter: FilterHandle,
    source: Option<AnySoundHandle>,
    pub looping: bool,
}

impl AmbienceChannel {
    pub fn new(
        tag: AmbienceChannelTag,
        init_volume: f32,
        manager: &mut AudioManager,
        parent_track: TrackId,
        looping: bool,
    ) -> Result<Self, kira::ResourceLimitReached> {
        let ambience_filter_builder = FilterBuilder::new().cutoff(Value::Fixed(20000.0));
        let mut ambience_track_builder = TrackBuilder::new();
        let filter = ambience_track_builder.add_effect(ambience_filter_builder);
        let track = manager.add_sub_track(
            ambience_track_builder
                .volume(0.0)
                .routes(TrackRoutes::parent(parent_track)),
        )?;

        Ok(Self {
            tag,
            target_volume: init_volume,
            track,
            filter,
            source: None,
            looping,
        })
    }

    pub fn set_source(&mut self, source_handle: Option<AnySoundHandle>) {
        self.source = source_handle;
    }

    /// Stop whatever is playing on this channel with an optional fadeout and
    /// delay
    pub fn stop(&mut self, duration: Option<f32>, delay: Option<f32>) {
        if let Some(source) = self.source.as_mut() {
            let tween = Tween {
                duration: Duration::from_secs_f32(duration.unwrap_or(0.1)),
                start_time: StartTime::Delayed(Duration::from_secs_f32(delay.unwrap_or(0.0))),
                ..Default::default()
            };
            source.stop(tween)
        }
    }

    /// Set the channel to a volume, fading over a given duration
    pub fn fade_to(&mut self, volume: f32, duration: f32) {
        self.track
            .set_volume(Volume::Amplitude(volume as f64), Tween {
                start_time: StartTime::Immediate,
                duration: Duration::from_secs_f32(duration),
                easing: Easing::Linear,
            });
        self.target_volume = volume;
    }

    /// Set the cutoff for the lowpass filter on this channel
    pub fn set_filter(&mut self, frequency: u32) {
        self.filter
            .set_cutoff(Value::Fixed(frequency as f64), Tween::default());
    }

    /// Set whether this channel's sound loops or not
    pub fn set_looping(&mut self, loops: bool) {
        if let Some(source) = self.source.as_mut() {
            if loops {
                source.set_loop_region(0.0..);
            } else {
                source.set_loop_region(None);
            }
        }
    }

    pub fn get_source(&mut self) -> Option<&mut AnySoundHandle> { self.source.as_mut() }

    /// Get an immutable reference to the channel's track for purposes of
    /// setting the output destination of a sound
    pub fn get_track(&self) -> &TrackHandle { &self.track }

    /// Get a mutable reference to the channel's track
    pub fn get_track_mut(&mut self) -> &mut TrackHandle { &mut self.track }

    /// Get the volume of this channel. The volume may be in the process of
    /// being faded to.
    pub fn get_target_volume(&self) -> f32 { self.target_volume }

    pub fn get_tag(&self) -> AmbienceChannelTag { self.tag }

    pub fn set_tag(&mut self, tag: AmbienceChannelTag) { self.tag = tag }

    pub fn is_active(&self) -> bool { self.get_target_volume() == 0.0 }

    pub fn is_stopped(&self) -> bool {
        if let Some(source) = self.source.as_ref() {
            source.state() == PlaybackState::Stopped
        } else {
            false
        }
    }
}

/// An SfxChannel uses a positional audio sink, and is designed for short-lived
/// audio which can be spatially controlled, but does not need control over
/// playback or fading/transitions
///
/// Note: currently, emitters are static once spawned
#[derive(Debug)]
pub struct SfxChannel {
    source: Option<AnySoundHandle>,
    emitter: EmitterHandle,
    pub pos: Vec3<f32>,
}

impl SfxChannel {
    pub fn new(emitter: EmitterHandle) -> Self {
        Self {
            source: None,
            emitter,
            pos: Vec3::zero(),
        }
    }

    pub fn set_source(&mut self, source_handle: Option<AnySoundHandle>) {
        self.source = source_handle;
    }

    pub fn stop(&mut self) {
        if let Some(source) = self.source.as_mut() {
            source.stop(Tween::default())
        }
    }

    pub fn set_volume(&mut self, volume: f32) {
        if let Some(source) = self.source.as_mut() {
            source.set_volume(Volume::Amplitude(volume as f64), Tween::default())
        }
    }

    pub fn get_emitter(&self) -> &EmitterHandle { &self.emitter }

    pub fn is_done(&self) -> bool {
        self.source
            .as_ref()
            .map_or(true, |source| source.state() == PlaybackState::Stopped)
    }

    pub fn update(&mut self, pos: Vec3<f32>) {
        self.pos = pos;

        self.emitter.set_position(pos, Tween {
            duration: Duration::from_secs_f32(0.0),
            ..Default::default()
        });
    }
}

/// An UiChannel uses a non-spatial audio sink, and is designed for short-lived
/// audio which is not spatially controlled, but does not need control over
/// playback or fading/transitions
pub struct UiChannel {
    track: Option<TrackHandle>,
    source: Option<AnySoundHandle>,
}

impl UiChannel {
    pub fn new(manager: &mut AudioManager, parent_track: TrackId) -> Self {
        let new_track = manager
            .add_sub_track(TrackBuilder::default().routes(TrackRoutes::parent(parent_track)));
        match new_track {
            Ok(track) => Self {
                track: Some(track),
                source: None,
            },
            Err(_) => {
                warn!(
                    ?new_track,
                    "Failed to create track. May not play UI sounds."
                );
                Self {
                    track: None,
                    source: None,
                }
            },
        }
    }

    pub fn set_source(&mut self, source_handle: Option<AnySoundHandle>) {
        self.source = source_handle;
    }

    pub fn stop(&mut self) {
        if let Some(source) = self.source.as_mut() {
            source.stop(Tween::default())
        }
    }

    pub fn set_volume(&mut self, volume: f32) {
        if let Some(track) = self.track.as_mut() {
            track.set_volume(Volume::Amplitude(volume as f64), Tween::default())
            // } else {
            //     warn!("UI track not present; cannot set volume")
        }
    }

    pub fn is_done(&self) -> bool {
        self.source
            .as_ref()
            .map_or(true, |source| source.state() == PlaybackState::Stopped)
    }
}