veloren/voxygen/src/audio/channel.rs

109 lines
2.8 KiB
Rust
Raw Normal View History

2019-08-31 09:06:24 +00:00
use crate::audio::fader::Fader;
use rodio::{Device, Sample, Source, SpatialSink};
use vek::*;
2019-08-31 09:06:24 +00:00
#[derive(PartialEq, Clone, Copy)]
pub enum AudioType {
Sfx,
Music,
2019-09-05 09:03:24 +00:00
None,
2019-08-31 09:06:24 +00:00
}
#[derive(PartialEq, Clone, Copy)]
enum ChannelState {
2019-09-06 10:25:17 +00:00
// Init,
// ToPlay,
// Loading,
2019-08-31 09:06:24 +00:00
Playing,
Stopping,
Stopped,
}
#[derive(PartialEq, Clone, Copy)]
pub enum ChannelTag {
TitleMusic,
Soundtrack,
}
2019-08-31 09:06:24 +00:00
pub struct Channel {
id: usize,
sink: SpatialSink,
audio_type: AudioType,
state: ChannelState,
fader: Fader,
tag: Option<ChannelTag>,
2019-09-05 09:11:18 +00:00
pub pos: Vec3<f32>,
2019-08-31 09:06:24 +00:00
}
2019-09-05 09:03:24 +00:00
// TODO: Implement asynchronous loading
2019-08-31 09:06:24 +00:00
impl Channel {
2019-09-05 09:03:24 +00:00
/// Create an empty channel for future use
pub fn new(device: &Device) -> Self {
Self {
id: 0,
sink: SpatialSink::new(device, [0.0; 3], [1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]),
2019-09-05 09:03:24 +00:00
audio_type: AudioType::None,
state: ChannelState::Stopped,
fader: Fader::fade_in(0.0),
tag: None,
2019-09-05 09:03:24 +00:00
pos: Vec3::zero(),
}
}
pub fn play<S>(&mut self, source: S)
where
S: Source + Send + 'static,
S::Item: Sample,
S::Item: Send,
2019-09-05 09:11:18 +00:00
<S as std::iter::Iterator>::Item: std::fmt::Debug,
2019-09-05 09:03:24 +00:00
{
self.state = ChannelState::Playing;
self.sink.append(source);
}
pub fn is_done(&self) -> bool { self.sink.empty() || self.state == ChannelState::Stopped }
2019-08-31 09:06:24 +00:00
pub fn set_tag(&mut self, tag: Option<ChannelTag>) { self.tag = tag; }
pub fn get_tag(&self) -> Option<ChannelTag> { self.tag }
2019-08-31 09:06:24 +00:00
pub fn stop(&mut self, fader: Fader) {
self.state = ChannelState::Stopping;
self.fader = fader;
}
pub fn get_id(&self) -> usize { self.id }
2019-08-31 09:06:24 +00:00
pub fn set_id(&mut self, new_id: usize) { self.id = new_id; }
2019-09-05 09:03:24 +00:00
pub fn get_audio_type(&self) -> AudioType { self.audio_type }
2019-08-31 09:06:24 +00:00
pub fn set_audio_type(&mut self, audio_type: AudioType) { self.audio_type = audio_type; }
pub fn set_volume(&mut self, volume: f32) { self.sink.set_volume(volume); }
2019-08-31 09:06:24 +00:00
pub fn set_emitter_position(&mut self, pos: [f32; 3]) { self.sink.set_emitter_position(pos); }
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 {
2019-09-06 10:25:17 +00:00
// ChannelState::Init | ChannelState::ToPlay | ChannelState::Loading => {}
ChannelState::Playing => {},
2019-09-05 09:11:18 +00:00
ChannelState::Stopping => {
2019-08-31 09:06:24 +00:00
self.fader.update(dt);
self.sink.set_volume(self.fader.get_volume());
2019-09-05 09:03:24 +00:00
2019-08-31 09:06:24 +00:00
if self.fader.is_finished() {
self.state = ChannelState::Stopped;
}
},
ChannelState::Stopped => {},
2019-08-31 09:06:24 +00:00
}
}
}