Skip to content

Commit 8c19080

Browse files
authored
Merge pull request #1 from glmnet/windows-volume-control
windows set volume by pid
2 parents 009e307 + 193a263 commit 8c19080

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

ap2-receiver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from ap2.connections.audio import RTPBuffer
2222
from ap2.playfair import PlayFair
23-
from ap2.utils import get_volume, set_volume
23+
from ap2.utils import get_volume, set_volume, set_volume_pid
2424
from ap2.pairing.hap import Hap, HAPSocket
2525
from ap2.connections.event import Event
2626
from ap2.connections.stream import Stream
@@ -401,6 +401,7 @@ def do_SETUP(self):
401401
print("Sending CONTROL/DATA:")
402402
buff = 8388608 # determines how many CODEC frame size 1024 we can hold
403403
stream = Stream(plist["streams"][0], buff)
404+
set_volume_pid(stream.data_proc.pid)
404405
self.server.streams.append(stream)
405406
sonos_one_setup_data["streams"][0]["controlPort"] = stream.control_port
406407
sonos_one_setup_data["streams"][0]["dataPort"] = stream.data_port

ap2/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ISimpleAudioVolume = None
1414
print('[!] Pycaw is not installed - volume control will be unavailable', )
1515

16-
16+
1717
def get_logger(name, level="INFO"):
1818
logging.basicConfig(
1919
filename="%s.log" % name,
@@ -55,21 +55,27 @@ def interpolate(value, from_min, from_max, to_min, to_max):
5555

5656
return to_min + (value_scale * to_span)
5757

58-
58+
59+
audio_pid = 0
60+
61+
def set_volume_pid(pid):
62+
global audio_pid
63+
audio_pid = pid
64+
5965
def get_pycaw_volume_session():
6066
if platform.system() != 'Windows' or AudioUtilities is None:
6167
return
6268
session = None
6369
for s in AudioUtilities.GetAllSessions():
6470
try:
65-
if s.Process.name() == 'python.exe':
71+
if s.Process.pid == audio_pid:
6672
session = s._ctl.QueryInterface(ISimpleAudioVolume)
6773
break
6874
except AttributeError:
6975
pass
7076
return session
7177

72-
78+
7379
def get_volume():
7480
subsys = platform.system()
7581
if subsys == "Darwin":

0 commit comments

Comments
 (0)