From b5aea464f3b92065da879a909163a18b0d081555 Mon Sep 17 00:00:00 2001 From: jiminycrick Date: Mon, 19 Oct 2020 17:28:25 -0700 Subject: [PATCH] Switch music at biome transitions --- assets/voxygen/audio/soundtrack.ron | 8 ++++---- voxygen/src/audio/ambient.rs | 1 - voxygen/src/audio/channel.rs | 12 +++++++++--- voxygen/src/audio/mod.rs | 15 +++++++++++++++ voxygen/src/audio/music.rs | 27 ++++++++++++++++++++++++--- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/assets/voxygen/audio/soundtrack.ron b/assets/voxygen/audio/soundtrack.ron index 7f628765a3..77b33a9978 100644 --- a/assets/voxygen/audio/soundtrack.ron +++ b/assets/voxygen/audio/soundtrack.ron @@ -31,8 +31,8 @@ title: "Wandering Voices", path: "voxygen.audio.soundtrack.wandering_voices", length: 137.0, - timing: Some(Night), - biome: Some(Snowlands), + timing: Some(Day), + biome: Some(Grassland), artist: "Aeronic", ), ( @@ -48,7 +48,7 @@ path: "voxygen.audio.soundtrack.mineral_deposits", length: 148.0, timing: Some(Day), - biome: Some(Snowlands), + biome: Some(Forest), artist: "Aeronic", ), ( @@ -56,7 +56,7 @@ path: "voxygen.audio.soundtrack.moonbeams", length: 158.0, timing: Some(Night), - biome: Some(Snowlands), + biome: Some(Forest), artist: "Aeronic", ), ( diff --git a/voxygen/src/audio/ambient.rs b/voxygen/src/audio/ambient.rs index d5e283e291..0f82d28583 100644 --- a/voxygen/src/audio/ambient.rs +++ b/voxygen/src/audio/ambient.rs @@ -113,7 +113,6 @@ impl AmbientMgr { let game_time = (state.get_time_of_day() as u64 % 86400) as u32; let current_period_of_day = Self::get_current_day_period(game_time); let current_biome = Self::get_current_biome(client); - println!("Ambient Current biome: {:?}", current_biome); let mut rng = thread_rng(); let maybe_track = self diff --git a/voxygen/src/audio/channel.rs b/voxygen/src/audio/channel.rs index 5780dc7ac8..b51fd69590 100644 --- a/voxygen/src/audio/channel.rs +++ b/voxygen/src/audio/channel.rs @@ -62,9 +62,9 @@ impl MusicChannel { } } - // Play a music track item on this channel. If the channel has an existing track - // playing, the new sounds will be appended and played once they complete. - // Otherwise it will begin playing immediately. + /// Play a music track item on this channel. If the channel has an existing + /// track playing, the new sounds will be appended and played once they + /// complete. Otherwise it will begin playing immediately. pub fn play(&mut self, source: S, tag: MusicChannelTag) where S: Source + Send + 'static, @@ -82,6 +82,12 @@ impl MusicChannel { }; } + /// Stop whatever is playing on a given channel + pub fn stop(&mut self, tag: MusicChannelTag) { + self.tag = tag; + self.sink.stop(); + } + /// Set the volume of the current channel. If the channel is currently /// fading, the volume of the fader is updated to this value. pub fn set_volume(&mut self, volume: f32) { diff --git a/voxygen/src/audio/mod.rs b/voxygen/src/audio/mod.rs index 9f065bc577..2ad50f8476 100644 --- a/voxygen/src/audio/mod.rs +++ b/voxygen/src/audio/mod.rs @@ -205,6 +205,15 @@ impl AudioFrontend { let sound = Decoder::new(file).expect("Failed to decode sound"); channel.play(sound, channel_tag); + //channel.set_fader(Fader::fade_in(2.0, music_volume)); + } + } + + fn stop_music(&mut self, channel_tag: MusicChannelTag) { + let music_volume = self.music_volume; + if let Some(channel) = self.get_music_channel(channel_tag) { + //channel.set_fader(Fader::fade_out(5.0, music_volume)); + channel.stop(channel_tag); } } @@ -249,6 +258,12 @@ impl AudioFrontend { } } + pub fn stop_exploration_music(&mut self) { + if self.music_enabled() { + self.stop_music(MusicChannelTag::Exploration) + } + } + pub fn play_exploration_ambient(&mut self, item: &str) { if self.ambient_enabled() { self.play_ambient(item, AmbientChannelTag::Exploration) diff --git a/voxygen/src/audio/music.rs b/voxygen/src/audio/music.rs index 5f11fb9c7d..b81500ff1c 100644 --- a/voxygen/src/audio/music.rs +++ b/voxygen/src/audio/music.rs @@ -83,6 +83,8 @@ pub struct MusicMgr { /// The title of the last track played. Used to prevent a track /// being played twice in a row last_track: String, + last_biome: BiomeKind, + playing: bool, } impl MusicMgr { @@ -93,27 +95,46 @@ impl MusicMgr { began_playing: Instant::now(), next_track_change: 0.0, last_track: String::from("None"), + last_biome: BiomeKind::Void, + playing: false, } } /// Checks whether the previous track has completed. If so, sends a /// request to play the next (random) track pub fn maintain(&mut self, audio: &mut AudioFrontend, state: &State, client: &Client) { + // Gets the current player biome + let current_biome: BiomeKind = match client.current_chunk() { + Some(chunk) => chunk.meta().biome(), + _ => self.last_biome, + }; + if audio.music_enabled() && !self.soundtrack.tracks.is_empty() - && self.began_playing.elapsed().as_secs_f64() > self.next_track_change + && (self.began_playing.elapsed().as_secs_f64() > self.next_track_change + || !self.playing) { + println!("It shoooooooooooooooooooooooooooooooooooooooooooooooooooooooould play!!!"); self.play_random_track(audio, state, client); + self.playing = true; + } else if current_biome != self.last_biome { + println!( + "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSStop!\ + !!" + ); + audio.stop_exploration_music(); + self.playing = false; } + self.last_biome = current_biome; } fn play_random_track(&mut self, audio: &mut AudioFrontend, state: &State, client: &Client) { - const SILENCE_BETWEEN_TRACKS_SECONDS: f64 = 45.0; + //const SILENCE_BETWEEN_TRACKS_SECONDS: f64 = 45.0; + const SILENCE_BETWEEN_TRACKS_SECONDS: f64 = 5.0; let game_time = (state.get_time_of_day() as u64 % 86400) as u32; let current_period_of_day = Self::get_current_day_period(game_time); let current_biome = Self::get_current_biome(client); - println!("======> Music Current biome: {:?}", current_biome); let mut rng = thread_rng(); let maybe_track = self