#!/usr/bin/env python3 from device import ArctisNova from blessed import Terminal from PIL import Image,ImageDraw from pymemcache.client.base import Client import json import time import pulsectl term = Terminal() headphones = ArctisNova() im = Image.new("1",(128,64)) draw = ImageDraw.Draw(im) count = 0 cache = Client("localhost") def printData(): with term.location(0,term.height-4): print(f'{term.clear_eol}BT Default: {headphones.bt_default} BT Mute: {headphones.bt_auto_mute} BT Power: {headphones.bt_power} BT Connection: {headphones.bt_connection} Battery: {headphones.headset_battery} Charge: {headphones.charge_battery}') print(f'{term.clear_eol}Noise Cancel: Enabled {headphones.noise_cancel} level {headphones.noise_cancel_level} Muted: {headphones.muted} Mic LED: {headphones.mic_led} Auto-off Timeout: {headphones.auto_off}') print(f'{term.clear_eol}Wireless Mode: {headphones.wireless_mode} Pairing state: {headphones.pairing} Headset Status: {headphones.headset_status}', end = "") def getModuleSink(pulse,index): res = 0 for dev in pulse.sink_list(): if dev.owner_module == index: res = dev.index return res def getSinkDevice(pulse,vid,pid): res = 0 for dev in pulse.sink_list(): if "device.product.id" in dev.proplist: if dev.proplist["device.product.id"] == pid and dev.proplist["device.vendor.id"] == vid: res = dev.index return res def getLoopbackSink(pulse,loopback): res = 0 for dev in pulse.sink_input_list(): if dev.owner_module == loopback: res = dev.index return res def getLoopbackSource(pulse,loopback): res = 0 for dev in pulse.source_output_list(): if dev.owner_module == loopback: res = dev.index return res with term.fullscreen(), term.scroll_region(height=term.height - 5), headphones, pulsectl.Pulse('headphone_controller') as pulse: print(term.home + term.clear) data = [0x01] ignoredSinks = [] cache.set("client.headphone_manager",json.dumps(["ignored_audio"]),60) res = False while not res: val,b = cache.gets("clients") if val: existingVal = json.loads(val) if "headphone_manager" not in existingVal: existingVal.append("headphone_manager") val = json.dumps(existingVal) res = cache.cas("clients",val,b) else: val = json.dumps(["headphone_manager"]) res = cache.set("clients",val) cache.touch("client.headphone_manager",60) try: headphones.chat_mixer(True) headphones.drawImage(im) gameNullModule = pulse.module_load("module-null-sink","sink_name=GAME_AUDIO") gameNullSink = getModuleSink(pulse,gameNullModule) gameNullSource = pulse.sink_info(gameNullSink).monitor_source chatNullModule = pulse.module_load("module-null-sink","sink_name=CHAT_AUDIO") chatNullSink = getModuleSink(pulse,chatNullModule) chatNullSource = pulse.sink_info(chatNullSink).monitor_source headphonesSink = getSinkDevice(pulse,"0x1038","0x12e0") loopbackChatModule = pulse.module_load("module-loopback","channels=2") loopbackChatSink = getLoopbackSink(pulse,loopbackChatModule) loopbackChatSource = getLoopbackSource(pulse,loopbackChatModule) ignoredSinks.append(loopbackChatSink) loopbackGameModule = pulse.module_load("module-loopback","channels=2") loopbackGameSink = getLoopbackSink(pulse,loopbackGameModule) loopbackGameSource = getLoopbackSource(pulse,loopbackGameModule) ignoredSinks.append(loopbackGameSink) time.sleep(1) pulse.sink_default_set(pulse.sink_info(gameNullSink)) pulse.sink_input_move(loopbackGameSink,headphonesSink) pulse.sink_input_move(loopbackChatSink,headphonesSink) pulse.source_output_move(loopbackGameSource,gameNullSource) pulse.source_output_move(loopbackChatSource,chatNullSource) cache.set("ignored_audio.headphone_manager",json.dumps([loopbackChatSink,loopbackGameSink])) while data: data = headphones.readData() headphones.updateData() while True: if count > 100: count = -1 headphones.drawImage(im) elif count>0: count += 1 data = headphones.readData() data2 = headphones._dev_output.read(16,1) printData() if(data): print([hex(x) for x in data]) count = 1 if(data2): print("data2") print([hex(x) for x in data2]) if headphones.update: gameSinkDev = pulse.sink_info(gameNullSink) chatSinkDev = pulse.sink_info(chatNullSink) vol = headphones.game_volume/100 if vol >= 0: pulse.volume_set_all_chans(gameSinkDev,vol) vol = headphones.chat_volume/100 if vol >= 0: pulse.volume_set_all_chans(chatSinkDev,vol) headphones.update = False time.sleep(0.1) cache.touch("client.headphone_manager",60) except Exception as e: pulse.module_unload(loopbackGameModule) pulse.module_unload(loopbackChatModule) pulse.module_unload(chatNullModule) pulse.module_unload(gameNullModule) headphones.chat_mixer(False) cache.delete("ignored_audio.headphone_manager") raise e except KeyboardInterrupt as e: pulse.module_unload(loopbackGameModule) pulse.module_unload(loopbackChatModule) pulse.module_unload(chatNullModule) pulse.module_unload(gameNullModule) headphones.chat_mixer(False) cache.delete("ignored_audio.headphone_manager") raise e