From 39c0a57b13a17c61eef08be622547d5d114fdb99 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 5 Dec 2017 11:07:52 +1000 Subject: [PATCH] work around pops in recording start https://anki.tenderapp.com/discussions/ankidesktop/26005-when-recording-sound-on-mbp on my machine, the pops start around sample 2048 of a 44.1khz recording, and only for the first recording after the audio hardware has gone to sleep --- anki/sound.py | 11 +++++++++-- aqt/sound.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/anki/sound.py b/anki/sound.py index 983a9bcd8..dc573b9af 100644 --- a/anki/sound.py +++ b/anki/sound.py @@ -288,8 +288,9 @@ class _Recorder: class PyAudioThreadedRecorder(threading.Thread): - def __init__(self): + def __init__(self, startupDelay): threading.Thread.__init__(self) + self.startupDelay = startupDelay self.finish = False def run(self): @@ -297,6 +298,7 @@ class PyAudioThreadedRecorder(threading.Thread): p = pyaudio.PyAudio() rate = int(p.get_default_input_device_info()['defaultSampleRate']) + wait = int(rate * self.startupDelay) stream = p.open(format=PYAU_FORMAT, channels=PYAU_CHANNELS, @@ -305,6 +307,8 @@ class PyAudioThreadedRecorder(threading.Thread): input_device_index=PYAU_INPUT_INDEX, frames_per_buffer=chunk) + stream.read(wait) + data = b"" while not self.finish: try: @@ -325,6 +329,9 @@ class PyAudioThreadedRecorder(threading.Thread): class PyAudioRecorder(_Recorder): + # discard first 250ms which may have pops/cracks + startupDelay = 0.25 + def __init__(self): for t in recFiles + [processingSrc, processingDst]: try: @@ -334,7 +341,7 @@ class PyAudioRecorder(_Recorder): self.encode = False def start(self): - self.thread = PyAudioThreadedRecorder() + self.thread = PyAudioThreadedRecorder(startupDelay=self.startupDelay) self.thread.start() def stop(self): diff --git a/aqt/sound.py b/aqt/sound.py index 2c8fb39b6..7b035da94 100644 --- a/aqt/sound.py +++ b/aqt/sound.py @@ -22,6 +22,7 @@ def getAudio(parent, encode=True): mb.setEscapeButton(but) t = time.time() r.start() + time.sleep(r.startupDelay) QApplication.instance().processEvents() while not mb.clickedButton(): txt =_("Recording...
Time: %0.1f")