python 解码 音频:
import time
import audioread
import matplotlib.pyplot as plt
import numpy as np
import sys, os
filename = '~/Music/a.mp3'
with audioread.audio_open(filename) as f:
print(f.channels, f.samplerate, f.duration)
for buf_i, buf in enumerate(f):
if buf_i < 100:
continue
data = np.frombuffer(buf, np.int16)
plt.plot(range(buf_i*len(data)//2, (buf_i+1)*len(data)//2), data[0::2])
plt.plot(range(buf_i*len(data)//2, (buf_i+1)*len(data)//2), data[1::2])
if buf_i > 120:
break
plt.show()
做了一个简单的变化,方便后期处理音频数据:
import time
import audioread
import matplotlib.pyplot as plt
import numpy as np
import sys, os
filename = '~/Music/a.mp3'
with audioread.audio_open(filename) as f:
print(f.channels, f.samplerate, f.duration)
audio_cs = [[]]*f.channels
for buf_i, buf in enumerate(f):
data = np.frombuffer(buf, np.int16)
for ci in range(f.channels):
audio_cs[ci]+=(data[ci::f.channels].tolist())
for c in audio_cs:
print(len(c))
plt.plot(range(0,len(c)), c)
plt.show()
参考: