From d3d3d4d040211226b1b2856cfb611efb5daab2b7 Mon Sep 17 00:00:00 2001 From: admar Date: Sun, 23 Apr 2023 16:06:05 +0200 Subject: [PATCH] Fade in/out volume --- README.md | 2 -- clock.py | 49 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e6b66c7..5e2c032 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ A wake-up light for Raspberry Pi # To do / ideas in no specific order: -* themes * theme automatic (switch at intensity = 50% + simulate sunrise with background) * store / recall settings * calendar @@ -11,4 +10,3 @@ A wake-up light for Raspberry Pi * moonshine (esp32 + ledstrip + MQTT) * bluetooth speaker: https://www.collabora.com/news-and-blog/blog/2022/09/02/using-a-raspberry-pi-as-a-bluetooth-speaker-with-pipewire-wireplumber/ * replace VLC with GStreamer -* fade volume diff --git a/clock.py b/clock.py index 91f4d4a..54375d7 100644 --- a/clock.py +++ b/clock.py @@ -199,6 +199,11 @@ class MyClockWidget(FloatLayout): intensity_target_prev = None rgbw_prev = None + volume_target = 0 + volume_prev = 0 + volume_curr = 0 + volume_target_prev = None + # Ugly workaround for issue with Kivy and Raspberry Pi 3 + touch screen # For each type of touch event, handling of the event is ignored until the last event is more than touch_delay_time seconds old touch_delay_time = 0.1 @@ -379,6 +384,11 @@ class MyClockWidget(FloatLayout): x.add_widget(b) self.settings_menu_wake_up_sound_Ok_button = b + def set_volume(self, x): + self.mixer.setvolume(int(x * 100)) + self.volume_prev = self.volume_curr + self.volume_curr = x + def play_sound(self, source): alarm_settings = App.get_running_app().alarm_settings @@ -388,6 +398,7 @@ class MyClockWidget(FloatLayout): (self.player.get_state() == vlc.State.Ended) or \ (self.player.get_state() == vlc.State.Error): print("beep beep! " + source) + self.volume_target = alarm_settings.volume / 20.0 media = self.vlc.media_new(source) self.player.set_media(media) self.player.play() @@ -646,6 +657,27 @@ class MyClockWidget(FloatLayout): self.intensity_curr = self.intensity_curr + step + def process_volume_state(self): + alarm_settings = App.get_running_app().alarm_settings + + if (alarm_settings.alarm_playing) and (self.player.get_state() == vlc.State.Playing): + step = 0.005 + + if (self.volume_curr <= self.volume_target): + if (self.volume_curr + step > self.volume_target): + step = self.volume_target - self.volume_curr + else: + step = -step + if (self.volume_curr + step < self.volume_target): + step = self.volume_target - self.volume_curr + + if step != 0: + volume_next = self.volume_curr + step + self.set_volume(volume_next) + + if volume_next == 0: + self.stop_sound() + def check_play_sound(self): alarm_settings = App.get_running_app().alarm_settings @@ -677,6 +709,7 @@ class MyClockWidget(FloatLayout): self.set_leds() self.update_backlight() self.check_play_sound() + self.process_volume_state() def update_clock(self): self.draw_list_curr_frame.append(["self.hide_widget", self.ids["face_plate"], False]) @@ -981,9 +1014,9 @@ class MyClockWidget(FloatLayout): print("Volume changed to " + str(alarm_settings.volume)) - # Volume is in 0-100 range old_vol = self.mixer.getvolume() - self.mixer.setvolume(int(alarm_settings.volume * 5.0)) + self.volume_target = alarm_settings.volume / 20.0 + self.set_volume(self.volume_target) new_vol = self.mixer.getvolume() print("HW volume changed from " + str(old_vol) + " to " + str(new_vol)) @@ -1017,6 +1050,8 @@ class MyClockWidget(FloatLayout): elif self.light_state == "reading": self.light_state = "off" self.intensity_target = 0 + if alarm_settings.alarm_playing: + self.volume_target = 0 elif self.light_state == "sunrise": # allow enabling reading mode when sunrise has not yet reached that level if self.intensity_target < alarm_settings.reading_light_brightness / 20.0: @@ -1028,14 +1063,12 @@ class MyClockWidget(FloatLayout): elif self.light_state == "on": self.light_state = "reading" self.intensity_target = 0 - if alarm_settings.alarm_playing: - self.stop_sound() def on_play_button_pressed(self): print("play button pressed from view " + self.view) alarm_settings = App.get_running_app().alarm_settings if alarm_settings.alarm_playing: - self.stop_sound() + self.volume_target = 0 else: self.play_sound(alarm_settings.sound_source) @@ -1238,7 +1271,7 @@ class MyClockWidget(FloatLayout): elif self.view == "settings_menu_theme_select": pass elif self.view == "clock": - self.stop_sound() + self.volume_target = 0 super(MyClockWidget, self).on_touch_down(touch) @@ -1322,9 +1355,9 @@ class MyApp(App): if is_arm(): Window.borderless = True - # Volume is in 0-100 range + # Volume is in 0-1.0 range old_vol = clock_widget.mixer.getvolume() - clock_widget.mixer.setvolume(int(self.alarm_settings.volume * 5.0)) + clock_widget.volume_target = self.alarm_settings.volume / 20.0 new_vol = clock_widget.mixer.getvolume() print("HW volume changed from " + str(old_vol) + " to " + str(new_vol))