Skip to content

Commit

Permalink
Add volume bar
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Dec 22, 2022
1 parent a54cea1 commit 548a434
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 30 deletions.
13 changes: 10 additions & 3 deletions AudioHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ def __init__(self, settings_handler):
self.measurement_timer_start_signal = None
self.chunk_size = 0
self.stop_flag = False
self.update_volume_signal = None

def calculate_chunk_size(self, sample_rate):
"""
Expand Down Expand Up @@ -660,17 +661,20 @@ def get_device_index_by_name(self, device_name: str):

def measure_latency(self, label_latency_update_signal: QtCore.pyqtSignal,
update_label_info: QtCore.pyqtSignal,
measurement_timer_start_signal: QtCore.pyqtSignal):
measurement_timer_start_signal: QtCore.pyqtSignal,
update_volume_signal: QtCore.pyqtSignal):
"""
Starts measure_latency_loop
:param label_latency_update_signal:
:param update_label_info:
:param measurement_timer_start_signal:
:param update_volume_signal:
:return:
"""
self.label_latency_update_signal = label_latency_update_signal
self.update_label_info = update_label_info
self.measurement_timer_start_signal = measurement_timer_start_signal
self.update_volume_signal = update_volume_signal

# Reset error message
self.error_message = ''
Expand Down Expand Up @@ -817,10 +821,13 @@ def measure_latency_loop(self, sample_rate, recording_channels, window_type):

# Print info
if self.update_label_info is not None:
self.update_label_info.emit('Mean level: ' + str(int(fft_mean)) + ' dBFS, Peak level: '
+ str(int(fft_max)) + ' dBFS, Expected f: '
self.update_label_info.emit('Mean level: ' + str(int(fft_mean)) + ' dBFS, Expected f: '
+ str(int(expected_frequency_hz)) + ' Hz')

# Update volume
if self.update_volume_signal is not None:
self.update_volume_signal.emit(int(fft_max))

# Detect that recording started
if fft_max / fft_mean < 0.5 \
and abs(expected_frequency_hz - fft_max_frequency_hz) < MEASURE_LATENCY_ACCEPTED_DEVIATION_HZ:
Expand Down
15 changes: 11 additions & 4 deletions NoiseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,29 @@ def __init__(self, settings_handler, audio_handler):
self.update_label_info = None
self.update_measurement_progress = None
self.measurement_timer_start_signal = None
self.update_volume_signal = None
self.measurement_completed = False
self.stop_flag = False

def start_measurement(self, update_label_info: QtCore.pyqtSignal,
update_measurement_progress: QtCore.pyqtSignal,
measurement_timer_start_signal: QtCore.pyqtSignal,
plot_on_graph_signal: QtCore.pyqtSignal):
plot_on_graph_signal: QtCore.pyqtSignal,
update_volume_signal: QtCore.pyqtSignal):
"""
Starts sweep_loop
:param update_label_info:
:param update_measurement_progress:
:param measurement_timer_start_signal:
:param plot_on_graph_signal:
:param update_volume_signal:
:return:
"""
self.update_label_info = update_label_info
self.update_measurement_progress = update_measurement_progress
self.measurement_timer_start_signal = measurement_timer_start_signal
self.plot_on_graph_signal = plot_on_graph_signal
self.update_volume_signal = update_volume_signal

# Clear flag
self.measurement_completed = False
Expand Down Expand Up @@ -240,9 +244,12 @@ def noise_loop(self):

# Print info
if self.update_label_info is not None:
self.update_label_info.emit('Peak: ' + str(int(fft_peak_hz_avg)) + ' Hz '
+ str(int(fft_peak_dbfs_avg)) + ' dBFS, Mean lvl: '
+ str(int(fft_mean_avg_dbfs_avg)) + ' dBFS')
self.update_label_info.emit('Peak: ' + str(int(fft_peak_hz_avg)) + ' Hz , Peak lvl: '
+ str(int(fft_peak_dbfs_avg)) + ' dBFS')

# Volume
if self.update_volume_signal is not None:
self.update_volume_signal.emit(int(fft_mean_avg_dbfs_avg))

# Set progress
if self.update_measurement_progress is not None:
Expand Down
26 changes: 21 additions & 5 deletions Pulsely.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Window(QMainWindow):
update_measurement_progress = QtCore.pyqtSignal(int) # QtCore.Signal(int)
measurement_continue_timer_start_signal = QtCore.pyqtSignal(int) # QtCore.Signal(int)
plot_on_graph_signal = QtCore.pyqtSignal() # QtCore.Signal()
update_volume_signal = QtCore.pyqtSignal(int) # QtCore.Signal(int)

def __init__(self):
super(Window, self).__init__()
Expand Down Expand Up @@ -108,6 +109,7 @@ def __init__(self):
self.update_measurement_progress.connect(self.measurement_progress.setValue)
self.measurement_continue_timer_start_signal.connect(self.measurement_continue_timer.start)
self.plot_on_graph_signal.connect(self.plot_data)
self.update_volume_signal.connect(self.update_volume)

# Initialize window combobox
self.fft_window_type.addItems(['None', 'Hamming', 'Hanning', 'Blackman'])
Expand Down Expand Up @@ -164,13 +166,14 @@ def measurement_start(self):

# Reset progress bar
self.measurement_progress.setValue(0)
self.update_volume(-100)

# Measure internal reference in sweep mode
if int(self.settings_handler.settings['signal_type']) == AudioHandler.TEST_SIGNAL_TYPE_SWEEP:
self.measurement_stage = MEASUREMENT_STAGE_REFERENCE
self.sweep_handler.start_measurement(self.update_label_info, self.update_measurement_progress,
self.measurement_continue_timer_start_signal,
self.plot_on_graph_signal, True)
self.plot_on_graph_signal, self.update_volume_signal, True)

# Measure latency
else:
Expand All @@ -188,7 +191,8 @@ def measurement_start(self):
self.measurement_stage = MEASUREMENT_STAGE_LATENCY
self.audio_handler.measure_latency(self.update_label_latency,
self.update_label_info,
self.measurement_continue_timer_start_signal)
self.measurement_continue_timer_start_signal,
self.update_volume_signal)

# Stop button pressed
elif self.btn_measurement_action == BTN_MEASUREMENT_ACTION_STOP:
Expand Down Expand Up @@ -218,6 +222,7 @@ def measurement_continue(self):
# Clear labels and progress bar
self.label_info.setText('')
self.measurement_progress.setValue(0)
self.update_volume(-100)

# Previous stage is internal reference measurement
if self.measurement_stage == MEASUREMENT_STAGE_REFERENCE:
Expand All @@ -237,7 +242,8 @@ def measurement_continue(self):
self.measurement_stage = MEASUREMENT_STAGE_LATENCY
self.audio_handler.measure_latency(self.update_label_latency,
self.update_label_info,
self.measurement_continue_timer_start_signal)
self.measurement_continue_timer_start_signal,
self.update_volume_signal)

# Reference measurement failed
elif self.sweep_handler.error_message != '':
Expand All @@ -260,11 +266,11 @@ def measurement_continue(self):
if int(self.settings_handler.settings['signal_type']) == AudioHandler.TEST_SIGNAL_TYPE_SWEEP:
self.sweep_handler.start_measurement(self.update_label_info, self.update_measurement_progress,
self.measurement_continue_timer_start_signal,
self.plot_on_graph_signal)
self.plot_on_graph_signal, self.update_volume_signal)
else:
self.noise_handler.start_measurement(self.update_label_info, self.update_measurement_progress,
self.measurement_continue_timer_start_signal,
self.plot_on_graph_signal)
self.plot_on_graph_signal, self.update_volume_signal)

# Latency measurement failed
else:
Expand Down Expand Up @@ -313,6 +319,16 @@ def measurement_continue(self):
self.btn_measurement_start.setText(BTN_MEASUREMENT_START_TEXT)
self.btn_measurement_action = BTN_MEASUREMENT_ACTION_START

def update_volume(self, volume_dbfs: int):
"""
Updates volume progress bar and label
:param volume_dbfs:
:return:
"""
volume_percents = AudioHandler.clamp(AudioHandler._map(volume_dbfs, -60, 3, 0, 100), 0, 100)
self.volume_bar.setValue(volume_percents)
self.volume_label.setText(str(volume_dbfs) + ' dBFS')

def show_error_message(self, error_message):
"""
Shows error message dialog
Expand Down
21 changes: 13 additions & 8 deletions SweepHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def __init__(self, settings_handler, audio_handler):
self.update_label_info = None
self.update_measurement_progress = None
self.measurement_timer_start_signal = None
self.update_volume_signal = None
self.graph_curves = []
self.meas_or_calib_completed = False
self.internal_reference_dbfs = []
Expand Down Expand Up @@ -169,20 +170,23 @@ def map_sweep_frequencies(self):
def start_measurement(self, update_label_info: QtCore.pyqtSignal,
update_measurement_progress: QtCore.pyqtSignal,
measurement_timer_start_signal: QtCore.pyqtSignal,
plot_on_graph_signal: QtCore.pyqtSignal, internal_calibration=False):
plot_on_graph_signal: QtCore.pyqtSignal,
update_volume_signal: QtCore.pyqtSignal, internal_calibration=False):
"""
Starts sweep_loop
:param update_label_info:
:param update_measurement_progress:
:param measurement_timer_start_signal:
:param plot_on_graph_signal:
:param update_volume_signal:
:param internal_calibration:
:return:
"""
self.update_label_info = update_label_info
self.update_measurement_progress = update_measurement_progress
self.measurement_timer_start_signal = measurement_timer_start_signal
self.plot_on_graph_signal = plot_on_graph_signal
self.update_volume_signal = update_volume_signal

