Handle exceptions in update
This commit is contained in:
parent
efb73128ab
commit
89242a9530
55
main.py
55
main.py
@ -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,35 +128,34 @@ class AudioAnalyzer:
|
|||||||
time_info: Dict,
|
time_info: Dict,
|
||||||
status_flags,
|
status_flags,
|
||||||
):
|
):
|
||||||
#TODO deal with exceptions happening in the callback!
|
|
||||||
|
|
||||||
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_argmax = numpy.abs(fft[1:]).argmax() + 1 # excluding 0-frequency
|
|
||||||
frame_data = {
|
|
||||||
'fft': fft,
|
|
||||||
'fft_argmax': fft_argmax,
|
|
||||||
'frequency': self.fft_freqs[fft_argmax],
|
|
||||||
'magnitude': numpy.abs(fft[fft_argmax]),
|
|
||||||
}
|
|
||||||
|
|
||||||
time_per_buffer = self._samples_per_buffer / self._sample_rate
|
|
||||||
try:
|
try:
|
||||||
self.frame_queue.put(frame_data, timeout=time_per_buffer * 10)
|
in_buffer = numpy.fromstring(in_data, numpy.int16)
|
||||||
except queue.Full:
|
samples_per_buffer = in_buffer.size
|
||||||
logger.warning('Frame queue was full for more than 10 buffer periods!')
|
|
||||||
|
|
||||||
if self.stop:
|
with self._fft_lock:
|
||||||
return None, pyaudio.paComplete
|
self._fft_buffer[:-samples_per_buffer] = self._fft_buffer[samples_per_buffer:]
|
||||||
else:
|
self._fft_buffer[-samples_per_buffer:] = in_buffer
|
||||||
|
fft = numpy.fft.rfft(self._fft_buffer * self._hann_window)
|
||||||
|
|
||||||
|
fft_argmax = numpy.abs(fft[1:]).argmax() + 1 # excluding 0-frequency
|
||||||
|
frame_data = {
|
||||||
|
'fft': fft,
|
||||||
|
'fft_argmax': fft_argmax,
|
||||||
|
'frequency': self.fft_freqs[fft_argmax],
|
||||||
|
'magnitude': numpy.abs(fft[fft_argmax]),
|
||||||
|
}
|
||||||
|
|
||||||
|
time_per_buffer = self._samples_per_buffer / self._sample_rate
|
||||||
|
try:
|
||||||
|
self.frame_queue.put(frame_data, timeout=time_per_buffer * 10)
|
||||||
|
except queue.Full:
|
||||||
|
logger.warning('Frame queue was full for more than 10 buffer periods!')
|
||||||
|
|
||||||
|
if self.stop:
|
||||||
|
return None, pyaudio.paComplete
|
||||||
return None, pyaudio.paContinue
|
return None, pyaudio.paContinue
|
||||||
|
except:
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
|
||||||
def monitor_pitch(pyaudio_device: int,
|
def monitor_pitch(pyaudio_device: int,
|
||||||
|
Loading…
Reference in New Issue
Block a user