def start(self): import soundcard as sc try: sc.set_name('Panon') except (AttributeError, NotImplementedError): pass if self.device_id == 'all': mics = sc.all_microphones(exclude_monitors=False) elif self.device_id == 'allspeakers': mics = [ mic for mic in sc.all_microphones(exclude_monitors=False) if mic.id.endswith('.monitor') ] elif self.device_id == 'default': mics = [sc.default_microphone()] else: mics = [ sc.get_microphone( self.device_id, include_loopback=False, exclude_monitors=False, ) ] self.streams = [] for mic in mics: stream = mic.recorder( self.sample_rate, self.channel_count, self.blocksize, ) stream.__enter__() self.streams.append(stream)
def start_mixer(): all_speakers = sc.all_speakers() (speaker1, ) = filter(speaker_name_matcher, all_speakers) output1 = speaker1.player(samplerate=SAMPLERATE, blocksize=BLOCKSIZE) output1.__enter__() all_inputs = sc.all_microphones() (mic1, mic2) = filter(mic_name_matcher, all_inputs) input1 = mic1.recorder(samplerate=SAMPLERATE, blocksize=BLOCKSIZE) input1.__enter__() input2 = mic1.recorder(samplerate=SAMPLERATE, blocksize=BLOCKSIZE) input2.__enter__() all_inputs = sc.all_microphones(include_loopback=True) (loopback, ) = filter(loopback_name_matcher, all_inputs) opendsh = loopback.recorder(samplerate=SAMPLERATE, blocksize=BLOCKSIZE) opendsh.__enter__() player = None playing_count = 0 while True: playing = None i1 = input1.record(numframes=NUMFRAMES) i2 = input2.record(numframes=NUMFRAMES) od = opendsh.record(numframes=NUMFRAMES) if player == Inputs.CHANNEL1 and playing_count < MIN_PLAYING_COUNT: output1.play(i1) playing = Inputs.CHANNEL1 elif player == Inputs.CHANNEL2 and playing_count < MIN_PLAYING_COUNT: output1.play(i2) playing = Inputs.CHANNEL2 elif not is_silent(i1, "channel1"): output1.play(i1) playing = Inputs.CHANNEL1 elif not is_silent(i2, "channel2"): output1.play(i2) playing = Inputs.CHANNEL2 else: output1.play(od) playing = Inputs.OPENDSH if playing != player: logger.debug("switching to source: {} after {} iterations".format( playing, playing_count)) player = playing playing_count = 0 else: playing_count += 1
def init(self, recorder_device=None, search_for_loopback=False): import soundcard debug_prefix = "[AudioSourceRealtime.init]" # Search for the first loopback device (monitor of the current audio output) # Probably will fail on Linux if not using PulseAudio but oh well if (search_for_loopback) and (recorder_device is None): logging.info( f"{debug_prefix} Attempting to find the first loopback device for recording" ) # Iterate on every "microphone", or recorder-capable devices to be more precise for device in soundcard.all_microphones(include_loopback=True): # If it's marked as loopback then we'll use it if device.isloopback: self.recorder = device logging.info( f"{debug_prefix} Found loopback device: [{device}]") break # If we didn't match anyone then recorder_device will be None and we'll error out soon else: # Assign the recorder given by the user since self.recorder = recorder_device # Recorder device should not be none assert ( self.recorder is not None ), "Auto search is off and didn't give a target recorder device"
def set_speaker(): lst = sc.all_microphones(include_loopback=True) global my_speaker if len(lst)==0: print(colored('U have no callback-speakers thus u will not be able to recognize messages from speaker',on_color='on_yellow',attrs=['bold'])) return if len(lst)==1: print(colored(f'Single speaker {lst[0]} was choosen',on_color='on_yellow',attrs=['bold'])) my_speaker = lst[0] return print_on_blue('Hello! Please set the correct speaker from list or write 0 to disable speaker recognition:') for i, s in enumerate(lst): print(f"\t{i+1}) {s}") while True: res = input(f'Just write the number from {0} to {len(lst)} (0 to disable): ') if res.isdigit(): number = int(res) if number == 0: print() print_on_blue('Speaker recognition was disabled') print() break if 1 <= number <= len(lst): print() print_on_blue(f'Speaker {lst[number-1]} was choosen') print() my_speaker = lst[number-1] break
def all_mics(self): """! Transform all microphones to streams """ from soundcard import all_microphones return [Microphone(mic) for mic in all_microphones()]
def devices(): """ return dict with names and ids of sound devices for acquisition (aka microphones) """ result=dict() if HAS_SOUNDCARD: for m in soundcard.all_microphones(): result[m.name]=m.id return result
def get_microphones() -> List[str]: """ :return: All microphone names, including loopbacks, plus DEFAULT_SOUND_NAME. """ l = [DEFAULT_SOUND_NAME] for m in sc.all_microphones(include_loopback=True): l.append(m.name) return l
def __init__(self): self.history = LastAvg(HISTORY_LENGTH) self.mic = sc.all_microphones(True)[0] self.old_light_value = 0 with self.mic.recorder(samplerate=48000, channels=1) as rec: while True: data = rec.record(numframes=2**10) self.calculate(data)
def list_captures(self): debug_prefix = "[MMVShadersCLI.list_captures]" logging.info(f"{debug_prefix} Available devices to record audio:") for index, device in enumerate( soundcard.all_microphones(include_loopback=True)): logging.info(f"{debug_prefix} > ({index}) [{device.name}]") logging.info( f"{debug_prefix} :: Run [realtime] command with argument --cap N")
def __init__(self): # get a list of all speakers: speakers = sc.all_speakers() # get the current default speaker on your system: self.default_speaker = sc.default_speaker() # get a list of all microphones: mics = sc.all_microphones() # get the current default microphone on your system: self.default_mic = self.__get_build_in_mic(mics)
def __init__(self, topic, interface_no, freq_buffer=5 * samplingFreq, bootstrap='localhost:9092'): self.freq_buffer = freq_buffer self.loopback = sc.all_microphones(True)[interface_no] self.producer = KafkaProducer(bootstrap_servers=bootstrap, client_id='app', api_version=(0, 10, 1)) self.topic = topic
def find_loopback_port(): r, pr = 0.0, 0.0 for m in sc.all_microphones(include_loopback=True): r = fuzz.ratio(LOOPBACK_NAME, m.name) pr = fuzz.ratio(LOOPBACK_NAME, m.name) print("{} : {} {}".format(m.name, r, pr)) if r > FUZZ_MATCHING and pr > FUZZ_MATCHING: return m logger.warning("could not find loopback port: {} {}".format(r, pr)) return None
def realtime( self, window_class: str = typer.Option( "glfw", help= "ModernGL Window backend to use, see [https://moderngl-window.readthedocs.io/en/latest/guide/window_guide.html], values are [sdl2, pyglet, glfw, pyqt5], GLFW works dynshader mode so I advise that. Please install the others if you wanna use them [poetry add / pip install pysdl2, pyqt5 etc]" ), cap: int = typer.Option( None, help= "Capture device index to override first loopback we find. Run command [list-captures] to see available indexes, None (empty) is to get automatically" )): debug_prefix = "[MMVShadersCLI.realtime]" self.__mgl_target_render_settings() self.mode = "view" # # Audio source Real Time self.audio_source = self.mmv_package_interface.get_audio_source_realtime( ) # Configure audio source self.audio_source.configure(batch_size=self._audio_batch_size, sample_rate=self._sample_rate, recorder_numframes=None, do_calculate_fft=True) # Search for loopback or get index of recorder device if (cap is None): self.audio_source.init(search_for_loopback=True) else: for index, device in enumerate( soundcard.all_microphones(include_loopback=True)): if index == cap: self.audio_source.init(recorder_device=device) self.__configure_audio_processing() self.__load_preset() repo_dir = self.mmv_package_interface.MMV_PACKAGE_ROOT / ".." / ".." / "repo" # Start mgl window self.mgl.mode(window_class=window_class, vsync=False, msaa=self._msaa, strict=False, icon=repo_dir / "icon.png") # Load master shader self.__load_master_shader() # Start reading data self.audio_source.start_async() self._core_loop()
def get_input_device_list(): """Return tuple containing: list of audio devices (tuples of SoundCard Device instance and name), index of default input device on this list, index of default loopback device on this list """ devices = [(mic, mic.name) for mic in soundcard.all_microphones(include_loopback=True)] try: default_output_name = soundcard.default_speaker().name default_loopback = next( (mic for mic in soundcard.all_microphones(include_loopback=True) if mic.isloopback and default_output_name in mic.name), None) except RuntimeError: default_loopback = None try: default_input = soundcard.default_microphone() except RuntimeError: default_input = None return devices, _find_device_on_list(default_input, devices), \ _find_device_on_list(default_loopback, devices)
def print_all_devices() -> None: print("speakers") spk_all = sc.all_speakers() spk_default = sc.default_speaker() for spk in spk_all: prefix = "*" if str(spk) == str(spk_default) else " " print(f"{prefix} {spk.name} id: {spk.id}") print("microphones") mic_all = sc.all_microphones() mic_default = sc.default_microphone() for mic in mic_all: prefix = "*" if str(mic) == str(mic_default) else " " print(f"{prefix} {mic.name} id: {mic.id}")
def __init__(self, rate=44100, period=1024, *args, **kwargs): threading.Thread.__init__(self) self.daemon = True self._s_lock = threading.Lock() Sampler.__init__(self, rate, period, *args, **kwargs) # Query for which card/device to use selections = sc.all_microphones(include_loopback=True) for i, s in enumerate(selections): print("{}: {}".format(i, s)) dev = int(input("Please enter the number of the device to use: ")) dev_name = selections[dev].name # Instantiate our Soundcard mixin self._mixin = sc.get_microphone(id=dev_name, include_loopback=True)
def main(): """ Main program """ soundcardlist = sc.all_microphones(include_loopback=True) args = parse_args(soundcardlist) pixels = int(args.pixels) if args.list is True: i = 0 print("Default\t| Index\t| Name\n" + "-" * 50) while i < len(soundcardlist): if soundcardlist[i].id == sc.default_speaker().id: print(" X\t|", str(i) + "\t|", soundcardlist[i].name) else: print("\t|", str(i) + "\t|", soundcardlist[i].name) i += 1 raise Exception elif args.ip is None: print("IP address required, use --help") raise Exception elif args.brightness > 100: print("Brightness cannot be above 100%") raise Exception if args.id is None: if str(sc.default_speaker().id) is None: print("No default speaker provided by OS, please use --list") raise Exception deviceid = str(sc.default_speaker().id) else: deviceid = str(args.id) start_sequence( deviceid=deviceid, sampleRate=48000, fps=args.fps, brightness=args.brightness, defaultframes=args.frames, pixels=pixels, multi=args.multi, rr=args.rr, rl=args.rl, ip=args.ip, )
def test(self): self.sound_cards = soundcard.all_speakers() self.mics = soundcard.all_microphones() if len(self.sound_cards) > 0: self.sound_card_present = True self.default_sound_card = str(soundcard.default_speaker()).replace( "<", "").replace(">", "") else: self.sound_card_present = False self.default_sound_card = "No default sound card found. May not be enabled or plugged in." if len(self.mics) > 0: self.mic_present = True self.default_mic = str(soundcard.default_microphone()).replace( "<", "").replace(">", "") else: self.mic_present = False self.default_mic = "No default mic found. May not be enabled or plugged in." return self
def main(RED, GREEN): channel = CHANNEL samplerate = SAMPLERATE numframes = NUMFRAMES T = 1.0 / samplerate x = np.linspace(0.0, numframes * T, numframes) xf = np.linspace(0.0, 1.0 / (2.0 * T), numframes // 2) input = sc.all_microphones()[MIC_NUM] print(input) with input.recorder(samplerate) as mic: for i in range(200000): y = mic.record(numframes) yf = np.fft.fft(y[:, channel]) ampl = 2.0 / numframes * np.abs(yf[10]) print(ampl) play(ampl, RED, GREEN, sys.argv)
def normalize_loudness(dev_l, dev_r): global mic_data pulse = pulsectl.Pulse('SplitSpkr') devices = [dev_l, dev_r] pulse_devices = get_pulse_speakers(pulse, devices) mic = sc.default_microphone() for m in sc.all_microphones(): if m.name == "USB PnP Audio Device Analog Mono": mic = m sample_volume(devices, mic) sample_volume(devices, mic) l_vol = mic_data[0] r_vol = mic_data[1] print(mic_data) if l_vol > r_vol: pulse.volume_set_all_chans(pulse_devices[0], \ (r_vol/l_vol)) else: pulse.volume_set_all_chans(pulse_devices[1], \ (l_vol/r_vol)) sample_volume(devices, mic) print(mic_data) return [dev_l, dev_r]
def start_mixer(): all_speakers = sc.all_speakers() (spkr1, spkr2) = filter(speaker_name_matcher, all_speakers) all_inputs = sc.all_microphones() (mic1, mic2) = filter(mic_name_matcher, all_inputs) input1 = mic1.recorder(samplerate=SAMPLE_RATE, blocksize=BLOCK_SIZE) input1.__enter__() input2 = mic2.recorder(samplerate=SAMPLE_RATE, blocksize=BLOCK_SIZE) input2.__enter__() lb = find_loopback_port() opendsh = lb.recorder(samplerate=SAMPLE_RATE, blocksize=BLOCK_SIZE) opendsh.__enter__() output1 = spkr1.player(samplerate=SAMPLE_RATE) output1.__enter__() output2 = spkr2.player(samplerate=SAMPLE_RATE) output2.__enter__() player = None playing_count = 0 while True: playing = None i1 = input1.record(numframes=NUM_FRAMES) i2 = input2.record(numframes=NUM_FRAMES) od = opendsh.record(numframes=NUM_FRAMES) if player == Inputs.CHANNEL1 and playing_count < MIN_PLAYING_COUNT: output1.play(i1) output2.play(i1) playing = Inputs.CHANNEL1 elif player == Inputs.CHANNEL2 and playing_count < MIN_PLAYING_COUNT: output1.play(i2) output2.play(i2) playing = Inputs.CHANNEL2 elif player == Inputs.OPENDSH and playing_count < MIN_PLAYING_COUNT: output1.play(od) output2.play(od) playing = Inputs.OPENDSH elif not is_silent(i1): output1.play(i1) output2.play(i1) playing = Inputs.CHANNEL1 elif not is_silent(i2): output1.play(i2) output2.play(i2) playing = Inputs.CHANNEL2 else: output1.play(od) output2.play(od) playing = Inputs.OPENDSH if playing != player: logger.debug("switching to player: {}".format(playing)) player = playing playing_count = 0 else: playing_count += 1
) my_parser.add_argument( '--topic', help='Kafka topic to publish messages ( default: audio ).', default='audio', type=str) my_parser.add_argument('--interfaceNo', help='The number of the audio interface to sample.', type=int) my_parser.add_argument( 'mode', help= '`stream` to start streaming, `listInterfaces` to list all available interfaces', default='stream', choices=['stream', 'listInterfaces']) args = my_parser.parse_args() if args.mode == 'listInterfaces': print("Default speaker is: {}\n".format(sc.default_speaker())) print("Available audio interfaces are:") for num, name in enumerate(sc.all_microphones(True), start=0): print('{}: {}'.format(num, name)) else: print("ctrl+c to interrupt") Sampler(args.topic, args.interfaceNo).record() # # sampler = Sampler("audio") # sampler.record()
buffer[:] = array(unpack('f' * bufferSize, data)) mfccBuffer = np.zeros([numberBands]) reset(vectorInput) run(vectorInput) mfccBuffer = np.roll(mfccBuffer, -patchSize) mfccBuffer = pool['mfcc'][-patchSize] features = mfccBuffer features = features.tolist() return features def tf_handler(args): headers = {"content-type": "application/json"} data = {"instances": [args]} r = requests.post(url = "http://localhost:8551/v1/models/improv_class:predict", data=json.dumps(data), headers=headers) response = r.json() data = response["predictions"] client.send_message("/clase", *data) clases=data[0] event = max(clases) index = clases.index(event) print ("Clase Predominante", index) print(data) # capture and process the speakers loopback # the 2 selects the external interface Zoom h5 #3 for jack with sc.all_microphones(include_loopback=True)[3].recorder(samplerate=sampleRate) as mic: while True: tf_handler(callback(mic.record(numframes=bufferSize).mean(axis=1)) ) #print ('\n', prediction)
""" konwersja tablicy data """ def convert_sound_data(data): max_amplitude = 2**15 - 1 data = max_amplitude * data data = data.astype(np.int16).tostring() return data speakers = sc.all_speakers() default_speaker = sc.default_speaker() mics = sc.all_microphones() default_mic = sc.default_microphone() amp = 100 #wzmocnienie samplerate = 48000 #f_probkowania channels = 2 numframes = 150000 #liczba_probek chanel_left = np.array([[1, 0]]) chanel_right = np.array([[0, 1]]) print(default_mic) print(default_speaker) print('start nagrywania \n') data = default_mic.record(samplerate=samplerate,
def get_interface(cfo): _test0 = soundcard.all_speakers() _test1 = soundcard.all_microphones() sp = soundcard.get_speaker(cfo.OUT_CARD_NAME) mc = soundcard.get_microphone(cfo.IN_CARD_NAME) return sp, mc
def test_microphones(): for microphone in soundcard.all_microphones(): assert isinstance(microphone.name, str) assert hasattr(microphone, 'id') assert isinstance(microphone.channels, int) assert microphone.channels > 0
def select_audio_mixers(self): self._input_mixer = sc.all_speakers()[0] if len( sc.all_speakers()) > 0 else None self._output_mixer = sc.all_microphones()[0] if len( sc.all_microphones()) > 0 else None
def turn_off(_LED): _LED.on() channel = 1 turn_on(red_bulb) samplerate = 48000 numframes = 4800 T = 1.0 / samplerate x = np.linspace(0.0, numframes * T, numframes) xf = np.linspace(0.0, 1.0 / (2.0 * T), numframes // 2) input = sc.all_microphones()[3] print(input) prev = 0 with input.recorder(samplerate) as mic: for i in range(2000): y = mic.record(numframes) yf = np.fft.fft(y[:, channel]) # print(xf[10]) num = 2.0 / numframes * np.abs(yf[10]) # print(1/numframes) print(num) if num > 0.015: turn_on(red_bulb) if num < 0.015: turn_off(red_bulb)
import soundcard as sc import struct BLOCKSIZE = 512 NUMFRAMES = 256 mics = sc.all_microphones(include_loopback=True) mic = mics[5] print(mic) recorder = mic.recorder(samplerate=48000, blocksize=BLOCKSIZE) recorder.__enter__() spkrs = sc.all_speakers() spkr = spkrs[0] print(spkr) player = spkr.player(samplerate=48000, blocksize=BLOCKSIZE) player.__enter__() while True: data = recorder.record(numframes=NUMFRAMES) player.play(data) recorder.__exit__() player.__exit__()
def get_cards(self): return sc.all_microphones()