import tkinter as tk
import pyaudio
import wave
from idna import uts46_remap
# 录音参数设置
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 8000
WAVE_OUTPUT_FILENAME = "output.wav"
first_click = True
frames = []
# 录制音频数据
def luyin():
# 初始化 PyAudio 对象
p = pyaudio.PyAudio()
# 打开音频流
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
if first_click:
data = stream.read(CHUNK)
frames.append(data)
root.after_idle(luyin)
def ting():
global first_click
first_click = False
stream.stop_stream()
# 关闭音频流
stream.close()
# 终止 PyAudio 对象
p.terminate()
# 保存录制的音频为 WAV 文件
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()
# 创建主窗口
root = tk.Tk()
root.title("录音")
button1 = tk.Button(root, text="录音",width=6,height=1,command=luyin)
button1.place(x=10, y=10)
button2 =tk.Button(root,text="停止",width=6,height=1,command=ting)
button2.place(x=100,y=10)
text_box=tk.Text(root,height=10,width=30)
text_box.place(x=50,y=100)
root.geometry("400x300+100+100")
#button.pack()
root.mainloop()
04-23
07-31
2074
