2424from PyQt5 .QtCore import pyqtSignal , pyqtProperty
2525from friture .audiobackend import AudioBackend
2626from friture .ui_settings import Ui_Settings_Dialog
27+ import csv
2728
2829no_input_device_title = "No audio input device found"
2930
@@ -82,6 +83,9 @@ def __init__(self, parent):
8283 self .checkbox_showPlayback .stateChanged .connect (self .show_playback_checkbox_changed )
8384 self .spinBox_historyLength .editingFinished .connect (self .history_length_edit_finished )
8485
86+ self .fileBox_compensation .clicked .connect (self .browse_compensation_file )
87+ self .clearButton .clicked .connect (self .compensation_file_cleared )
88+
8589 @pyqtProperty (bool , notify = show_playback_changed ) # type: ignore
8690 def show_playback (self ) -> bool :
8791 return bool (self .checkbox_showPlayback .checkState ())
@@ -178,6 +182,37 @@ def show_playback_checkbox_changed(self, state: int) -> None:
178182 # slot
179183 def history_length_edit_finished (self ) -> None :
180184 self .history_length_changed .emit (self .spinBox_historyLength .value ())
185+
186+ def browse_compensation_file (self ):
187+ file_name = QtWidgets .QFileDialog .getOpenFileName (self , "Open compensation file" , "" , "Compensation files (*.*)" )[0 ]
188+ self .fileBox_compensation .setText (file_name )
189+ self .load_compensation_file ()
190+
191+ def load_compensation_file (self ):
192+ file_name = self .fileBox_compensation .text ()
193+ if file_name :
194+ try :
195+ with open (file_name , newline = '' ) as csvfile :
196+ csvreader = csv .reader (csvfile )
197+ self .parent ().original_calibration = []
198+ self .parent ().original_calibration_freqs = []
199+ for row in csvreader :
200+ if row [0 ][0 ].isdigit ():
201+ dict = row [0 ].split ()
202+ self .parent ().original_calibration .append (float (dict [1 ]))
203+ self .parent ().original_calibration_freqs .append (float (dict [0 ]))
204+
205+ except Exception as e :
206+ self .logger .error (f"Failed to load compensation file: { e } " )
207+ else :
208+ self .parent ().original_calibration = [0 , 0 ]
209+ self .parent ().original_calibration_freqs = [0 , 20000 ]
210+
211+ self .parent ().recalculate_calibration ()
212+
213+ def compensation_file_cleared (self ):
214+ self .fileBox_compensation .setText ("" )
215+ self .load_compensation_file ()
181216
182217 # method
183218 def saveState (self , settings ):
@@ -189,13 +224,17 @@ def saveState(self, settings):
189224 settings .setValue ("duoInput" , self .inputTypeButtonGroup .checkedId ())
190225 settings .setValue ("showPlayback" , self .checkbox_showPlayback .checkState ())
191226 settings .setValue ("historyLength" , self .spinBox_historyLength .value ())
227+ settings .setValue ("compensationFile" , self .fileBox_compensation .text ())
192228
193229 # method
194230 def restoreState (self , settings ):
195231 device_name = settings .value ("deviceName" , "" )
196232 device_index = self .comboBox_inputDevice .findText (device_name )
197233 # change the device only if it exists in the device list
198234 if device_index >= 0 :
235+ compensation_file = settings .value ("compensationFile" , "" )
236+ self .fileBox_compensation .setText (compensation_file )
237+ self .load_compensation_file ()
199238 self .comboBox_inputDevice .setCurrentIndex (device_index )
200239 channel = settings .value ("firstChannel" , 0 , type = int )
201240 self .comboBox_firstChannel .setCurrentIndex (channel )
0 commit comments