Handle exceptions in update
This commit is contained in:
parent
efb73128ab
commit
89242a9530
13
main.py
13
main.py
@ -50,7 +50,7 @@ def get_supported_sample_rates(pyaudio_device: int,
|
||||
class AudioAnalyzer:
|
||||
frame_queue = None # type: queue.Queue
|
||||
|
||||
_hanning_window = None
|
||||
_hann_window = None
|
||||
_fft_buffer = None
|
||||
_fft_lock = None
|
||||
_stream = None
|
||||
@ -90,7 +90,7 @@ class AudioAnalyzer:
|
||||
|
||||
self._sample_rate = sample_rate
|
||||
self._samples_per_buffer = samples_per_buffer
|
||||
self._hanning_window = (1 - numpy.cos(numpy.linspace(0, 2 * pi, samples_per_fft, False))) / 2
|
||||
self._hann_window = (1 - numpy.cos(numpy.linspace(0, 2 * pi, samples_per_fft, False))) / 2
|
||||
self._fft_freqs = numpy.fft.fftfreq(samples_per_fft, 1 / sample_rate)
|
||||
self._fft_buffer = numpy.zeros(num_buffers * samples_per_buffer, dtype=numpy.float32)
|
||||
self.stop = False
|
||||
@ -128,15 +128,14 @@ class AudioAnalyzer:
|
||||
time_info: Dict,
|
||||
status_flags,
|
||||
):
|
||||
#TODO deal with exceptions happening in the callback!
|
||||
|
||||
try:
|
||||
in_buffer = numpy.fromstring(in_data, numpy.int16)
|
||||
samples_per_buffer = in_buffer.size
|
||||
|
||||
with self._fft_lock:
|
||||
self._fft_buffer[:-samples_per_buffer] = self._fft_buffer[samples_per_buffer:]
|
||||
self._fft_buffer[-samples_per_buffer:] = in_buffer
|
||||
fft = numpy.fft.rfft(self._fft_buffer * self._hanning_window)
|
||||
fft = numpy.fft.rfft(self._fft_buffer * self._hann_window)
|
||||
|
||||
fft_argmax = numpy.abs(fft[1:]).argmax() + 1 # excluding 0-frequency
|
||||
frame_data = {
|
||||
@ -154,9 +153,9 @@ class AudioAnalyzer:
|
||||
|
||||
if self.stop:
|
||||
return None, pyaudio.paComplete
|
||||
else:
|
||||
return None, pyaudio.paContinue
|
||||
|
||||
except:
|
||||
self.close()
|
||||
|
||||
|
||||
def monitor_pitch(pyaudio_device: int,
|
||||
|
Loading…
Reference in New Issue
Block a user