class AudioBackend(QtCore.QObject): underflow = QtCore.pyqtSignal() new_data_available_from_callback = QtCore.pyqtSignal( bytes, int, float, int) new_data_available = QtCore.pyqtSignal(ndarray, float, int) def callback(self, in_data, frame_count, time_info, status): # do the minimum from here to prevent overflows, just pass the data to the main thread input_time = time_info['input_buffer_adc_time'] # some API drivers in PortAudio do not return a valid time, so fallback to the current stream time if input_time == 0.: input_time = time_info['current_time'] if input_time == 0.: input_time = self.stream.get_time() self.new_data_available_from_callback.emit(in_data, frame_count, input_time, status) return (None, 0) def __init__(self, logger): QtCore.QObject.__init__(self) self.logger = logger self.duo_input = False self.terminated = False self.logger.push("Initializing PyAudio") self.pa = PyAudio() # look for devices self.input_devices = self.get_input_devices() self.output_devices = self.get_output_devices() self.device = None self.first_channel = None self.second_channel = None # we will try to open all the input devices until one # works, starting by the default input device for device in self.input_devices: self.logger.push("Opening the stream") try: self.stream = self.open_stream(device) self.stream.start_stream() self.device = device self.logger.push("Success") break except: self.logger.push("Fail") if self.device is not None: self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 # counter for the number of input buffer overflows self.xruns = 0 self.chunk_number = 0 self.new_data_available_from_callback.connect(self.handle_new_data) def close(self): if self.stream is not None: self.stream.stop_stream() self.stream.close() self.stream = None if not self.terminated: # call terminate on PortAudio self.logger.push("Terminating PortAudio") self.pa.terminate() self.logger.push("PortAudio terminated") # avoid calling PortAudio methods in the callback/slots self.terminated = True # method def get_readable_devices_list(self): devices_list = [] default_device_index = self.get_default_input_device() for device in self.input_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index( dev_info['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index( device)['maxInputChannels'] desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_readable_output_devices_list(self): devices_list = [] default_device_index = self.get_default_output_device() for device in self.output_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index( dev_info['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index( device)['maxOutputChannels'] desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_default_input_device(self): try: index = self.pa.get_default_input_device_info()['index'] except IOError: index = None return index # method def get_default_output_device(self): try: index = self.pa.get_default_output_device_info()['index'] except IOError: index = None return index # method def get_device_count(self): return self.pa.get_device_count() # method # returns a list of input devices index, starting with the system default def get_input_devices(self): device_count = self.get_device_count() device_range = list(range(0, device_count)) default_input_device = self.get_default_input_device() if default_input_device is not None: # start by the default input device device_range.remove(default_input_device) device_range = [default_input_device] + device_range # select only the input devices by looking at the number of input channels input_devices = [] for device in device_range: n_input_channels = self.pa.get_device_info_by_index( device)['maxInputChannels'] if n_input_channels > 0: input_devices += [device] return input_devices # method # returns a list of output devices index, starting with the system default def get_output_devices(self): device_count = self.get_device_count() device_range = list(range(0, device_count)) default_output_device = self.get_default_output_device() if default_output_device is not None: # start by the default input device device_range.remove(default_output_device) device_range = [default_output_device] + device_range # select only the output devices by looking at the number of output channels output_devices = [] for device in device_range: n_output_channels = self.pa.get_device_info_by_index( device)['maxOutputChannels'] if n_output_channels > 0: output_devices += [device] return output_devices # method. # The index parameter is the index in the self.input_devices list of devices ! # The return parameter is also an index in the same list. def select_input_device(self, index): device = self.input_devices[index] # save current stream in case we need to restore it previous_stream = self.stream previous_device = self.device self.logger.push("Trying to open input device #%d" % (index)) try: self.stream = self.open_stream(device) self.device = device self.stream.start_stream() success = True except: self.logger.push("Fail") success = False if self.stream is not None: self.stream.close() # restore previous stream self.stream = previous_stream self.device = previous_device if success: self.logger.push("Success") previous_stream.close() self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 return success, self.input_devices.index(self.device) # method def select_first_channel(self, index): self.first_channel = index success = True return success, self.first_channel # method def select_second_channel(self, index): self.second_channel = index success = True return success, self.second_channel # method def open_stream(self, device): # by default we open the device stream with all the channels # (interleaved in the data buffer) max_input_channels = self.pa.get_device_info_by_index( device)['maxInputChannels'] stream = self.pa.open(format=paInt16, channels=max_input_channels, rate=SAMPLING_RATE, input=True, input_device_index=device, stream_callback=self.callback, frames_per_buffer=FRAMES_PER_BUFFER) lat_ms = 1000 * stream.get_input_latency() self.logger.push("Device claims %d ms latency" % (lat_ms)) return stream # method def open_output_stream(self, device, callback): # by default we open the device stream with all the channels # (interleaved in the data buffer) max_output_channels = self.pa.get_device_info_by_index( device)['maxOutputChannels'] stream = self.pa.open(format=paInt16, channels=max_output_channels, rate=SAMPLING_RATE, output=True, frames_per_buffer=FRAMES_PER_BUFFER, output_device_index=device, stream_callback=callback) return stream def is_output_format_supported(self, device, output_format): max_output_channels = self.pa.get_device_info_by_index( device)['maxOutputChannels'] success = self.pa.is_format_supported( SAMPLING_RATE, output_device=device, output_channels=max_output_channels, output_format=output_format) return success # method # return the index of the current input device in the input devices list # (not the same as the PortAudio index, since the latter is the index # in the list of *all* devices, not only input ones) def get_readable_current_device(self): return self.input_devices.index(self.device) # method def get_readable_current_channels(self): dev_info = self.pa.get_device_info_by_index(self.device) nchannels = dev_info['maxInputChannels'] if nchannels == 2: channels = ['L', 'R'] else: channels = [] for channel in range(0, dev_info['maxInputChannels']): channels += ["%d" % channel] return channels # method def get_current_first_channel(self): return self.first_channel # method def get_current_second_channel(self): return self.second_channel # method def get_current_device_nchannels(self): return self.pa.get_device_info_by_index( self.device)['maxInputChannels'] def get_device_outputchannels_count(self, device): return self.pa.get_device_info_by_index(device)['maxOutputChannels'] def handle_new_data(self, in_data, frame_count, input_time, status): if self.terminated: return if status & paInputOverflow: print("Stream overflow!") self.xruns += 1 self.underflow.emit() intdata_all_channels = fromstring(in_data, int16) int16info = iinfo(int16) norm_coeff = max(abs(int16info.min), int16info.max) floatdata_all_channels = intdata_all_channels.astype(float64) / float( norm_coeff) channel = self.get_current_first_channel() nchannels = self.get_current_device_nchannels() if self.duo_input: channel_2 = self.get_current_second_channel() if len(floatdata_all_channels) != frame_count * nchannels: print( "Incoming data is not consistent with current channel settings." ) return floatdata1 = floatdata_all_channels[channel::nchannels] if self.duo_input: floatdata2 = floatdata_all_channels[channel_2::nchannels] floatdata = vstack((floatdata1, floatdata2)) else: floatdata = floatdata1 floatdata.shape = (1, floatdata.size) self.new_data_available.emit(floatdata, input_time, status) self.chunk_number += 1 def set_single_input(self): self.duo_input = False def set_duo_input(self): self.duo_input = True # returns the stream time in seconds def get_stream_time(self): try: return self.stream.get_time() except OSError: return 0 def pause(self): self.stream.stop_stream() def restart(self): self.stream.start_stream()
def __getAudioDevice(audio: pyaudio.PyAudio, deviceIndex, isInput: bool): parameter = f'Audio{("input" if isInput else "Output")}Device' # Use default device if not specified if deviceIndex == None: try: if isInput: a = audio.get_default_input_device_info() deviceIndex = audio.get_default_input_device_info().get( 'index') else: deviceIndex = audio.get_default_output_device_info().get( 'index') except: deviceIndex = 0 if deviceIndex == None: __error('Неверное имя или индекс аудиоустройтсва', parameter) return (None, None) # Extract device index if possible try: deviceIndex = int(deviceIndex) except: deviceIndex = str(deviceIndex) # Resolve audio input & output devices over the list of devices # available deviceName = None device = 0 for i in range(audio.get_device_count()): #print(audio.get_device_info_by_index( i )) device = audio.get_device_info_by_index(i) name = str(device.get("name")) # Resolve index by device name if isinstance(deviceIndex, str) and name.lower().startswith( deviceIndex.lower()): deviceIndex = i # Assign original device name if isinstance(deviceIndex, int) and (deviceIndex == i): deviceName = name break # check if device was resolved if deviceIndex == None or deviceName == None: __error('Неверное имя или индекс аудиоустройтсва', parameter) channels = device.get('maxInputChannels') if isInput else device.get( 'maxOutputChannels') if channels <= 0: __error( f'Устройство не имеет {"аудиовходов" if isInput else "аудиовыходов"}', parameter) return (deviceIndex, deviceName)
def ask_for_device( p: pyaudio.PyAudio, as_input: bool = False ) -> Optional[int]: # todo return device_info? and filter for input/output """returns the device_id selected by the user""" if p.get_device_count() == 0: raise Exception("No devices available") default_device_index = p.get_default_input_device_info( )["index"] if as_input else p.get_default_output_device_info()["index"] for i in (get_input_device_indexes(p) if as_input else get_output_device_indexes(p)): info = p.get_device_info_by_index(i) index = info["index"] api_name = p.get_host_api_info_by_index(info["hostApi"])["name"] device_name = info["name"] print(f"{index}: \t {api_name} \t {device_name}") device_id = None while device_id is None: user_input = input("Choose device index: ") if user_input: try: device_id = int(user_input) except ValueError: print(f"Could not cast to int: {user_input}") else: print(f"Using default device index: {default_device_index}") device_id = default_device_index return device_id
class AudioStream(object): def __init__(self, sample_rate=44100, channels=1, width=2, chunk=1024, input_device_index=None): self.sample_rate = sample_rate self.channels = channels self.width = width self.chunk = chunk self.input_device_index = input_device_index def __enter__(self): self._pa = PyAudio() if self.input_device_index is None: self.input_device_index = \ self._pa.get_default_input_device_info()['index'] self._stream = self._pa.open( format=self._pa.get_format_from_width(self.width), channels=self.channels, rate=self.sample_rate, input=True, frames_per_buffer=self.chunk, input_device_index=self.input_device_index) self._stream.start_stream() return self def read(self): ''' On a buffer overflow this returns 0 bytes. ''' try: return self._stream.read(self.chunk) except IOError: return '' except AttributeError: raise Exception('Must be used as a context manager.') def stream(self): try: while True: bytes = self.read() if bytes: self.handle(bytes) except (KeyboardInterrupt, SystemExit): pass def __exit__(self, type, value, traceback): self._stream.stop_stream() self._stream.close() self._pa.terminate() def handle(self, bytes): pass
def __init__(self): #初始化窗体(R ,G ,B ,Light ,Width, Height) super(eight_note_game, self).__init__(255, 255, 255, 255, 800, 600) # 初始化参数 # frames_per_buffer self.numSamples = 1000 self.trip = 0 #音量条 self.vbar = Sprite("resources/black.png") self.vbar.position = 50, 450 self.vbar.scale_y = 0.1 self.vbar.image_anchor = 0, 0 self.add(self.vbar) # 音量示数 self.label = cocos.text.Label( '音量:', font_name='Times New Roman', #字体样式 font_size=10, #字体大小 color=(0, 0, 0, 255), #字体颜色 anchor_x='center', anchor_y='center' #字体位置 ) self.label.position = 30, 455 self.add(self.label) #输入声音 audio = PyAudio() SampleRate = int(audio.get_default_input_device_info() ['defaultSampleRate']) #读取默认的速率 self.stream = audio.open(format=paInt16, channels=1, input=True, rate=SampleRate, frames_per_buffer=self.numSamples) #创建八分音符酱 self.eight_note = eight_note() self.add(self.eight_note) #创建地板 self.floor = cocos.cocosnode.CocosNode() self.add(self.floor) position = 0, 100 for i in range(200): b = Block(position) self.floor.add(b) position = b.x + b.width, b.height #游戏主循环 self.schedule(self.loop)
def record(self): pa = PyAudio() print("get_default_input_device_info()", pa.get_default_input_device_info()) stream = pa.open(format=paInt16, channels=1, rate=self.framerate, input=True, frames_per_buffer=self.NUM_SAMPLES) my_buf = [] count = 0 # 开始录音 while count < self.TIME: #控制录音时间 string_audio_data = stream.read(self.NUM_SAMPLES) if count == 0: # speech.say("一秒后请说") print("一秒后请说:") my_buf.append(string_audio_data) count += 1 # print('.') self.save_wave_file(self.path, my_buf) stream.close()
class MainWindow(QtGui.QMainWindow): """ A Qt QMainWindow that is home to a matplotlib figure and two combo boxes. The combo boxes allow the selection of a sound card by API and name. The figure will show the waveform of the audio input of that sound card. """ def __init__(self, parent=None): super(MainWindow, self).__init__(parent) # Monkey patch missing methods into PyAudio. PyAudio.device_index_to_host_api_device_index = ( device_index_to_host_api_device_index) self.pyaudio = PyAudio() # Create the UI widgets. central_widget = QtGui.QWidget(self) self.setCentralWidget(central_widget) main_layout = QtGui.QVBoxLayout(central_widget) self.figure = FigureWidget() main_layout.addWidget(self.figure) horizontal_layout = QtGui.QHBoxLayout() main_layout.addLayout(horizontal_layout) api_list = QtGui.QComboBox() api_list.setModel(APIListModel(self.pyaudio)) horizontal_layout.addWidget(api_list) device_list = QtGui.QComboBox() device_list_model = DeviceListModel(self.pyaudio) device_list.setModel(device_list_model) horizontal_layout.addWidget(device_list) # Connect the moving parts api_list.currentIndexChanged.connect(device_list_model.set_api_index) api_list.currentIndexChanged.connect(self.change_api_index) device_list.currentIndexChanged.connect(self.change_device_index) # Tell all widgets to use the default audio device. default_api_index = ( self.pyaudio.get_default_input_device_info()["hostApi"]) default_device_index = ( self.pyaudio.device_index_to_host_api_device_index( self.pyaudio.get_default_host_api_info()["defaultInputDevice"], default_api_index)) self.api_index = default_api_index self.device_index = default_device_index self.stream = None api_list.setCurrentIndex(default_api_index) device_list_model.set_api_index(default_api_index) device_list.setCurrentIndex(default_device_index) def closeEvent(self, event): """ Called by Qt when the program quits. Stops audio processing. """ self.stream.close() # wait for audio processing to clear its buffers time.sleep(0.1) def change_api_index(self, api_index): """ Restarts audio processing with new index. """ self.api_index = api_index self.restart_audio() def change_device_index(self, device_index): """ Restarts audio processing with new index. """ self.device_index = device_index self.restart_audio() def restart_audio(self): """ Restarts audio processing with current API and device indices. """ device_info = (self.pyaudio.get_device_info_by_host_api_device_index( self.api_index, self.device_index)) self.num_channels = device_info['maxInputChannels'] if self.stream: self.stream.close() self.stream = self.pyaudio.open( rate=int(device_info['defaultSampleRate']), channels=self.num_channels, input_device_index=device_info['index'], format=paFloat32, input=True, stream_callback=self.audio_callback) self.figure.create_plots(self.num_channels) def audio_callback(self, in_data, frame_count, time_info, status_flags): """ Called by pyaudio whenever audio data is available. Updates the matplotlib figure. """ data = numpy.fromstring(in_data, dtype=numpy.float32) data = numpy.reshape( data, (len(data) / self.num_channels, self.num_channels)) self.figure.draw(data) return (None, paContinue)
class AudioDevice(QtCore.QObject): def __init__(self, logger): QtCore.QObject.__init__(self) self.logger = logger self.duo_input = False self.logger.push("Initializing PyAudio") self.pa = PyAudio() # look for devices self.input_devices = self.get_input_devices() self.output_devices = self.get_output_devices() for device in self.input_devices: self.logger.push("Opening the stream") self.stream = self.open_stream(device) self.device = device self.logger.push("Trying to read from input device %d" % device) if self.try_input_stream(self.stream): self.logger.push("Success") break else: self.logger.push("Fail") self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 # counter for the number of input buffer overflows self.xruns = 0 # method def get_readable_devices_list(self): devices_list = [] default_device_index = self.get_default_input_device() for device in self.input_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index(dev_info ['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index(device)[ 'maxInputChannels'] desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_readable_output_devices_list(self): devices_list = [] default_device_index = self.get_default_output_device() for device in self.output_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index(dev_info['hostApi'] )['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index(device)[ 'maxOutputChannels'] desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_default_input_device(self): return self.pa.get_default_input_device_info()['index'] # method def get_default_output_device(self): return self.pa.get_default_output_device_info()['index'] # method def get_device_count(self): # FIXME only input devices should be chosen, not all of them ! return self.pa.get_device_count() # method # returns a list of input devices index, starting with the system default def get_input_devices(self): device_count = self.get_device_count() default_input_device = self.get_default_input_device() device_range = range(0, device_count) # start by the default input device device_range.remove(default_input_device) device_range = [default_input_device] + device_range # select only the input devices by looking at the number of input channels input_devices = [] for device in device_range: n_input_channels = self.pa.get_device_info_by_index(device)[ 'maxInputChannels'] if n_input_channels > 0: input_devices += [device] return input_devices # method # returns a list of output devices index, starting with the system default def get_output_devices(self): device_count = self.get_device_count() default_output_device = self.get_default_output_device() device_range = range(0, device_count) # start by the default input device device_range.remove(default_output_device) device_range = [default_output_device] + device_range # select only the output devices by looking at the number of output channels output_devices = [] for device in device_range: n_output_channels = self.pa.get_device_info_by_index(device)[ 'maxOutputChannels'] if n_output_channels > 0: output_devices += [device] return output_devices # method def select_input_device(self, device): # save current stream in case we need to restore it previous_stream = self.stream previous_device = self.device self.stream = self.open_stream(device) self.device = device self.logger.push("Trying to read from input device #%d" % (device)) if self.try_input_stream(self.stream): self.logger.push("Success") previous_stream.close() success = True self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 else: self.logger.push("Fail") self.stream.close() self.stream = previous_stream self.device = previous_device success = False return success, self.device # method def select_first_channel(self, index): self.first_channel = index success = True return success, self.first_channel # method def select_second_channel(self, index): self.second_channel = index success = True return success, self.second_channel # method def open_stream(self, device): ''' by default we open the device stream with all the channels # (interleaved in the data buffer)''' maxInputChannels = self.pa.get_device_info_by_index(device)[ 'maxInputChannels'] stream = self.pa.open(format=paInt32, channels=maxInputChannels, rate=SAMPLING_RATE, input=True, frames_per_buffer=FRAMES_PER_BUFFER, input_device_index=device) return stream # method # return the index of the current input device in the input devices list # (not the same as the PortAudio index, since the latter is the index # in the list of *all* devices, not only input ones) def get_readable_current_device(self): i = 0 for device in self.input_devices: if device == self.device: break else: i += 1 return i # method def get_readable_current_channels(self): dev_info = self.pa.get_device_info_by_index(self.device) nchannels = dev_info['maxInputChannels'] if nchannels == 2: channels = ['L', 'R'] else: channels = [] for channel in range(0, dev_info['maxInputChannels']): channels += ["%d" % channel] return channels # method def get_current_first_channel(self): return self.first_channel # method def get_current_second_channel(self): return self.second_channel # method def get_current_device_nchannels(self): return self.pa.get_device_info_by_index(self.device)[ 'maxInputChannels'] # method # return True on success def try_input_stream(self, stream): n_try = 0 while (stream.get_read_available() < FRAMES_PER_BUFFER and n_try < 1000000): n_try += 1 if n_try == 1000000: return False else: lat_ms = 1000 * stream.get_input_latency() self.logger.push("Device claims %d ms latency" % (lat_ms)) return True # try to update the audio buffer # return the number of chunks retrieved, and the time elapsed def update(self, ringbuffer): t = QtCore.QTime() t.start() channel = self.get_current_first_channel() nchannels = self.get_current_device_nchannels() if self.duo_input: channel_2 = self.get_current_second_channel() chunks = 0 available = self.stream.get_read_available() available = int(floor(available / FRAMES_PER_BUFFER)) for _ in range(0, available): try: rawdata = self.stream.read(FRAMES_PER_BUFFER) except IOError as inst: # FIXME specialize this exception handling code # to treat overflow errors particularly self.xruns += 1 print "Caught an IOError on stream read.", inst break intdata_all_channels = fromstring(rawdata, int32) int32info = iinfo(int32) norm_coeff = max(abs(int32info.min), int32info.max) floatdata_all_channels = (intdata_all_channels.astype(float64) / float(norm_coeff)) floatdata1 = floatdata_all_channels[channel::nchannels] if self.duo_input: floatdata2 = floatdata_all_channels[channel_2::nchannels] floatdata = vstack((floatdata1, floatdata2)) else: floatdata = floatdata1 floatdata.shape = (1, FRAMES_PER_BUFFER) # update the circular buffer ringbuffer.push(floatdata) chunks += 1 return (chunks, t.elapsed(), chunks * FRAMES_PER_BUFFER) def set_single_input(self): self.duo_input = False def set_duo_input(self): self.duo_input = True # returns the stream time in seconds def get_stream_time(self): return self.stream.get_time()
class AudioDevice(QtCore.QObject): def __init__(self, logger): QtCore.QObject.__init__(self) self.logger = logger self.duo_input = False self.logger.push("Initializing PyAudio") self.pa = PyAudio() # look for devices self.input_devices = self.get_input_devices() self.output_devices = self.get_output_devices() for device in self.input_devices: self.logger.push("Opening the stream") self.stream = self.open_stream(device) self.device = device self.logger.push("Trying to read from input device %d" % device) if self.try_input_stream(self.stream): self.logger.push("Success") break else: self.logger.push("Fail") self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 # counter for the number of input buffer overflows self.xruns = 0 # method def get_readable_devices_list(self): devices_list = [] default_device_index = self.get_default_input_device() for device in self.input_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index( dev_info['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index( device)['maxInputChannels'] desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_readable_output_devices_list(self): devices_list = [] default_device_index = self.get_default_output_device() for device in self.output_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index( dev_info['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index( device)['maxOutputChannels'] desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_default_input_device(self): return self.pa.get_default_input_device_info()['index'] # method def get_default_output_device(self): return self.pa.get_default_output_device_info()['index'] # method def get_device_count(self): # FIXME only input devices should be chosen, not all of them ! return self.pa.get_device_count() # method # returns a list of input devices index, starting with the system default def get_input_devices(self): device_count = self.get_device_count() default_input_device = self.get_default_input_device() device_range = range(0, device_count) # start by the default input device device_range.remove(default_input_device) device_range = [default_input_device] + device_range # select only the input devices by looking at the number of input channels input_devices = [] for device in device_range: n_input_channels = self.pa.get_device_info_by_index( device)['maxInputChannels'] if n_input_channels > 0: input_devices += [device] return input_devices # method # returns a list of output devices index, starting with the system default def get_output_devices(self): device_count = self.get_device_count() default_output_device = self.get_default_output_device() device_range = range(0, device_count) # start by the default input device device_range.remove(default_output_device) device_range = [default_output_device] + device_range # select only the output devices by looking at the number of output channels output_devices = [] for device in device_range: n_output_channels = self.pa.get_device_info_by_index( device)['maxOutputChannels'] if n_output_channels > 0: output_devices += [device] return output_devices # method def select_input_device(self, device): # save current stream in case we need to restore it previous_stream = self.stream previous_device = self.device self.stream = self.open_stream(device) self.device = device self.logger.push("Trying to read from input device #%d" % (device)) if self.try_input_stream(self.stream): self.logger.push("Success") previous_stream.close() success = True self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 else: self.logger.push("Fail") self.stream.close() self.stream = previous_stream self.device = previous_device success = False return success, self.device # method def select_first_channel(self, index): self.first_channel = index success = True return success, self.first_channel # method def select_second_channel(self, index): self.second_channel = index success = True return success, self.second_channel # method def open_stream(self, device): ''' by default we open the device stream with all the channels # (interleaved in the data buffer)''' maxInputChannels = self.pa.get_device_info_by_index( device)['maxInputChannels'] stream = self.pa.open(format=paInt32, channels=maxInputChannels, rate=SAMPLING_RATE, input=True, frames_per_buffer=FRAMES_PER_BUFFER, input_device_index=device) return stream # method # return the index of the current input device in the input devices list # (not the same as the PortAudio index, since the latter is the index # in the list of *all* devices, not only input ones) def get_readable_current_device(self): i = 0 for device in self.input_devices: if device == self.device: break else: i += 1 return i # method def get_readable_current_channels(self): dev_info = self.pa.get_device_info_by_index(self.device) nchannels = dev_info['maxInputChannels'] if nchannels == 2: channels = ['L', 'R'] else: channels = [] for channel in range(0, dev_info['maxInputChannels']): channels += ["%d" % channel] return channels # method def get_current_first_channel(self): return self.first_channel # method def get_current_second_channel(self): return self.second_channel # method def get_current_device_nchannels(self): return self.pa.get_device_info_by_index( self.device)['maxInputChannels'] # method # return True on success def try_input_stream(self, stream): n_try = 0 while (stream.get_read_available() < FRAMES_PER_BUFFER and n_try < 1000000): n_try += 1 if n_try == 1000000: return False else: lat_ms = 1000 * stream.get_input_latency() self.logger.push("Device claims %d ms latency" % (lat_ms)) return True # try to update the audio buffer # return the number of chunks retrieved, and the time elapsed def update(self, ringbuffer): t = QtCore.QTime() t.start() channel = self.get_current_first_channel() nchannels = self.get_current_device_nchannels() if self.duo_input: channel_2 = self.get_current_second_channel() chunks = 0 available = self.stream.get_read_available() available = int(floor(available / FRAMES_PER_BUFFER)) for _ in range(0, available): try: rawdata = self.stream.read(FRAMES_PER_BUFFER) except IOError as inst: # FIXME specialize this exception handling code # to treat overflow errors particularly self.xruns += 1 print "Caught an IOError on stream read.", inst break intdata_all_channels = fromstring(rawdata, int32) int32info = iinfo(int32) norm_coeff = max(abs(int32info.min), int32info.max) floatdata_all_channels = (intdata_all_channels.astype(float64) / float(norm_coeff)) floatdata1 = floatdata_all_channels[channel::nchannels] if self.duo_input: floatdata2 = floatdata_all_channels[channel_2::nchannels] floatdata = vstack((floatdata1, floatdata2)) else: floatdata = floatdata1 floatdata.shape = (1, FRAMES_PER_BUFFER) # update the circular buffer ringbuffer.push(floatdata) chunks += 1 return (chunks, t.elapsed(), chunks * FRAMES_PER_BUFFER) def set_single_input(self): self.duo_input = False def set_duo_input(self): self.duo_input = True # returns the stream time in seconds def get_stream_time(self): return self.stream.get_time()
from print_color import print_cyan, print_green from print_speed import print_slow from pyaudio import PyAudio from record import record_user_audio from respond import respond from speak import speak, remove_audio_file from time import sleep # Initialize a PyAudio instance pa = PyAudio() # Gets info on default input device device_index = pa.get_default_input_device_info()['index'] device_name = pa.get_default_input_device_info()['name'] device_sample_rate = int(pa.get_default_input_device_info()['defaultSampleRate']) def main(): intro = "\nHi, I am JoiBot!" intro2 = "Your virtual assistant." intro3 = f"\nSpeak to me using your device called {device_name}." intro4 = "Go ahead. Ask me a question...\n" print(intro+"👋") audio_file = speak(intro) remove_audio_file(audio_file) print(intro2) audio_file = speak(intro2) remove_audio_file(audio_file) print(intro3)
class AudioRecorder(DIWA_THREAD): """ A thread for capturing audio continuously. It keeps a buffer that can be saved to a file. By convention AudioRecorder is usually written in mixed case even as we prefer upper case for threading types. :param parent: Parent of the thread. :type parent: :py:class:`diwacs.GraphicalUserInterface` """ def __init__(self, parent): DIWA_THREAD.__init__(self, name='AudioRecorder') self.parent = parent self.py_audio = PyAudio() self.stream = self.open_mic_stream() self.buffer = deque(maxlen=diwavars.MAX_LENGTH) def stop(self): """ Stop the audio recorder thread. """ DIWA_THREAD.stop(self) sleep(0.1) self.stream.close() def find_input_device(self): """ Find a microphone device. """ for i in range(self.py_audio.get_device_count()): # Internationalization hack... # LOGGER.debug("Selecting audio device %s / %s " % # (str(i),str(self.py_audio.get_device_count()))) # device_index = i # return device_index devinfo = self.py_audio.get_device_info_by_index(i) for keyword in ['microphone']: if keyword in devinfo['name'].lower(): return i default_device = self.py_audio.get_default_input_device_info() if default_device: return default_device['index'] return None def open_mic_stream(self): """ Opens the stream object for microphone. """ device_index = None # uncomment the next line to search for a device. # device_index = self.find_input_device() stream = self.py_audio.open( format=diwavars.FORMAT, channels=diwavars.CHANNELS, rate=diwavars.RATE, input=True, input_device_index=device_index, frames_per_buffer=diwavars.INPUT_FRAMES_PER_BLOCK) return stream def run(self): """ Continuously record from the microphone to the buffer. The size should be limited at diwavars.MAX_LENGTH constant. The implementation keeps only the most recent data in the case that there's too much data to store. """ while not self._stop.is_set(): try: data = self.stream.read(diwavars.INPUT_FRAMES_PER_BLOCK) while len(self.buffer) >= self.buffer.maxlen: element = self.buffer.popleft() del element self.buffer.append(data) except IOError as excp: _logger().exception('Error recording: {0!s}'.format(excp)) def save(self, event_id, path): """ Save the buffer to a file. """ try: _logger().debug('Saving audio buffer') date_string = datetime.now().strftime('%d%m%Y%H%M') filename = '{0}_{1}.wav'.format(event_id, date_string) filepath = os.path.join(path, 'Audio') if not os.path.exists(filepath): os.makedirs(filepath) filepath = os.path.join(filepath, filename) sample_size = self.py_audio.get_sample_size(diwavars.FORMAT) wave_file = wave.open(filepath, 'wb') wave_file.setnchannels(diwavars.CHANNELS) wave_file.setsampwidth(sample_size) wave_file.setframerate(diwavars.RATE) wave_file.writeframes(b''.join(self.buffer)) wave_file.close() except: _logger().exception('audio save exception') #CallAfter(self.parent.ClearStatusText) self.parent.diwa_state.remove_from_swnp_data('audio') CallAfter(self.parent.UpdateScreens(update=True))
class MainWindow(QtGui.QMainWindow): """ A Qt QMainWindow that is home to a matplotlib figure and two combo boxes. The combo boxes allow the selection of a sound card by API and name. The figure will show the waveform of the audio input of that sound card. """ def __init__(self, parent=None): super(MainWindow, self).__init__(parent) # Monkey patch missing methods into PyAudio. PyAudio.device_index_to_host_api_device_index = ( device_index_to_host_api_device_index) self.pyaudio = PyAudio() # Create the UI widgets. central_widget = QtGui.QWidget(self) self.setCentralWidget(central_widget) main_layout = QtGui.QVBoxLayout(central_widget) self.figure = FigureWidget() main_layout.addWidget(self.figure) horizontal_layout = QtGui.QHBoxLayout() main_layout.addLayout(horizontal_layout) api_list = QtGui.QComboBox() api_list.setModel(APIListModel(self.pyaudio)) horizontal_layout.addWidget(api_list) device_list = QtGui.QComboBox() device_list_model = DeviceListModel(self.pyaudio) device_list.setModel(device_list_model) horizontal_layout.addWidget(device_list) # Connect the moving parts api_list.currentIndexChanged.connect(device_list_model.set_api_index) api_list.currentIndexChanged.connect(self.change_api_index) device_list.currentIndexChanged.connect(self.change_device_index) # Tell all widgets to use the default audio device. default_api_index = ( self.pyaudio.get_default_input_device_info()["hostApi"]) default_device_index = ( self.pyaudio.device_index_to_host_api_device_index( self.pyaudio.get_default_host_api_info()["defaultInputDevice"], default_api_index)) self.api_index = default_api_index self.device_index = default_device_index self.stream = None api_list.setCurrentIndex(default_api_index) device_list_model.set_api_index(default_api_index) device_list.setCurrentIndex(default_device_index) def closeEvent(self, event): """ Called by Qt when the program quits. Stops audio processing. """ self.stream.close() # wait for audio processing to clear its buffers time.sleep(0.1) def change_api_index(self, api_index): """ Restarts audio processing with new index. """ self.api_index = api_index self.restart_audio() def change_device_index(self, device_index): """ Restarts audio processing with new index. """ self.device_index = device_index self.restart_audio() def restart_audio(self): """ Restarts audio processing with current API and device indices. """ device_info = ( self.pyaudio.get_device_info_by_host_api_device_index(self.api_index, self.device_index)) self.num_channels = device_info['maxInputChannels'] if self.stream: self.stream.close() self.stream = self.pyaudio.open( rate=int(device_info['defaultSampleRate']), channels=self.num_channels, input_device_index=device_info['index'], format=paFloat32, input=True, stream_callback=self.audio_callback) self.figure.create_plots(self.num_channels) def audio_callback(self, in_data, frame_count, time_info, status_flags): """ Called by pyaudio whenever audio data is available. Updates the matplotlib figure. """ data = numpy.fromstring(in_data, dtype=numpy.float32) data = numpy.reshape(data, (len(data)/self.num_channels,self.num_channels)) self.figure.draw(data) return (None, paContinue)
class AudioBackend(QtCore.QObject): underflow = QtCore.pyqtSignal() new_data_available_from_callback = QtCore.pyqtSignal(bytes, int, float, int) new_data_available = QtCore.pyqtSignal(ndarray, float, int) def callback(self, in_data, frame_count, time_info, status): #do the minimum from here to prevent overflows, just pass the data to the main thread input_time = time_info['input_buffer_adc_time'] # some API drivers in PortAudio do not return a valid time, so fallback to the current stream time if input_time == 0.: input_time = time_info['current_time'] if input_time == 0.: input_time = self.stream.get_time() self.new_data_available_from_callback.emit(in_data, frame_count, input_time, status) return (None, 0) def __init__(self, logger): QtCore.QObject.__init__(self) self.logger = logger self.duo_input = False self.logger.push("Initializing PyAudio") self.pa = PyAudio() # look for devices self.input_devices = self.get_input_devices() self.output_devices = self.get_output_devices() self.device = None self.first_channel = None self.second_channel = None # we will try to open all the input devices until one # works, starting by the default input device for device in self.input_devices: self.logger.push("Opening the stream") try: self.stream = self.open_stream(device) self.stream.start_stream() self.device = device self.logger.push("Success") break except: self.logger.push("Fail") if self.device is not None: self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 # counter for the number of input buffer overflows self.xruns = 0 self.chunk_number = 0 self.new_data_available_from_callback.connect(self.handle_new_data) def close(self): self.stream.stop_stream() self.stream.close() self.stream = None # method def get_readable_devices_list(self): devices_list = [] default_device_index = self.get_default_input_device() for device in self.input_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index(dev_info['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index(device)['maxInputChannels'] desc = "%s (%d channels) (%s) %s" %(dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_readable_output_devices_list(self): devices_list = [] default_device_index = self.get_default_output_device() for device in self.output_devices: dev_info = self.pa.get_device_info_by_index(device) api = self.pa.get_host_api_info_by_index(dev_info['hostApi'])['name'] if device is default_device_index: extra_info = ' (system default)' else: extra_info = '' nchannels = self.pa.get_device_info_by_index(device)['maxOutputChannels'] desc = "%s (%d channels) (%s) %s" %(dev_info['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list # method def get_default_input_device(self): try: index = self.pa.get_default_input_device_info()['index'] except IOError: index = None return index # method def get_default_output_device(self): try: index = self.pa.get_default_output_device_info()['index'] except IOError: index = None return # method def get_device_count(self): # FIXME only input devices should be chosen, not all of them ! return self.pa.get_device_count() # method # returns a list of input devices index, starting with the system default def get_input_devices(self): device_count = self.get_device_count() device_range = list(range(0, device_count)) default_input_device = self.get_default_input_device() if default_input_device is not None: # start by the default input device device_range.remove(default_input_device) device_range = [default_input_device] + device_range # select only the input devices by looking at the number of input channels input_devices = [] for device in device_range: n_input_channels = self.pa.get_device_info_by_index(device)['maxInputChannels'] if n_input_channels > 0: input_devices += [device] return input_devices # method # returns a list of output devices index, starting with the system default def get_output_devices(self): device_count = self.get_device_count() device_range = list(range(0, device_count)) default_output_device = self.get_default_output_device() if default_output_device is not None: # start by the default input device device_range.remove(default_output_device) device_range = [default_output_device] + device_range # select only the output devices by looking at the number of output channels output_devices = [] for device in device_range: n_output_channels = self.pa.get_device_info_by_index(device)['maxOutputChannels'] if n_output_channels > 0: output_devices += [device] return output_devices # method. # The index parameter is the index in the self.input_devices list of devices ! # The return parameter is also an index in the same list. def select_input_device(self, index): device = self.input_devices[index] # save current stream in case we need to restore it previous_stream = self.stream previous_device = self.device self.logger.push("Trying to open input device #%d" % (index)) try: self.stream = self.open_stream(device) self.device = device self.stream.start_stream() success = True except: self.logger.push("Fail") success = False if self.stream is not None: self.stream.close() # restore previous stream self.stream = previous_stream self.device = previous_device if success: self.logger.push("Success") previous_stream.close() self.first_channel = 0 nchannels = self.get_current_device_nchannels() if nchannels == 1: self.second_channel = 0 else: self.second_channel = 1 return success, self.input_devices.index(self.device) # method def select_first_channel(self, index): self.first_channel = index success = True return success, self.first_channel # method def select_second_channel(self, index): self.second_channel = index success = True return success, self.second_channel # method def open_stream(self, device): # by default we open the device stream with all the channels # (interleaved in the data buffer) maxInputChannels = self.pa.get_device_info_by_index(device)['maxInputChannels'] stream = self.pa.open(format=paInt16, channels=maxInputChannels, rate=SAMPLING_RATE, input=True, input_device_index=device, stream_callback=self.callback, frames_per_buffer = FRAMES_PER_BUFFER) lat_ms = 1000*stream.get_input_latency() self.logger.push("Device claims %d ms latency" %(lat_ms)) return stream # method # return the index of the current input device in the input devices list # (not the same as the PortAudio index, since the latter is the index # in the list of *all* devices, not only input ones) def get_readable_current_device(self): return self.input_devices.index(self.device) # method def get_readable_current_channels(self): dev_info = self.pa.get_device_info_by_index(self.device) nchannels = dev_info['maxInputChannels'] if nchannels == 2: channels = ['L', 'R'] else: channels = [] for channel in range(0, dev_info['maxInputChannels']): channels += ["%d" %channel] return channels # method def get_current_first_channel(self): return self.first_channel # method def get_current_second_channel(self): return self.second_channel # method def get_current_device_nchannels(self): return self.pa.get_device_info_by_index(self.device)['maxInputChannels'] def handle_new_data(self, in_data, frame_count, input_time, status): if (status & paInputOverflow): print("Stream overflow!") self.xruns += 1 self.underflow.emit() intdata_all_channels = fromstring(in_data, int16) int16info = iinfo(int16) norm_coeff = max(abs(int16info.min), int16info.max) floatdata_all_channels = intdata_all_channels.astype(float64)/float(norm_coeff) channel = self.get_current_first_channel() nchannels = self.get_current_device_nchannels() if self.duo_input: channel_2 = self.get_current_second_channel() floatdata1 = floatdata_all_channels[channel::nchannels] if self.duo_input: floatdata2 = floatdata_all_channels[channel_2::nchannels] floatdata = vstack((floatdata1, floatdata2)) else: floatdata = floatdata1 floatdata.shape = (1, floatdata.size) self.new_data_available.emit(floatdata, input_time, status) self.chunk_number += 1 def set_single_input(self): self.duo_input = False def set_duo_input(self): self.duo_input = True # returns the stream time in seconds def get_stream_time(self): return self.stream.get_time() def pause(self): self.stream.stop_stream() def restart(self): self.stream.start_stream()
class RadioChronicle: # Default parameter values fileNameFormat = './RC-%Y%m%d-%H%M%S.wav' monitor = False volumeTreshold = 5.0 maxPauseLength = 1.0 trailLength = 1.0 minRecordingLength = 0.5 chunkSize = 1024 inputDevice: Optional[int] = None outputDevice: Optional[int] = None audioBits = 16 sampleRate = 44100 inputStream: Optional[AudioStream] = None outputStream: Optional[AudioStream] = None audio: PyAudio logger: Logger audioFile: Optional[WaveWriter] sample: bytes sampleLength: int audioFileLength: int inLoop: bool recording: bool quitAfterRecording: bool lastSecondVolumes: List[float] fileName: str localMaxVolume: float def __init__(self) -> None: # pylint: disable=too-complex, too-many-statements '''Fully constructs class instance, including reading configuration file and configuring audio devices.''' try: # Reading command line options configFileName = DEFAULT_CONFIG_FILE_NAME (options, _args) = getopt(argv[1:], 'c:h', ['config=', 'help']) for (option, optionValue) in options: if option in ('-c', '--config'): configFileName = optionValue.strip() else: usage() except Exception as e: usage(e) try: # Reading config file and configuring logging config = ConfigParser(interpolation=None, inline_comment_prefixes=(';', )) config.read_file( open(configFileName )) # Using read_file(open()) to make sure file exists if config.has_section('loggers'): fileConfig(config) self.logger = getLogger() if not self.logger.handlers: # Provide default logger self.logger.addHandler(StreamHandler()) self.logger.setLevel(NOTSET) signal(SIGTERM, self.sigTerm) except Exception as e: print(f"{TITLE}\n\nConfig error: {e}") print(format_exc()) sysExit(1) # Above this point, use print for diagnostics # From this point on, we have self.logger to use instead self.logger.info(TITLE) self.logger.info(f"Using {configFileName}") print() # Empty line to console only try: # Applying configuration channel = 'MONO' value: str try: section = 'general' try: self.fileNameFormat = config.get(section, 'fileNameFormat').strip() except NoOptionError: pass try: self.monitor = config.getboolean(section, 'monitor') except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].monitor: '{config.get(section, 'monitor')}', must be 1/yes/true/on or 0/no/false/off" ) from e except NoSectionError: pass try: section = 'tuning' try: value = config.get(section, 'volumeTreshold') self.volumeTreshold = float(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].volumeTreshold: '{value}', must be a float" ) from e try: value = config.get(section, 'maxPauseLength') self.maxPauseLength = float(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].maxPauseLength: '{value}', must be a float" ) from e try: value = config.get(section, 'minRecordingLength') self.minRecordingLength = float(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].minRecordingLength: '{value}', must be a float" ) from e try: value = config.get(section, 'trailLength') self.trailLength = float(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].trailLength: '{value}', must be a float" ) from e except NoSectionError: pass try: section = 'device' try: value = config.get(section, 'chunkSize') self.chunkSize = int(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].chunkSize: '{value}', must be an integer" ) from e try: value = config.get(section, 'inputDevice') self.inputDevice = int(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].inputDevice: '{value}', must be an integer" ) from e try: value = config.get(section, 'outputDevice') self.outputDevice = int(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].outputDevice: '{value}', must be an integer" ) from e try: value = config.get(section, 'audioBits') self.audioBits = int(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].audioBits: '{value}', must be an integer" ) from e try: value = config.get(section, 'sampleRate') self.sampleRate = int(value) except NoOptionError: pass except ValueError as e: raise ValueError( f"Bad value for [{section}].sampleRate: '{value}', must be an integer" ) from e try: channel = config.get(section, 'channel') # pylint: disable=redefined-variable-type # Will be processed later except NoOptionError: pass except NoSectionError: pass # Validating configuration parameters if not self.fileNameFormat: raise ValueError( "Bad value for fileNameFormat: must be not empty") if not 0 <= self.volumeTreshold <= 100: raise ValueError( f"Bad value for volumeTreshold: {self.volumeTreshold:.2f}, must be 0-100" ) if self.maxPauseLength < 0: self.maxPauseLength = 0.0 if self.minRecordingLength < 0: self.minRecordingLength = 0.0 if self.trailLength < 0: self.trailLength = 0.0 if self.chunkSize < 1: raise ValueError( f"Bad value for chunkSize: {self.chunkSize}, must be 1 or more" ) if self.inputDevice: if self.inputDevice == -1: self.inputDevice = None elif self.inputDevice < -1: raise ValueError( f"Bad value for input device: {self.inputDevice}, must be -1 or more" ) if self.outputDevice: if self.outputDevice == -1: self.outputDevice = None elif self.outputDevice < -1: raise ValueError( f"Bad value for output device: {self.outputDevice}, must be -1 or more" ) if self.audioBits not in (8, 16, 32): raise ValueError( f"Bad value for audioBits: {self.audioBits}, must be 8, 16, or 32" ) if self.sampleRate < 1: raise ValueError( f"Bad value for chunkSize: {self.sampleRate}, must be positive" ) try: intChannel: Optional[int] = int(channel) assert intChannel is not None if intChannel <= 0: intChannel = None # Exception will be thrown below except ValueError: intChannel = CHANNEL_NUMBERS.get( channel.strip().upper()) # Would be None if not found if intChannel is None: raise ValueError( f"Bad value for channel: {channel}, must be LEFT/RIGHT/STEREO/ALL/MONO or a number of 1 or more" ) self.channel = intChannel # Accessing PyAudio engine self.audio = PyAudio() print(f"{self.deviceInfo()}\n" ) # Using print for non-functional logging # Accessing audio devices try: if self.inputDevice is not None: inputDeviceInfo = self.audio.get_device_info_by_index( self.inputDevice) self.logger.info( f"Using input device {self.deviceInfo(inputDeviceInfo, False)}" ) else: inputDeviceInfo = self.audio.get_default_input_device_info( ) self.logger.info( f"Using default input device {self.deviceInfo(inputDeviceInfo, False)}" ) except ValueError as e: raise ValueError( f"{f'Input device {self.inputDevice}' if self.inputDevice is not None else 'Default input device'} is not in fact an input device" ) from e except IOError as e: raise IOError( f"Can't access {f'input device {self.inputDevice}' if self.inputDevice is not None else 'default input device'}: {e}" ) from e try: if self.outputDevice is not None: outputDeviceInfo = self.audio.get_device_info_by_index( self.outputDevice) self.logger.info( f"Using output device {self.deviceInfo(outputDeviceInfo, True)}" ) else: outputDeviceInfo = self.audio.get_default_output_device_info( ) self.logger.info( f"Using default output device {self.deviceInfo(outputDeviceInfo, True)}" ) except ValueError as e: raise ValueError( f"{f'output device {self.outputDevice}' if self.outputDevice is not None else 'Default output device'} is not in fact an output device" ) from e except IOError as e: raise IOError( f"Can't access {f'output device {self.outputDevice}' if self.outputDevice is not None else 'default output device'}: {e}" ) from e print() # Empty line to console only # Calculating derivative paratemers self.numInputChannels = 1 if self.channel == MONO else cast( int, inputDeviceInfo['maxInputChannels']) assert self.numInputChannels > 0 if self.channel > self.numInputChannels: raise ValueError( f"Bad value for channel: {self.channel}, must be no more than {self.numInputChannels}" ) self.numOutputChannels = self.numInputChannels if self.channel == STEREO else 1 assert self.numOutputChannels > 0 self.audioBytes = self.audioBits // 8 self.maxVolume = 1 << (self.audioBits - 1) self.audioFormat = self.audio.get_format_from_width( self.audioBytes, False) self.packFormat = PACK_FORMATS[self.audioBits] self.inputBlockSize = self.numInputChannels * self.chunkSize * self.audioBytes self.outputBlockSize = self.numOutputChannels * self.chunkSize * self.audioBytes self.inputSecondSize = self.numInputChannels * self.sampleRate * self.audioBytes self.outputSecondSize = self.numOutputChannels * self.sampleRate * self.audioBytes self.chunksInSecond = self.sampleRate // self.chunkSize self.chunksToStop = self.chunksInSecond * self.maxPauseLength self.chunksOfFadeout = self.chunksInSecond * self.trailLength # Diagnosting audio devices if not self.createInputStream(): raise Exception("Can't create input stream") self.closeInputStream() if not self.createOutputStream(): raise Exception("Can't create output stream") self.closeOutputStream() # Printing configuration info self.logger.info( f"Recording {self.sampleRate}Hz/{self.audioBits}-bit/{CHANNEL_NAMES.get(self.channel) or f'channel {self.channel}'} to {self.fileNameFormat}" ) self.logger.info( f"Volume threshold {self.volumeTreshold:.2f}%, max pause {self.maxPauseLength:.1f} seconds, min recording length {self.minRecordingLength:.1f} seconds, trail {self.trailLength:.1f} seconds" ) self.logger.info(f"Monitor is {'ON' if self.monitor else 'OFF'}") print("Type 'help' for console commands reference" ) # Using print for non-functional logging print() # Empty line to console only except Exception as e: self.logger.error(f"Configuration error: {e}") print(format_exc()) sysExit(1) def __del__(self) -> None: '''Frees the PyAudio resources.''' if self.audio: self.closeInputStream() self.closeOutputStream() self.audio.terminate() self.logger.debug("destroyed") def deviceInfo(self, device: Union[int, Mapping[str, Union[str, int, float]], None] = None, expectOutput: Optional[bool] = None) -> str: '''Provides string information about system audio device(s).''' if device is None: # Return info on all available devices inputDevices = [] outputDevices = [] for i in range(self.audio.get_device_count()): device = self.audio.get_device_info_by_index(i) if device['maxOutputChannels']: outputDevices.append(device) if device['maxInputChannels']: inputDevices.append(device) return '\n'.join( ("Detected audio input devices:", '\n'.join(self.deviceInfo(device) for device in inputDevices), "\nDetected audio output devices:", '\n'.join( self.deviceInfo(device) for device in outputDevices))) # else Return info on a particular device if isinstance(device, int): device = self.audio.get_device_info_by_index(device) inputChannels = device['maxInputChannels'] outputChannels = device['maxOutputChannels'] if expectOutput is not None and not bool( outputChannels if expectOutput else inputChannels): raise ValueError return f"{device['index']}: {device['name']} ({inputChannels}/{outputChannels} channels)" def createInputStream(self) -> bool: '''Creates an input stream if it doesn't already exist. Returns True if stream already exists or was created successfuly, False otherwise.''' if self.inputStream: return True try: self.inputStream = self.audio.open(self.sampleRate, self.numInputChannels, self.audioFormat, True, False, self.inputDevice, None, self.chunkSize) return True except Exception as e: self.logger.warning( f"Error creating input stream: {(type(e).__name__)}: {e}") return False def createOutputStream(self) -> bool: '''Creates an output stream if it doesn't already exist. Returns True if stream already exists or was created successfuly, False otherwise.''' if self.outputStream: return True try: self.outputStream = self.audio.open(self.sampleRate, self.numOutputChannels, self.audioFormat, False, True, None, self.outputDevice, self.chunkSize) return True except Exception as e: self.logger.warning( f"Error creating output stream: {(type(e).__name__)}: {e}") return False def closeInputStream(self) -> None: if self.inputStream: self.inputStream.close() self.inputStream = None def closeOutputStream(self) -> None: if self.outputStream: self.outputStream.close() self.outputStream = None def readAudioData(self) -> Optional[bytes]: '''Reads a chunk of audio data from the input stream. Returns the retrieved data if successful, None otherwise.''' if not self.createInputStream(): return None try: assert self.inputStream data = self.inputStream.read(self.chunkSize) return data except Exception as e: # Note: IOError: [Errno Input overflowed] -9981 often occurs when running under debugger # Note: IOError: [Errno Unanticipated host error] -9999 occurs when audio device is removed (cable unplugged) # Note: After 5-10 occurences of the above exception system hangs, so stream re-create seems necessary self.logger.warning( f"Audio input error: {(type(e).__name__)}: {e}") self.closeInputStream() self.saveSample() return None def writeAudioData(self, data: bytes) -> bool: '''Writes a chunk of audio data to the output stream. Returns True if successful, False otherwise.''' if not self.createOutputStream(): return False try: assert self.outputStream self.outputStream.write(data) return True except Exception as e: self.logger.warning( f"Audio output error: {(type(e).__name__)}: {e}") self.closeOutputStream() return False def saveSample(self) -> bool: '''Saves the curent sample to the audio file. If the file does not exists, it is created. If the sample length is not equal to the self.sampleLength value, it means, we've cut the silence at the end of the sample, so it's the end of the file and it should be closed. The function returns True on success or if the recording is off, False otherwise.''' if not self.recording: return True try: if self.sampleLength: finalSample = True else: # If sampleLength wasn't set manualy, all the sample is saved. # It means the recording isn't over yet. self.sampleLength = len(self.sample) finalSample = False self.audioFileLength += self.sampleLength recordLength = (float(self.audioFileLength) / self.outputSecondSize) if recordLength > self.minRecordingLength: # The save-to-file process starts only when the sample is long enough if not self.audioFile: # Creating the file if necessary self.audioFile = waveOpen(self.fileName, 'wb') assert self.audioFile self.audioFile.setnchannels(self.numOutputChannels) self.audioFile.setsampwidth(self.audioBytes) self.audioFile.setframerate(self.sampleRate) self.audioFile.writeframes( self.sample[:self.sampleLength] ) # Removing extra silence at the end, if needed self.sample = b'' self.sampleLength = 0 if finalSample or not self.inLoop: self.recording = False self.audioFile.close() self.audioFile = None self.logger.info( f"Recording finished, max volume {self.localMaxVolume:.2f}%, {recordLength:.1f} seconds" ) return True if finalSample or not self.inLoop: self.recording = False self.logger.info( f"Recording discarded as it's too short ({recordLength:.1f} seconds)" ) else: self.audioFileLength -= self.sampleLength # If the sample is short we do not operate with it, so param changes should be undone return True except Exception as e: self.logger.warning( f"File output error: {(type(e).__name__)}: {e}") return False def run(self) -> None: '''Runs main audio processing loop.''' self.audioFile = None self.sampleLength = 0 self.audioFileLength = 0 self.inLoop = True self.recording = False self.quitAfterRecording = False self.lastSecondVolumes = [0.0] * self.chunksInSecond chunkInSecond = 0 start_new_thread(self.commandConsole, ()) # Start command console thread self.logger.info("Listening started") # Main audio processing loop try: while self.inLoop: # Retrieve next chunk of audio data data = self.readAudioData() if not data: # Error occurred sleep(1.0 / self.chunksInSecond ) # Avoid querying malfunctioning device too often continue assert len(data) == self.inputBlockSize if self.channel not in ( MONO, STEREO): # Extract the data for particular channel data = b''.join( data[i:i + self.audioBytes] for i in range((self.channel - 1) * self.audioBytes, len(data), self.numInputChannels * self.audioBytes)) assert len(data) == self.outputBlockSize if self.monitor: # Provide monitor output self.writeAudioData(data) # Gathering volume statistics volume = (mean( abs( cast( int, unpack(self.packFormat, data[i:i + self.audioBytes])[0])) for i in range(0, len(data), self.audioBytes)) * 100 + self.maxVolume // 2) / self.maxVolume # pylint: disable=old-division self.lastSecondVolumes[ chunkInSecond] = volume # Logging the sound volume during the last second chunkInSecond = (chunkInSecond + 1) % self.chunksInSecond if volume >= self.volumeTreshold: # The chunk is loud enough if not self.recording: # Start recording # ToDo: check inputStream.get_time(), latency etc. to provide exact time stamp for file naming self.fileName = strftime(self.fileNameFormat) self.logger.info(f"{self.fileName} recording started") self.recording = True self.sample = b'' self.localMaxVolume = volume self.audioFileLength = 0 elif volume > self.localMaxVolume: self.localMaxVolume = volume self.sampleLength = 0 chunksOfSilence = 0 self.sample += data self.saveSample() elif self.recording: # Check for stop recording self.sample += data chunksOfSilence += 1 if not self.sampleLength and chunksOfSilence > self.chunksOfFadeout: # Enough silence for a trail self.sampleLength = len( self.sample) # Removing extra silence at the end if chunksOfSilence > self.chunksToStop: # Enough silence to stop recording self.saveSample() # Stopping recording if self.quitAfterRecording: self.inLoop = False except Exception as e: self.logger.warning(f"Processing error: {(type(e).__name__)}: {e}") except KeyboardInterrupt: self.logger.warning("Ctrl-C detected at input, exiting") self.inLoop = False self.saveSample() self.closeInputStream() self.closeOutputStream() self.logger.info("Done") def commandConsole(self) -> None: '''Runs in a separate thread to provide a command line operation adjustments.''' try: while self.inLoop: inp = input().split(' ') command = inp[0].lower() if 'help'.startswith(command): print( """\nAvailable console commands (first letter is enough): Help - Show this information EXit/Quit - Exit the program immediately Last - Exit the program after completion of the current file Volume - Print the current mean volume level Monitor [on/off] - Show or toggle monitor status Threshold [value] - Show or set the volume threshold level\n""") elif 'exit'.startswith( command) or command == 'x' or 'quit'.startswith( command): self.logger.info("Exiting") self.inLoop = False elif 'volume'.startswith(command): print(f"{mean(self.lastSecondVolumes):.2f}%" ) # Using print for non-functional logging elif 'monitor'.startswith(command): if len(inp) < 2: print(f"Monitor is {'ON' if self.monitor else 'OFF'}" ) # Using print for non-functional logging else: self.monitor = inp[1].lower().strip() in ('true', 'yes', 'on', '1') self.logger.info( f"Monitor is set to {'ON' if self.monitor else 'OFF'}" ) elif 'last'.startswith(command): if self.recording: self.quitAfterRecording = True self.logger.info( "Going to exit after the end of the recording") else: self.logger.info("Exiting") self.inLoop = False elif 'threshold'.startswith(command): if len(inp) < 2: print( f"Current volume treshold: {self.volumeTreshold:.2f}%" ) # Using print for non-functional logging else: try: self.volumeTreshold = float(inp[1]) if not 0 <= self.volumeTreshold <= 100: raise ValueError() self.logger.info( f"New volume treshold: {self.volumeTreshold:.2f}%" ) except ValueError: print("Bad value, expected 0-100" ) # Using print for non-functional logging except EOFError: self.logger.warning("Console EOF detected") except Exception as e: self.logger.warning( f"Console error: {type(e).__name__}: {e}\n{format_exc()}") self.inLoop = False except KeyboardInterrupt: self.logger.warning("Ctrl-C detected at console, exiting") self.inLoop = False def sigTerm(self, _signum: int, _frame: FrameType) -> None: '''SIGTERM handler.''' self.logger.warning("SIGTERM caught, exiting") self.inLoop = False