# Path: hearinglosssimulator/gui/myqt.py
# QT = ModuleProxy(['', 'Q', 'Qt'], [QtCore.Qt, QtCore, QtGui, QtWidgets])
# 
# Path: hearinglosssimulator/gui/guitools.py
# class FreqGainDuration(QT.QWidget):
#     def __init__(self, parent = None):
#         QT.QWidget.__init__(self, parent)
#         mainlayout  =QT.QHBoxLayout()
#         self.setLayout(mainlayout)
# 
#         mainlayout.addWidget(QT.QLabel(u'Gain (dBFs)'))
#         self.spinbox_gain = QT.QSpinBox(maximum = 0, minimum = -100)
#         mainlayout.addWidget(self.spinbox_gain)
#         mainlayout.addStretch()
#         
#         mainlayout.addWidget(QT.QLabel(u'Duration (s)'))
#         self.spinbox_duration = QT.QSpinBox(maximum = 15, minimum = .5)
#         mainlayout.addWidget(self.spinbox_duration)
#         mainlayout.addStretch()
# 
#         mainlayout.addWidget(QT.QLabel(u'Freq (Hz)'))
#         self.spinbox_freq = QT.QSpinBox(maximum = 20000, minimum = 1)
#         mainlayout.addWidget(self.spinbox_freq)
#         
#         self.set()
# 
#     def set(self, gain = -15, duration = 2., freq = 1000.):
#         pass
#         self.spinbox_gain.setValue(gain)
#         self.spinbox_duration.setValue(duration)
#         self.spinbox_freq.setValue(freq)
#     
#     def get(self):
#         return {
#                         'gain' : float(self.spinbox_gain.value()),
#                         'duration' : float(self.spinbox_duration.value()),
#                         'freq' : float(self.spinbox_freq.value()),
#                     }
# 
# def play_sinus(freq, dbgain, duration, device='default', nb_channel=2):
#     if device is None:
#         device='default'
#     dev = sd.query_devices(device=device)
#     print(dev)
#     
#     sample_rate = dev['default_samplerate']
#     
#     
#     length = int(sample_rate * duration)+1
#     sound = hls.several_sinus(length, freqs=[freq], sample_rate=sample_rate, ampl = 1.)
#     sound = np.tile(sound[:, None],(1, nb_channel))
#     gain = 10**(dbgain/20.)
#     sound *= gain
#     
#     sd.play(sound, device=device, blocking=True)
#     
#     sd.stop()
# 
# def play_input_to_output(duration, device, sample_rate=44100, chunksize=1024, nb_channel=2):
#     #~ duration = 5  # seconds
#     dev = sd.query_devices(device=device)
#     sample_rate = dev['default_samplerate']
#     print(dev)
#     
# 
#     def callback(indata, outdata, frames, time, status):
#         if status:
#             print(status, flush=True)
#         outdata[:] = indata
# 
#     with sd.Stream(device=device, channels=nb_channel, callback=callback, samplerate=sample_rate):
#         sd.sleep(int(duration * 1000)    )
#     
#     
#     
#     
#     #~ pa = pyaudio.PyAudio()
#     
#     #~ def callback(in_data, frame_count, time_info, status):
#         #~ return (in_data, pyaudio.paContinue)
#     
#     #~ audiostream = pa.open(rate=int(sample_rate), channels=int(nb_channel), format= pyaudio.paFloat32,
#                     #~ input=True, output=True, input_device_index=input_device_index, output_device_index=output_device_index,
#                     #~ frames_per_buffer=chunksize, stream_callback=callback, start=False)
#     
#     
#     #~ audiostream.start_stream()
#     
#     #~ t_start = time.perf_counter()
#     #~ while (time.perf_counter()-t_start)<duration:
#         #~ time.sleep(0.01)
# 
#     #~ audiostream.stop_stream()
#     #~ audiostream.close()
# 
#     #~ pa.terminate()


from .myqt import QT
from hearinglosssimulator.gui.guitools import FreqGainDuration, play_sinus, play_input_to_output
import pyqtgraph as pg
import numpy as np
        but = QT.QPushButton('Input to output')
        but.clicked.connect(self.play_input_to_output)
        mainlayout.addWidget(but)
        
        
        
        mainlayout.addWidget(but)
        self.label_db_input = QT.QLabel(u'')
        mainlayout.addWidget(self.label_db_input)
        
        mainlayout.addStretch()
        
    
    
    def set_configuration(self, gain = -15., duration = 2., freq = 1000., 
                                            spl_calibration_at_zero_dbfs = 93.979400086720375):
        self.freqgainduration.set( gain = gain, duration = duration, freq = freq)
        self.spinbox_spllevel.setValue(spl_calibration_at_zero_dbfs+gain)
        
    def get_configuration(self):
        config = self.freqgainduration.get()
        config['spl_calibration_at_zero_dbfs'] =  float(self.spinbox_spllevel.value())-config['gain']
        return config
    
    def refresh_label_calibration(self):
        text = u'<h1><b>0 dBFs = {:0.2f} dBSpl</b>'.format(self.get_configuration()['spl_calibration_at_zero_dbfs'])
        self.label_calibration.setText(text)

    def play_sinus(self):
        p = self.freqgainduration.get()

