mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
add a small delay before terminating recording
https://forums.ankiweb.net/t/anki-crashes-periodically-after-clicking-record-audio-button/5824/12
This commit is contained in:
parent
729293f5e3
commit
7d1c8c29f9
1 changed files with 32 additions and 23 deletions
|
@ -530,6 +530,7 @@ class QtAudioInputRecorder(Recorder):
|
||||||
super().__init__(output_path)
|
super().__init__(output_path)
|
||||||
|
|
||||||
self.mw = mw
|
self.mw = mw
|
||||||
|
self._parent = parent
|
||||||
|
|
||||||
from PyQt5.QtMultimedia import (
|
from PyQt5.QtMultimedia import (
|
||||||
QAudio,
|
QAudio,
|
||||||
|
@ -567,34 +568,42 @@ class QtAudioInputRecorder(Recorder):
|
||||||
self._buffer += self._iodevice.readAll()
|
self._buffer += self._iodevice.readAll()
|
||||||
|
|
||||||
def stop(self, on_done: Callable[[str], None]):
|
def stop(self, on_done: Callable[[str], None]):
|
||||||
# read anything remaining in buffer & stop
|
def on_stop_timer():
|
||||||
self._on_read_ready()
|
# read anything remaining in buffer & stop
|
||||||
self._audio_input.stop()
|
self._on_read_ready()
|
||||||
|
self._audio_input.stop()
|
||||||
|
|
||||||
if err := self._audio_input.error():
|
if err := self._audio_input.error():
|
||||||
showWarning(f"recording failed: {err}")
|
showWarning(f"recording failed: {err}")
|
||||||
return
|
|
||||||
|
|
||||||
def write_file():
|
|
||||||
# swallow the first 300ms to allow audio device to quiesce
|
|
||||||
wait = int(44100 * self.STARTUP_DELAY)
|
|
||||||
if len(self._buffer) <= wait:
|
|
||||||
return
|
return
|
||||||
self._buffer = self._buffer[wait:]
|
|
||||||
|
|
||||||
# write out the wave file
|
def write_file():
|
||||||
wf = wave.open(self.output_path, "wb")
|
# swallow the first 300ms to allow audio device to quiesce
|
||||||
wf.setnchannels(self._format.channelCount())
|
wait = int(44100 * self.STARTUP_DELAY)
|
||||||
wf.setsampwidth(self._format.sampleSize() // 8)
|
if len(self._buffer) <= wait:
|
||||||
wf.setframerate(self._format.sampleRate())
|
return
|
||||||
wf.writeframes(self._buffer)
|
self._buffer = self._buffer[wait:]
|
||||||
wf.close()
|
|
||||||
|
|
||||||
def and_then(fut):
|
# write out the wave file
|
||||||
fut.result()
|
wf = wave.open(self.output_path, "wb")
|
||||||
Recorder.stop(self, on_done)
|
wf.setnchannels(self._format.channelCount())
|
||||||
|
wf.setsampwidth(self._format.sampleSize() // 8)
|
||||||
|
wf.setframerate(self._format.sampleRate())
|
||||||
|
wf.writeframes(self._buffer)
|
||||||
|
wf.close()
|
||||||
|
|
||||||
self.mw.taskman.run_in_background(write_file, and_then)
|
def and_then(fut):
|
||||||
|
fut.result()
|
||||||
|
Recorder.stop(self, on_done)
|
||||||
|
|
||||||
|
self.mw.taskman.run_in_background(write_file, and_then)
|
||||||
|
|
||||||
|
# schedule the stop for half a second in the future,
|
||||||
|
# to avoid truncating the end of the recording
|
||||||
|
self._stop_timer = t = QTimer(self._parent)
|
||||||
|
t.timeout.connect(on_stop_timer) # type: ignore
|
||||||
|
t.setSingleShot(True)
|
||||||
|
t.start(500)
|
||||||
|
|
||||||
|
|
||||||
# PyAudio recording
|
# PyAudio recording
|
||||||
|
|
Loading…
Reference in a new issue