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 __init__(self, file="audio"): self.format = paInt16 audio = PyAudio() if hasattr(settings, 'AUDIO_DEVICE_INDEX'): self.device_index = settings.AUDIO_DEVICE_INDEX elif hasattr(settings, 'AUDIO_DEVICE'): for i in range(audio.get_device_count()): curr_device = audio.get_device_info_by_index(i) print 'Found device: %s' % curr_device['name'] if curr_device['name'] == settings.AUDIO_DEVICE: print 'Assigning %s (Index: %s)' % ( settings.AUDIO_DEVICE, i ) self.device_index = i elif not hasattr(self, 'device_index'): print 'No Audio device specified. Discovering...' for i in range(audio.get_device_count()): curr_device = audio.get_device_info_by_index(i) print 'Found device: %s' % curr_device['name'] if curr_device['maxInputChannels'] > 0: self.device_index = curr_device['index'] print 'Using device: %s' % curr_device['name'] break print audio.get_device_info_by_index(self.device_index) try: device = audio.get_device_info_by_index(self.device_index) calc_rate = device['defaultSampleRate'] print 'Discovered Sample Rate: %s' % calc_rate self.rate = int(calc_rate) except: print 'Guessing sample rate of 44100' self.rate = 44100 self.channel = 1 self.chunk = 1024 self.file = file
def __init__(self, file="audio"): self.format = paInt16 audio = PyAudio() if hasattr(settings, 'AUDIO_DEVICE_INDEX'): self.device_index = settings.AUDIO_DEVICE_INDEX elif hasattr(settings, 'AUDIO_DEVICE'): for i in range(audio.get_device_count()): curr_device = audio.get_device_info_by_index(i) print 'Found device: %s' % curr_device['name'] if curr_device['name'] == settings.AUDIO_DEVICE: print 'Assigning %s (Index: %s)' % (settings.AUDIO_DEVICE, i) self.device_index = i elif not hasattr(self, 'device_index'): print 'No Audio device specified. Discovering...' for i in range(audio.get_device_count()): curr_device = audio.get_device_info_by_index(i) print 'Found device: %s' % curr_device['name'] if curr_device['maxInputChannels'] > 0: self.device_index = curr_device['index'] print 'Using device: %s' % curr_device['name'] break print audio.get_device_info_by_index(self.device_index) try: device = audio.get_device_info_by_index(self.device_index) calc_rate = device['defaultSampleRate'] print 'Discovered Sample Rate: %s' % calc_rate self.rate = int(calc_rate) except: print 'Guessing sample rate of 44100' self.rate = 44100 self.channel = 1 self.chunk = 1024 self.file = file
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
def get_microphone_index(audio: pyaudio.PyAudio, name: str) -> int: for i in range(audio.get_device_count()): dev = audio.get_device_info_by_index(i) if dev["name"].lower() != name.lower(): logger.debug(f"Skipping audio device '{dev['name']}' (index {i})") continue logger.debug(f"Using audio device '{dev['name']}' (index {i})") return i
def find_tigerjet_audio_device(): p = PyAudio() for dev_idx in range(0, p.get_device_count()): if 'TigerJet' in p.get_device_info_by_index(dev_idx).get('name'): global TJ_DEV_INDEX TJ_DEV_INDEX = dev_idx break else: raise RuntimeError('TigerJet audio output device not found!')
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)
class AfWidget(GridLayout): def __init__(self, **kwargs): self.p = PyAudio() self.rows = 1 self.cols = 1 GridLayout.__init__(self, **kwargs) self.mainPanel = TabbedPanel() print "WIDTH", self.width self.mainPanel.default_tab_text = "AF Output Devices" self.add_widget(self.mainPanel) self.inputPanel = TabbedPanelHeader(text="AF Input Devices") self.inputPanel.content = AfInputManager() self.mainPanel.add_widget(self.inputPanel) self.mainPanel.tab_width = 200 #topLayout = BoxLayout(orientation = "vertical") #topLayout.add_widget(Label(text="Input device", )) #self.inputDevs = Spinner(text = "Select input") #topLayout.add_widget(self.inputDevs) #topLayout.add_widget(Label(text="Output device", )) #self.outputDevs = Spinner(text = "Select output") #topLayout.add_widget(self.outputDevs) #self.updateSoundDevices() #self.add_widget(topLayout) def updateSoundDevices(self): api_cnt = self.p.get_host_api_count() dev_cnt = self.p.get_device_count() inputs = [] outputs = [] print "Number of API's", api_cnt, "Number of sound devices", dev_cnt for i in range(dev_cnt): d = self.p.get_device_info_by_index(i) if d['maxInputChannels'] > 0: inputs.append(d['name']) if d['maxOutputChannels'] > 0: outputs.append(d['name']) print "inputs", inputs print "outputs", outputs self.inputDevs.values = inputs self.outputDevs.values = outputs
def ears_setup(): p = PyAudio() count = p.get_device_count() device = [i for i in range(count) if "Logitech" in p.get_device_info_by_index(i)["name"]][0] source = Microphone(device_index=device) # yup, I'm playing with the internals of this class. source.CHUNK = 512 source.RATE = 8000 source.CHANNELS = 1 try: source.__enter__() source.stream.stop_stream() except: vprint(1, "Microphone initialization failed.") source.__exit__() return source
def get_input_device(p: pyaudio.PyAudio, name: str): """ Returns Example: {'index': 1, 'structVersion': 2, 'name': 'MacBook Pro麦克风', 'hostApi': 0, 'maxInputChannels': 1, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.04852607709750567, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.05868480725623583, 'defaultHighOutputLatency': 0.1, 'defaultSampleRate': 44100.0} """ device_info = None for idx in range(p.get_device_count()): info = p.get_device_info_by_index(idx) channels = info["maxInputChannels"] if channels == 0: continue logger.debug("device name: %s", info['name']) if info['name'] == name: device_info = info if not device_info: sys.exit("Missing iShowU Audio Capture") return device_info
def get_output_device_indexes(p: pyaudio.PyAudio): return [ i for i in range(p.get_device_count()) if p.get_device_info_by_index(i)["maxOutputChannels"] > 0 ]
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 RTAudio(object): def __init__(self, input_device_index, output_device_index, fs=48000, frame_length=1024, channels=1, callback=None): self.input_device_index = input_device_index self.output_device_index = output_device_index self.fs = fs self.stream_callback = callback self.p = PyAudio() self.frame_length = frame_length self.channels = channels self.dostop = False self.sleeptime = 0.1 self.frames = 0 def run(self): self.stream_start() if False: self.stream_run() else: t = Thread(target=self.stream_run) t.start() def stop(self): self.do_stop = True def _callback(self, in_data, frame_count, time_info, status): self.frames += 1 in_data = np.frombuffer(in_data, dtype=np.int16) in_data = in_data.astype(np.float32) / 32767 out_data = self(in_data) * 32767 out_data = out_data.astype(np.int16) return out_data.tobytes(), paContinue def stream_start(self): self.stream = self.p.open(format=paInt16, channels=self.channels, rate=self.fs, input=True, output=True, input_device_index=self.input_device_index, output_device_index=self.output_device_index, frames_per_buffer=self.frame_length, stream_callback=self._callback) self.stream.start_stream() def stream_run(self): self.do_stop = False while self.stream.is_active() and not self.do_stop: time.sleep(self.sleeptime) self.stream_stop() def stream_stop(self): self.stream.stop_stream() self.stream.close() #self.p.terminate() def devices(self): devices = [] for m in range(self.p.get_device_count()): dev = self.p.get_device_info_by_index(m) devices.append({'name': dev['name'], 'inputs': dev['maxInputChannels'], 'outputs': dev['maxOutputChannels']}) return devices
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 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
from pprint import pprint from pyaudio import PyAudio pyaud = PyAudio() for i in range(pyaud.get_device_count()): dev = pyaud.get_device_info_by_index(i) pprint(dev)
def get_pyaudio_devices_info(): pa = PyAudio() return [ pa.get_device_info_by_index(i) for i in range(pa.get_device_count()) ]
def get_audio_info(): audio = PyAudio() info = audio.get_host_api_info_by_index(0) print(info, '\n') for i in range(audio.get_device_count()): print(audio.get_device_info_by_index(i))
def get_input_device_indexes( p: pyaudio.PyAudio): # todo add a star next to the default device return [ i for i in range(p.get_device_count()) if p.get_device_info_by_index(i)["maxInputChannels"] > 0 ]
class AudioPlayer: def __init__(self, delay_time): self.pyaudio = PyAudio() self.frame_size = 512 self.sample_rate = 44100 self.frame_time = self.frame_size / self.sample_rate self.sample_width = 2 # bytes self.channels = 2 self.chunk_size = self.frame_size * self.sample_width * self.channels self.device = None self.ffmpeg_process = None self.data_stream = None self.stream_open = Event() self.unpaused = Event() self.unpaused.set() self.idle = Event() self.idle.set() self.time = 0 self.delay_time = delay_time self.fast_forward_time = 0 def get_devices(self): return [ self.pyaudio.get_device_info_by_index(i) for i in range(self.pyaudio.get_device_count()) if self.pyaudio.get_device_info_by_index(i)['maxOutputChannels'] == self.channels ] def set_device(self, device_index): self.device = self.pyaudio.open( format=self.pyaudio.get_format_from_width(self.sample_width), channels=self.channels, rate=self.sample_rate, output=True, output_device_index=device_index) def open_audio(self, filepath): if not self.device: print('Device not initialized') return ffmpeg_command = [ 'ffmpeg', '-i', filepath, '-loglevel', 'error', '-f', 's16le', '-ac', str(self.channels), '-ar', str(self.sample_rate), '-' ] self.ffmpeg_process = Popen(ffmpeg_command, stdout=PIPE, stderr=DEVNULL) self.data_stream = self.ffmpeg_process.stdout self.device.start_stream() if self.fast_forward_time == 0: self.fast_forward_time = self.device.get_write_available( ) / self.frame_size * self.frame_time self.stream_open.set() def get_time(self): return self.time def pause(self): self.unpaused.clear() def unpause(self): self.unpaused.set() def play_chunk(self): if self.unpaused.is_set(): chunk = self.data_stream.read(self.chunk_size) if chunk: self.time += self.frame_time / 2 self.device.write(chunk) self.time += self.frame_time / 2 else: self.stream_open.clear() else: self.device.stop_stream() self.unpaused.wait() self.device.start_stream() def stop_stream(self): self.stream_open.clear() self.device.stop_stream() if self.ffmpeg_process.poll(): self.ffmpeg_process.kill() if self.data_stream: self.data_stream.flush() self.data_stream = None def close(self): self.device.close() self.pyaudio.terminate() def play(self): self.idle.clear() Thread(target=self.play_thread).start() def play_thread(self): sleep(self.delay_time) while self.stream_open.is_set(): self.play_chunk() self.stop_stream() self.time = 0 self.idle.set()
class SearchSoundSource(): def __init__(self, numUseChannel): # PyAudioを初期化 self.audio = PyAudio() # setup関数が呼ばれたらAudio機器関連のデータが格納される self.streams = {} # 使用するマイクの数 self.numUseChannel = numUseChannel + 1 # マイクの固有値を定義 self.chunk = 1024 self.FORMAT = pyaudio.paInt16 self.Rate = 16000 self.RECORD_SECONDS = 1 self.device_index = None # Tamagoマイクを特定 for num in range(0, self.audio.get_device_count()): if "TAMAGO" in self.audio.get_device_info_by_index(num)["name"]: print("[*] Found Device!") print(self.audio.get_device_info_by_index(num)["name"]) self.device_index = self.audio.get_device_info_by_index( num)["index"] # デバッグ用 print(self.audio.get_device_info_by_index(self.device_index)) # たまごマイクの初期化 self.audioSetup() def audioSetup(self): # 使用するたまごマイクの設定 for num in range(1, self.numUseChannel): self.streams[num] = ( self.audio.open( format=self.FORMAT, channels=num, rate=self.Rate, input=True, #input_device_index = self.device_index, frames_per_buffer=self.chunk)) # 使用するたまごマイクが格納されているかの # print(self.streams) self.processSetup() # 複数プロセスの設定 def processSetup(self): # デバッグ #for num in range(1, 9): # process = Process(target=self.Power, args=(num, )) # process.start() for num in range(1, self.numUseChannel): process = Process(target=self.readWave, args=(self.streams[num], )) process.run() process.terminate() # デバッグ用 def Power(self, num): for i in range(1, num): for j in range(1, num): for k in range(1, num): print(i * j * k) # 波形読み込み def readWave(self, stream): while True: #print(stream.read(1024)) num_data = fromstring(stream.read(1024), dtype='int16') / 32768.0 print(num_data)
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 InstructionRecogniser(QThread): ''' You should only use keyIn/keyOut, and shutdown after use. The thread starts itself when appropriate. Signals are emitted with any recognised instructions. ''' def __init__(self, gui): QThread.__init__(self, gui) if settings.sphinx_acoustic_model_dir == '': # use default acoustic model acoustic_model_directory = path.join(get_model_path(), 'en-us') else: # use custom acoustic model acoustic_model_directory = settings.sphinx_acoustic_model_dir config = Decoder.default_config() config.set_string('-hmm', acoustic_model_directory) # acoustic model config.set_string( '-dict', settings.prepared_lexicon_file) # lexicon pronunciation config.set_string( '-jsgf', settings.prepared_grammar_file) # language model from grammar config.set_string( '-logfn', settings.outputFileName(sphinx_decoder_log_file_base_name, ext='log')) self.listen = False self.decoder = Decoder(config) self.audio = None self.device = None def startup(self): self.audio = PyAudio() if 0 <= settings.audio_input_device_index < self.audio.get_device_count( ): # out of range or -1 for default self.device = settings.audio_input_device_index else: self.device = None def shutdown(self): self.listen = False self.wait() self.audio.terminate() self.audio = None def keyIn(self): if not self.isRunning(): self.listen = True self.start() def keyOut(self): self.listen = False def run(self): audio_stream = self.audio.open(input_device_index=self.device, channels=1, format=paInt16, rate=audio_sample_rate, frames_per_buffer=audio_chunk_size, input=True) chunks = [] msg_duration = 0 buff = audio_stream.read(audio_chunk_size) while self.listen and len( buff) > 0 and msg_duration < message_duration_limit: chunks.append(buff) buff = audio_stream.read(audio_chunk_size) msg_duration += audio_chunk_size / audio_sample_rate audio_stream.close() audio_message = b''.join(chunks) self.decoder.start_utt( ) # STYLE catch failures here (e.g. grammar/lex files not found) self.decoder.process_raw(audio_message, False, True) self.decoder.end_utt() hyp = self.decoder.hyp() if hyp: SR_log('VOICE: "%s"' % hyp.hypstr) if settings.show_recognised_voice_strings: signals.statusBarMsg.emit('VOICE: "%s"' % hyp.hypstr) callsign_tokens, instr_lst = interpret_string(hyp.hypstr) signals.voiceMsgRecognised.emit(callsign_tokens, instr_lst) else: SR_log('VOICE: no hypothesis, message duration was %g s' % msg_duration) signals.voiceMsgNotRecognised.emit()