Skip to content

Instantly share code, notes, and snippets.

@wrist
Created February 11, 2015 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrist/600a878fb69c59c21cb8 to your computer and use it in GitHub Desktop.
Save wrist/600a878fb69c59c21cb8 to your computer and use it in GitHub Desktop.
audioreadのテスト
#!/usr/bin/env python
# vim:fileencoding=utf8
import numpy as np
import matplotlib.pyplot as plt
import audioread as ar
def pcm2float(short_ndary):
float_ndary = np.array(short_ndary, dtype=np.float64)
return np.where(float_ndary > 0.0, float_ndary / 32767.0, float_ndary / 32768.0)
plt.close("all")
plt.figure(1)
filename = "/path/to/your/wavfile.wav"
wav_bary = bytearray()
with ar.audio_open(filename) as f:
print("ch: {0}, fs: {1}, duration [s]: {2}".format(f.channels, f.samplerate, f.duration))
# "block_samples"で指定されたチャンクサイズずつ処理する(デフォルト1024)
for buf in f:
wav_bary.extend(buf)
wav_ary = np.frombuffer(wav_bary, dtype=np.int16) # 常時16bitで読み込まれる
wav_l = wav_ary[0::2]
wav_r = wav_ary[1::2]
print(wav_l.shape)
print(wav_r.shape)
# shortをfloat64に変換
wav_float_l = pcm2float(wav_l)
wav_float_r = pcm2float(wav_r)
plt.plot(wav_float_l)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment