Skip to content

Commit

Permalink
Merge pull request #1 from glmnet/windows-volume-control
Browse files Browse the repository at this point in the history
windows set volume by pid
  • Loading branch information
TheSpookyCat authored Jul 11, 2021
2 parents 009e307 + 193a263 commit 8c19080
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ap2-receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from ap2.connections.audio import RTPBuffer
from ap2.playfair import PlayFair
from ap2.utils import get_volume, set_volume
from ap2.utils import get_volume, set_volume, set_volume_pid
from ap2.pairing.hap import Hap, HAPSocket
from ap2.connections.event import Event
from ap2.connections.stream import Stream
Expand Down Expand Up @@ -401,6 +401,7 @@ def do_SETUP(self):
print("Sending CONTROL/DATA:")
buff = 8388608 # determines how many CODEC frame size 1024 we can hold
stream = Stream(plist["streams"][0], buff)
set_volume_pid(stream.data_proc.pid)
self.server.streams.append(stream)
sonos_one_setup_data["streams"][0]["controlPort"] = stream.control_port
sonos_one_setup_data["streams"][0]["dataPort"] = stream.data_port
Expand Down
14 changes: 10 additions & 4 deletions ap2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ISimpleAudioVolume = None
print('[!] Pycaw is not installed - volume control will be unavailable', )


def get_logger(name, level="INFO"):
logging.basicConfig(
filename="%s.log" % name,
Expand Down Expand Up @@ -55,21 +55,27 @@ def interpolate(value, from_min, from_max, to_min, to_max):

return to_min + (value_scale * to_span)



audio_pid = 0

def set_volume_pid(pid):
global audio_pid
audio_pid = pid

def get_pycaw_volume_session():
if platform.system() != 'Windows' or AudioUtilities is None:
return
session = None
for s in AudioUtilities.GetAllSessions():
try:
if s.Process.name() == 'python.exe':
if s.Process.pid == audio_pid:
session = s._ctl.QueryInterface(ISimpleAudioVolume)
break
except AttributeError:
pass
return session


def get_volume():
subsys = platform.system()
if subsys == "Darwin":
Expand Down

0 comments on commit 8c19080

Please sign in to comment.