-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpp_audiomanager.py
147 lines (118 loc) · 4.63 KB
/
pp_audiomanager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import configparser
import os
import subprocess
class AudioManager(object):
config=None
profile_names=('hdmi','hdmi0','hdmi1','A/V','local','USB','USB2','bluetooth','default')
sink_map={}
audio_sys= ''
#called for every class that uses it.
def __init__(self):
return
#called once at start
def init(self,pp_dir):
AudioManager.sink_map={}
status,message=self.read_config(pp_dir)
if status=='error':
return status,message
status,message,val=self.read_audio_sys()
if status=='error':
return status,message
AudioManager.audio_sys=val
#print(AudioManager.audio_sys)
if AudioManager.audio_sys == 'pulse':
status,message=self.read_sinks()
if status=='error':
return status,message
#print (self.get_sink('hdmi'))
return 'normal','audio.cfg read'
def get_audio_sys(self):
return AudioManager.audio_sys
def get_sink(self,name):
if name =='':
return 'normal','',''
if name in AudioManager.sink_map:
return 'normal','',AudioManager.sink_map[name]
else:
return 'error',name+' not in audio.cfg',''
def sink_connected(self,sink):
if sink=='':
return True
#command=['pacmd','list-sinks']
command=['pactl','list','short','sinks']
l_reply=subprocess.run(command,stdout=subprocess.PIPE)
l_reply_utf=l_reply.stdout.decode('utf-8')
#print (l_reply_utf)
if sink in l_reply_utf:
return True
else:
return False
# ****************************
# configuration
# ****************************
def read_audio_sys(self):
if not self.section_in_config('system'):
return 'error','no system section in audio.cfg',''
if not self.item_in_config('system','audio-system'):
return 'error','no audio-system in audio.cfg',''
val=self.get_item_in_config('system','audio-system').lower()
if val not in ('pulse','alsa','cset'):
return 'error','unknown audio-system in audio.cfg - '+ val,''
return 'normal','',val
def read_sinks(self):
if not self.section_in_config('pulse'):
return 'error','no pulse section in audio.cfg'
for name in AudioManager.profile_names:
if not self.item_in_config('pulse',name):
return 'error','audio device name not in audio.cfg - '+name
val=self.get_item_in_config('pulse',name)
AudioManager.sink_map[name]=val
#print (AudioManager.sink_map)
return 'normal',''
# read pp_audio.cfg
def read_config(self,pp_dir):
filename=pp_dir+os.sep+'pp_config'+os.sep+'pp_audio.cfg'
if os.path.exists(filename):
AudioManager.config = configparser.ConfigParser(inline_comment_prefixes = (';',))
AudioManager.config.read(filename)
return 'normal','pp_audio.cfg read OK'
else:
return 'error',"Failed to find audio.cfg at "+ filename
def section_in_config(self,section):
return AudioManager.config.has_section(section)
def get_item_in_config(self,section,item):
return AudioManager.config.get(section,item)
def item_in_config(self,section,item):
return AudioManager.config.has_option(section,item)
class PiPresents(object):
def init(self):
self.am=AudioManager()
path='/home/pi/pipresents'
status,message=self.am.init(path)
if status=='error':
print (message)
exit(0)
return
#print (self.am.sink_connected('alsa_output.platform-bcm2835_audio.digital-stereo'))
def info(self):
audio_sys=self.am.get_audio_sys()
print ('\nAudio System: '+audio_sys)
if audio_sys=='pulse':
print ('\nDevices:')
print ('%-10s%-5s%-50s ' % ('Name','Connected',' Sink Name'))
for name in AudioManager.profile_names:
sink= self.am.get_sink(name)[2]
if sink=='':
sink='sink not defined, default device will be used '
connected = ' '
else:
conn= self.am.sink_connected(sink)
if conn:
connected='yes'
else:
connected ='No'
print ('%-10s%-6s%-50s ' % (name,connected,sink))
if __name__ == '__main__':
pp=PiPresents()
pp.init()
pp.info()