Add volume and stop, improve fading
This commit is contained in:
parent
1c078a2d69
commit
f452362c2a
@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
import os
|
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
|
# Iterate through flac files
|
||||||
|
|
||||||
@ -12,13 +13,13 @@ DIR = "/home/kae/git/musicmuster/archive"
|
|||||||
def process(path):
|
def process(path):
|
||||||
audio = AudioSegment.from_file(path, "flac")
|
audio = AudioSegment.from_file(path, "flac")
|
||||||
print(path)
|
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("-----------------")
|
print("-----------------")
|
||||||
|
# normalised = effects.normalize(audio)
|
||||||
|
# normalised.export(os.path.basename(path) + "n.flac", format="flac")
|
||||||
|
|
||||||
|
|
||||||
for f in os.scandir(DIR):
|
for f in os.scandir(DIR):
|
||||||
|
|||||||
@ -19,7 +19,7 @@ class Config(object):
|
|||||||
DISPLAY_SQL = False
|
DISPLAY_SQL = False
|
||||||
ERRORS_TO = ['kae@midnighthax.com']
|
ERRORS_TO = ['kae@midnighthax.com']
|
||||||
FADE_STEPS = 20
|
FADE_STEPS = 20
|
||||||
FADE_TIME = 5000
|
FADE_TIME = 3000
|
||||||
LOG_LEVEL_STDERR = logging.DEBUG
|
LOG_LEVEL_STDERR = logging.DEBUG
|
||||||
LOG_LEVEL_SYSLOG = logging.DEBUG
|
LOG_LEVEL_SYSLOG = logging.DEBUG
|
||||||
LOG_NAME = "musicmuster"
|
LOG_NAME = "musicmuster"
|
||||||
|
|||||||
40
app/music.py
40
app/music.py
@ -13,10 +13,11 @@ class Music:
|
|||||||
Manage the playing of music tracks
|
Manage the playing of music tracks
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, max_volume=100):
|
||||||
self.fading = False
|
self.fading = False
|
||||||
self.player = None
|
self.player = None
|
||||||
self.track_path = None
|
self.track_path = None
|
||||||
|
self.max_volume = max_volume
|
||||||
|
|
||||||
def fade(self):
|
def fade(self):
|
||||||
"""
|
"""
|
||||||
@ -50,14 +51,26 @@ class Music:
|
|||||||
DEBUG("_fade()")
|
DEBUG("_fade()")
|
||||||
|
|
||||||
fade_time = Config.FADE_TIME / 1000
|
fade_time = Config.FADE_TIME / 1000
|
||||||
sleep_time = fade_time / Config.FADE_STEPS
|
steps = Config.FADE_STEPS
|
||||||
step_percent = int((Config.VOLUME_VLC_MAX / Config.FADE_STEPS) * -1)
|
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
|
# Take a copy of current player to allow another track to be
|
||||||
# started without interfering here
|
# started without interfering here
|
||||||
p = self.player
|
p = self.player
|
||||||
for i in range(Config.VOLUME_VLC_MAX, 0, step_percent):
|
measures_to_reduce_by = 0
|
||||||
p.audio_set_volume(i)
|
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)
|
sleep(sleep_time)
|
||||||
|
|
||||||
p.pause()
|
p.pause()
|
||||||
@ -85,7 +98,7 @@ class Music:
|
|||||||
self.track_path = path
|
self.track_path = path
|
||||||
|
|
||||||
self.player = vlc.MediaPlayer(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()
|
self.player.play()
|
||||||
|
|
||||||
def playing(self):
|
def playing(self):
|
||||||
@ -108,3 +121,18 @@ class Music:
|
|||||||
"Set current play time in milliseconds from start"
|
"Set current play time in milliseconds from start"
|
||||||
|
|
||||||
return self.player.set_time(ms)
|
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
|
||||||
|
|||||||
@ -119,6 +119,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
self.btnPrevious.clicked.connect(self.play_previous)
|
self.btnPrevious.clicked.connect(self.play_previous)
|
||||||
self.btnSetNext.clicked.connect(self.set_next_track)
|
self.btnSetNext.clicked.connect(self.set_next_track)
|
||||||
self.btnSkipNext.clicked.connect(self.play_next)
|
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)
|
self.timer.timeout.connect(self.tick)
|
||||||
|
|
||||||
@ -133,6 +135,13 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
if ok:
|
if ok:
|
||||||
self.playlist.create_playlist(dlg.textValue())
|
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):
|
def disable_play_next_controls(self):
|
||||||
DEBUG("disable_play_next_controls()")
|
DEBUG("disable_play_next_controls()")
|
||||||
self.actionPlay_next.setEnabled(False)
|
self.actionPlay_next.setEnabled(False)
|
||||||
|
|||||||
@ -282,7 +282,8 @@ class Playlist(QTableWidget):
|
|||||||
self.repaint()
|
self.repaint()
|
||||||
|
|
||||||
def fade(self):
|
def fade(self):
|
||||||
self.music.fade()
|
self.previous_track = self.current_track
|
||||||
|
self.previous_track_position = self.music.fade()
|
||||||
|
|
||||||
def get_current_artist(self):
|
def get_current_artist(self):
|
||||||
try:
|
try:
|
||||||
@ -602,6 +603,12 @@ class Playlist(QTableWidget):
|
|||||||
|
|
||||||
return False
|
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):
|
def music_ended(self):
|
||||||
"Update display"
|
"Update display"
|
||||||
|
|
||||||
|
|||||||
@ -435,6 +435,16 @@ border: 1px solid rgb(85, 87, 83);</string>
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSpinBox" name="spnVolume">
|
||||||
|
<property name="maximum">
|
||||||
|
<number>100</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>100</number>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user