def __init__(self, logger, audio_buffer): super().__init__() self._logger = logger self._audio_buffer = audio_buffer self._state = AudioBackend.IDLE self._input_devices = [device for device in sound.query_devices() if device['max_input_channels'] > 0] self._output_devices = [device for device in sound.query_devices() if device['max_output_channels'] > 0] self._input_stream = None self._output_stream = None self.new_data_available.connect(self._audio_buffer.handle_new_data)
def init_audio_source(): header = column_header("audio source") widgets = [header,] def callback(_radio_button, state, device_index): if state: audio_input.change_stream(device_index) radio_group = [] for device_index, device in enumerate(sounddevice.query_devices()): if device['max_input_channels'] < 2: continue radio = urwid.RadioButton( radio_group, f"{device['name']}", state=device['name'] == audio_input.INPUT_DEVICE_NAME, on_state_change=callback, user_data=device_index ) widgets.append(radio) pile = urwid.Filler(urwid.Pile(widgets), valign='top') return pile
def run(self, conn, freq): import sounddevice as sd import generate self.tone = generate.GenerateTone(freq=freq, vol=1.0/400000) self.playing = False self.running = True self.broken = False self.pos = 0 self.thread = threading.Thread(target=self.input_thread, args=(conn, )) self.thread.start() print sd.query_devices() print sd.default.device #raise SystemExit() with sd.OutputStream(channels=1, callback=self.callback,samplerate=48000, blocksize=2048, latency='low') as stream: while self.running: sd.sleep(1000) self.thread.join()
def __init__(self, parent, name, value, spec, plabel): """Create a set of ctrls for a particular preference entry """ super(PrefCtrls, self).__init__() self.pref = value self.parent = parent self.name = name valueWidth = 200 labelWidth = 200 self.nameCtrl = self.valueCtrl = None _style = wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL self.nameCtrl = wx.StaticText(self.parent, -1, plabel, size=(labelWidth, -1), style=_style) if type(value) == bool: # only True or False - use a checkbox self.valueCtrl = wx.CheckBox(self.parent) self.valueCtrl.SetValue(value) elif spec.startswith('option') or name == 'audioDevice': if name == 'audioDevice': options = copy.copy(value) value = value[0] try: # getting device name using sounddevice import sounddevice devices = sounddevice.query_devices() for device in devices: if device['max_output_channels'] > 0: # newline characters must be removed thisDevName = device['name'].replace('\r\n','') if thisDevName not in options: options.append(thisDevName) except (ValueError, OSError, ImportError): pass else: options = spec.replace("option(", "").replace("'", "") # item -1 is 'default=x' from spec options = options.replace(", ", ",").split(',')[:-1] labels = [] # display only for opt in options: try: labels.append(_localized[opt]) except Exception: labels.append(opt) self.valueCtrl = wx.Choice(self.parent, choices=labels) self.valueCtrl._choices = copy.copy(options) # internal values try: self.valueCtrl.SetSelection(options.index(value)) except: pass elif spec.startswith('list'): # list valuestring = self.listToString(value) self.valueCtrl = wx.TextCtrl(self.parent, -1, valuestring, size=(valueWidth, -1)) else: # just use a string self.valueCtrl = wx.TextCtrl(self.parent, -1, str(value), size=(valueWidth, -1))
def get_input_devices(): devices = sounddevice.query_devices() names = [] ids = [] for id, device in enumerate(devices): if device['max_input_channels'] > 0: names.append(device['name']) ids.append(id) return (names, ids)
def get_output_devices(self): devices = sounddevice.query_devices() default_output_device = sounddevice.query_devices(kind='output') output_devices = [] if default_output_device is not None: # start by the default input device default_output_device['index'] = devices.index(default_output_device) output_devices += [default_output_device] for device in devices: # select only the output devices by looking at the number of output channels if device['max_output_channels'] > 0: device['index'] = devices.index(device) # default output device has already been inserted if default_output_device is not None and device['index'] != default_output_device['index']: output_devices += [device] return output_devices
def initDevices(self): id = 0 print("Gathering devices...") for dev in sounddevice.query_devices(): d = device(dev, id) print(repr(d)) if d.getType() == "input": self.deviceList.append(d) else: self.outputDevices.append(d) id += 1
def get_readable_output_devices_list(self): output_devices = self.get_output_devices() raw_devices = sounddevice.query_devices() default_output_device = sounddevice.query_devices(kind='output') default_output_device['index'] = raw_devices.index(default_output_device) devices_list = [] for device in output_devices: api = sounddevice.query_hostapis(device['hostapi'])['name'] if default_output_device is not None and device['index'] == default_output_device['index']: extra_info = ' (default)' else: extra_info = '' nchannels = device['max_output_channels'] desc = "%s (%d channels) (%s) %s" % (device['name'], nchannels, api, extra_info) devices_list += [desc] return devices_list
def choose_device(is_input): max_dsr = (MIN_INPUT_RATE or 0) if is_input else (MIN_OUTPUT_RATE or 0) best_index= -1 best = None channel_string = get_channel_string(is_input) for i, info in enumerate(sd.query_devices()): print info if int(info.get(channel_string)) == CHANNELS: if info.get(DEFAULT_SAMPLE_RATE) > max_dsr: max_dsr = info.get(DEFAULT_SAMPLE_RATE) best_index = i best = info if best is None: raise Exception("Failed to find appropriate device") return Device(best_index, best, is_input)
def getDevices(kind=None): """Returns a dict of dict of audio devices of sepcified `kind` The dict keys are names and items are dicts of properties """ devs = {} if travisCI: # travis-CI testing does not have a sound device return devs else: allDevs = sd.query_devices(kind=kind) # annoyingly query_devices is a DeviceList or a dict depending on number if type(allDevs) == dict: allDevs = [allDevs] for ii, dev in enumerate(allDevs): devs[dev['name']] = dev dev['id'] = ii return devs
def get_devices_infos(): devices = sounddevice.query_devices() in_devices = {} out_devices = {} for id, device in enumerate(devices): if device['max_input_channels'] > 0: param = {'host api index':device['hostapi'], 'latency':device['default_low_input_latency'], 'default sr':device['default_samplerate'], 'name':device['name']} in_devices[id] = param if device['max_output_channels'] > 0: param = {'host api index':device['hostapi'], 'latency':device['default_low_output_latency'], 'default sr':device['default_samplerate'], 'name':device['name']} out_devices[id] = param return (in_devices, out_devices)
Form, Window = uic.loadUiType("form.ui") app = QApplication(sys.argv) window = Window() form = Form() form.setupUi(window) window.show() mc = MidiHandler() sh = SaveHandler() qtwt = QtWindowThings() # Populate Device Combo boxes # Populate Midi Device Combo Boxes form.cb_midi_in.addItems(mido.get_input_names()) form.cb_midi_out.addItems(mido.get_output_names()) # Populate Sound Device Combo boxes devices = sd.query_devices() for o in devices: if (o["max_input_channels"] > 0): form.cb_audio_in.addItem(o["name"]) if (o["max_output_channels"] > 0): form.cb_audio_out.addItem(o["name"]) # Load Config File, and Set Device Combo Boxes to their previous values sh.load_config() form.cb_midi_in.setCurrentText(sh.config['default']["midi_in_device"]) form.cb_midi_out.setCurrentText(sh.config['default']["midi_out_device"]) form.cb_audio_in.setCurrentText(sh.config['default']["audio_in_device"]) form.cb_audio_out.setCurrentText(sh.config['default']["audio_out_device"]) # Connect Signals/Slots # Save Device Combo Box Values to config form.cb_midi_in.currentTextChanged.connect(confchange) form.cb_midi_out.currentTextChanged.connect(confchange)
corr = np.correlate(newdata[:,0], newdata[:,1], mode='full') corr = np.abs(corr)/np.max(np.max(corr)) lines[0].set_ydata(corr) return lines try: from matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import numpy as np import sounddevice as sd if args.list_devices: print(sd.query_devices()) parser.exit() if args.samplerate is None: device_info = sd.query_devices(args.device, 'input') args.samplerate = device_info['default_samplerate'] length = int(args.window * args.samplerate/ (1000 * args.downsample)) ran = args.distance/343*args.samplerate/args.downsample newdata = np.zeros((length, len(args.channels))) corr = np.zeros((2*length-1)) phi = np.linspace(0, 180, 6); phi = np.append(phi, 90) xtics = ran*np.cos(phi*np.pi/180) +length-1 xlabels = phi
def start(self): """ Starts metering forever """ def callback(indata, frames, time, status): """ The handler for input stream audio chunks """ if status: print(f'----- {status} -----') qIn.put(indata) def measure(block, duration, mode): """ Compute the measured level for each audio block""" if mode == 'rms': # Mean square calculation for audio blocks msqL = np.sum(np.square(block[:, 0])) / (fs * dur) msqR = np.sum(np.square(block[:, 1])) / (fs * dur) # Combine 2 channels if msqL or msqR: # avoid log10(0+0) M = 20 * np.log10(msqL + msqR) / 2 else: M = -100.0 elif mode == 'peak': ML, MR = np.max(block[:, 0]), np.max(block[:, 1]) M = max(ML, MR) if M: M = 20 * np.log10(M) else: M = -100.0 else: print('bad mode') sys.exit() return round(M, 1) def loop_forever(): """ loop capturing stream and processing audio blocks """ with sd.InputStream(device=self.device, callback=callback, blocksize=bs, samplerate=fs, channels=2, dither_off=True): while True: # Reading captured (b)locks: b = qIn.get() # Compute the measured level self.L = measure(block=b, duration=dur, mode=self.mode) # Print a nice bar meter if self.bar: I = max(-60, int(self.L)) print( f' {"#" * (60 + I + 1)}{" " * (-I - 1)} {self.L}', end='\r') h1 = f'-60 -50 -40 -30 -20 -10 0' + \ f' {self.mode.upper()}' h2 = ' | | | | | | | | | | | | |' if self.bar: print(h1) print(h2) # Prepare an internal FIFO queue for the callback function qIn = queue.Queue() # Getting current Fs fs = sd.query_devices(self.device, 'input')['default_samplerate'] # Audio block duration in seconds dur = 0.100 # lenght in samples of the audio block bs = int(fs * dur) # Launch a thread that loops metering audio blocks jloop = threading.Thread(target=loop_forever, args=()) jloop.start()
for char in chars: if char == '\t': bg, fg = fg, bg else: gradient.append('\x1b[{};{}m{}'.format(fg, bg + 10, char)) try: import sounddevice as sd if args.list_devices: sd.print_devices() parser.exit() if args.device is None: args.device = sd.default.device['input'] samplerate = sd.query_devices(args.device)['default_samplerate'] delta_f = (high - low) / (args.columns - 1) fftsize = np.ceil(samplerate / delta_f).astype(int) low_bin = np.floor(low / delta_f) statuses = sd.CallbackFlags() def callback(indata, frames, time, status): global statuses statuses |= status if any(indata): magnitude = np.abs(np.fft.rfft(indata[:, 0], n=fftsize)) magnitude *= args.gain / fftsize line = (gradient[int(np.clip(x, 0, 1) * (len(gradient) - 1))] for x in magnitude[low_bin:low_bin + args.columns])
def int_or_str(text): """Helper function for argument parsing""" try: return int(text) except ValueError: return text parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-l', '--list-devices', action='store_true', help='show list of audio devices and exit') args, remaining = parser.parse_known_args() if args.list_devices: print(sd.query_devices()) parser.exit(0) parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, parents=[parser]) parser.add_argument('filename', metavar='FILENAME', help='audio file to be played back') parser.add_argument('-d', '--device', type=int_or_str, help='output device (numeric ID or substring)') args = parser.parse_args(remaining) try:
samplerate = None device = None filename = None channels = 1 subtype = None def callback(indata, frames, time, status): if status: print(status, file=sys.stderr) q.put(indata.copy()) try: if samplerate is None: device_info = sd.query_devices(device, 'input') samplerate = int(device_info['default_samplerate']) if filename is None: filename = tempfile.mktemp(prefix='recorded_audio_', suffix='.wav', dir='') with sf.SoundFile(filename, mode='x', samplerate=samplerate, channels=channels, subtype=subtype) as file: with sd.InputStream(samplerate=samplerate, device=device, channels=channels, callback=callback):
plotdata[-shift:, :] = data #print(data.shape) for column, line in enumerate(lines): #line.set_ydata(plotdata[:, column]/(column+1)) line.set_ydata(plotdata[:, column]) return lines try: from matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import numpy as np import sounddevice as sd if args.list_devices: print(sd.query_devices()) parser.exit(0) if args.samplerate is None: device_info = sd.query_devices(args.device, 'input') args.samplerate = device_info['default_samplerate'] length = int(args.window * args.samplerate / (1000 * args.downsample)) plotdata = np.zeros((length, len(args.channels))) fig, ax = plt.subplots() lines = ax.plot(plotdata) if len(args.channels) > 1: ax.legend(['channel {}'.format(c) for c in args.channels], loc='lower left', ncol=len(args.channels)) ax.axis((0, len(plotdata), -1, 1))
def main(): parser, args = parse() print('list') if args.list_devices: print(sounddevice.query_devices()) parser.exit(0) if args.samplerate is None: device_info = sounddevice.query_devices(args.device, 'input') args.samplerate = device_info['default_samplerate'] mapping = [c - 1 for c in args.channels] # Channel numbers start with 1 q = queue.Queue() def audio_callback(indata, frames, time, status): """This is called (from a separate thread) for each audio block.""" if status: print(status, file=sys.stderr) # Fancy indexing with mapping creates a (necessary!) copy: q.put(indata[:, mapping]) #print(indata.shape) stream = sounddevice.InputStream( device=args.device, channels=max(args.channels), samplerate=args.samplerate, callback=audio_callback) print('stream open', args.device, stream) assert args.samplerate == 16000 n_samples = int(args.samplerate*0.960*1.05) hop_length = n_samples * (1-args.overlap) new_samples = 0 audio_buffer = numpy.zeros(shape=(n_samples,)) model = yamnet.Model() import keras import tensorflow with stream: while True: data = q.get() data = numpy.squeeze(data) #print(data.shape) # move existing data over audio_buffer = numpy.roll(audio_buffer, len(data), axis=0) # add the new data audio_buffer[len(audio_buffer)-len(data):len(audio_buffer)] = data # check if we have received enough new data to do new classification new_samples += len(data) if new_samples >= hop_length: t = datetime.datetime.now() new_samples = 0 waveform = audio_buffer with model.graph.as_default(): #x = numpy.reshape(waveform, [1, -1]) x = numpy.expand_dims(waveform, 0) #x = numpy.expand_dims(x, -1) scores, _spec, embeddings = model.yamnet.predict(x, steps=1, batch_size=1) # Report the highest-scoring classes and their scores. prediction = numpy.mean(scores, axis=0) top_n = 3 top = numpy.argsort(prediction)[::-1][:top_n] def format_pred(i): return ' {:12s}: {:.3f}'.format(model.class_names[i], prediction[i]) if numpy.max(prediction) > 0.3: print('classify', t) print('\n'.join(format_pred(i) for i in top)) print('stopped')
def list(args): print(vars(args)) print(sd.query_devices()) parser.exit()
def query_device_details(self, device: Union[int, str] = None, kind: str = None) -> Any: return sounddevice.query_devices(device, kind) # type: ignore
def query_devices(self) -> List[Dict]: return list(sounddevice.query_devices()) # type: ignore
def __init__(self): self.device = sd.query_devices(None, 'input') self.samplerate = int(self.device['default_samplerate'])
def __init__(self, parent, name, value, spec, plabel): """Create a set of ctrls for a particular preference entry """ super(PrefCtrls, self).__init__() self.pref = value self.parent = parent self.name = name valueWidth = 200 labelWidth = 200 self.nameCtrl = self.valueCtrl = None _style = wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL self.nameCtrl = wx.StaticText(self.parent, -1, plabel, size=(labelWidth, -1), style=_style) if type(value) == bool: # only True or False - use a checkbox self.valueCtrl = wx.CheckBox(self.parent) self.valueCtrl.SetValue(value) elif spec.startswith('option') or name == 'audioDevice': if name == 'audioDevice': options = copy.copy(value) value = value[0] try: # getting device name using sounddevice import sounddevice devices = sounddevice.query_devices() for device in devices: if device['max_output_channels'] > 0: # newline characters must be removed thisDevName = device['name'].replace('\r\n', '') if thisDevName not in options: options.append(thisDevName) except (ValueError, OSError, ImportError): pass else: options = spec.replace("option(", "").replace("'", "") # item -1 is 'default=x' from spec options = options.replace(", ", ",").split(',')[:-1] labels = [] # display only for opt in options: try: labels.append(_localized[opt]) except Exception: labels.append(opt) self.valueCtrl = wx.Choice(self.parent, choices=labels) self.valueCtrl._choices = copy.copy(options) # internal values try: self.valueCtrl.SetSelection(options.index(value)) except: pass elif spec.startswith('list'): # list valuestring = self.listToString(value) self.valueCtrl = wx.TextCtrl(self.parent, -1, valuestring, size=(valueWidth, -1)) else: # just use a string self.valueCtrl = wx.TextCtrl(self.parent, -1, str(value), size=(valueWidth, -1))
import sounddevice as sd import numpy as np import sys device = sd.query_devices('MAYA44') duration = 5 #seconds print(device) def callback(indata, outdata, frames, time, status): if status: print(status) #print(indata) outdata[:] = indata #print(outdata) #print(sys.getsizeof(indata)) #print() stream = sd.Stream(samplerate=device['default_samplerate'], device=device['name'], channels=(device['max_input_channels'], device['max_output_channels']), latency=(device['default_low_input_latency'], device['default_low_output_latency']), never_drop_input=True, callback=callback) with stream: for x in range(5): #print(sd.get_status())
def printDevices(): print '--- Devices ---' print sd.query_devices()
def getDevice(name=u'ASTRA S: USB Audio (hw:2,0)'): devices = sd.query_devices() for i in range(len(devices)): if devices[i]['name'] == name: return devices[i] return None
def __init__(self, device=None, rate=None, updates_per_second=1000, FFT_window_size=None, verbose=False): print("Available audio devices:") device_dict = sd.query_devices() print(device_dict) try: sd.check_input_settings(device=device, channels=1, dtype=np.float32, extra_settings=None, samplerate=rate) except: print( "Input sound settings for device %s and samplerate %s Hz not supported, using defaults..." % (str(device), str(rate))) rate = None device = None self.rate = rate if rate is not None: sd.default.samplerate = rate self.device = device if device is not None: sd.default.device = device self.verbose = verbose self.data_buffer = None # This part is a bit hacky, need better solution for this: # Determine what the optimal buffer shape is by streaming some test audio self.optimal_data_lengths = [] with sd.InputStream(samplerate=self.rate, blocksize=0, device=self.device, channels=1, dtype=np.float32, latency='low', callback=self.test_stream_read): time.sleep(0.2) self.update_window_n_frames = max(self.optimal_data_lengths) del self.optimal_data_lengths #Alternative: #self.update_window_n_frames = round_up_to_even(44100 / updates_per_second) self.stream = sd.InputStream(samplerate=self.rate, blocksize=self.update_window_n_frames, device=None, channels=1, dtype=np.float32, latency='low', extra_settings=None, callback=self.non_blocking_stream_read) self.rate = self.stream.samplerate self.device = self.stream.device self.updates_per_second = self.rate / self.update_window_n_frames self.info = '' self.data_capture_delays = deque(maxlen=20) self.new_data = False if self.verbose: self.data_capture_delays = deque(maxlen=20) self.num_data_captures = 0 self.device_latency = device_dict[ self.device]['default_low_input_latency'] print( "\n##################################################################################################" ) print( "\nDefaulted to using first working mic, Running on mic %s with properties:" % str(self.device)) print(device_dict[self.device]) print('Which has a latency of %.2f ms' % (1000 * self.device_latency)) print( "\n##################################################################################################" ) print( 'Recording audio at %d Hz\nUsing (non-overlapping) data-windows of %d samples (updating at %.2ffps)' % (self.rate, self.update_window_n_frames, self.updates_per_second))
dataset_home_dir_path = 'D:/dataset' data_dir_path = dataset_home_dir_path + '/failure_analysis/defect/knock_sound/500-1500Hz' #-------------------------------------------------------------------- import numpy as np import sounddevice as sd fs = 44100 data = np.random.uniform(-1, 1, fs) sd.play(data, fs) sd.default.dtype sd.query_devices() sd.default.samplerate = 44100 # A single value sets both input and output at the same time. #sd.default.device = 'digital output' #sd.default.device = 7 # Different values for input and output. sd.default.channels = 5, 7 sd.play(data) sd.default.reset() #-------------------------------------------------------------------- import scipy.io.wavfile # For reading the .wav file.
pcm = opus_file.as_array() print("Playing...") sd.play(pcm, opus_file.frequency) if __name__ == "__main__": print("") print("Playing Audio") print("=============") print("") print( "This test checks that we can play an Opus file and that you can hear it.\n" ) device_strings = sd.query_devices() default_output_index = sd.default.device[1] default_device_string = device_strings[default_output_index]["name"] print("Selected output device:", default_device_string) print("\nCan you hear the audio?") print("Type 'y' followed by enter if you can hear the audio correctly.") print("") print( "If you can't hear Choeur Adleisia warming up, then try turning up the" ) print( "volume on your speakers. If your speakers have an 'on' switch, check" ) print( "that they're turned on. Check that you haven't muted your speakers.")
def set_audio_device(self, device_name): device_found = False try: if gv.AUDIO_DEVICE_ID >= 0: print '>>>> Using user-defined AUDIO_DEVICE_ID (%d)' % gv.AUDIO_DEVICE_ID return else: i = 0 for d in sounddevice.query_devices(): if device_name in d[ 'name'] and d['max_output_channels'] > 0: gv.AUDIO_DEVICE_ID = i device_name = d['name'] gv.AUDIO_DEVICE_NAME = device_name print '\r>>>> Device selected by name: [%i]: %s\r' % ( i, device_name) device_found = True break i += 1 if device_found is not True and gv.IS_DEBIAN: print ">>>> Device defined in config.ini could not be found. Looking for other connected audio devices." i = 0 for d in sounddevice.query_devices(): print d['name'] if 'bcm2835 ALSA' not in d['name'] and 'sysdefault' not in d['name'] \ and 'default' not in d['name'] and 'dmix' not in d['name'] \ and d['max_output_channels'] > 0: gv.AUDIO_DEVICE_ID = i device_name = d['name'] gv.AUDIO_DEVICE_NAME = device_name print '\r>>>> Device selected by name: [%i]: %s\r' % ( i, device_name) device_found = True break i += 1 # Default to the Raspberry Pi on-board audio if device in config is not found if device_found is not True and gv.IS_DEBIAN: print ">>>> No connected audio devices found. Defaulting to RPi on-board soundcard." i = 0 device_name = 'bcm2835' for d in sounddevice.query_devices(): if device_name in d[ 'name'] and d['max_output_channels'] > 0: gv.AUDIO_DEVICE_ID = i device_name = d['name'] gv.AUDIO_DEVICE_NAME = device_name print '\r>>>> Default RPi audio device selected: [%i]: %s\r' % ( i, device_name) device_found = True break i += 1 return device_name except: print "There was an error setting the audio device" pass
if __name__ == "__main__": LIST_DEVICES = False # list audio devices and exit DEVICE = None # input device (numeric ID or substring) logging.basicConfig(level=config.LOG_LEVEL, format=config.LOG_FORMAT, datefmt=config.LOG_DATE_FMT) _model = make_model(evaluate=True, sample_duration=SAMPLE_DURATION) try: if LIST_DEVICES: logging.info(f"Devices: {sd.query_devices()}") exit(0) _samplerate = sd.query_devices(DEVICE, "input")["default_samplerate"] _block_size = int(_samplerate * config.BLOCK_DURATION / 1000) _window = ssw.blackman(_block_size) _fft_size = config.FFT_SIZE mln = MedianLastN(5, -1) def _callback(in_data, _, __, ___): sound = in_data.T[0] if max(sound) < 0.01: return # logging.info(f"in_data: {len(in_data.flatten())}") _this_fft = abs( fft(_window * sound, _fft_size * config.FFT_RATIO)[:_fft_size]) _this_set = _this_fft[None, :] _this_labels = _model.predict(_this_set)
# Stolen from https://gist.github.com/maurisvh/df919538bcef391bc89f colors = 30, 34, 35, 91, 93, 97 chars = ' :%#\t#%:' gradient = [] for bg, fg in zip(colors, colors[1:]): for char in chars: if char == '\t': bg, fg = fg, bg else: gradient.append('\x1b[{};{}m{}'.format(fg, bg + 10, char)) try: import sounddevice as sd if args.list_devices: print(sd.query_devices()) parser.exit(0) samplerate = sd.query_devices(args.device, 'input')['default_samplerate'] delta_f = (high - low) / (args.columns - 1) fftsize = math.ceil(samplerate / delta_f) low_bin = math.floor(low / delta_f) def callback(indata, frames, time, status): if status: text = ' ' + str(status) + ' ' print('\x1b[34;40m', text.center(args.columns, '#'), '\x1b[0m', sep='')
from flask import Flask, render_template import datetime import sounddevice as sd import sys import numpy as np app = Flask(__name__) def getdata(): return datetime.datetime.today().timestamp() samplerate = sd.query_devices('hw:1,0', 'output')['default_samplerate'] start_idx = 0 def callback(outdata, frames, time, status): if status: print(status, file=sys.stderr) global start_idx t = (start_idx + np.arange(frames)) / samplerate t = t.reshape(-1, 1) outdata[:] = 1.0 * np.sin(2 * np.pi * 1000.0 * t) start_idx += frames with sd.OutputStream(device='hw:1,0', channels=1, callback=callback, samplerate=samplerate):
def main(): parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-l', '--list-devices', action='store_true', help='show list of audio devices and exit') args, remaining = parser.parse_known_args() if args.list_devices: print(sd.query_devices()) parser.exit(0) parser = argparse.ArgumentParser( description=__doc__ + '\n\nSupported keys:' + usage_line, formatter_class=argparse.RawDescriptionHelpFormatter, parents=[parser]) parser.add_argument('-b', '--block-duration', type=float, metavar='DURATION', default=50, help='block size (default %(default)s milliseconds)') parser.add_argument('-c', '--columns', type=int, default=columns, help='width of spectrogram') parser.add_argument('-d', '--device', type=int_or_str, help='input device (numeric ID or substring)') parser.add_argument('-g', '--gain', type=float, default=10, help='initial gain factor (default %(default)s)') parser.add_argument('-r', '--range', type=float, nargs=2, metavar=('LOW', 'HIGH'), default=[100, 2000], help='frequency range (default %(default)s Hz)') args = parser.parse_args(remaining) low, high = args.range if high <= low: parser.error('HIGH must be greater than LOW') # Create a nice output gradient using ANSI escape sequences. # Stolen from https://gist.github.com/maurisvh/df919538bcef391bc89f colors = 30, 34, 35, 91, 93, 97 chars = ' :%#\t#%:' gradient = [] for bg, fg in zip(colors, colors[1:]): for char in chars: if char == '\t': bg, fg = fg, bg else: gradient.append('\x1b[{};{}m{}'.format(fg, bg + 10, char)) try: samplerate = sd.query_devices(args.device, 'input')['default_samplerate'] delta_f = (high - low) / (args.columns - 1) fftsize = math.ceil(samplerate / delta_f) low_bin = math.floor(low / delta_f) def callback(indata, frames, time, status): if status: text = ' ' + str(status) + ' ' print('\x1b[34;40m', text.center(args.columns, '#'), '\x1b[0m', sep='') if any(indata): magnitude = np.abs(np.fft.rfft(indata[:, 0], n=fftsize)) magnitude *= args.gain / fftsize line = (gradient[int(np.clip(x, 0, 1) * (len(gradient) - 1))] for x in magnitude[low_bin:low_bin + args.columns]) print(*line, sep='', end='\x1b[0m\n') else: print('no input') with sd.InputStream(device=args.device, channels=1, callback=callback, blocksize=int(samplerate * args.block_duration / 1000), samplerate=samplerate): while True: response = input() if response in ('', 'q', 'Q'): break for ch in response: if ch == '+': args.gain *= 2 elif ch == '-': args.gain /= 2 else: print('\x1b[31;40m', usage_line.center(args.columns, '#'), '\x1b[0m', sep='') break except KeyboardInterrupt: parser.exit('Interrupted by user') except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e))
def get_readable_current_channels(): if sound.query_devices(kind='input')['max_input_channels'] == 2: return ['L', 'R'] else: return list(range(sound.query_devices(kind='input')['max_input_channels']))
# Stolen from https://gist.github.com/maurisvh/df919538bcef391bc89f colors = 30, 34, 35, 91, 93, 97 chars = ' :%#\t#%:' gradient = [] for bg, fg in zip(colors, colors[1:]): for char in chars: if char == '\t': bg, fg = fg, bg else: gradient.append('\x1b[{};{}m{}'.format(fg, bg + 10, char)) try: import sounddevice as sd if args.list_devices: print(sd.query_devices()) parser.exit() samplerate = sd.query_devices(args.device, 'input')['default_samplerate'] delta_f = (high - low) / (args.columns - 1) fftsize = np.ceil(samplerate / delta_f).astype(int) low_bin = np.floor(low / delta_f) cumulated_status = sd.CallbackFlags() def callback(indata, frames, time, status): global cumulated_status cumulated_status |= status if any(indata): magnitude = np.abs(np.fft.rfft(indata[:, 0], n=fftsize))
def main(): import sounddevice as sd print(sd.query_devices())
def list_devices(): import sounddevice as sd return sd.query_devices()
Cpp.c_filters.setSampleRate(f) # align the filters try: f = SAMPLERATE if USE_48kHz: if PITCHBITS < 7: print( "==> Can't tune to 48kHz, please set PITCHBITS to 7 or higher <==" ) else: PITCHCORR = -147 * (2**(PITCHBITS - 7)) f = 48000 i = 0 device_found = False for d in sounddevice.query_devices(): if (AUDIO_DEVICE_NAME in d['name'] or AUDIO_DEVICE_ID == i): # does the device match the config requested one? AUDIO_DEVICE_ID = i AUDIO_DEVICE_NAME = d['name'] if d['max_output_channels'] == 0 or re.search( '\(hw:(.*),', AUDIO_DEVICE_NAME) != None: try: OpenDevice(f) device_found = True # found device requested by configuration.txt can be started except: pass if not device_found: print('Ignored requested audio device #%i %s' % (AUDIO_DEVICE_ID, AUDIO_DEVICE_NAME)) break
pass if __name__ == '__main__': # Startup loop. Populates options and starts up ProcessAudio. # # Loads the options class, and if it was one the special cases like # writing the inifile or printing out the midi and sound device # information, do that then quit. # # Otherwise, start the ProcessAudio class and get out of the way. main_options = Options() if main_options.settings['writeinifile']: main_options.write_options_ini() quit() elif main_options.settings['listsounddevices'] or main_options.settings[ 'listmidiports']: if main_options.settings['listsounddevices']: print("\nAvailable sound devices:") print(sd.query_devices()) if main_options.settings['listmidiports']: print("\nAvailable MIDI ports:") print("\n".join(mido.get_output_names())) print("") quit() print("Control-C to quit") process_audio = ProcessAudio(main_options) process_audio.start() while True: time.sleep(.1)
texts = json.load(fopen) try: with open('rejected.json') as fopen: rejected = json.load(fopen) except BaseException: rejected = {} import queue import sys import sounddevice as sd import soundfile as sf import os device_info = sd.query_devices('LCS USB Audio') device = None samplerate = int(44100) channels = 1 subtype = 'PCM_24' for no, text in enumerate(texts): if str(no) in rejected: continue try: filename = f'audio/{no}.wav' if os.path.isfile(filename): continue c = input_char( f'say: {text} , press `c` to continue or press any key except `c` to skip.'
def _init_mixer(fs, n_channels, api=None, name=None): """Select the API and device.""" devices = query_devices() if len(devices) == 0: raise OSError('No sound devices found!') apis = query_hostapis() # API if api is None: api = get_config('SOUND_CARD_API', None) if api is None: # Eventually we should maybe allow 'Windows WDM-KS', # 'Windows DirectSound', or 'MME' api = dict( darwin='Core Audio', win32='Windows WASAPI', linux='ALSA', )[sys.platform] for ai, this_api in enumerate(apis): if this_api['name'] == api: api = this_api break else: raise RuntimeError('Could not find host API %s' % (api,)) del this_api # Name if name is None: name = get_config('SOUND_CARD_NAME', None) if name is None: global _DEFAULT_NAME if _DEFAULT_NAME is None: di = api['default_output_device'] _DEFAULT_NAME = devices[di]['name'] logger.exp('Selected default sound device: %r' % (_DEFAULT_NAME,)) name = _DEFAULT_NAME possible = list() for di, device in enumerate(devices): if device['hostapi'] == ai: possible.append(device['name']) if name == device['name']: break else: raise RuntimeError('Could not find device on API %r with name ' 'containing %r, found:\n%s' % (api['name'], name, '\n'.join(possible))) param_str = ('sound card %r (devices[%d]) via %r, %d channels' % (device['name'], di, api['name'], n_channels)) if fs is not None: param_str += ' @ %d Hz' % (fs,) try: mixer = Mixer( samplerate=fs, latency='low', channels=n_channels, dither_off=True, device=di) except Exception as exp: raise RuntimeError('Could not set up %s:\n%s' % (param_str, exp)) assert mixer.channels == n_channels if fs is None: param_str += ' @ %d Hz' % (mixer.samplerate,) else: assert mixer.samplerate == fs mixer.start() try: mixer.start_time = mixer.time except Exception: mixer.start_time = 0 logger.info('Expyfun: using %s, %0.1f ms latency' % (param_str, 1000 * device['default_low_output_latency'])) return mixer
import os import tempfile import json with open('dumping-wiki-6-july-2019.json') as fopen: wiki = json.load(fopen) combined_wiki = ' '.join(wiki).split() len(combined_wiki) length = 4 texts = [] for i in range(0, len(combined_wiki), length): texts.append(' '.join(combined_wiki[i:i + length])) device_info = sd.query_devices(None, 'input') device = None samplerate = int(device_info['default_samplerate']) channels = 1 subtype = None for no, text in enumerate(texts): try: filename = 'streaming/%s.wav' % (text) if os.path.isfile(filename): continue print( 'say: %s, if too hard, press c + enter to skip, else any button + enter to continue' % (text)) skip = input().lower()
def init_ui(self): self.input_source = widgets.QComboBox(self) self.input_source_channels = widgets.QComboBox(self) self.save_button = widgets.QPushButton("Set save location", self) self.save_button.setToolTip("Save new recordings to this folder") self.save_location = widgets.QLabel("[No path]", self) self.last_save = widgets.QLabel("", self) self.save_mode_toggle = widgets.QCheckBox("Save channels separately", self) self.save_mode_toggle.setChecked(Settings.SAVE_CHANNELS_SEPARATELY) self.monitor_button = widgets.QRadioButton("Monitor only") self.monitor_button.setToolTip("Do not save new files") self.monitor_button.setChecked(True) self.trigger_button = widgets.QRadioButton( "Triggered recording") self.trigger_button.setToolTip("Record files when sound amplitude triggers detector") self.continuous_button = widgets.QRadioButton("Continuous recording") self.continuous_button.setToolTip("Record audio continuously") if Settings.USE_SOUNDDEVICE: for i, device in enumerate(sd.query_devices()): if not device["max_input_channels"]: continue device["index"] = i self.input_source.addItem(device.get("name"), device) else: for i in range(self.p.get_device_count()): device = self.p.get_device_info_by_index(i) if not device.get("maxInputChannels"): continue self.input_source.addItem(device.get("name"), device) self.input_source.currentIndexChanged.connect(self.update_channel_dropdown) self.update_channel_dropdown(None) layout = widgets.QGridLayout() layout.addWidget(widgets.QLabel("Device:", parent=self), 1, 1) layout.addWidget(self.input_source, 1, 2) layout.addWidget(widgets.QLabel("Channels:", parent=self), 1, 3) layout.addWidget(self.input_source_channels, 1, 4) layout.setColumnStretch(1, 0) layout.setColumnStretch(2, 0) layout.setColumnStretch(3, 0) layout.setColumnStretch(4, 0) # layout.setColumnStretch(3, 1) layout.addWidget(self.monitor_button, 2, 2) layout.addWidget(self.trigger_button, 3, 2) layout.addWidget(self.continuous_button, 4, 2) layout.setColumnStretch(5, 1) layout.setColumnStretch(5, 1) layout.addWidget(self.save_button, 1, 5) layout.addWidget(self.save_mode_toggle, 2, 5, 1, 2) layout.addWidget(self.save_location, 3, 5, 1, 2) layout.addWidget(self.last_save, 4, 5, 1, 2) self.setLayout(layout)
import cv2 import sounddevice as sd import soundfile as sf data, samplerate = sf.read("./beep.wav") for idx, device in enumerate(sd.query_devices()): print(device) if 'USB PnP Sound Device' in device['name']: sd.default.device = idx break myrecording = sd.playrec(data, samplerate, channels=1) sd.wait() sd.play(myrecording, samplerate) sd.wait() cap = cv2.VideoCapture(1) cap.set(3, 640) cap.set(4, 480) while cap.isOpened(): # 카메라 프레임 읽기 success, frame = cap.read() if success: # 프레임 출력 cv2.imshow('Camera Window', frame) # ESC를 누르면 종료 key = cv2.waitKey(1) & 0xFF if (key == 27):
def get_current_output_device_index(self): return self._output_devices.index(sound.query_devices(device=sound.default.device[1]))
def reset(self): """Resets plug-in to initial values.""" # Set default experimental variables and values self.var.dummy_mode = u'no' self.var.verbose = u'no' self.var.bitdepth = str(16) self.var.samplerate = str(44100) self.var.channels = str(2) self.var.period_size = 128 self.experiment.audio_low_latency_play_module_list = list() self.experiment.audio_low_latency_play_device_dict = dict() self.experiment.audio_low_latency_play_device_selected_dict = dict() self.pyalsaaudio_module_name = u'PyAlsaAudio (Linux only)' self.oss4_module_name = u'ossaudiodev (Linux only)' self.pyaudio_module_name = u'PyAudio (PortAudio)' self.sounddevice_module_name = u'SoundDevice (PortAudio)' self.experiment.oss4_module_name = self.oss4_module_name self.experiment.pyalsaaudio_module_name = self.pyalsaaudio_module_name self.experiment.sounddevice_module_name = self.sounddevice_module_name self.experiment.pyaudio_module_name = self.pyaudio_module_name if os.name == 'posix': if u'alsaaudio' in self.modules_enabled: try: import alsaaudio alsa_cards = alsaaudio.pcms(alsaaudio.PCM_PLAYBACK) if alsa_cards: self.experiment.audio_low_latency_play_module_list.append(self.pyalsaaudio_module_name) self.experiment.audio_low_latency_play_device_dict[self.pyalsaaudio_module_name] = alsa_cards self.experiment.audio_low_latency_play_device_selected_dict[self.pyalsaaudio_module_name] = alsa_cards[0] except: self.show_message(u'Could not import alsaaudio') if u'ossaudiodev' in self.modules_enabled: try: import ossaudiodev self.experiment.audio_low_latency_play_module_list.append(self.oss4_module_name) self.experiment.audio_low_latency_play_device_dict[self.oss4_module_name] = [u'Exclusive Mode',u'Shared Mode'] self.experiment.audio_low_latency_play_device_selected_dict[self.oss4_module_name] = u'Exclusive Mode' except: self.show_message(u'Could not import ossaudiodev') if u'sounddevice' in self.modules_enabled: try: import sounddevice sounddevice_cards = list() cards = sounddevice.query_devices() self.experiment.audio_low_latency_play_module_list.append(self.sounddevice_module_name) for di in range(0, len(cards)): sounddevice_cards_dict = cards[di] sounddevice_cards.append(sounddevice_cards_dict['name']) self.experiment.audio_low_latency_play_device_dict[self.sounddevice_module_name] = sounddevice_cards self.experiment.audio_low_latency_play_device_selected_dict[self.sounddevice_module_name] = sounddevice_cards[0] except: self.show_message(u'Could not import sounddevice') if u'pyaudio' in self.modules_enabled: try: import pyaudio pyaudio_cards = list() pyaudio_device = pyaudio.PyAudio() self.experiment.audio_low_latency_play_module_list.append(self.pyaudio_module_name) for di in range(0, pyaudio_device.get_device_count()): pyaudio_cards_dict = pyaudio_device.get_device_info_by_index(di) pyaudio_cards.append(pyaudio_cards_dict['name']) self.experiment.audio_low_latency_play_device_dict[self.pyaudio_module_name] = pyaudio_cards self.experiment.audio_low_latency_play_device_selected_dict[self.pyaudio_module_name] = pyaudio_cards[0] except: self.show_message(u'Could not import pyaudio') self.show_message(u'Audio Low Latency Play plug-in has been initialized!') if self.pyalsaaudio_module_name in self.experiment.audio_low_latency_play_module_list: self.var.module = self.pyalsaaudio_module_name elif self.sounddevice_module_name in self.experiment.audio_low_latency_play_module_list: self.var.module = self.sounddevice_module_name elif self.pyaudio_module_name in self.experiment.audio_low_latency_play_module_list: self.var.module = self.pyaudio_module_name elif self.oss4_module_name in self.experiment.audio_low_latency_play_module_list: self.var.module = self.oss4_module_name device_list = self.experiment.audio_low_latency_play_device_dict[self.var.module] self.var.device_name = device_list[0]
try: print("Accessing", card, "...") for sampling_rate in [48000, 96000]: sc = alsaaudio_soundcard(card, 1024, sampling_rate) sc.info() except alsaaudio.ALSAAudioError as err: print("! ERROR capturing sound on card", card) print(err) else: print("not installed.") print("\n", "- "*60) print("sounddevice:") if 'sounddevice' in audioModule: print(sounddevice.query_devices()) for device, card in enumerate(sounddevice.query_devices()): # try: print("Accessing", card['name'], "...") for sampling_rate in [48000]: sc = sounddevice_soundcard(device, sampling_rate) sc.info() # except alsaaudio.ALSAAudioError as err: # print("! ERROR capturing sound on card", card) # print(err) else: print("not installed.") print("\n", "- "*60) print("pyaudio:")
def InterfaceIO(): """ Input: None Output: Printed Values = devinfo = device info. gives fulle info about audio devices devopt = give list of indexes (not their names) witch contain full duplex devices. read out info from port audio / sounddevice and filter this in readble information. """ # Signal to soundcard # Soundcard information devinfo = sd.query_devices() # print(devinfo) IOs = np.zeros((len(devinfo), 3)) for idx in range(len(devinfo)): temp = devinfo[idx] IOs[idx][0] = idx IOs[idx][1] = temp['max_input_channels'] IOs[idx][2] = temp['max_output_channels'] # nonzeroval = IOs[:, [1, 2]] # filter on arrays!! # http://stackoverflow.com/questions/8386675/extracting-specific-columns-in-numpy-array nzin = np.nonzero(IOs[:, 1])[0] # nzin = np.append(5, nzin) nzout = np.nonzero(IOs[:, 2])[0] devopt = [] for idx in range(len(nzin)): devopt = np.append(devopt, np.where(nzout == nzin[idx])) if len(devopt) != 0: # raise InterfaceWarning('This are full dupplex interfaces under all circumstances:', 'Interface.py', 32) # raise Interface print('This are full dupplex interfaces under all circumstances:') for idx in devopt: temp = devinfo[np.int(idx)] print(idx, temp['name']) else: if sys.platform.startswith('linux'): # raise InterfaceWarning('This are full dupplex interfaces under all circumstances:') print('This are full dupplex interfaces under all circumstances:') print('Inputs:') for idx in nzin: temp = devinfo[np.int(idx)] print(idx, temp['name']) print('Outputs:') for idx in int(nzout): temp = devinfo[np.int(idx)] print(idx, temp['name']) devopt = (nzin, nzout) elif sys.platform.startswith('win32'): raise InterfaceError(str(devopt),'No full dupplex interfaces are availlable. Create Aggrigate Device or virtual In/Out Device:') elif sys.platform.startswith('cygwin'): raise InterfaceError(str(devopt),'No full dupplex interfaces are availlable. Create Aggrigate Device or virtual In/Out Device:') elif sys.platform.startswith('darwin'): # raise InterfaceWarning('No full dupplex interfaces are availlable. Any valid combination of interfaces will work :') print('This are full dupplex interfaces under all circumstances:') print('Inputs:') for idx in int(nzin): Interfaces_print = devinfo[np.int(idx)] print(idx, Interfaces_print['name']) print('Outputs:') for idx in int(nzout): Interfaces_print = devinfo[np.int(idx)] print(idx, Interfaces_print['name']) devopt = (nzin, nzout) return(devinfo, devopt)