Add audio to settings

Former-commit-id: 6e18b95bb3460a3b6d971b89c767045dcdbe7344
This commit is contained in:
Louis Pearson 2019-05-20 10:54:54 -06:00
parent 50fc7549b9
commit 8dc35b8609
3 changed files with 61 additions and 12 deletions

View File

@ -23,15 +23,6 @@ impl AudioFrontend {
pub fn new() -> Self {
let mut device = rodio::default_output_device().unwrap();
for d in rodio::devices() {
if d.name().contains("jack") {
continue;
}
device = d;
break;
}
let mut sink =
rodio::SpatialSink::new(&device, [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]);
@ -80,4 +71,26 @@ impl AudioFrontend {
pub fn set_volume(&mut self, volume: f32) {
self.stream.set_volume(volume.min(1.0).max(0.0))
}
/// Returns a vec of the audio devices available.
/// Does not return rodio Device struct in case our audio backend changes.
// TODO: Decide if this should be an associated function
pub fn get_devices(&self) -> Vec<String> {
rodio::output_devices().map(|x| x.name()).collect()
}
/// Returns the name of the current audio device.
/// Does not return rodio Device struct in case our audio backend changes.
pub fn get_device(&self) -> String {
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;
}
}
}

View File

@ -165,9 +165,21 @@ fn main() {
audio: AudioFrontend::new(),
};
// TODO: Remove this when the volume setting can be saved
// Lower the volume to 50%
global_state.audio.set_volume(0.5);
// Load volume from audio file
global_state.audio.set_volume(settings.audio.music_volume);
global_state.settings.audio.audio_devices = global_state.audio.get_devices();
// Load last used audio device, or set the current audio device as the last
// used if there is no last used
if global_state.settings.audio.audio_device != "" {
global_state
.audio
.set_device(global_state.settings.audio.audio_device);
} else {
global_state.settings.audio.audio_device = global_state.audio.get_device();
global_state.settings.save_to_file();
}
// Set up the initial play state.
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(MainMenuState::new(&mut global_state))];

View File

@ -12,6 +12,7 @@ pub struct Settings {
pub controls: ControlSettings,
pub networking: NetworkingSettings,
pub log: Log,
pub audio: AudioSettings,
}
/// `ControlSettings` contains keybindings.
@ -52,6 +53,23 @@ pub struct Log {
pub file: PathBuf,
}
/// AudioSettings controls the volume of different audio subsystems and which
/// which device is used.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AudioSettings {
pub music_volume: f32,
pub sfx_volume: f32,
/// Audio Device that Voxygen wil use to play audio.
pub audio_device: String,
/// Audio devices that are available. Listed here so that it can be accessed
/// from the settings editor in the HUD, but skipped over because it is a
/// runtime specific detail that should not be persisted.
#[serde(skip)]
pub audio_devices: Vec<String>,
}
impl Default for Settings {
fn default() -> Self {
Settings {
@ -86,6 +104,12 @@ impl Default for Settings {
log: Log {
file: "voxygen.log".into(),
},
audio: AudioSettings {
music_volume: 0.5,
sfx_volume: 0.5,
audio_device: "".to_string(),
audio_devices: vec![],
},
}
}
}