1 Star 4 Fork 2

QuanHaHQuan/AI based chatbot.(AI聊天机器人)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
recordingThread.py 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
Quan-star 提交于 2022-01-10 09:20 . 项目代码
# -*- coding: utf-8 -*-
import pyaudio
import wave
import threading
CHUNK_SIZE = 1024 # wav文件是由若干个CHUNK组成的,CHUNK我们就理解成数据包或者数据片段。
FORMAT = pyaudio.paInt16 # 这个参数后面写的pyaudio.paInt16表示我们使用量化位数 16位来进行录音。
CHANNELS = 1 # 代表的是声道,这里使用的单声道。
SAMPLE_RATE = 16000 # 采样率16k
REC_QUESTION_WAV = "recQuestion.wav" # 输出文件名
isRecording = False # 是否正在录音
pya = pyaudio.PyAudio()
pyaStream = 0
pyaFrames = []
thRec = threading.Thread() # 录音线程
thRec.start()
thRec.join()
class recordingThread(threading.Thread):
def __init__(self) -> None:
super().__init__()
def run(self):
global isRecording, pya, pyaStream, pyaFrames
pya = pyaudio.PyAudio()
pyaStream = pya.open(rate=SAMPLE_RATE,
channels=CHANNELS,
format=FORMAT,
input=True,
frames_per_buffer=CHUNK_SIZE)
pyaFrames = []
while isRecording:
data = pyaStream.read(CHUNK_SIZE)
pyaFrames.append(data)
def startRecording():
global thRec, isRecording
isRecording = True
thRec = recordingThread()
thRec.start()
def stopRecording():
global thRec, isRecording, pya
isRecording = False
thRec.join()
pyaStream.stop_stream()
pyaStream.close()
pya.terminate()
wf = wave.open(REC_QUESTION_WAV, "wb")
wf.setnchannels(CHANNELS)
wf.setsampwidth(pya.get_sample_size(FORMAT))
wf.setframerate(SAMPLE_RATE)
wf.writeframes(b''.join(pyaFrames))
wf.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/quanquaqu/ai-based-chatbot.git
git@gitee.com:quanquaqu/ai-based-chatbot.git
quanquaqu
ai-based-chatbot
AI based chatbot.(AI聊天机器人)
master

搜索帮助