# Calculate sweep frequencies
self.map_sweep_frequencies()
Expand Down Expand Up @@ -345,8 +349,8 @@ def sweep_loop(self, chunk_size, sample_rate, volume, recording_channels,

# Info data
fft_actual_peak_hz_avg = 0
fft_in_range_peak_hz_avg = 0
fft_mean_avg_dbfs = 0
fft_max_avg_dbfs = 0

# Get frequency from delay buffer
frequency_delayed = self.sweep_frequencies[frequency_indexes_buffer[-1]]
Expand Down Expand Up @@ -398,9 +402,7 @@ def sweep_loop(self, chunk_size, sample_rate, volume, recording_channels,
actual_peak = index_to_frequency(
np.where(fft_dbfs == np.max(fft_dbfs))[0][0], sample_rate, data_length)
fft_actual_peak_hz_avg += actual_peak

# Frequency of measured peak (from frequency_start_index to frequency_stop_index)
fft_in_range_peak_hz_avg += index_to_frequency(peak_index, sample_rate, data_length)
fft_max_avg_dbfs += np.max(fft_dbfs)

# Mean signal level
fft_mean_avg_dbfs += np.average(fft_dbfs)
Expand All @@ -415,8 +417,8 @@ def sweep_loop(self, chunk_size, sample_rate, volume, recording_channels,

# Calculate average info
fft_actual_peak_hz_avg /= recording_channels
fft_in_range_peak_hz_avg /= recording_channels
fft_mean_avg_dbfs /= recording_channels
fft_max_avg_dbfs /= recording_channels

# Increment frequency change counter
frequency_last_played_counter += 1
Expand Down Expand Up @@ -470,8 +472,11 @@ def sweep_loop(self, chunk_size, sample_rate, volume, recording_channels,
if self.update_label_info is not None:
self.update_label_info.emit('Exp. peak: ' + str(int(frequency_delayed)) + ' Hz'
+ ', Act. peak: ' + str(int(fft_actual_peak_hz_avg)) + ' Hz'
+ ', Meas. peak: ' + str(int(fft_in_range_peak_hz_avg))
+ ' Hz, Mean lvl: ' + str(int(fft_mean_avg_dbfs)) + ' dBFS')
+ ', Mean lvl: ' + str(int(fft_mean_avg_dbfs)) + ' dBFS')

# Volume
if self.update_volume_signal is not None:
self.update_volume_signal.emit(int(fft_max_avg_dbfs))

# Set progress (2nd part, 90%)
if self.update_measurement_progress is not None:
Expand Down
72 changes: 62 additions & 10 deletions gui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>865</width>
<height>445</height>
<width>912</width>
<height>451</height>
</rect>
</property>
<property name="font">
Expand All @@ -23,7 +23,7 @@
<enum>Qt::LeftToRight</enum>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_17">
<layout class="QHBoxLayout" name="horizontalLayout_18">
<item>
<widget class="QSplitter" name="splitter">
<property name="sizePolicy">
Expand Down Expand Up @@ -65,7 +65,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>307</width>
<width>325</width>
<height>1176</height>
</rect>
</property>
Expand Down Expand Up @@ -833,11 +833,63 @@ color: #fcc88d;</string>
</layout>
</item>
<item>
<widget class="QLabel" name="label_info">
<property name="text">
<string/>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_17">
<item>
<widget class="QLabel" name="label_info">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>80</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_19">
<property name="text">
<string>Vol:</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="volume_bar">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>20</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>100</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="volume_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>- dBFS</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_5">
Expand Down Expand Up @@ -895,7 +947,7 @@ color: #fcc88d;</string>
<rect>
<x>0</x>
<y>0</y>
<width>865</width>
<width>912</width>
<height>21</height>
</rect>
</property>
Expand Down

0 comments on commit 548a434

Please sign in to comment.