验证中...
私信发送成功
语言: Python
分类: 其他
最后更新于 2017-12-07 19:54
录音,播放
原始数据 复制代码
# 播放
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()
# 录音并保存
import pyaudio
import wave
file_path=r"C:\Users\leon\Desktop\ok.wav"
# wf=wave.open(file_path,"rb")
p=pyaudio.PyAudio()
stream = p.open(
format=pyaudio.paInt16,
channels=2,
rate=44000,
frames_per_buffer=1000,
input=True
)
frames=[]
print("start")
for i in range(0,4400):
data=stream.read(1000)
frames.append(data)
print("stop")
stream.stop_stream()
stream.close()
p.terminate()
wf=wave.open(file_path,"wb")
wf.setnchannels(2)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
wf.setframerate(44000)
wf.writeframes(b''.join(frames))
wf.close()
# 录音并保存
# 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()

评论列表( 0 )

你可以在登录后,对此项目发表评论

4_float_left_people 4_float_left_close