From f452362c2acbfdabae3488f3eca79f91e268b079 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Wed, 21 Apr 2021 08:39:49 +0100 Subject: [PATCH] Add volume and stop, improve fading --- analyse_tracks.py | 17 +++++++++-------- app/config.py | 2 +- app/music.py | 40 ++++++++++++++++++++++++++++++++++------ app/musicmuster.py | 9 +++++++++ app/playlists.py | 9 ++++++++- app/ui/main_window.ui | 10 ++++++++++ 6 files changed, 71 insertions(+), 16 deletions(-) diff --git a/analyse_tracks.py b/analyse_tracks.py index 985046c..4a8eb5b 100755 --- a/analyse_tracks.py +++ b/analyse_tracks.py @@ -2,9 +2,10 @@ import os -from pydub import AudioSegment +from pydub import AudioSegment, effects -DIR = "/home/kae/git/musicmuster/archive" +# DIR = "/home/kae/git/musicmuster/archive" +DIR = "/home/kae/git/musicmuster" # Iterate through flac files @@ -12,13 +13,13 @@ DIR = "/home/kae/git/musicmuster/archive" def process(path): audio = AudioSegment.from_file(path, "flac") print(path) - print(f"{audio.dBFS=}") - - - - - + print(f"audio.dBFS={audio.dBFS}") + print(f"audio.max_dBFS={audio.max_dBFS}") + print(f"audio.rms={audio.rms}") + print(f"audio.max={audio.max}") print("-----------------") + # normalised = effects.normalize(audio) + # normalised.export(os.path.basename(path) + "n.flac", format="flac") for f in os.scandir(DIR): diff --git a/app/config.py b/app/config.py index c14f5bf..e284bae 100644 --- a/app/config.py +++ b/app/config.py @@ -19,7 +19,7 @@ class Config(object): DISPLAY_SQL = False ERRORS_TO = ['kae@midnighthax.com'] FADE_STEPS = 20 - FADE_TIME = 5000 + FADE_TIME = 3000 LOG_LEVEL_STDERR = logging.DEBUG LOG_LEVEL_SYSLOG = logging.DEBUG LOG_NAME = "musicmuster" diff --git a/app/music.py b/app/music.py index 69cdc3a..16031bb 100644 --- a/app/music.py +++ b/app/music.py @@ -13,10 +13,11 @@ class Music: Manage the playing of music tracks """ - def __init__(self): + def __init__(self, max_volume=100): self.fading = False self.player = None self.track_path = None + self.max_volume = max_volume def fade(self): """ @@ -50,14 +51,26 @@ class Music: DEBUG("_fade()") fade_time = Config.FADE_TIME / 1000 - sleep_time = fade_time / Config.FADE_STEPS - step_percent = int((Config.VOLUME_VLC_MAX / Config.FADE_STEPS) * -1) + steps = Config.FADE_STEPS + sleep_time = fade_time / steps + + # We reduce volume by one mesure first, then by two measures, + # then three, and so on. + + # The sum of the arithmetic sequence 1, 2, 3, ..n is + # (n**2 + n) / 2 + total_measures_count = (steps**2 + steps) / 2 # Take a copy of current player to allow another track to be # started without interfering here p = self.player - for i in range(Config.VOLUME_VLC_MAX, 0, step_percent): - p.audio_set_volume(i) + measures_to_reduce_by = 0 + for i in range(1, steps + 1): + measures_to_reduce_by += i + DEBUG(f"measures_to_reduce_by={measures_to_reduce_by}") + volume_factor = 1 - (measures_to_reduce_by / total_measures_count) + DEBUG(f"volume_factor={volume_factor}") + p.audio_set_volume(int(self.max_volume * volume_factor)) sleep(sleep_time) p.pause() @@ -85,7 +98,7 @@ class Music: self.track_path = path self.player = vlc.MediaPlayer(path) - self.player.audio_set_volume(Config.VOLUME_VLC_MAX) + self.player.audio_set_volume(self.max_volume) self.player.play() def playing(self): @@ -108,3 +121,18 @@ class Music: "Set current play time in milliseconds from start" return self.player.set_time(ms) + + def set_volume(self, volume): + "Set maximum volume used for player" + + self.max_volume = volume + self.player.audio_set_volume(volume) + + def stop(self): + "Immediately stop playing" + + position = self.player.get_position() + self.player.stop() + self.player.release() + + return position diff --git a/app/musicmuster.py b/app/musicmuster.py index c335ccd..0127189 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -119,6 +119,8 @@ class Window(QMainWindow, Ui_MainWindow): self.btnPrevious.clicked.connect(self.play_previous) self.btnSetNext.clicked.connect(self.set_next_track) self.btnSkipNext.clicked.connect(self.play_next) + self.btnStop.clicked.connect(self.playlist.stop) + self.spnVolume.valueChanged.connect(self.change_volume) self.timer.timeout.connect(self.tick) @@ -133,6 +135,13 @@ class Window(QMainWindow, Ui_MainWindow): if ok: self.playlist.create_playlist(dlg.textValue()) + def change_volume(self, volume): + "Change player maximum volume" + + DEBUG(f"change_volume({volume})") + + self.playlist.music.set_volume(volume) + def disable_play_next_controls(self): DEBUG("disable_play_next_controls()") self.actionPlay_next.setEnabled(False) diff --git a/app/playlists.py b/app/playlists.py index 8aa38fc..68b9c63 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -282,7 +282,8 @@ class Playlist(QTableWidget): self.repaint() def fade(self): - self.music.fade() + self.previous_track = self.current_track + self.previous_track_position = self.music.fade() def get_current_artist(self): try: @@ -602,6 +603,12 @@ class Playlist(QTableWidget): return False + def stop(self): + "Stop playing immediately" + + self.previous_track = self.current_track + self.previous_track_position = self.music.stop() + def music_ended(self): "Update display" diff --git a/app/ui/main_window.ui b/app/ui/main_window.ui index 7e84781..29ee290 100644 --- a/app/ui/main_window.ui +++ b/app/ui/main_window.ui @@ -435,6 +435,16 @@ border: 1px solid rgb(85, 87, 83); + + + + 100 + + + 100 + + +