2019-05-21 01:21:31 +00:00
|
|
|
use crate::settings::AudioSettings;
|
2019-05-11 20:39:46 +00:00
|
|
|
use common::assets;
|
2019-06-28 18:19:18 +00:00
|
|
|
use log::error;
|
2019-06-06 14:48:41 +00:00
|
|
|
use rodio::{Decoder, Device, SpatialSink};
|
|
|
|
use std::iter::Iterator;
|
2019-06-28 17:05:20 +00:00
|
|
|
use std::panic::catch_unwind;
|
2019-05-11 20:39:46 +00:00
|
|
|
|
|
|
|
pub struct AudioFrontend {
|
|
|
|
device: Device,
|
2019-05-21 05:07:57 +00:00
|
|
|
// Performance optimisation, iterating through available audio devices takes time
|
|
|
|
devices: Vec<Device>,
|
2019-05-31 01:33:54 +00:00
|
|
|
// streams: HashMap<String, SpatialSink>, //always use SpatialSink even if no position is used for now
|
2019-06-28 17:05:20 +00:00
|
|
|
stream: Option<SpatialSink>,
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioFrontend {
|
2019-05-21 01:21:31 +00:00
|
|
|
pub fn new(settings: &AudioSettings) -> Self {
|
2019-06-06 14:48:41 +00:00
|
|
|
let device = match &settings.audio_device {
|
2019-05-22 11:29:38 +00:00
|
|
|
Some(dev) => rodio::output_devices()
|
|
|
|
.find(|x| &x.name() == dev)
|
|
|
|
.or_else(rodio::default_output_device)
|
|
|
|
.expect("No Audio devices found"),
|
|
|
|
None => rodio::default_output_device().expect("No audio devices found"),
|
|
|
|
};
|
2019-05-11 20:39:46 +00:00
|
|
|
|
2019-06-28 17:05:20 +00:00
|
|
|
let sink = catch_unwind(|| {
|
|
|
|
let sink = rodio::SpatialSink::new(
|
|
|
|
&device,
|
|
|
|
[0.0, 0.0, 0.0],
|
|
|
|
[1.0, 0.0, 0.0],
|
|
|
|
[-1.0, 0.0, 0.0],
|
|
|
|
);
|
|
|
|
sink.set_volume(settings.music_volume);
|
|
|
|
Some(sink)
|
|
|
|
})
|
2019-06-28 18:19:18 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!("Error creating a SpatialSink (audio): ");
|
|
|
|
None
|
|
|
|
});
|
2019-05-18 19:28:12 +00:00
|
|
|
|
2019-05-11 20:39:46 +00:00
|
|
|
AudioFrontend {
|
|
|
|
device,
|
2019-05-18 19:28:12 +00:00
|
|
|
// streams: HashMap::<String, SpatialSink>::new(),
|
|
|
|
stream: sink,
|
2019-05-22 11:29:38 +00:00
|
|
|
devices: AudioFrontend::list_devices_raw(),
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn play_music(&mut self, path: &str) {
|
2019-05-18 19:28:12 +00:00
|
|
|
let bufreader = assets::load_from_path(path).unwrap();
|
|
|
|
let src = Decoder::new(bufreader).unwrap();
|
2019-05-11 20:39:46 +00:00
|
|
|
|
2019-05-20 18:25:44 +00:00
|
|
|
// TODO: stop previous audio from playing. Sink has this ability, but
|
|
|
|
// SpatialSink does not for some reason. This means that we will
|
|
|
|
// probably want to use sinks for music, and SpatialSink for sfx.
|
2019-06-28 17:05:20 +00:00
|
|
|
if let Some(s) = &self.stream {
|
|
|
|
s.append(src);
|
|
|
|
}
|
2019-05-18 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maintain(&mut self) {
|
|
|
|
let music = [
|
|
|
|
"voxygen/audio/soundtrack/Ethereal_Bonds.ogg",
|
2019-05-20 13:46:11 +00:00
|
|
|
"voxygen/audio/soundtrack/field_grazing.ogg",
|
2019-05-18 19:28:12 +00:00
|
|
|
"voxygen/audio/soundtrack/fiesta_del_pueblo.ogg",
|
|
|
|
"voxygen/audio/soundtrack/library_theme_with_harpsichord.ogg",
|
|
|
|
"voxygen/audio/soundtrack/Mineral_Deposits.ogg",
|
2019-05-22 09:00:16 +00:00
|
|
|
//"voxygen/audio/soundtrack/Ruination.ogg",
|
2019-05-18 19:28:12 +00:00
|
|
|
"voxygen/audio/soundtrack/sacred_temple.ogg",
|
2019-05-28 18:29:16 +00:00
|
|
|
"voxygen/audio/soundtrack/Snowtop_volume.ogg",
|
2019-05-18 19:28:12 +00:00
|
|
|
"voxygen/audio/soundtrack/veloren_title_tune-3.ogg",
|
|
|
|
];
|
2019-06-28 17:05:20 +00:00
|
|
|
|
|
|
|
if let Some(s) = &self.stream {
|
|
|
|
if s.empty() {
|
|
|
|
let i = rand::random::<usize>() % music.len();
|
|
|
|
self.play_music(music[i]);
|
|
|
|
}
|
2019-05-18 19:28:12 +00:00
|
|
|
}
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
2019-05-19 19:31:32 +00:00
|
|
|
|
|
|
|
pub fn set_volume(&mut self, volume: f32) {
|
2019-06-28 17:05:20 +00:00
|
|
|
if let Some(s) = &self.stream {
|
|
|
|
s.set_volume(volume.min(1.0).max(0.0));
|
|
|
|
}
|
2019-05-19 19:31:32 +00:00
|
|
|
}
|
2019-05-20 16:54:54 +00:00
|
|
|
|
|
|
|
/// Returns a vec of the audio devices available.
|
|
|
|
/// Does not return rodio Device struct in case our audio backend changes.
|
2019-05-22 11:29:38 +00:00
|
|
|
pub fn list_device_names(&self) -> Vec<String> {
|
2019-05-21 05:07:57 +00:00
|
|
|
self.devices.iter().map(|x| x.name()).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns vec of devices
|
2019-05-22 11:29:38 +00:00
|
|
|
fn list_devices_raw() -> Vec<Device> {
|
2019-05-21 05:07:57 +00:00
|
|
|
rodio::output_devices().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Caches vec of devices for later reference
|
2019-05-22 11:29:38 +00:00
|
|
|
fn maintain_devices(&mut self) {
|
|
|
|
self.devices = AudioFrontend::list_devices_raw()
|
2019-05-20 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 01:21:31 +00:00
|
|
|
/// Returns the default audio device.
|
|
|
|
/// Does not return rodio Device struct in case our audio backend changes.
|
|
|
|
pub fn get_default_device() -> String {
|
|
|
|
rodio::default_output_device()
|
|
|
|
.expect("No audio output devices detected.")
|
|
|
|
.name()
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:54:54 +00:00
|
|
|
/// Returns the name of the current audio device.
|
|
|
|
/// Does not return rodio Device struct in case our audio backend changes.
|
2019-05-22 11:29:38 +00:00
|
|
|
pub fn get_device_name(&self) -> String {
|
2019-05-20 16:54:54 +00:00
|
|
|
self.device.name()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the current audio device from a string.
|
|
|
|
/// Does not use the rodio Device struct in case that detail changes.
|
|
|
|
/// If the string is an invalid audio device, then no change is made.
|
|
|
|
pub fn set_device(&mut self, name: String) {
|
|
|
|
if let Some(dev) = rodio::output_devices().find(|x| x.name() == name) {
|
|
|
|
self.device = dev;
|
2019-06-28 17:05:20 +00:00
|
|
|
self.stream = catch_unwind(|| {
|
|
|
|
let sink = rodio::SpatialSink::new(
|
|
|
|
&self.device,
|
|
|
|
[0.0, 0.0, 0.0],
|
|
|
|
[1.0, 0.0, 0.0],
|
|
|
|
[-1.0, 0.0, 0.0],
|
|
|
|
);
|
|
|
|
Some(sink)
|
|
|
|
})
|
2019-06-28 18:19:18 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!("Error creating a SpatialSink (audio): ");
|
|
|
|
None
|
|
|
|
});
|
2019-05-20 16:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|