You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

264 lines
11 KiB

from pydub import AudioSegment
from pydub.effects import normalize
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QLabel, QLineEdit, QHBoxLayout, QVBoxLayout, QFileDialog, QErrorMessage, QComboBox, QMessageBox
import soundfile as sf
import pyloudnorm as pyln
import os
import sys
def trueAppPath():
if getattr(sys, 'frozen', False):
# If the application is run as a bundle, the PyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
return sys._MEIPASSy
else:
return os.path.dirname(os.path.abspath(__file__))
# app = gui()
app = QApplication([])
win = QWidget()
grid = QGridLayout()
# windgets
file_override_dialog = QMessageBox()
file_override_dialog.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
file_override_dialog.setIcon(QMessageBox.Warning)
error_dialog = QErrorMessage()
left_input_layout = QHBoxLayout()
left_input_button = QPushButton("Left input file")
left_input_display = QLineEdit('')
left_input_layout.addWidget(left_input_display)
left_input_layout.addWidget(left_input_button)
right_input_layout = QHBoxLayout()
right_input_button = QPushButton("Right input file")
right_input_display = QLineEdit('')
right_input_layout.addWidget(right_input_display)
right_input_layout.addWidget(right_input_button)
data_input_layout = QHBoxLayout()
gain_input = QLineEdit("0")
gain_label = QLabel("dB gain")
channel_input = QComboBox()
channel_input.addItems(["Equalize left channel", "Equalize right channel"])
data_input_layout.addWidget(channel_input)
data_input_layout.addWidget(gain_input)
data_input_layout.addWidget(gain_label, 2)
output_layout = QHBoxLayout()
output_button = QPushButton("Output path")
output_display = QLineEdit()
output_layout.addWidget(output_display)
output_layout.addWidget(output_button)
buttons_layout = QHBoxLayout()
padding_L = QLabel()
padding_R = QLabel()
proceed_button = QPushButton("Proceed")
buttons_layout.addWidget(padding_L)
buttons_layout.addWidget(proceed_button)
buttons_layout.addWidget(padding_R)
global_layout = QVBoxLayout()
global_layout.addLayout(left_input_layout)
global_layout.addLayout(right_input_layout)
global_layout.addLayout(data_input_layout)
global_layout.addLayout(output_layout)
global_layout.addLayout(buttons_layout)
def input_button_clicked(channel):
file_picker = QFileDialog()
filter = 'wav(*.wav)'
path, status = file_picker.getOpenFileName(file_picker, "Select an input file", "", filter)
if channel == 'L':
left_input_display.setText(path)
if channel == 'R':
right_input_display.setText(path)
def output_button_clicked():
file_picker = QFileDialog()
path = file_picker.getExistingDirectory(file_picker, 'Select directory')
output_display.setText(path)
def proceed_button_clicked():
exists_L = False
exists_R = False
if(os.path.isfile(left_input_display.text())):
left_input_file = left_input_display.text()
exists_L = True
else:
if left_input_display.text() == '':
exists_L = False
else:
error_dialog.showMessage('Left input file invalid or inexistant!')
return
if(os.path.isfile(right_input_display.text())):
right_input_file = right_input_display.text()
exists_R = True
else:
if right_input_display.text() == '':
exists_R = False
else:
error_dialog.showMessage('Right input file invalid or inexistant!')
return
if(os.path.isdir(output_display.text())):
output_file = output_display.text()
else:
error_dialog.showMessage('Output folder invalid or inexistant!')
return
dB = float(gain_input.text().replace(',', '.'))
channel_selection = channel_input.currentIndex()
# comparaison normalisation:
# déterminer le seuil avant saturation: max_value = - (signal.max_dBFS + 0.1)
# mesurer la différence d'amplitude entre entrée et appliquée: loudness = input_dB - max_value
# appliquer la normalisation sur le bon canal: signal.apply_gain_stereo(max_value, 0)
if(channel_selection == 0):
if exists_L:
left_input_filename = left_input_file.split("/")[-1]
left_input_file = AudioSegment.from_wav(left_input_file)
max_value = -(left_input_file.split_to_mono()[0].max_dBFS + 0.1)
# loudness = dB - max_value
if(dB > max_value):
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
equalized_left_input_file = left_input_file.apply_gain_stereo(max_value, 0)
normalized_left_input_file = equalized_left_input_file
# print("max value input = " + str(left_input_file.max_dBFS))
# print("max value output = " + str(normalized_left_input_file.max_dBFS))
else:
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
equalized_left_input_file = left_input_file.apply_gain_stereo(dB, 0)
normalized_left_input_file = equalized_left_input_file
# print("max value input = " + str(left_input_file.max_dBFS))
# print("max value output = " + str(normalized_left_input_file.max_dBFS))
if exists_R:
right_input_filename = right_input_file.split("/")[-1]
right_input_file = AudioSegment.from_wav(right_input_file)
max_value = -(right_input_file.split_to_mono()[0].max_dBFS + 0.1)
# loudness = dB - max_value
if(dB > max_value):
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
equalized_right_input_file = right_input_file.apply_gain_stereo(max_value, 0)
normalized_right_input_file = equalized_right_input_file
# print("max value input = " + str(right_input_file.max_dBFS))
# print("max value output = " + str(normalized_right_input_file.max_dBFS))
else:
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
equalized_right_input_file = right_input_file.apply_gain_stereo(dB, 0)
normalized_right_input_file = equalized_right_input_file
# print("max value input = " + str(right_input_file.max_dBFS))
# print("max value output = " + str(normalized_right_input_file.max_dBFS))
if(channel_selection == 1):
if exists_L:
left_input_filename = left_input_file.split("/")[-1]
left_input_file = AudioSegment.from_wav(left_input_file)
max_value = -(left_input_file.split_to_mono()[1].max_dBFS + 0.1)
# loudness = dB - max_value
if(dB > max_value):
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
normalized_left_input_file = left_input_file.apply_gain_stereo(0, max_value)
# print("max value input = " + str(left_input_file.max_dBFS))
# print("max value output = " + str(normalized_left_input_file.max_dBFS))
else:
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
normalized_left_input_file = left_input_file.apply_gain_stereo(0, dB)
# print("max value input = " + str(left_input_file.max_dBFS))
# print("max value output = " + str(normalized_left_input_file.max_dBFS))
if exists_R:
right_input_filename = right_input_file.split("/")[-1]
right_input_file = AudioSegment.from_wav(right_input_file)
max_value = -(right_input_file.split_to_mono()[0].max_dBFS + 0.1)
# loudness = dB - max_value
if(dB > max_value):
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
equalized_right_input_file = right_input_file.apply_gain_stereo(0, max_value)
normalized_right_input_file = equalized_right_input_file
# print("max value input = " + str(right_input_file.max_dBFS))
# print("max value output = " + str(normalized_right_input_file.max_dBFS))
else:
# print("max applicable = " + str(max_value))
# print("différence entre entrée et appliquée = " + str(loudness))
equalized_right_input_file = right_input_file.apply_gain_stereo(0, dB)
normalized_right_input_file = equalized_right_input_file
# print("max value input = " + str(right_input_file.max_dBFS))
# print("max value output = " + str(normalized_right_input_file.max_dBFS))
if(channel_selection < 0 or channel_selection > 1):
print(channel_selection)
error_dialog.showMessage('Something went wrong while applying gain!')
return
if exists_L:
left_output_file_path = output_file + '/' + left_input_filename
if(os.path.isfile(left_output_file_path)):
file_override_dialog.setText(left_input_filename + ' already exists and will be overriten!')
# cancel : 4194304 ok : 1024
if(file_override_dialog.exec_() == 1024):
normalized_left_input_file.export(left_output_file_path, "wav")
else:
return
else:
normalized_left_input_file.export(left_output_file_path, "wav")
if exists_R:
right_output_file_path = output_file + '/' + right_input_filename
if(os.path.isfile(right_output_file_path)):
file_override_dialog.setText(right_input_filename + ' already exists and will be overriten!')
# cancel : 4194304 ok : 1024
if(file_override_dialog.exec_() == 1024):
normalized_right_input_file.export(right_output_file_path, "wav")
else:
return
else:
normalized_right_input_file.export(right_output_file_path, "wav")
if (not exists_L) and (not exists_R):
error_dialog.showMessage('There is no file to process')
return
return_message = ""
if exists_L:
if os.path.isfile(left_output_file_path):
return_message += ' Left file created! '
else:
return_message += 'Someting went wrong while processing the left file!'
if exists_R:
if os.path.isfile(right_output_file_path):
return_message += ' Right file created! '
else:
return_message += 'Someting went wrong while processing the right file!'
error_dialog.showMessage(return_message)
left_input_button.clicked.connect(lambda:input_button_clicked('L'))
right_input_button.clicked.connect(lambda:input_button_clicked('R'))
output_button.clicked.connect(lambda:output_button_clicked())
proceed_button.clicked.connect(lambda:proceed_button_clicked())
win.setLayout(global_layout)
# xpos, ypos, width, height
win.setGeometry(600,300,800,200)
win.setWindowTitle("AXDMicEqualizer")
win.show()
sys.exit(app.exec_())
app.exec_()