-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstartaudio.py
275 lines (224 loc) · 8.47 KB
/
startaudio.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# This file is part of pre.di.c
# pre.di.c, a preamp and digital crossover
# Copyright (C) Roberto Ripio
"""
Starts pre.di.c audio system.
Usage:
startaudio.py [ core | clients | all ] (default 'all')
core: jack, brutefir, server
players: everything else (players and clients)
all: all of the above
"""
import sys
import os
import time
import re
import subprocess as sp
import init
import stopaudio
import pdlib as pd
from camilladsp import CamillaClient
port = init.config['control_port']
def stop_all():
"""Stop all audio and the current script."""
stopaudio.main('all')
sys.exit()
def init_jack():
"""Load jack server."""
try:
print('\n(startaudio) starting jack\n')
fs = init.speaker['devices']['samplerate']
sp.Popen(f'{init.config["jack_command"]} -r {fs}'.split())
# waiting for jackd:
tmax = init.config['command_delay'] * 5
interval = init.config['command_delay'] * 0.1
if pd.wait4result('jack_lsp', 'system', tmax, interval):
print('\n(startaudio) jack started :-)')
else:
raise Exception('no ports available')
stop_all()
except Exception as e:
print('\n(startaudio) error starting jack: ', e)
stop_all()
def init_camilladsp():
"""Load camilladsp."""
try:
# 'cd' to louspeaker folder so filter paths are relative to this
# folder in speaker.yml config file.
os.chdir(init.loudspeaker_path)
print(f'\n(startaudio) starting camilladsp on {os.getcwd()}')
sp.Popen((f'{init.config["camilladsp_command"]} -m -w ' +
f'-p {init.config["websocket_port"]}').split())
# Wait a bit.
time.sleep(init.config['command_delay'] * 1)
# Connect to camilladsp.
cdsp = CamillaClient("localhost", init.config['websocket_port'])
cdsp.connect()
# Get general part of camilladsp config.
cdsp_config = init.camilladsp
# Get loudspeaker specific parts of camilladsp config.
# Merge loudspeaker specific settings in camilladsp config.
cdsp_config['title'] = init.speaker['title']
cdsp_config['description'] = init.speaker['description']
cdsp_config['devices'].update(init.speaker['devices'])
cdsp_config['filters'].update(init.speaker['filters'])
cdsp_config['mixers'].update(init.speaker['mixers'])
cdsp_config['pipeline'].extend(init.speaker['pipeline'])
# Send full config to camilladsp.
# Probably we should check for jack ports here...
cdsp.config.set_active(cdsp_config)
cdsp.disconnect()
except Exception as e:
print('\n(startaudio) error starting camilladsp: ', e)
stop_all()
def init_server():
"""Load server."""
print('\n(startaudio) starting server')
try:
sp.Popen((f'{init.config["python_command"]} ' +
f'{init.main_folder}/server.py').split())
# Waiting for server.
tmax = init.config['command_delay'] * 5
interval = init.config['command_delay'] * 0.1
if pd.wait4result(
'echo ping| nc localhost 9999 2>/dev/null',
'OK', tmax, interval):
print('\n(startaudio) server started :-)')
else:
print('\n(startaudio) server not accesible Bye :-/')
stop_all()
except Exception as e:
print('\n(startaudio) server didn\'t load: ', e)
stop_all()
def set_initial_state():
"""Set initial state state as last saved or user determined."""
state = init.state
if init.config['use_state_init']:
for setting in init.state_init:
state[setting] = init.state_init[setting]
return state
def init_state_settings(state):
"""Restore audio settings from state.yaml."""
"""
Source and mute are not restored.
Take care of options to reset some of them.
It is assumed that command name and setting name are the same.
Source associated phase_EQ will prevail if use_source_phase_EQ is set
because init_source function is executed after this one.
"""
for setting in (
'balance',
'bass',
'channels',
'channels_flip',
'clamp',
'drc',
'drc_set',
'eq',
'eq_filter',
'level',
'loudness',
'loudness_ref',
'phase_eq',
'polarity',
'polarity_flip',
'solo',
'stereo',
'tones',
'treble'
):
print(f'{setting} {state[setting]}')
pd.client_socket(f'{setting} {state[setting]}', port)
def init_source(state):
"""Restore selected source as stored in state.yml."""
source = state['source']
print(f"\n(startaudio) restoring source '{source}'")
# Wait for source ports to be up.
tmax = init.config['command_delay'] * 15
interval = init.config['command_delay'] * 0.5
if pd.wait4source(source, tmax, interval):
# Source ports up and ready :-)
# Switch on source and leave function.
if init.sources[source]['wait_on_start']:
# Some clients (namely mpd) seems to need some extra time after
# ports detection for whatever reason.
time.sleep(init.config['command_delay'] * 2)
else:
print(f"\n(startaudio) could not connect '{source}' ports")
# Disconnect sources from eventual bad behaving clients.
pd.client_socket('sources off', port, quiet=True)
# Actual source connection.
pd.client_socket('sources on', port, quiet=True)
def main(run_level):
"""Start loading function."""
# jack, brutefir, camilladsp, server
if run_level in {'core', 'all'}:
init_jack()
# camillaDSP:
# Hack to care for unconsistent port name creation upstream.
# Get camilladsp port names:
# Join all music output ports in a list:
ports = sum(init.config["audio_ports"], [])
# Filter camillaDSP ports:
ports = [x for x in ports if re.match("cpal_client_in.*", x)]
count = 0
while count < 5:
init_camilladsp()
if pd.wait4ports(ports):
break
else:
sp.Popen('pkill -fe camilladsp'.split())
count += 1
init_server()
# Inboard players.
if run_level in {'clients', 'all'}:
# Getting operating state. Do it before launching clients
# so they get the correct setting from state file if needed.
state = set_initial_state()
# Activate command_unmute mode downstream.
pd.client_socket('command_unmute', port, quiet=True)
# Restoring previous state.
# Exceptionally we add a line feed at the end
# since settings restoring messages don't.
print('\n(startaudio): restoring previous settings:\n')
init_state_settings(state)
# Write source to state file for use of clients if config ask for it.
if state['sources'] == 'on':
# Just refresh state file.
init.state['source'] = state['source']
pd.client_socket('save', port, quiet=True)
# Launch external clients.
# Exceptionally we add a line feed at the end
# since client load messages don't.
print('\n(startaudio): starting clients...\n')
for client in pd.read_clients('start'):
try:
p = sp.Popen(client.split())
print(f'pid {p.pid:4}: {client}')
except Exception as e:
print(f"\n(startaudio) problem launching client '{client}':",
e)
# Restoring sources if config mandates so.
if state['sources'] == 'on':
init_source(state)
# Restore mute state.
pd.client_socket(f'mute {state["mute"]}', port)
print(f'mute {state["mute"]}')
# Cancel command_unmute mode downstream.
# Restoring config value.
if init.config['do_mute']:
pd.client_socket('command_mute', port, quiet=True)
# Save changes to file.
pd.client_socket('save', port, quiet=True)
print('\n(startaudio): pre.di.c started :-)')
if __name__ == '__main__':
# Switch runlevels.
if sys.argv[1:]:
run_level = sys.argv[1]
else:
run_level = 'all'
if run_level in {'core', 'clients', 'all'}:
print(f'\n(startaudio) starting runlevel {run_level}')
main(run_level)
else:
print(__doc__)