Normalise tracks on import

This commit is contained in:
Keith Edmunds 2021-04-24 19:35:10 +01:00
parent 6b7b5abdaf
commit 2e41a673f4
3 changed files with 17 additions and 3 deletions

View File

@ -30,6 +30,7 @@ class Config(object):
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
MILLISECOND_SIGFIGS = 0
MYSQL_CONNECT = "mysql+mysqldb://songdb:songdb@localhost/musicmuster"
NORMALISE_ON_IMPORT = True
ROOT = "/home/kae/music"
TESTMODE = True
TIMER_MS = 500

View File

@ -67,9 +67,7 @@ class Music:
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)

View File

@ -2,11 +2,13 @@
import argparse
import os
import shutil
import tempfile
from config import Config
from log import INFO
from model import Tracks, session
from pydub import AudioSegment
from pydub import AudioSegment, effects
from tinytag import TinyTag
@ -49,6 +51,19 @@ def add_path_to_db(path):
track.mtime = os.path.getmtime(path)
session.commit()
if Config.NORMALISE_ON_IMPORT:
# Get current file gid, uid and permissions
stats = os.stat(path)
normalised = effects.normalize(audio)
try:
fd, temp_path = tempfile.mkstemp()
normalised.export(temp_path, format=os.path.splitext(path)[1][1:])
shutil.copyfile(temp_path, path)
os.chown(path, stats.st_uid, stats.st_gid)
os.chmod(path, stats.st_mode)
finally:
os.remove(temp_path)
return track