This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
cadence_aloop_daemon.py
executable file
·219 lines (171 loc) · 7.21 KB
/
cadence_aloop_daemon.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Cadence ALSA-Loop daemon
# Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For a full copy of the GNU General Public License see the COPYING file
# ------------------------------------------------------------------------------------------------------------
# Imports (Global)
import os
import sys
from signal import signal, SIGINT, SIGTERM
from time import sleep
if True:
from PyQt5.QtCore import QProcess
else:
from PyQt4.QtCore import QProcess
# ------------------------------------------------------------------------------------------------------------
# Imports (Custom Stuff)
import jacklib
# --------------------------------------------------
# Auto re-activate if on good kernel
global reactivateCounter
reactivateCounter = -1
isKernelGood = [int(i) for i in os.uname()[2].split("-", 1)[0].split(".", 2)[:2]] >= [3,8]
# --------------------------------------------------
# Global loop check
global doLoop, doRunNow, useZita, procIn, procOut
doLoop = True
doRunNow = True
useZita = False
procIn = QProcess()
procOut = QProcess()
checkFile = "/tmp/.cadence-aloop-daemon.x"
# --------------------------------------------------
# Global JACK variables
global bufferSize, sampleRate, channels
bufferSize = 1024
sampleRate = 44100
channels = 2
# --------------------------------------------------
# quit on SIGINT or SIGTERM
def signal_handler(sig, frame=0):
global doLoop
doLoop = False
# --------------------------------------------------
# listen to jack buffer-size and sample-rate changes
def buffer_size_callback(newBufferSize, arg):
global doRunNow, bufferSize
bufferSize = newBufferSize
doRunNow = True
return 0
def sample_rate_callback(newSampleRate, arg):
global doRunNow, sampleRate
sampleRate = newSampleRate
doRunNow = True
return 0
# --------------------------------------------------
# listen to jack2 master switch
def client_registration_callback(clientName, register, arg):
global doLoop, doRunNow, reactivateCounter, useZita
if clientName in (b"alsa2jack", b"jack2alsa") and not register:
if doRunNow or not doLoop:
return
if isKernelGood:
if reactivateCounter == -1:
reactivateCounter = 0
print("NOTICE: %s has been stopped, waiting 5 secs to reactivate" % ("zita-a2j/j2a" if useZita else "alsa_in/out"))
elif doLoop:
doLoop = False
print("NOTICE: %s has been stopped, quitting now..." % ("zita-a2j/j2a" if useZita else "alsa_in/out"))
# --------------------------------------------------
# listen to jack shutdown
def shutdown_callback(arg):
global doLoop
doLoop = False
# --------------------------------------------------
# run alsa_in and alsa_out
def run_alsa_bridge():
global reactivateCounter
global bufferSize, sampleRate, channels
global procIn, procOut
global useZita
if procIn.state() != QProcess.NotRunning:
procIn.terminate()
procIn.waitForFinished(1000)
if procOut.state() != QProcess.NotRunning:
procOut.terminate()
procOut.waitForFinished(1000)
reactivateCounter = -1
if useZita:
procIn.start("env", ["JACK_SAMPLE_RATE=%i" % sampleRate, "JACK_PERIOD_SIZE=%i" % bufferSize, "zita-a2j", "-d", "hw:Loopback,1,0", "-r", "%i" % sampleRate, "-p", "%i" % bufferSize, "-j", "alsa2jack", "-c", "%i" % channels])
procOut.start("env", ["JACK_SAMPLE_RATE=%i" % sampleRate, "JACK_PERIOD_SIZE=%i" % bufferSize, "zita-j2a", "-d", "hw:Loopback,1,1", "-r", "%i" % sampleRate, "-p", "%i" % bufferSize, "-j", "jack2alsa", "-c", "%i" % channels])
else:
procIn.start("env", ["JACK_SAMPLE_RATE=%i" % sampleRate, "JACK_PERIOD_SIZE=%i" % bufferSize, "alsa_in", "-d", "cloop", "%i" % sampleRate, "-p", "%i" % bufferSize, "-j", "alsa2jack", "-q", "1", "-c", "%i" % channels])
procOut.start("env", ["JACK_SAMPLE_RATE=%i" % sampleRate, "JACK_PERIOD_SIZE=%i" % bufferSize, "alsa_out", "-d", "ploop", "%i" % sampleRate, "-p", "%i" % bufferSize, "-j", "jack2alsa", "-q", "1", "-c", "%i" % channels])
if procIn.waitForStarted():
sleep(1)
jacklib.connect(client, "alsa2jack:capture_1", "system:playback_1")
jacklib.connect(client, "alsa2jack:capture_2", "system:playback_2")
if procOut.waitForStarted():
sleep(1)
jacklib.connect(client, "system:capture_1", "jack2alsa:playback_1")
jacklib.connect(client, "system:capture_2", "jack2alsa:playback_2")
#--------------- main ------------------
if __name__ == '__main__':
for i in range(len(sys.argv)):
if i == 0: continue
argv = sys.argv[i]
if argv == "--zita":
useZita = True
elif argv.startswith("--channels="):
chStr = argv.replace("--channels=", "")
if chStr.isdigit():
channels = int(chStr)
# Init JACK client
client = jacklib.client_open("cadence-aloop-daemon", jacklib.JackUseExactName, None)
if not client:
print("cadence-aloop-daemon is already running, delete \"/tmp/.cadence-aloop-daemon.x\" to close it")
quit()
if jacklib.JACK2:
jacklib.set_client_registration_callback(client, client_registration_callback, None)
jacklib.set_buffer_size_callback(client, buffer_size_callback, None)
jacklib.set_sample_rate_callback(client, sample_rate_callback, None)
jacklib.on_shutdown(client, shutdown_callback, None)
jacklib.activate(client)
# Quit when requested
signal(SIGINT, signal_handler)
signal(SIGTERM, signal_handler)
# Get initial values
sampleRate = jacklib.get_sample_rate(client)
bufferSize = jacklib.get_buffer_size(client)
# Create check file
if not os.path.exists(checkFile):
os.mknod(checkFile)
# Keep running until told otherwise
firstStart = True
while doLoop and os.path.exists(checkFile):
if doRunNow:
if firstStart:
firstStart = False
print("cadence-aloop-daemon started, using %s and %i channels" % ("zita-a2j/j2a" if useZita else "alsa_in/out", channels))
run_alsa_bridge()
doRunNow = False
elif isKernelGood and reactivateCounter >= 0:
if reactivateCounter == 5:
reactivateCounter = -1
doRunNow = True
else:
reactivateCounter += 1
sleep(1)
# Close JACK client
jacklib.deactivate(client)
jacklib.client_close(client)
if os.path.exists(checkFile):
os.remove(checkFile)
if procIn.state() != QProcess.NotRunning:
procIn.terminate()
procIn.waitForFinished(1000)
if procOut.state() != QProcess.NotRunning:
procOut.terminate()
procOut.waitForFinished(1000)