录音机
import sys
import sounddevice as sd
import soundfile as sf
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
class AudioRecorderApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Audio Recorder")
self.record_button = QPushButton("Record", self)
self.record_button.clicked.connect(self.start_recording)
self.record_button.setGeometry(50, 50, 100, 30)
self.recording = False
self.recorded_data = []
def start_recording(self):
if not self.recording:
self.record_button.setText("Recording...")
self.recording = True
self.recorded_data = []
samplerate = 44100
channels = 2
def callback(indata, frames, time, status):
if status:
print(status)
self.recorded_data.extend(indata.copy())
self.stream = sd.InputStream(callback=callback, samplerate=samplerate, channels=channels)
self.stream.start()
else:
self.record_button.setText("Record")
self.recording = False
if self.stream.active:
self.stream.stop()
self.stream.close()
self.save_recorded_audio()
def save_recorded_audio(self):
filename = "recorded_audio.wav"
sf.write(filename, self.recorded_data, samplerate=44100)
print("Recording saved as:", filename)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = AudioRecorderApp()
window.setGeometry(100, 100, 300, 150)
window.show()
sys.exit(app.exec_())