#!/usr/bin/env python3 import sys import os import time import pyaudio unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) # Make unbuffered stdout def parseFlags(): rtn = [] for arg in sys.argv[1:]: arg = arg.replace(" ","") arg = arg.replace("\t","") arg = arg.replace("\r","") arg = arg.replace("\n","") if arg[0] != "-": continue if arg == "-": continue if arg[:2] == "--": rtn.append(arg[2:]) continue for i in arg[1:]: rtn.append(i) return rtn def parseSettings(): rtn = {} for arg in sys.argv[1:]: arg = arg.split("=",1) if len(arg) < 2: arg.append("") arg[0] = arg[0].strip(" \t\r\n") arg[1] = arg[1].strip(" \t\r\n") rtn[arg[0]] = arg[1] return rtn def getSetting(lst,tp,keys,default = None): for key in keys: if key in lst: return tp(lst[key]) return default def streamHandler(in_data, frame_count, time_info, status): unbufferedStdout.write(in_data) return (in_data, pyaudio.paContinue) flags = parseFlags() if "l" in flags: devices = {} audio = pyaudio.PyAudio() defaultDevice = audio.get_default_host_api_info()["defaultInputDevice"] devices["default"] = defaultDevice for i in range(0, audio.get_device_count()): info = audio.get_device_info_by_index(i) if info["maxInputChannels"] == 0: continue devices[i] = { "name": info["name"], "api": audio.get_host_api_info_by_index(info["hostApi"])["name"] } if not "h" in flags: import json print(json.dumps(devices)) else: print("\nAvailable input devices:") for i in devices: if i == "default": continue st = str(i)+ ": " +devices[i]["name"]+ " [" +devices[i]["api"]+ "]" if i == defaultDevice: st = st + " *" print(st) sys.exit(0) settings = parseSettings() audioFormat = getSetting(settings,str,["f","format"],"paUInt8") audioChannels = getSetting(settings,int,["c","channel","channels"],1) audioRate = getSetting(settings,int,["r","rate","bitrate"],8000) audioBuffer = getSetting(settings,float,["b","buffer","buffersize"],0.05) audioDevice = getSetting(settings,int,["d","device"],None) if audioDevice == -1: audioDevice = None audioFormat = getattr(pyaudio,audioFormat) audioBuffer = round(audioRate * audioBuffer) kwargs = { "input": True, "input_device_index": audioDevice, "format": audioFormat, "channels": audioChannels, "frames_per_buffer": audioBuffer, "rate": audioRate, "stream_callback": streamHandler } audio = pyaudio.PyAudio() stream = audio.open(**kwargs) try: while stream.is_active(): time.sleep(0.1) except KeyboardInterrupt: pass