2019-08-31 09:06:24 +00:00
|
|
|
pub mod channel;
|
2019-09-05 09:11:18 +00:00
|
|
|
pub mod fader;
|
2020-02-03 11:55:32 +00:00
|
|
|
pub mod music;
|
2019-11-23 08:26:39 +00:00
|
|
|
pub mod sfx;
|
2019-09-01 23:00:12 +00:00
|
|
|
pub mod soundcache;
|
2019-11-23 08:26:39 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
use channel::{MusicChannel, MusicChannelTag, SfxChannel};
|
2019-09-05 09:11:18 +00:00
|
|
|
use fader::Fader;
|
2019-09-01 23:00:12 +00:00
|
|
|
use soundcache::SoundCache;
|
2019-08-31 08:30:23 +00:00
|
|
|
|
2019-08-31 06:37:09 +00:00
|
|
|
use common::assets;
|
2019-10-06 09:18:23 +00:00
|
|
|
use cpal::traits::DeviceTrait;
|
2020-02-23 01:26:51 +00:00
|
|
|
use rodio::{source::Source, Decoder, Device};
|
2019-09-01 03:18:03 +00:00
|
|
|
use vek::*;
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2019-09-05 09:11:18 +00:00
|
|
|
const FALLOFF: f32 = 0.13;
|
2019-05-11 20:39:46 +00:00
|
|
|
|
|
|
|
pub struct AudioFrontend {
|
2019-08-31 06:37:09 +00:00
|
|
|
pub device: String,
|
|
|
|
pub device_list: Vec<String>,
|
|
|
|
audio_device: Option<Device>,
|
2019-09-01 23:00:12 +00:00
|
|
|
sound_cache: SoundCache,
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
music_channels: Vec<MusicChannel>,
|
|
|
|
sfx_channels: Vec<SfxChannel>,
|
2019-08-31 06:37:09 +00:00
|
|
|
|
|
|
|
sfx_volume: f32,
|
|
|
|
music_volume: f32,
|
2019-09-01 21:40:54 +00:00
|
|
|
|
2019-09-05 09:11:18 +00:00
|
|
|
listener_pos: Vec3<f32>,
|
|
|
|
listener_ori: Vec3<f32>,
|
2019-09-01 21:40:54 +00:00
|
|
|
|
2019-09-06 10:44:15 +00:00
|
|
|
listener_ear_left: Vec3<f32>,
|
|
|
|
listener_ear_right: Vec3<f32>,
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioFrontend {
|
2019-08-31 06:37:09 +00:00
|
|
|
/// Construct with given device
|
2020-02-15 21:30:44 +00:00
|
|
|
pub fn new(device: String, max_sfx_channels: usize) -> Self {
|
|
|
|
let mut sfx_channels = Vec::with_capacity(max_sfx_channels);
|
2019-09-05 09:03:24 +00:00
|
|
|
let audio_device = get_device_raw(&device);
|
2020-02-15 21:30:44 +00:00
|
|
|
|
2019-09-05 09:03:24 +00:00
|
|
|
if let Some(audio_device) = &audio_device {
|
2020-02-15 21:30:44 +00:00
|
|
|
for _ in 0..max_sfx_channels {
|
|
|
|
sfx_channels.push(SfxChannel::new(&audio_device));
|
2019-09-05 09:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 21:30:44 +00:00
|
|
|
|
2019-08-31 06:37:09 +00:00
|
|
|
Self {
|
|
|
|
device: device.clone(),
|
|
|
|
device_list: list_devices(),
|
2019-09-05 09:03:24 +00:00
|
|
|
audio_device,
|
2019-09-01 23:00:12 +00:00
|
|
|
sound_cache: SoundCache::new(),
|
2020-02-15 21:30:44 +00:00
|
|
|
music_channels: Vec::new(),
|
|
|
|
sfx_channels,
|
2019-08-31 06:37:09 +00:00
|
|
|
sfx_volume: 1.0,
|
|
|
|
music_volume: 1.0,
|
2019-09-01 21:40:54 +00:00
|
|
|
listener_pos: Vec3::zero(),
|
|
|
|
listener_ori: Vec3::zero(),
|
2019-09-06 10:44:15 +00:00
|
|
|
listener_ear_left: Vec3::zero(),
|
|
|
|
listener_ear_right: Vec3::zero(),
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct in `no-audio` mode for debugging
|
|
|
|
pub fn no_audio() -> Self {
|
2019-07-02 04:10:55 +00:00
|
|
|
Self {
|
2019-08-31 06:37:09 +00:00
|
|
|
device: "none".to_string(),
|
2019-09-07 09:46:21 +00:00
|
|
|
device_list: Vec::new(),
|
2019-08-31 06:37:09 +00:00
|
|
|
audio_device: None,
|
2019-09-01 23:00:12 +00:00
|
|
|
sound_cache: SoundCache::new(),
|
2020-02-15 21:30:44 +00:00
|
|
|
music_channels: Vec::new(),
|
|
|
|
sfx_channels: Vec::new(),
|
2019-08-31 06:37:09 +00:00
|
|
|
sfx_volume: 1.0,
|
|
|
|
music_volume: 1.0,
|
2019-09-01 21:40:54 +00:00
|
|
|
listener_pos: Vec3::zero(),
|
|
|
|
listener_ori: Vec3::zero(),
|
2019-09-06 10:44:15 +00:00
|
|
|
listener_ear_left: Vec3::zero(),
|
|
|
|
listener_ear_right: Vec3::zero(),
|
2019-06-28 17:05:20 +00:00
|
|
|
}
|
2019-05-18 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
/// Drop any unused music channels, and update their faders
|
2019-08-31 08:30:23 +00:00
|
|
|
pub fn maintain(&mut self, dt: f32) {
|
2020-02-15 21:30:44 +00:00
|
|
|
self.music_channels.retain(|c| !c.is_done());
|
|
|
|
|
|
|
|
for channel in self.music_channels.iter_mut() {
|
|
|
|
channel.maintain(dt);
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
fn get_sfx_channel(&mut self) -> Option<&mut SfxChannel> {
|
|
|
|
if self.audio_device.is_some() {
|
|
|
|
if let Some(channel) = self.sfx_channels.iter_mut().find(|c| c.is_done()) {
|
|
|
|
channel.set_volume(self.sfx_volume);
|
|
|
|
|
|
|
|
return Some(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve a music channel from the channel list. This inspects the
|
|
|
|
/// MusicChannelTag to determine whether we are transitioning between
|
|
|
|
/// music types and acts accordingly. For example transitioning between
|
|
|
|
/// `TitleMusic` and `Exploration` should fade out the title channel and
|
|
|
|
/// fade in a new `Exploration` channel.
|
|
|
|
fn get_music_channel(
|
2020-02-03 11:55:32 +00:00
|
|
|
&mut self,
|
2020-02-15 21:30:44 +00:00
|
|
|
next_channel_tag: MusicChannelTag,
|
|
|
|
) -> Option<&mut MusicChannel> {
|
|
|
|
if let Some(audio_device) = &self.audio_device {
|
|
|
|
if self.music_channels.is_empty() {
|
|
|
|
let mut next_music_channel = MusicChannel::new(&audio_device);
|
|
|
|
next_music_channel.set_volume(self.music_volume);
|
|
|
|
|
|
|
|
self.music_channels.push(next_music_channel);
|
|
|
|
} else {
|
|
|
|
let existing_channel = self.music_channels.last_mut()?;
|
|
|
|
|
|
|
|
if existing_channel.get_tag() != next_channel_tag {
|
|
|
|
// Fade the existing channel out. It will be removed when the fade completes.
|
|
|
|
existing_channel.set_fader(Fader::fade_out(2.0, self.music_volume));
|
|
|
|
|
|
|
|
let mut next_music_channel = MusicChannel::new(&audio_device);
|
|
|
|
|
|
|
|
next_music_channel.set_fader(Fader::fade_in(12.0, self.music_volume));
|
|
|
|
|
|
|
|
self.music_channels.push(next_music_channel);
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 14:02:51 +00:00
|
|
|
}
|
2020-02-15 21:30:44 +00:00
|
|
|
|
|
|
|
self.music_channels.last_mut()
|
2019-12-10 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 01:26:51 +00:00
|
|
|
pub fn play_sfx(&mut self, sound: &str, pos: Vec3<f32>, vol: Option<f32>) {
|
2019-12-10 14:02:51 +00:00
|
|
|
if self.audio_device.is_some() {
|
2019-09-06 10:36:42 +00:00
|
|
|
let calc_pos = ((pos - self.listener_pos) * FALLOFF).into_array();
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2020-02-23 01:26:51 +00:00
|
|
|
let sound = self
|
|
|
|
.sound_cache
|
|
|
|
.load_sound(sound)
|
|
|
|
.amplify(vol.unwrap_or(1.0));
|
2019-08-31 21:39:40 +00:00
|
|
|
|
2019-09-06 10:44:15 +00:00
|
|
|
let left_ear = self.listener_ear_left.into_array();
|
|
|
|
let right_ear = self.listener_ear_right.into_array();
|
2019-09-05 09:03:24 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
if let Some(channel) = self.get_sfx_channel() {
|
2019-09-05 09:03:24 +00:00
|
|
|
channel.set_emitter_position(calc_pos);
|
|
|
|
channel.set_left_ear_position(left_ear);
|
|
|
|
channel.set_right_ear_position(right_ear);
|
|
|
|
channel.play(sound);
|
2019-12-10 14:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
fn play_music(&mut self, sound: &str, channel_tag: MusicChannelTag) {
|
|
|
|
if let Some(channel) = self.get_music_channel(channel_tag) {
|
|
|
|
let file = assets::load_file(&sound, &["ogg"]).expect("Failed to load sound");
|
|
|
|
let sound = Decoder::new(file).expect("Failed to decode sound");
|
2019-12-10 14:02:51 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
channel.play(sound, channel_tag);
|
2019-08-31 21:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 09:11:18 +00:00
|
|
|
pub fn set_listener_pos(&mut self, pos: &Vec3<f32>, ori: &Vec3<f32>) {
|
2019-09-01 21:40:54 +00:00
|
|
|
self.listener_pos = pos.clone();
|
|
|
|
self.listener_ori = ori.normalized();
|
|
|
|
|
|
|
|
let up = Vec3::new(0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
let pos_left = up.cross(self.listener_ori.clone()).normalized();
|
|
|
|
let pos_right = self.listener_ori.cross(up.clone()).normalized();
|
2019-09-01 03:18:03 +00:00
|
|
|
|
2019-09-06 10:44:15 +00:00
|
|
|
self.listener_ear_left = pos_left;
|
|
|
|
self.listener_ear_right = pos_right;
|
2019-09-01 03:18:03 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
for channel in self.sfx_channels.iter_mut() {
|
|
|
|
if !channel.is_done() {
|
2019-12-10 14:02:51 +00:00
|
|
|
// TODO: Update this to correctly determine the updated relative position of
|
|
|
|
// the SFX emitter when the player (listener) moves
|
|
|
|
// channel.set_emitter_position(
|
|
|
|
// ((channel.pos - self.listener_pos) * FALLOFF).into_array(),
|
|
|
|
// );
|
2019-09-01 03:18:03 +00:00
|
|
|
channel.set_left_ear_position(pos_left.into_array());
|
|
|
|
channel.set_right_ear_position(pos_right.into_array());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
pub fn play_title_music(&mut self) {
|
2020-02-03 11:55:32 +00:00
|
|
|
if self.music_enabled() {
|
|
|
|
self.play_music(
|
|
|
|
"voxygen.audio.soundtrack.veloren_title_tune",
|
2020-02-15 21:30:44 +00:00
|
|
|
MusicChannelTag::TitleMusic,
|
2020-02-03 11:55:32 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
pub fn play_exploration_music(&mut self, item: &str) {
|
|
|
|
if self.music_enabled() {
|
|
|
|
self.play_music(item, MusicChannelTag::Exploration)
|
2019-08-31 08:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn get_sfx_volume(&self) -> f32 { self.sfx_volume }
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn get_music_volume(&self) -> f32 { self.music_volume }
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2020-02-03 11:55:32 +00:00
|
|
|
pub fn sfx_enabled(&self) -> bool { self.sfx_volume > 0.0 }
|
|
|
|
|
|
|
|
pub fn music_enabled(&self) -> bool { self.music_volume > 0.0 }
|
|
|
|
|
2019-09-24 16:25:24 +00:00
|
|
|
pub fn set_sfx_volume(&mut self, sfx_volume: f32) {
|
|
|
|
self.sfx_volume = sfx_volume;
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
for channel in self.sfx_channels.iter_mut() {
|
|
|
|
channel.set_volume(sfx_volume);
|
2019-05-20 16:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2019-09-24 16:25:24 +00:00
|
|
|
pub fn set_music_volume(&mut self, music_volume: f32) {
|
|
|
|
self.music_volume = music_volume;
|
2019-08-31 06:37:09 +00:00
|
|
|
|
2020-02-15 21:30:44 +00:00
|
|
|
for channel in self.music_channels.iter_mut() {
|
|
|
|
channel.set_volume(music_volume);
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 03:18:03 +00:00
|
|
|
// TODO: figure out how badly this will break things when it is called
|
2019-08-31 06:37:09 +00:00
|
|
|
pub fn set_device(&mut self, name: String) {
|
|
|
|
self.device = name.clone();
|
2019-09-05 09:03:24 +00:00
|
|
|
self.audio_device = get_device_raw(&name);
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the default audio device.
|
|
|
|
/// Does not return rodio Device struct in case our audio backend changes.
|
2020-03-28 13:12:35 +00:00
|
|
|
pub fn get_default_device() -> Option<String> {
|
|
|
|
rodio::default_output_device().map(|dev| dev.name().expect("Unable to get device name"))
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vec of the audio devices available.
|
|
|
|
/// Does not return rodio Device struct in case our audio backend changes.
|
|
|
|
pub fn list_devices() -> Vec<String> {
|
2019-10-06 09:18:23 +00:00
|
|
|
list_devices_raw()
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.name().expect("Unable to get device name"))
|
|
|
|
.collect()
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns vec of devices
|
|
|
|
fn list_devices_raw() -> Vec<Device> {
|
2019-10-06 09:18:23 +00:00
|
|
|
rodio::output_devices()
|
|
|
|
.expect("Unable to get output devices")
|
|
|
|
.collect()
|
2019-08-31 06:37:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 09:03:24 +00:00
|
|
|
fn get_device_raw(device: &str) -> Option<Device> {
|
2019-10-06 09:18:23 +00:00
|
|
|
rodio::output_devices()
|
|
|
|
.expect("Unable to get output devices")
|
|
|
|
.find(|d| d.name().expect("Unable to get device name") == device)
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|