2019-08-31 09:06:24 +00:00
|
|
|
use rodio::SpatialSink;
|
|
|
|
use crate::audio::fader::Fader;
|
2019-09-01 21:40:54 +00:00
|
|
|
use vek::*;
|
2019-08-31 09:06:24 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Clone, Copy)]
|
|
|
|
pub enum AudioType {
|
|
|
|
Sfx,
|
|
|
|
Music,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Clone, Copy)]
|
|
|
|
enum ChannelState {
|
|
|
|
Playing,
|
|
|
|
Stopping,
|
|
|
|
Stopped,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Channel {
|
|
|
|
id: usize,
|
|
|
|
sink: SpatialSink,
|
|
|
|
audio_type: AudioType,
|
|
|
|
state: ChannelState,
|
|
|
|
fader: Fader,
|
2019-09-01 21:40:54 +00:00
|
|
|
pub pos: Vec3::<f32>,
|
2019-08-31 09:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Channel {
|
|
|
|
pub fn music(id: usize, sink: SpatialSink) -> Self {
|
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
sink,
|
|
|
|
audio_type: AudioType::Music,
|
|
|
|
state: ChannelState::Playing,
|
2019-08-31 21:39:40 +00:00
|
|
|
fader: Fader::fade_in(0.0),
|
2019-09-01 21:40:54 +00:00
|
|
|
pos: Vec3::zero(),
|
2019-08-31 09:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 21:40:54 +00:00
|
|
|
pub fn sfx(id: usize, sink: SpatialSink, pos: Vec3::<f32>) -> Self {
|
2019-08-31 09:06:24 +00:00
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
sink,
|
|
|
|
audio_type: AudioType::Sfx,
|
|
|
|
state: ChannelState::Playing,
|
|
|
|
fader: Fader::fade_in(0.0),
|
2019-09-01 21:40:54 +00:00
|
|
|
pos
|
2019-08-31 09:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_done(&self) -> bool {
|
|
|
|
self.sink.empty() || self.state == ChannelState::Stopped
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop(&mut self, fader: Fader) {
|
|
|
|
self.state = ChannelState::Stopping;
|
|
|
|
self.fader = fader;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_id(&self) -> usize {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_audio_type(&self) -> AudioType {
|
|
|
|
self.audio_type
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_volume(&mut self, volume: f32) {
|
|
|
|
self.sink.set_volume(volume);
|
|
|
|
}
|
|
|
|
|
2019-09-01 21:40:54 +00:00
|
|
|
pub fn set_emitter_position(&mut self, pos: [f32; 3]) {
|
|
|
|
self.sink.set_emitter_position(pos);
|
|
|
|
}
|
|
|
|
|
2019-09-01 03:18:03 +00:00
|
|
|
pub fn set_left_ear_position(&mut self, pos: [f32; 3]) {
|
|
|
|
self.sink.set_left_ear_position(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_right_ear_position(&mut self, pos: [f32; 3]) {
|
|
|
|
self.sink.set_right_ear_position(pos);
|
|
|
|
}
|
|
|
|
|
2019-08-31 09:06:24 +00:00
|
|
|
pub fn update(&mut self, dt: f32) {
|
|
|
|
match self.state {
|
|
|
|
ChannelState::Playing => {},
|
|
|
|
ChannelState::Stopping => {
|
|
|
|
self.fader.update(dt);
|
|
|
|
self.sink.set_volume(self.fader.get_volume());
|
|
|
|
if self.fader.is_finished() {
|
|
|
|
self.state = ChannelState::Stopped;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ChannelState::Stopped => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|