0
2

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.

残プロ 第-49回 ~PythonでWAVファイル再生~

Last updated at Posted at 2021-07-21

pyaudioを使う

pythonにはwavファイルを扱うwaveライブラリが標準で搭載されています.しかし,再生する関数は提供されていないため自分で用意する必要が有ります.

今回はpyaudioを使用してwavファイルを再生します.
pyaudioのインストール方法についてはこの記事を参考にしてください.python3.7以降ではpipインストール時にエラーが発生します.

PlayWav.py
import wave
import pyaudio

def main():
    wf = wave.open('sample.wav', mode='rb')
    p = pyaudio.PyAudio()
    stream = p.open(
        format=p.get_format_from_width(wf.getsampwidth()),
        channels=wf.getnchannels(),
        rate=wf.getframerate(),
        output=True) 
    chunk = 1024 
    wf.rewind() 
    buf = wf.readframes(chunk)
    while buf:
        stream.write(buf)
        buf = wf.readframes(chunk)
    stream.close()
    p.terminate()


if __name__ == '__main__':
    main()
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?