From dcdfeac607fb7392067d53cebc9c467d24afea04 Mon Sep 17 00:00:00 2001 From: billypom on debian Date: Sun, 30 Mar 2025 23:26:02 -0400 Subject: [PATCH] cursor suggested performance improvement lol --- components/AudioVisualizer.py | 2 ++ main.py | 26 ++++++++++++++++++++------ ui.py | 14 ++++++-------- ui.ui | 22 ++++++++-------------- utils/fft_analyser.py | 6 +++--- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/components/AudioVisualizer.py b/components/AudioVisualizer.py index 375f991..5110ace 100644 --- a/components/AudioVisualizer.py +++ b/components/AudioVisualizer.py @@ -21,6 +21,8 @@ class AudioVisualizer(QtWidgets.QWidget): self.fft_analyser.calculated_visual.connect(self.set_amplitudes) self.fft_analyser.start() self.amps = np.array([]) + self._plot_item = None + self._x_data = np.arange(self.x_resolution) def get_amplitudes(self): return self.amps diff --git a/main.py b/main.py index 98dd309..995a6a5 100644 --- a/main.py +++ b/main.py @@ -162,7 +162,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self.config.read(self.cfg_file) self.player: QMediaPlayer = QMediaPlayer() # Audio player object self.probe: QAudioProbe = QAudioProbe() # Gets audio data - self.analyzer_x_resolution = 200 + self.analyzer_x_resolution = 150 self.audio_visualizer: AudioVisualizer = AudioVisualizer(self.player, self.analyzer_x_resolution) self.timer = QTimer(self) # Audio timing things self.clipboard = clipboard @@ -187,6 +187,10 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self.PlotWidget.setYRange(0, 1, padding=0) # y axis range self.PlotWidget.setLogMode(False, False) self.PlotWidget.setMouseEnabled(x=False, y=False) + # Performance optimizations + self.PlotWidget.setAntialiasing(False) + self.PlotWidget.setDownsampling(auto=True, mode='peak') + self.PlotWidget.setClipToView(True) # Remove x-axis ticks # ticks = ['20', '31.25', '62.5', '125', '250', '500', '1000', '2000', '4000', '10000', '20000'] # self.PlotWidget.getAxis("bottom").setTicks([]) @@ -421,15 +425,25 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): def update_audio_visualization(self) -> None: """Handles upading points on the pyqtgraph visual""" - self.clear_audio_visualization() y = self.audio_visualizer.get_amplitudes() - x = [i for i in range(len(y))] - # x = nparray([0, 31.25, 62.5, 125, 250, 500, 1000, 2000, 4000, 8000, 15000, 20000]) - self.PlotWidget.plot(x, y, fillLevel=0, fillBrush=mkBrush("b")) - self.PlotWidget.show() + if len(y) == 0: + return + + if self.audio_visualizer._plot_item is None: + self.PlotWidget.clear() + self.audio_visualizer._plot_item = self.PlotWidget.plot( + self.audio_visualizer._x_data, + y, + pen='b', # Use pen instead of fill for better performance + fillLevel=0, + fillBrush=mkBrush("b") + ) + else: + self.audio_visualizer._plot_item.setData(self.audio_visualizer._x_data, y) def clear_audio_visualization(self) -> None: self.PlotWidget.clear() + self.audio_visualizer._plot_item = None def move_slider(self) -> None: """Handles moving the playback slider""" diff --git a/ui.py b/ui.py index e7698fd..32e5f8d 100644 --- a/ui.py +++ b/ui.py @@ -157,12 +157,6 @@ class Ui_MainWindow(object): self.hLayoutControls2.addWidget(self.volumeLabel) self.hLayoutSongDetails = QtWidgets.QHBoxLayout() self.hLayoutSongDetails.setObjectName("hLayoutSongDetails") - self.titleLabel = QtWidgets.QLabel(self.centralwidget) - font = QtGui.QFont() - font.setPointSize(18) - self.titleLabel.setFont(font) - self.titleLabel.setObjectName("titleLabel") - self.hLayoutSongDetails.addWidget(self.titleLabel) self.artistLabel = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setPointSize(24) @@ -171,6 +165,12 @@ class Ui_MainWindow(object): self.artistLabel.setFont(font) self.artistLabel.setObjectName("artistLabel") self.hLayoutSongDetails.addWidget(self.artistLabel) + self.titleLabel = QtWidgets.QLabel(self.centralwidget) + font = QtGui.QFont() + font.setPointSize(18) + self.titleLabel.setFont(font) + self.titleLabel.setObjectName("titleLabel") + self.hLayoutSongDetails.addWidget(self.titleLabel) self.albumLabel = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setPointSize(16) @@ -251,8 +251,6 @@ class Ui_MainWindow(object): self.playButton.setText(_translate("MainWindow", "▶️")) self.nextButton.setText(_translate("MainWindow", "⏭️")) self.volumeLabel.setText(_translate("MainWindow", "50")) - self.artistLabel.setText(_translate("MainWindow", "artist")) - self.albumLabel.setText(_translate("MainWindow", "album")) self.pushButton.setText(_translate("MainWindow", "nothing")) self.menuFile.setTitle(_translate("MainWindow", "File")) self.menuEdit.setTitle(_translate("MainWindow", "Edit")) diff --git a/ui.ui b/ui.ui index c6c1496..7c9c72a 100644 --- a/ui.ui +++ b/ui.ui @@ -264,15 +264,6 @@ - - - - - 18 - - - - @@ -282,8 +273,14 @@ true - - artist + + + + + + + 18 + @@ -297,9 +294,6 @@ false - - album - diff --git a/utils/fft_analyser.py b/utils/fft_analyser.py index 7cbecfd..6d63a01 100644 --- a/utils/fft_analyser.py +++ b/utils/fft_analyser.py @@ -24,7 +24,7 @@ class FFTAnalyser(QtCore.QThread): # this length is a number, in seconds, of how much audio is sampled to determine the frequencies # of the audio at a specific point in time # in this case, it takes 5% of the samples at some point in time - self.sampling_window_length = 0.09 + self.sampling_window_length = 0.05 self.visual_delta_threshold = 1000 self.sensitivity = 10 @@ -124,7 +124,7 @@ class FFTAnalyser(QtCore.QThread): self.points[n] = 1e-5 # interpolate points - rs = gaussian_filter1d(self.points, sigma=2) + rs = gaussian_filter1d(self.points, sigma=1.5) # divide by the highest sample in the song to normalise the # amps in terms of decimals from 0 -> 1 @@ -142,4 +142,4 @@ class FFTAnalyser(QtCore.QThread): except ValueError: self.calculated_visual.emit(np.zeros(self.resolution)) self.start_animate = False - time.sleep(0.025) + time.sleep(0.033)