Handle exceptions in update

This commit is contained in:
jan 2018-01-17 15:06:59 -08:00
parent efb73128ab
commit 89242a9530

13
main.py
View File

@ -50,7 +50,7 @@ def get_supported_sample_rates(pyaudio_device: int,
class AudioAnalyzer: class AudioAnalyzer:
frame_queue = None # type: queue.Queue frame_queue = None # type: queue.Queue
_hanning_window = None _hann_window = None
_fft_buffer = None _fft_buffer = None
_fft_lock = None _fft_lock = None
_stream = None _stream = None
@ -90,7 +90,7 @@ class AudioAnalyzer:
self._sample_rate = sample_rate self._sample_rate = sample_rate
self._samples_per_buffer = samples_per_buffer 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_freqs = numpy.fft.fftfreq(samples_per_fft, 1 / sample_rate)
self._fft_buffer = numpy.zeros(num_buffers * samples_per_buffer, dtype=numpy.float32) self._fft_buffer = numpy.zeros(num_buffers * samples_per_buffer, dtype=numpy.float32)
self.stop = False self.stop = False
@ -128,15 +128,14 @@ class AudioAnalyzer:
time_info: Dict, time_info: Dict,
status_flags, status_flags,
): ):
#TODO deal with exceptions happening in the callback! try:
in_buffer = numpy.fromstring(in_data, numpy.int16) in_buffer = numpy.fromstring(in_data, numpy.int16)
samples_per_buffer = in_buffer.size samples_per_buffer = in_buffer.size
with self._fft_lock: with self._fft_lock:
self._fft_buffer[:-samples_per_buffer] = self._fft_buffer[samples_per_buffer:] self._fft_buffer[:-samples_per_buffer] = self._fft_buffer[samples_per_buffer:]
self._fft_buffer[-samples_per_buffer:] = in_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 fft_argmax = numpy.abs(fft[1:]).argmax() + 1 # excluding 0-frequency
frame_data = { frame_data = {
@ -154,9 +153,9 @@ class AudioAnalyzer:
if self.stop: if self.stop:
return None, pyaudio.paComplete return None, pyaudio.paComplete
else:
return None, pyaudio.paContinue return None, pyaudio.paContinue
except:
self.close()
def monitor_pitch(pyaudio_device: int, def monitor_pitch(pyaudio_device: int,