diff --git a/README.md b/README.md index e8dfbbe..eb5084f 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ Audio I/O for real-time applications, like VoIP. - `aspew-in.py` takes a microphone as an input, and outputs it to pipe. - `aspew-out.py` takes input from a pipe, and outputs it to a speaker. +## Flags +Flags formatted as `-s` or `--long`. + +### in & out: +- `-l`: List devices as JSON +- `-h`: Use human-readable format + ## Arguments Arguments are pairs of key=value. @@ -32,4 +39,10 @@ Arguments are pairs of key=value. Listen to your default microphone at default settings. - `./aspew-in.py format=paInt16 bitrate=48000 channels=2 | ./aspew-out.py format=paInt16 bitrate=48000 channels=2` -Listen to your default microphone at 16-bit, 48000Hz, stereo. \ No newline at end of file +Listen to your default microphone at 16-bit, 48000Hz, stereo. + +- `./aspew-in.py -lh` +List your input devices. + +- `./aspew-out.py -lh` +List your output devices. \ No newline at end of file diff --git a/aspew-in.py b/aspew-in.py index c8f1d90..d5677ce 100755 --- a/aspew-in.py +++ b/aspew-in.py @@ -5,6 +5,25 @@ 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:]: @@ -25,6 +44,34 @@ 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) diff --git a/aspew-out.py b/aspew-out.py index e04ff86..50d741d 100755 --- a/aspew-out.py +++ b/aspew-out.py @@ -58,6 +58,25 @@ def streamHandler(in_data, frame_count, time_info, status): data += getAudioFrame() return (data, pyaudio.paContinue) +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:]: @@ -74,6 +93,34 @@ def getSetting(lst,tp,keys,default = None): return tp(lst[key]) return default +flags = parseFlags() + +if "l" in flags: + devices = {} + audio = pyaudio.PyAudio() + defaultDevice = audio.get_default_host_api_info()["defaultOutputDevice"] + devices["default"] = defaultDevice + for i in range(0, audio.get_device_count()): + info = audio.get_device_info_by_index(i) + if info["maxOutputChannels"] == 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 output 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)