def in_dev_change(self, new_index): # Disconnect and stop the devices if they are connected. self.disconnect_devices() self.input_device = None # device object goes out of scope # Get the QAudioDeviceInfo corresponding to this index of the combox. audio_info = self.input_info_list[new_index] # Create a new QAudioInput based on that. preferred_format = audio_info.preferredFormat() self.input_device = QAudioInput(audio_info, preferred_format) # the input device volume is always 1.0, wide open. self.input_device.setVolume(1.0) # The choice of buffer size has a major impact on the lag. It needs # to be small or there is severe echo; but if it is too small, there # is a sputtering or "motor-boating" effect. self.input_device.setBufferSize(384) # hook up possible debug status display self.input_device.stateChanged.connect(self.in_dev_state_change) # reconnect the devices if possible. self.reconnect_devices()
def __init__(self, output_path: str, mw: aqt.AnkiQt, parent: QWidget) -> None: super().__init__(output_path) self.mw = mw self._parent = parent from PyQt5.QtMultimedia import QAudioDeviceInfo, QAudioFormat, QAudioInput format = QAudioFormat() format.setChannelCount(1) format.setSampleRate(44100) format.setSampleSize(16) format.setCodec("audio/pcm") format.setByteOrder(QAudioFormat.LittleEndian) format.setSampleType(QAudioFormat.SignedInt) device = QAudioDeviceInfo.defaultInputDevice() if not device.isFormatSupported(format): format = device.nearestFormat(format) print("format changed") print("channels", format.channelCount()) print("rate", format.sampleRate()) print("size", format.sampleSize()) self._format = format self._audio_input = QAudioInput(device, format, parent)
def initAUD(self): # info = QAudioDeviceInfo.defaultInputDevice() if (~info.isFormatSupported(self.audio.format)): # print("警告,设置的默认音频格式并不支持,将尝试采用最相近的支持格式") # 不知道这里面有什么神改动? self.audio.format = info.nearestFormat(self.audio.format) # update_interval = 160 self.audioRecorder = QAudioInput(self.audio.format) self.audioRecorder.setNotifyInterval(update_interval) #按毫秒ms 类似于QTimer的作用 self.audioRecorder.notify.connect(self.processAudioData) self.audioRecorder_TD = QThread() self.audioRecorder.moveToThread(self.audioRecorder_TD) self.audioRecorder_TD.started.connect(self.startRecord) self.audioRecorder.stateChanged.connect(self.recordStopped) # 总结来说线程只是一个容器,里面执行的循环要是没法结束,强制退出也不好操作 # 所以还是好好写好任务流然后发送信号比较合理 self.audioPlayer = QAudioOutput(self.audio.format) self.audioPlayer.setNotifyInterval(update_interval) self.audioPlayer.notify.connect(self.processAudioData) self.audioPlayer_TD = QThread() self.audioPlayer.moveToThread(self.audioPlayer_TD) self.audioPlayer_TD.started.connect(self.startPlay) self.audioPlayer.stateChanged.connect(self.playStopped)
def changeinput(self, val): audio = QAudioFormat() audio.setSampleRate(44100) audio.setSampleType(QAudioFormat.UnSignedInt) audio.setSampleSize(8) audio.setCodec('audio/pcm') audio.setChannelCount(1) self.input = QAudioInput(self.inputdevices[val], audio)
def setup(self): self.output.setFileName("record.pcm") self.output.open(QIODevice.WriteOnly | QIODevice.Truncate) settings = QAudioFormat() settings.setCodec("audio/pcm") settings.setSampleRate(16000) settings.setSampleSize(16) settings.setChannelCount(1) settings.setByteOrder(QAudioFormat.LittleEndian) settings.setSampleType(QAudioFormat.SignedInt) self.audio = QAudioInput(settings)
def __init__(self): super(Recorder, self).__init__() self.setupUi(self) self.output = QFile() self.audio = QAudioInput() self.NEED_ASR = False self.Open = True self.th1 = threading.Thread(target=self.ASR) self.th1.start() self.Pause_Button.clicked.connect(self.toggle_pause) self.Record_Button.clicked.connect(self.toggle_record)
def __init__(self): super().__init__() self.m_chart = QChart() chart_view = QChartView(self.m_chart) chart_view.setMinimumSize(800, 600) self.m_series = QLineSeries() self.m_chart.addSeries(self.m_series) axis_x = QValueAxis() axis_x.setRange(0, 2000) axis_x.setLabelFormat("%g") axis_x.setTitleText("Samples") axis_y = QValueAxis() axis_y.setRange(-1, 1) axis_y.setTitleText("Audio level") self.m_chart.setAxisX(axis_x, self.m_series) self.m_chart.setAxisY(axis_y, self.m_series) self.m_chart.setTitle("Data from the microphone") main_layout = QVBoxLayout() main_layout.addWidget(chart_view) self.setLayout(main_layout) format_audio = QAudioFormat() format_audio.setSampleRate(48000) format_audio.setChannelCount(1) format_audio.setSampleSize(8) format_audio.setCodec("audio/pcm") format_audio.setByteOrder(QAudioFormat.LittleEndian) format_audio.setSampleType(QAudioFormat.UnSignedInt) input_devices = QAudioDeviceInfo.defaultInputDevice() self.m_audio_input = QAudioInput(input_devices, format_audio) self.m_device = XYSeriesIODevice(self.m_series) self.m_device.open(QIODevice.WriteOnly) self.m_audio_input.start(self.m_device) self.init_ui()
def in_dev_change(self, new_index): if self.input_device: if self.otput_device: self.otput_device.stop() self.input_device.stop() self.input_device = None # goodby object # Get the QAudioDeviceInfo corresponding to this index of the combox. audio_info = self.input_info_list[new_index] # Create a new QAudioInput based on that. preferred_format = audio_info.preferredFormat() self.input_device = QAudioInput(audio_info, preferred_format) self.input_device.setVolume(1.0) self.input_device.setBufferSize(384) # If we have an output device, redirect it to this input. This is # done by asking the input device for its QIODevice, and passing that # to the output device's start() method. if self.otput_device: self.input_device.start(self.otput_device.start())
def initializeAudio(self, deviceInfo): """ Make a QAudioInput from the given device """ # make buffers of 40ms of samples self.refRate = 0.04 # mono, 32-bit float audio fmt = QAudioFormat() fmt.setSampleRate(self.getSampleRate()) fmt.setChannelCount(1) fmt.setSampleSize(32) fmt.setSampleType(QAudioFormat.Float) fmt.setByteOrder(QAudioFormat.LittleEndian) fmt.setCodec("audio/pcm") if not deviceInfo.isFormatSupported(fmt): fmt = deviceInfo.nearestFormat(fmt) self.audioInput = QAudioInput(deviceInfo, fmt) self.audioInput.setBufferSize(4*self.buflen) # set size in bytes
def initAudioInput(self, filepath): self.outputFile = QFile() self.outputFile.setFileName(filepath) self.outputFile.open(QIODevice.WriteOnly | QIODevice.Truncate) format = QAudioFormat() format.setSampleType(QAudioFormat.Float) format.setSampleRate(44100) format.setChannelCount(1) format.setSampleSize(32) format.setCodec("audio/pcm") format.setByteOrder(QAudioFormat.LittleEndian) print(format.codec()) #self.audio_input = QAudioInput(QAudioDeviceInfo.defaultInputDevice(), format); self.audio_input = QAudioInput(format) print(self.audio_input.error()) print(self.audio_input.state())
def on_actStart_triggered(self): audioFormat=QAudioFormat() #使用固定格式 audioFormat.setSampleRate(8000) audioFormat.setChannelCount(1) audioFormat.setSampleSize(8) audioFormat.setCodec("audio/pcm") audioFormat.setByteOrder(QAudioFormat.LittleEndian) audioFormat.setSampleType(QAudioFormat.UnSignedInt) index=self.ui.comboDevices.currentIndex() deviceInfo =self.__deviceList[index] #当前音频设备 if (False== deviceInfo.isFormatSupported(audioFormat)): QMessageBox.critical(self,"错误","测试失败,输入设备不支持此设置") return self.audioDevice = QAudioInput(deviceInfo,audioFormat) #音频输入设备 self.audioDevice.setBufferSize(self.BUFFER_SIZE) #设置的缓冲区大小,字节数,并不一定等于实际数据块大小 self.audioDevice.stateChanged.connect(self.do_stateChanged) #状态变化 ##1. 使用 start()->QIODevice 启动,返回的内置的IODevice, pull mode,利用readyRead()信号读出数据 if self.ui.radioSaveMode_Inner.isChecked(): self.ioDevice=self.audioDevice.start() #返回内建的IODevice self.ioDevice.readyRead.connect(self.do_IO_readyRead) ## 2. 自定义流设备QWAudioBlockReader,push mode, start(QIODevice),不行 ## if self.ui.radioSaveMode_External.isChecked(): ## self.externalReader = QmyAudioReader() ## self.externalReader.open(QIODevice.WriteOnly) ## self.externalReader.updateBlockInfo.connect(self.do_updateBlockInfo) ## self.audioDevice.start(self.externalReader) #使用外建的IODevice ##3. 写入文件,用 start(QIODevice)启动 if self.ui.radioSaveMode_QFile.isChecked(): self.recordFile.setFileName("test.raw") self.recordFile.open(QIODevice.WriteOnly) self.audioDevice.start(self.recordFile)