WAVEファイルを扱うための wave モジュールが標準で用意されている。
| testwavemodule.py |
# testwavemodule.py
# encoding: utf-8
# これは Python 2.x 用のプログラム
# Python 3.x では print("チャンネル数 = ", numch) のようにする。
import wave
fname = 'guitar-5-3.wav'
wfile = wave.open(fname, 'r')
numch = wfile.getnchannels()
samplewidth = wfile.getsampwidth()
samplerate = wfile.getframerate()
numsamples = wfile.getnframes()
print u"チャンネル数 = ", numch
print u"サンプル幅 (バイト数) = ", samplewidth
print u"サンプリングレート(Hz) =", samplerate
print u"サンプル数 =", numsamples
print u"録音時間 =", numsamples / samplerate
|
| 実行結果 |
$ ls guitar-5-3.wav testwavemodule2.py testwavemodule.py testwavemodule3.py $ python testwavemodule.py チャンネル数 = 2 サンプル幅 (バイト数) = 2 サンプリングレート(Hz) = 44100 サンプル数 = 452025 録音時間 = 10 $ |
桂田 祐史