arduino-mixer/python/start.py

95 lines
3.2 KiB
Python

from pycaw.pycaw import AudioUtilities
from pprint import pprint
import serial
import serial.tools.list_ports
import time
def main():
print ("Starting Arduino Mixer...\n")
# Get audio sessions
sessions = AudioUtilities.GetAllSessions()
print (str(len(sessions)) + " audio sessions found:")
for session in sessions:
print (" " + str(session))
# Init serial connection
ports = serial.tools.list_ports.comports()
if len(ports) == 0:
print("\nNo Serial ports Available")
exit()
else:
print ("\nSerial ports Available:")
for port, desc, hwid in sorted(ports):
print(" {}: {} [{}]".format(port, desc, hwid))
board = serial.Serial()
board.baudrate = 115200
board.port = input("\nSelect COM port: ")
board.open()
print ("Waiting for arduino...")
time.sleep(5)
board.write("<HELLO>".encode("utf-8"))
currentProgram = 0
line = []
newData = False
recvInProgress = False
startMarker = '60' # <
endMarker = '62' # >
while True:
for c in board.read():
if c == 62:
recvInProgress = False
newData = True
if recvInProgress == True:
line.append(chr(c))
if c == 60:
recvInProgress = True
break
if newData == True:
dataArray = ''.join(line).split(',')
command = dataArray[0]
if len(dataArray) > 1:
data = dataArray[1]
# Process data
# TODO: Listen for programs being added, then send this again
if command == "COUNT":
board.write(("<COUNT," + str(len(sessions)) + ">").encode())
elif command == "SWITCH":
currentProgram = int(data)
program = sessions[currentProgram]
volume = program.SimpleAudioVolume.GetMasterVolume()
if program.DisplayName == "@%SystemRoot%\System32\AudioSrv.Dll,-202":
name = "Systeem"
# TODO: This line is being overwritten somehow
elif session.Process and session.Process.name() == "firefox.exe":
name = "Firefox"
elif program.DisplayName != "":
name = str(program.DisplayName)
else:
name = str(session.Process and session.Process.name())
board.write(("<NAME," + name + ">").encode())
board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode())
elif command == "SETVOL":
audio = sessions[currentProgram].SimpleAudioVolume
audio.SetMasterVolume(float(data) / 100, None)
volume = audio.GetMasterVolume()
board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode())
line = []
newData = False
if __name__ == "__main__":
main()