Пример #1
0
class SimpleAudioRecorder(QAudioRecorder):
    def __init__(self, data: ConverterData, transcription: Transcription,
                 app_settings: AppSettings) -> None:
        super().__init__()
        self.app_settings = app_settings
        self.temp = data.get_temp_file()
        self.transcription = transcription
        self.file_path = os.path.join(self.temp,
                                      f'{self.transcription.id}.wav')
        self.settings = QAudioEncoderSettings()

    def start_recording(self) -> None:
        LOG_RECORDER.info("Audio recording started.")
        self.settings.setCodec('audio/pcm')
        self.settings.setChannelCount(1)
        self.settings.setBitRate(96000)
        self.settings.setSampleRate(44100)
        self.settings.setQuality(self.app_settings.audio_quality)
        self.settings.setEncodingMode(QMultimedia.ConstantQualityEncoding)
        container = 'audio/x-wav'
        self.setEncodingSettings(self.settings, QVideoEncoderSettings(),
                                 container)
        self.setOutputLocation(QUrl.fromLocalFile(self.file_path))
        self.record()

    def stop_recording(self) -> Union[str, None]:
        LOG_RECORDER.info("Audio recording finished.")
        LOG_RECORDER.info(f"Audio file: {self.file_path}")
        self.stop()
        return self.file_path
Пример #2
0
    def __setRecordParams(self):  ##设置音频输入参数
        selectedFile = self.ui.editOutputFile.text().strip()
        if (selectedFile == ""):
            QMessageBox.critical(self, "错误", "请先设置录音输出文件")
            return False

        if os.path.exists(selectedFile):
            os.remove(selectedFile)  # 删除已有文件
        ##         QMessageBox.critical(self,"错误","录音输出文件被占用,无法删除")
        ##         return False

        recordFile = QUrl.fromLocalFile(selectedFile)
        self.recorder.setOutputLocation(recordFile)  # 设置输出文件

        recordDevice = self.ui.comboDevices.currentText()
        self.recorder.setAudioInput(recordDevice)  # 设置录入设备

        settings = QAudioEncoderSettings()  # 音频编码设置
        settings.setCodec(self.ui.comboCodec.currentText())  # 编码

        sampRate = int(self.ui.comboSampleRate.currentText())
        settings.setSampleRate(sampRate)  # 采样率

        channelCount = int(self.ui.comboChannels.currentText())
        settings.setChannelCount(channelCount)  # 通道数

        settings.setEncodingMode(QMultimedia.ConstantBitRateEncoding)  # 固定比特率

        self.recorder.setAudioSettings(settings)  # 音频设置
        return True
Пример #3
0
    def __setRecordParams(self):  ##设置音频输入参数
        selectedFile = self.ui.editOutputFile.text().strip()
        if (selectedFile == ""):
            QMessageBox.critical(self, "错误", "请先设置录音输出文件")
            return False

        if os.path.exists(selectedFile):
            os.remove(selectedFile)  #删除已有文件

    ##         QMessageBox.critical(self,"错误","录音输出文件被占用,无法删除")
    ##         return False

        recordFile = QUrl.fromLocalFile(selectedFile)
        self.recorder.setOutputLocation(recordFile)  #设置输出文件

        recordDevice = self.ui.comboDevices.currentText()
        self.recorder.setAudioInput(recordDevice)  #设置录入设备

        settings = QAudioEncoderSettings()  #音频编码设置
        settings.setCodec(self.ui.comboCodec.currentText())  #编码

        sampRate = int(self.ui.comboSampleRate.currentText())
        settings.setSampleRate(sampRate)  #采样率

        bitRate = int(self.ui.comboBitrate.currentText())
        settings.setBitRate(bitRate)  #比特率

        channelCount = int(self.ui.comboChannels.currentText())
        settings.setChannelCount(channelCount)  #通道数

        ##      quality=self.ui.sliderQuality.value()   #也可以
        quality = QMultimedia.EncodingQuality(self.ui.sliderQuality.value())
        settings.setQuality(quality)  #品质

        if self.ui.radioQuality.isChecked():  #编码模式为固定品质,自动决定采样率,采样点大小
            settings.setEncodingMode(QMultimedia.ConstantQualityEncoding)
        else:
            settings.setEncodingMode(
                QMultimedia.ConstantBitRateEncoding)  #固定比特率

        self.recorder.setAudioSettings(settings)  #音频设置
        return True