环境:python3.6.3 pyaudio
基础知识:
声音在电子设备里的存储形式:在某一时间点的数值大小(0-255)
采样率:每秒采集的次数
采样格式:每次采集获取的位数
块(chunk):wave格式的文件格式
播放
# 播放
import wave,pyaudio
wf=wave.open("ok.wav","rb")
p=pyaudio.PyAudio()
stream=p.open(
format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True
)
data=wf.readframes(102)
while data !=" ":
stream.write(data)
data=wf.readframes(102)
stream.stop_stream()
stream.close()
p.terminate()
录音并保存
# 录音并保存
# MPG123 免费的命令行MP3播放器
import pyaudio
import wave
CHUNK = 1024 # 块大小
FORMAT = pyaudio.paInt16 # 每次采集的位数
CHANNELS = 2 # 声道数
RATE = 44100 # 采样率:每秒采集数据的次数
RECORD_SECONDS = 5 # 录音时间
WAVE_OUTPUT_FILENAME = "output.wav" # 文件存放位置
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
录音并画出每块的波形
import numpy,time
import pylab
import pyaudio
import wave
wf=wave.open("ok.wav","rb")
da=[]
p=pyaudio.PyAudio()
stream=p.open(
format=p.get_format_from_width(wf.getsampwidth()),
# format=p.get_format_from_width(wf.getsampwidth())
rate=wf.getframerate(),
channels=wf.getnchannels(),
output=True
)
data=wf.readframes(1000)
while data !=" ":
stream.write(data)
da.append(data)
data=wf.readframes(1000)
dd=numpy.fromstring(frames,dtype=numpy.int16)
pylab.plot(dd)
pylab.savefig("kkkk.png",dpi=100)
pylab.show(block=False)
time.sleep(10)
pylab.close()
stream.stop_stream()
stream.close()
p.terminate()