-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioController.py
62 lines (54 loc) · 2.78 KB
/
AudioController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Per session GetMute() SetMute() GetMasterVolume() SetMasterVolume() using SimpleAudioVolume.
"""
from __future__ import print_function
from pycaw.pycaw import AudioUtilities
class AudioController(object):
def __init__(self, process_name):
self.process_name = process_name
self.volume = self.process_volume()
def mute(self):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
interface = session.SimpleAudioVolume
if session.Process and session.Process.name() == self.process_name:
interface.SetMute(1, None)
print(self.process_name, 'has been muted.') # debug
def unmute(self):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
interface = session.SimpleAudioVolume
if session.Process and session.Process.name() == self.process_name:
interface.SetMute(0, None)
print(self.process_name, 'has been unmuted.') # debug
def process_volume(self):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
interface = session.SimpleAudioVolume
if session.Process and session.Process.name() == self.process_name:
print('Volume:', interface.GetMasterVolume()) # debug
return interface.GetMasterVolume()
def set_volume(self, decibels):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
interface = session.SimpleAudioVolume
if session.Process and session.Process.name() == self.process_name:
self.volume = min(1.0, max(0.0, decibels)) # only set volume in the range 0.0 to 1.0
interface.SetMasterVolume(self.volume, None)
print('Volume set to', self.volume) # debug
def decrease_volume(self, decibels):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
interface = session.SimpleAudioVolume
if session.Process and session.Process.name() == self.process_name:
self.volume = max(0.0, self.volume-decibels) # 0.0 is the min value, reduce by decibels
interface.SetMasterVolume(self.volume, None)
#print('Volume reduced to', self.volume) # debug
def increase_volume(self, decibels):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
interface = session.SimpleAudioVolume
if session.Process and session.Process.name() == self.process_name:
self.volume = min(1.0, self.volume+decibels) # 1.0 is the max value, raise by decibels
interface.SetMasterVolume(self.volume, None)
#print('Volume raised to', self.volume) # debug