import pyaudio import wave import tkinter as tk from tkinter import Button, Label CHUNK = 1024 FORMAT = pyaudio.paInt16 #paInt8 CHANNELS = 1 RATE = 44100 #sample rate 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) #buffer frames = [] root = tk.Tk() root.title("Voice Recorder") label = Label(root, text="Press button to start recording") label.pack() def start_record(): global frames frames = [] label.config(text="Recording...") stream.start_stream() def stop_record(): label.config(text="Recording stopped") stream.stop_stream() 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() record_button = Button(root, text="Record", command=start_record) record_button.pack() stop_button = Button(root, text="Stop", command=stop_record) stop_button.pack() def callback(in_data, frame_count, time_info, status): frames.append(in_data) return (in_data, pyaudio.paContinue) stream.start_stream() try: while stream.is_active(): stream.write(callback(in_data, frame_count, time_info, status)) except KeyboardInterrupt: pass stream.stop_stream() stream.close() p.terminate() root.mainloop()