help='show list of audio devices and exit') parser.add_argument('-d', '--device', type=int, default=DEVICE, help='numeric id of input deviec') args = parser.parse_args() list_devices = args.list_devices device = args.device if list_devices: print(sd.query_devices()) else: recorder = Recorder(device) raw_input('Press Enter to start recording') record_file = recorder.record() print('Recording...') raw_input('Press Enter to stop recording') recorder.stop() print('Recording finished.') transcriber = TranscribeClient() results = transcriber.transcribe(record_file) print('Transcript: {}'.format(results[0])) type(results) record_file = record_file.strip("'") os.remove(record_file)
class MyWidget(QWidget, Ui_Form): """ 主窗口 """ item_delete_signal = DeleteItemSignal.my_signal().signal def __init__(self, parent=None): super(MyWidget, self).__init__(parent) self.setupUi(self) self.setWindowTitle("VoiceReminder") self.pushButton.setStyleSheet("QPushButton{border-image: url(img/start.png)}") self.lcdNumber.setDigitCount(2) self.lcdNumber.setVisible(False) self.setting_time_dialog = SettingTimeDialog() self.setting_time_dialog.setWindowModality(Qt.ApplicationModal) self.record = Recorder() self.timer = QTimer() self.timing_thread = TimingThread() self.play_thread = PlayAudioThread() self.is_recording = False # 用于判断当前是否正在录音 self.display_all_reminders_list_from_existed_database() self.timer.timeout.connect(self.displaying_recording_time) self.pushButton.clicked.connect(lambda: self.start_or_stop_recording(self.is_recording)) self.setting_time_dialog.pushButton.clicked.connect(self.set_time_from_dialog) self.listWidget.itemClicked.connect(self.play_corresponding_audio_file) # itemClicked自带一个参数:item self.item_delete_signal.connect(self.display_all_reminders_list_from_existed_database) self.timing_thread.time_out_signal.connect(self.display_all_reminders_list_from_existed_database) def displaying_recording_time(self): """ 每1秒触发一次,使得LCD显示加1,作为录音时长的显示 """ self.lcdNumber.display(self.lcdNumber.intValue() + 1) def start_or_stop_recording(self, flag): """ 根据self.is_recording标记判断此时按下按钮是开始录音还是停止录音 """ if not flag: # 开始录音 self.lcdNumber.setVisible(True) self.start_recording() self.timer.start(1000) else: # 停止录音,并显示提醒时间的设置对话框 self.timer.stop() self.lcdNumber.setVisible(False) self.lcdNumber.display(0) self.stop_recording() self.setting_time_dialog.show_in_somewhere( self.pos().x() + (self.width() - self.setting_time_dialog.width()) // 2, self.pos().y() + (self.height() - self.setting_time_dialog.height()) // 2) self.setting_time_dialog.display_time_same_as_current_time() def start_recording(self): """ 开始录音 """ self.is_recording = True self.pushButton.setStyleSheet("QPushButton{border-image: url(img/stop.png)}") self.record.start() def stop_recording(self): """ 停止录音 """ self.is_recording = False self.pushButton.setStyleSheet("QPushButton{border-image: url(img/start.png)}") self.record.stop() self.record.save() def set_time_from_dialog(self): """ 设置时间后,将(时间串, 录音文件名)存入数据文件,并刷新窗口 """ date_string = self.setting_time_dialog.dateEdit.date().toString("yyyy-MM-dd") time_string = self.setting_time_dialog.timeEdit.time().toString("HH:mm") target_time_string = date_string + " " + time_string self.setting_time_dialog.setVisible(False) save_reminder_information_to_database(target_time_string, self.record.filename) self.display_all_reminders_list_from_existed_database() def display_all_reminders_list_from_existed_database(self, database_file=DATABASE_FILE): """ 读取数据文件中的所有待办事项并重新显示 """ self.listWidget.clear() try: reminder_list = get_data_from_database(database_file) if not reminder_list: print("无语音待办提醒") return current_time_string = get_current_time_string() # 获取当前时间,判断哪些是无效的那些是有效的 happened, not_happened = split_into_happened_and_not_happened_ones(current_time_string, reminder_list) if not not_happened: self.timing_thread.set_time_and_audio_filename('', '') # 没有not_happened待办时,传递空值参数使计时线程停止 print("所有语音待办提醒均已完成") else: self.timing_thread.set_time_and_audio_filename(not_happened[0][0], not_happened[0][1]) self.timing_thread.start() not_happened_length = len(not_happened) for i, reminder in enumerate(not_happened + happened[::-1]): item = MyQListItem(name=reminder[0]) item.setToolTip(reminder[1]) if i >= not_happened_length: # 已完成待办显示成灰色 item.label.setStyleSheet("color:#aaaaaa;") else: item.label.setStyleSheet("color:#000000;") self.listWidget.addItem(item) self.listWidget.setItemWidget(item, item.widget) except FileNotFoundError: print("您还没有创建任何语音待办提醒") def play_corresponding_audio_file(self, item): """ 播放列表中的此item对应的语音文件 """ print(item.label.text(), "Voice filename:", item.toolTip()) try: self.play_thread.set_wav_filename(item.toolTip()) self.play_thread.start() except FileNotFoundError: # 如果找不到对应的语音文件,则删除此条 msg_box = QMessageBox() ret = msg_box.warning(self, "Warning", "Can't find .wav file, will u want to delete it?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if ret == QMessageBox.Yes: delete_reminder_from_database(item.label.text(), item.toolTip()) self.display_all_reminders_list_from_existed_database()