This commit is contained in:
billypom on debian 2025-04-02 19:14:53 -04:00
parent f4685b4427
commit 8b4c1d0870
2 changed files with 12 additions and 3 deletions

View File

@ -12,6 +12,7 @@ class AudioVisualizer(QtWidgets.QWidget):
self.x_resolution = x_resolution
self.fft_analyser = FFTAnalyser(self.media_player, self.x_resolution)
self.fft_analyser.calculatedVisual.connect(self.set_amplitudes)
self.fft_analyser.calculatedVisualRs.connect(self.set_rs)
self.fft_analyser.start()
self.amps = np.array([])
self._plot_item = None
@ -55,6 +56,9 @@ class AudioVisualizer(QtWidgets.QWidget):
def get_amplitudes(self):
return self.amps
def get_rs(self):
return self.rs
def get_decibels(self):
"""Convert amplitude values to decibel scale
@ -63,7 +67,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-30
epsilon = 1e-6
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)
@ -71,6 +75,9 @@ class AudioVisualizer(QtWidgets.QWidget):
db_values = np.maximum(db_values, -96)
return db_values
def set_rs(self, rs):
self.rs = np.array(rs)
def set_amplitudes(self, amps):
"""
This function is hooked into the calculatedVisual signal from FFTAnalyzer() object

View File

@ -13,6 +13,7 @@ class FFTAnalyser(QtCore.QThread):
"""Analyses a song using FFTs."""
calculatedVisual = QtCore.pyqtSignal(np.ndarray)
calculatedVisualRs = QtCore.pyqtSignal(np.ndarray)
def __init__(self, player, x_resolution): # noqa: F821
super().__init__()
@ -122,11 +123,12 @@ class FFTAnalyser(QtCore.QThread):
self.points[n] = amp
# Set a lower threshold to properly reach zero
if self.points[n] < 1e-4:
if self.points[n] < 1e-2:
self.points[n] = 0
print(self.points)
# interpolate points
rs = gaussian_filter1d(self.points, sigma=1)
rs = gaussian_filter1d(self.points, sigma=2)
# divide by the highest sample in the song to normalise the
# amps in terms of decimals from 0 -> 1