現在、PythonでGUIアプリを作成しているのですが、その中で「ボタンを押したら録音を開始し、もう一回ボタンを押したら録音を止める」というボイスレコーダーのような機能を実装したくて奮闘していたので今回はそのお話をしたいと思います。
初めはPyaudioを使っていたが
初めはpyaudioで試していたのですが、あれはあらかじめ録音の時間を指定する必要があるため、冒頭の内容を実現するのは難しいです。
G◯◯gle先生に聞いてみると
以下のソースコードを見つけました。(参考)
import subprocess
import wave
print("Start recording...")
p = subprocess.Popen(("rec", "-q", "out.wav"))
input("Enter to stop: ")
p.terminate()
try:
p.wait(timeout=1)
except subprocess.TimeoutExpired:
p.kill()
subprocessを使えば短く簡単にwavファイルを作成できました。これならpyaudioの時よりシンプルでguiアプリに組み込みやすそうです。