From a115a3c423c48ce8b3ee762c163e93fff3bd56d1 Mon Sep 17 00:00:00 2001 From: tsi-billypom Date: Mon, 31 Mar 2025 11:22:53 -0400 Subject: [PATCH] cleanup --- components/AudioVisualizer.py | 22 +++++---------- main.py | 53 ++++++++++++++++++++--------------- utils/fft_analyser.py | 10 ++----- 3 files changed, 40 insertions(+), 45 deletions(-) diff --git a/components/AudioVisualizer.py b/components/AudioVisualizer.py index a874fd6..dc012e7 100644 --- a/components/AudioVisualizer.py +++ b/components/AudioVisualizer.py @@ -4,14 +4,7 @@ from utils import FFTAnalyser class AudioVisualizer(QtWidgets.QWidget): - """_Audio Visualizer component_ - - Args: - QtWidgets (_type_): _description_ - - Returns: - _type_: _description_ - """ + """Audio Visualizer component""" def __init__(self, media_player, x_resolution): super().__init__() @@ -28,14 +21,13 @@ class AudioVisualizer(QtWidgets.QWidget): # Generate logarithmic frequency scale (20Hz - 20kHz) self.min_freq = 20 self.max_freq = 23000 - self.frequency_values = np.logspace(np.log10(self.min_freq), np.log10(self.max_freq), self.x_resolution) + self.frequency_values = np.logspace( + np.log10(self.min_freq), np.log10(self.max_freq), self.x_resolution + ) - def get_frequency_ticks(self, num_ticks=10): + def get_frequency_ticks(self): """Returns frequency ticks for x-axis display - Args: - num_ticks (int): Approximate number of ticks to display - Returns: list: List of tuples with (position, label) for each tick """ @@ -51,7 +43,7 @@ class AudioVisualizer(QtWidgets.QWidget): if freq < 1000: label = f"{freq}Hz" else: - label = f"{freq/1000:.0f}kHz" + label = f"{freq / 1000:.0f}kHz" ticks.append((idx, label)) return ticks @@ -71,7 +63,7 @@ class AudioVisualizer(QtWidgets.QWidget): With a noise floor cutoff at around -96dB (for very small values) """ # Avoid log(0) by adding a small epsilon - epsilon = 1e-10 + epsilon = 1e-10 amplitudes = np.maximum(self.amps, epsilon) # Convert to decibels (20*log10 is the standard formula for amplitude to dB) db_values = 20 * np.log10(amplitudes) diff --git a/main.py b/main.py index 6856763..707e8c4 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,6 @@ from mutagen.id3._frames import APIC from configparser import ConfigParser from pathlib import Path from appdirs import user_config_dir -from numpy import array as nparray from logging import debug, error, warning, basicConfig, INFO, DEBUG from ui import Ui_MainWindow from PyQt5.QtWidgets import ( @@ -163,7 +162,9 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self.player: QMediaPlayer = QMediaPlayer() # Audio player object self.probe: QAudioProbe = QAudioProbe() # Gets audio data self.analyzer_x_resolution = 150 - self.audio_visualizer: AudioVisualizer = AudioVisualizer(self.player, self.analyzer_x_resolution) + self.audio_visualizer: AudioVisualizer = AudioVisualizer( + self.player, self.analyzer_x_resolution + ) self.timer = QTimer(self) # Audio timing things self.clipboard = clipboard self.tableView.load_qapp(self) @@ -185,30 +186,36 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): # Set fixed size for album art self.albumGraphicsView.setFixedSize(250, 250) - # Make sure PlotWidget doesn't exceed album art height - self.PlotWidget.setFixedHeight(225) # Adjust to leave room for playback controls - # Graphics plot - self.PlotWidget.setXRange(0, self.analyzer_x_resolution, padding=0) # x axis range - self.PlotWidget.setYRange(-96, 0, padding=0) # y axis range for decibels (-96dB to 0dB) - self.PlotWidget.setLogMode(x=False, y=False) # Logarithmic x-axis for frequency display + # Make sure PlotWidget doesn't exceed album art height + # Adjust to leave room for playback controls + self.PlotWidget.setFixedHeight(225) + # x range + self.PlotWidget.setXRange(0, self.analyzer_x_resolution, padding=0) + # y axis range for decibals (-96db to 0db) + self.PlotWidget.setYRange(-96, 0, padding=0) + # Logarithmic x-axis for frequency display + self.PlotWidget.setLogMode(x=False, y=False) self.PlotWidget.setMouseEnabled(x=False, y=False) + self.PlotWidget.showGrid(x=True, y=True) # Performance optimizations self.PlotWidget.setAntialiasing(False) - self.PlotWidget.setDownsampling(auto=True, mode='peak') + self.PlotWidget.setDownsampling(auto=True, mode="peak") self.PlotWidget.setClipToView(True) # Add tick marks for common decibel values (expanded range) - y_ticks = [(-84, '-84dB'), (-60, '-60dB'), - (-36, '-36dB'), (-12, '-12dB'), (0, '0dB')] - self.PlotWidget.getAxis('left').setTicks([y_ticks]) + y_ticks = [ + (-84, "-84dB"), + (-60, "-60dB"), + (-36, "-36dB"), + (-12, "-12dB"), + (0, "0dB"), + ] + self.PlotWidget.getAxis("left").setTicks([y_ticks]) # Add frequency ticks on x-axis freq_ticks = self.audio_visualizer.get_frequency_ticks() - self.PlotWidget.getAxis('bottom').setTicks([freq_ticks]) - - # Display grid for better readability - self.PlotWidget.showGrid(x=True, y=True) + self.PlotWidget.getAxis("bottom").setTicks([freq_ticks]) # Connections self.playbackSlider.sliderReleased.connect( @@ -228,10 +235,8 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self.actionExportPlaylist.triggered.connect(self.export_playlist) # EDIT MENU + self.actionPreferences.triggered.connect(self.open_preferences) # VIEW MENU - self.actionPreferences.triggered.connect( - self.open_preferences - ) # Open preferences menu # QUICK ACTIONS MENU self.actionScanLibraries.triggered.connect(self.scan_libraries) @@ -447,10 +452,12 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): # Use the actual frequency values for x-axis self.audio_visualizer._plot_item = self.PlotWidget.plot( self.audio_visualizer._x_data, # We'll keep using indices for drawing - y, - pen='b', # Use pen instead of fill for better performance - fillLevel=-96 if self.audio_visualizer.use_decibels else 0, # Fill from -96dB for decibel scale - fillBrush=mkBrush("b") + y, + pen="b", # Use pen instead of fill for better performance + fillLevel=-96 + if self.audio_visualizer.use_decibels + else 0, # Fill from -96dB for decibel scale + fillBrush=mkBrush("b"), ) # else: # self.audio_visualizer._plot_item.setData(self.audio_visualizer._x_data, y) diff --git a/utils/fft_analyser.py b/utils/fft_analyser.py index b8d8344..7883411 100644 --- a/utils/fft_analyser.py +++ b/utils/fft_analyser.py @@ -79,12 +79,8 @@ class FFTAnalyser(QtCore.QThread): # Logarithmic frequency scaling min_freq = np.min(freq[freq > 0]) # minimum positive frequency # 20hz - # print('min') - # print(min_freq * .05 * self.song.frame_rate) max_freq = np.max(freq) # maximum frequency # 20khz - # print('max') - # print(max_freq * .05 * self.song.frame_rate) log_freqs = np.logspace(np.log10(min_freq), np.log10(max_freq), self.resolution) point_samples = [] @@ -92,10 +88,10 @@ class FFTAnalyser(QtCore.QThread): if not data.size: return - #for i, freq in enumerate(np.arange(0, 1, point_range), start=1): + # for i, freq in enumerate(np.arange(0, 1, point_range), start=1): for i, log_freq in enumerate(log_freqs): # get the amps which are in between the frequency range - #amps = data[(freq - point_range < data[:, 0]) & (data[:, 0] < freq)] + # amps = data[(freq - point_range < data[:, 0]) & (data[:, 0] < freq)] amps = data[(log_freq - point_range < data[:, 0]) & (data[:, 0] < log_freq)] if not amps.size: point_samples.append(0) @@ -109,7 +105,7 @@ class FFTAnalyser(QtCore.QThread): ) # Add the point_samples to the self.points array, the reason we have a separate - # array (self.bars) is so that we can fade out the previous amplitudes from + # array (self.points) is so that we can fade out the previous amplitudes from # the past for n, amp in enumerate(point_samples): # amp *= 2