# Path: hearinglosssimulator/gui/myqt.py
# QT = ModuleProxy(['', 'Q', 'Qt'], [QtCore.Qt, QtCore, QtGui, QtWidgets])
# 
# Path: hearinglosssimulator/gui/guitools.py
# class FreqGainDuration(QT.QWidget):
#     def __init__(self, parent = None):
#         QT.QWidget.__init__(self, parent)
#         mainlayout  =QT.QHBoxLayout()
#         self.setLayout(mainlayout)
# 
#         mainlayout.addWidget(QT.QLabel(u'Gain (dBFs)'))
#         self.spinbox_gain = QT.QSpinBox(maximum = 0, minimum = -100)
#         mainlayout.addWidget(self.spinbox_gain)
#         mainlayout.addStretch()
#         
#         mainlayout.addWidget(QT.QLabel(u'Duration (s)'))
#         self.spinbox_duration = QT.QSpinBox(maximum = 15, minimum = .5)
#         mainlayout.addWidget(self.spinbox_duration)
#         mainlayout.addStretch()
# 
#         mainlayout.addWidget(QT.QLabel(u'Freq (Hz)'))
#         self.spinbox_freq = QT.QSpinBox(maximum = 20000, minimum = 1)
#         mainlayout.addWidget(self.spinbox_freq)
#         
#         self.set()
# 
#     def set(self, gain = -15, duration = 2., freq = 1000.):
#         pass
#         self.spinbox_gain.setValue(gain)
#         self.spinbox_duration.setValue(duration)
#         self.spinbox_freq.setValue(freq)
#     
#     def get(self):
#         return {
#                         'gain' : float(self.spinbox_gain.value()),
#                         'duration' : float(self.spinbox_duration.value()),
#                         'freq' : float(self.spinbox_freq.value()),
#                     }
# 
# def play_sinus(freq, dbgain, duration, device='default', nb_channel=2):
#     if device is None:
#         device='default'
#     dev = sd.query_devices(device=device)
#     print(dev)
#     
#     sample_rate = dev['default_samplerate']
#     
#     
#     length = int(sample_rate * duration)+1
#     sound = hls.several_sinus(length, freqs=[freq], sample_rate=sample_rate, ampl = 1.)
#     sound = np.tile(sound[:, None],(1, nb_channel))
#     gain = 10**(dbgain/20.)
#     sound *= gain
#     
#     sd.play(sound, device=device, blocking=True)
#     
#     sd.stop()
# 
# def play_input_to_output(duration, device, sample_rate=44100, chunksize=1024, nb_channel=2):
#     #~ duration = 5  # seconds
#     dev = sd.query_devices(device=device)
#     sample_rate = dev['default_samplerate']
#     print(dev)
#     
# 
#     def callback(indata, outdata, frames, time, status):
#         if status:
#             print(status, flush=True)
#         outdata[:] = indata
# 
#     with sd.Stream(device=device, channels=nb_channel, callback=callback, samplerate=sample_rate):
#         sd.sleep(int(duration * 1000)    )
#     
#     
#     
#     
#     #~ pa = pyaudio.PyAudio()
#     
#     #~ def callback(in_data, frame_count, time_info, status):
#         #~ return (in_data, pyaudio.paContinue)
#     
#     #~ audiostream = pa.open(rate=int(sample_rate), channels=int(nb_channel), format= pyaudio.paFloat32,
#                     #~ input=True, output=True, input_device_index=input_device_index, output_device_index=output_device_index,
#                     #~ frames_per_buffer=chunksize, stream_callback=callback, start=False)
#     
#     
#     #~ audiostream.start_stream()
#     
#     #~ t_start = time.perf_counter()
#     #~ while (time.perf_counter()-t_start)<duration:
#         #~ time.sleep(0.01)
# 
#     #~ audiostream.stop_stream()
#     #~ audiostream.close()
# 
#     #~ pa.terminate()


# Path: hearinglosssimulator/gui/calibration.py
from .myqt import QT
from hearinglosssimulator.gui.guitools import FreqGainDuration, play_sinus, play_input_to_output
import pyqtgraph as pg
import numpy as np

        but = QT.QPushButton('Input to output')
        but.clicked.connect(self.play_input_to_output)
        mainlayout.addWidget(but)
        
        
        
        mainlayout.addWidget(but)
        self.label_db_input = QT.QLabel(u'')
        mainlayout.addWidget(self.label_db_input)
        
        mainlayout.addStretch()
        
    
    
    def set_configuration(self, gain = -15., duration = 2., freq = 1000., 
                                            spl_calibration_at_zero_dbfs = 93.979400086720375):
        self.freqgainduration.set( gain = gain, duration = duration, freq = freq)
        self.spinbox_spllevel.setValue(spl_calibration_at_zero_dbfs+gain)
        
    def get_configuration(self):
        config = self.freqgainduration.get()
        config['spl_calibration_at_zero_dbfs'] =  float(self.spinbox_spllevel.value())-config['gain']
        return config
    
    def refresh_label_calibration(self):
        text = u'<h1><b>0 dBFs = {:0.2f} dBSpl</b>'.format(self.get_configuration()['spl_calibration_at_zero_dbfs'])
        self.label_calibration.setText(text)

    def play_sinus(self):
        p = self.freqgainduration.get()

        play_sinus(p['freq'], p['gain'], p['duration'], self.device)