码迷,mamicode.com
首页 > 编程语言 > 详细

python调用声卡录制声音

时间:2018-08-09 19:30:43      阅读:1255      评论:0      收藏:0      [点我收藏+]

标签:channels   声卡   author   coding   python   str   port   record   ram   

# _*_ coding: utf-8 _*_
__author__ = ‘pythonwu‘
__date__ = "2018/7/19 17:39"

import pyaudio
import wave #使用wave模式

CHUNK = 1024 #缓存区设置 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()

python调用声卡录制声音

标签:channels   声卡   author   coding   python   str   port   record   ram   

原文地址:https://www.cnblogs.com/wudeng/p/9450916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!