5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

マイクで拾った音をPyaudio経由で録音する

Posted at

PyAudio

  • クロスプラットフォームのオーディオI/Oライブラリ
  • MITライセンスで配布されています

インストール(Mac)

  • pip 経由でインストールします
  • 動作にはPortAudioが必要です
brew install portaudio 
pip install pyaudio

録音デバイス

  • Macのビルドインマイクを使って動作確認
  • デバイス情報はpyaudioのメソッドで確認ができます
In [21]: p = pyaudio.PyAudio()                                                  

In [22]: p.get_default_input_device_info()                                      
Out[22]: 
{'index': 0,
 'structVersion': 2,
 'name': 'Built-in Microphone',
 'hostApi': 0,
 'maxInputChannels': 2,
 'maxOutputChannels': 0,
 'defaultLowInputLatency': 0.0029478458049886623,
 'defaultLowOutputLatency': 0.01,
 'defaultHighInputLatency': 0.02471655328798186,
 'defaultHighOutputLatency': 0.1,
 'defaultSampleRate': 44100.0}

録音のための関数

  • PyAudioを使って取得した録音内容をリストに格納
  • 最後にwavファイルに書き出すようにしました

  • サンプリング周波数 12000Hz, 32bit浮動小数点で録音します

import pyaudio
import numpy as np
import scipy.io.wavfile 

RATE = 12000
def record_audio(output_name):
    CHUNK = 1024
    FORMAT = pyaudio.paFloat32
    CHANNELS = 1

    pa = pyaudio.PyAudio()
    stream = pa.open(rate=RATE,
        channels=CHANNELS,
        format=FORMAT,
        input=True,
        frames_per_buffer=CHUNK)

    print("RECORD START")
    print("ctrl-C : STOP RECORDING")

    # RECORDING ###############
    # Stop with Ctrl-C
    frame = []
    while True:
        try:
            # Record
            d = stream.read(CHUNK)
            d = np.frombuffer(d, dtype=np.float32)   # convert numpy   
            frame.append(d)

        except KeyboardInterrupt:
            # Ctrl - c  
            break
    # END # RECORDING ###############

    # CLOSE
    stream.stop_stream()
    stream.close()
    pa.terminate()

    # Numpy Array
    frame = np.array(frame).flatten()
    print("STOP {} samples  {:.2f}s".format(frame.size, frame.size/RATE))
    # Write Wavfile with scipy module
    scipy.io.wavfile.write(output_name, RATE, frame)
    print("OUTPUT: {}".format(basename(output_name))

録音結果

  • ファイル名をつけて呼び出します
record_audio('test.wav')    # 呼び出し
  • コンソールの表示内容
    • Ctrl-c で途中でSTOPしました
RECORD START
ctrl-C : STOP RECORDING
^C
STOP 111616 samples  9.30s
OUTPUT: test.wav
  • 'あ〜'と喋っている波形が録音できました

参考

PyAudio

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?