-
Notifications
You must be signed in to change notification settings - Fork 5
/
README
343 lines (274 loc) · 12.3 KB
/
README
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
Note: This is currently a direct clone of the SourceForge project at http://py-jack.sourceforge.net/
Original readme follows.
------------------------
PyJack version 0.5.2
Original code by Andrew W. Schmeder <[email protected]>, in2003
Transport support by Il'dar Akhmetgaleev <akhilman at gmail dot com>, in Jan 14 2008
Revision and packaging by falkTX <[email protected]>, in Feb 2010
Implemented changes by the Clam Team <[email protected]>, in Mar 2010
More Jack implementations by falkTX <[email protected]>, in Apr 2010
This is open source software released under the GPL license.
The full text of this license is found in the file 'LICENSE',
included with this source code package.
This software is presently distributed from http://www.a2hd.com.
Please check that site for the latest version.
------------------------------------------------------------------------
------------------------------------------------------------------------
Description:
PyJack is a module written in C which exposes the Jack API to Python.
For information about Jack see http://jackit.sourceforge.net. This
enables a Python program to connect to and interact with pro-audio
applications which use the Jack Audio Server.
------------------------------------------------------------------------
Purpose:
- To show that it can be done.
- For programmers who want to prototype DSP and sound synthesis
algorithms using Numeric Python and similar tools. PyJack provides the
means to capture and playback audio.
- For patchbay applications; A powerful Jack patchbay can be written
in Python using this module. This is planned for the future.
------------------------------------------------------------------------
Installation:
This package uses the excellent and simple Python distutils.
Installation is very simple. It works something like this;
# tar -xzvf pyjack-0.*.tar.gz (unpack archive)
# cd pyjack-0.* (cd to source dir)
# (sudo) python setup.py install (install...)
------------------------------------------------------------------------
Demos:
Demos can be found in the "demos" directory
------------------------------------------------------------------------
Realtime Issues:
Python is known to be relatively slow (compared to C/C++),
and non-realtime due to memory management details. Because of this,
Python is -NOT- a suitable language for realtime audio processing!
This means that it is unacceptable to place the Python intepreter
"inside" of a Jack client.
PyJack therefore uses a "semi-detached" method. The PyJack
client consists of two threads; a Jack client thread and a Python
thread. The Jack client thread is run in realtime context; it never
blocks, it is entirely deterministic, and does not touch any Python
data structures nor the interpreter. The Jack client thread merely
copies audio data in/out of socket buffers. On the Python side,
calls to jack.process() copy audio data in/out of the other end of
those sockets providing the connection to Python via Numeric arrays
of floats. In any case, use of a large buffer size (e.g. 1024 samples)
is recommended.
In order to capture or playback audio without missing a block,
Python must call jack.process() at least once every 500*(N/Sr)
milliseconds. (N = jack.get_buffer_size(), Sr = jack.get_sample_rate()).
If this rate is not kept up, you may get jack.InputSyncError or
jack.OutputSyncError exceptions thrown by jack.process().
Typically you will want to use the following design for a DSP
prototyping program:
1. Attach to the jack server (jack.attach)
Create input and output ports (jack.register_port)
Connect inputs and outputs to jack graph (jack.connect)
Activate client (jack.activate)
2. Preallocate matricies for input and output audio data
3. Capture X seconds of audio (jack.process)
4. Process audio using your algorithm
It does not matter how long this takes...
5. Playback X seconds of audio (jack.process)
6. Detach from jack server (jack.detach)
See the example code to get started; testtone.py and capture.py.
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
Module Documentation and Usage Examples:
Example: Importing the module:
>>> import jack
>>> dir(jack)
['CanMonitor', ... ]
>>> print jack.attach.__doc__
Attach client to the Jack server
------------------------------------------------------------------------
Exceptions Thrown by the Jack module:
These are called jack.Error, jack.InputSyncError, etc.
jack.Error:
A general exception thrown when things go wrong. Usually something
bad, like "can't connect to server", or "failed to connect ports"
jack.NotConnectedError:
Thrown when you try to access a jack API function before the client
has been attached.
jack.UsageError:
Thrown when you are misusing the PyJack API. For example, trying to
call jack.activate() when the client has already been activated.
jack.InputSyncError:
jack.OutputSyncError:
Often the low level jack client thread is not synchronized with the
Python side. This exception will be thrown to warn you that there a
potential synchronization problem. jack.OutputSyncError is extremely
uncommon, you should not ever see that error. jack.InputSyncError is
very common, for example if you have activated the client but do not
start calling jack.process() immediately.
>>> import jack
>>> import time
>>> jack.attach("test")
... etc
>>> jack.activate()
>>> time.sleep(1)
>>> jack.process(output, input)
Here you will get a InputSyncError, because you have been sleeping
for 1 second, the data in the audio buffer is about 1 second old.
In other words, the input stream has been stopped for 1 second,
holding old data. In spite of getting this error, you _will_ get
the old audio data in your input array.
Say that you want to write a monitoring application in Python; Now,
having a toolkit running along with audio processing in realtime is
nearly impossible (I've tried it; there is just not enough time to
redraw the screen without getting an InputSyncError). Basically what
you will want to do is only call jack.process() a few times per
second (say 5 times per second). Each time you call jack.process()
it will throw an InputSyncError, but you know that the audio data is
at most 1/5th of a second old; that is probably good enough for a
very simple metering of the input without making the GUI requirements
too extreme.
------------------------------------------------------------------------
Jack Port Flags:
jack.IsInput
jack.IsOutput
jack.CanMonitor
jack.IsPhysical
jack.IsTerminal
These are bit flags which indicate the properties of Jack ports. Use
the bitwise operators (| and &) with them;
Example:
>>> print jack.get_port_flags('alsa_pcm:capture_1')
22
>>> print (jack.get_port_flags('alsa_pcm:capture_1') & jack.IsInput) > 0
0
>>> print (jack.get_port_flags('alsa_pcm:capture_1') & jack.IsOutput) > 0
1
>>> print (jack.get_port_flags('alsa_pcm:capture_1') & jack.IsPhysical) > 0
1
>>> print (jack.get_port_flags('alsa_pcm:capture_1') & jack.IsTerminal) > 0
1
>>> print (jack.get_port_flags('alsa_pcm:capture_1') & jack.CanMonitor) > 0
0
When creating ports, you will want to use a bitwise | of the flags;
>>> jack.register_port("input_1", jack.IsInput | jack.CanMonitor)
>>> jack.register_port("input_1", jack.IsOutput | jack.CanMonitor)
------------------------------------------------------------------------
Function calls in the jack module:
---
jack.attach(name)
Attach to the jack server using the given client name.
All other function calls in the jack module require that you call
this function first. (otherwise you will get a NotConnectedError).
>>> jack.attach("foo_client")
---
jack.detach()
Detach from the jack server
>>> jack.detach()
>>> jack.get_sample_rate()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
jack.NotConnectedError: Jack connection has not yet been established.
---
jack.register_port(name, flags)
Create a new port for this client with the given flags.
Once a port is created it cannot be 'unregistered'.
(The Jack API does permit this, but the PyJack module does not support it.)
---
jack.activate()
Enables callbacks to the realtime jack thread.
This must be called before jack.process() is used.
Enabling the realtime thread has minimal CPU overhead.
---
jack.deactivate()
Disables Jack callbacks to the realtime thread.
Opposite of jack.activate()
jack.process() cannot be used after jack.deactivate() is called,
until jack.activate() is called again.
---
jack.process()
This function exchanges audio between Python and the realtime jack thread.
It requires two arguments, which are both 2D Numeric Python arrays.
The arrays -must- be of type 'Float'. The size in the first dimenision
must match the number of inputs or outputs, and the size of the second
dimension must match the buffer size. See capture.py and testtone.py
for examples of how this works. Following is a part of the code from
testtone.py. In this example there is only one input port and one output
port. input.shape = (1, 1024), output.shape = (1, 144000).
Notice that process will modify entries in the input array.
input = Numeric.zeros((1,N), 'f') # note the float type here...
output = Numeric.reshape(
Numeric.sin(
2*Numeric.pi*440.0 * (Numeric.arange(0, sec, 1.0/(Sr), 'f')[0:int(Sr*sec)])
), (1, Sr*sec)).astype('f')
i = 0
while i < output.shape[1] - N:
try:
jack.process(output[:,i:i+N], input)
i += N
except jack.InputSyncError:
pass
except jack.OutputSyncError:
pass
---
jack.get_ports()
Returns a list of all registered ports in the Jack graph.
The name of a port looks like: "client_name:port_name"
>>> jack.get_ports()
['alsa_pcm:capture_1', 'alsa_pcm:capture_2', 'alsa_pcm:capture_3', ... ]
---
jack.get_port_flags()
Returns an integer which is the bitwise-or of all flags for a given port.
>>> jack.get_port_flags('alsa_pcm:playback_6')
21
---
jack.get_connections()
Returns a list of ports connected to the given port.
If nothing is connected, returns a list of length zero.
>>> jack.get_connections('alsa_pcm:capture_1')
['foo_client:input_1']
---
jack.connect(source, destination)
Connect two ports on the Jack graph.
Note that source must have the IsOutput flag, and
destination must have the IsInput flag.
If you want to recieve or send audio, you must
connect your client's inputs/outputs (those
ports registered via jack.register_port()) to
something else which is generating or recieving
audio, e.g. the alsa_pcm ports.
You can connect ports which belong to other clients
as well, e.g. for a patchbay application.
At the time of writing, there is a bug in the Jack
API which causes audio data to be missing from
input buffers if ports are connected before
jack.activate() is called. Until this is fixed,
please make sure that you call jack.activate()
prior to using jack.connect().
---
jack.disconnect(source, destination)
Break a connection established by jack.connect().
---
jack.get_buffer_size()
Returns the current buffer size used by the Jack server.
If this number is small you may have a lot of synchronization problems.
---
jack.get_sample_rate()
Returns the current sample rate used by the Jack server.
---
jack.check_events()
Check if any asynchronous event callbacks have been raised since
the last call to jack.check_events().
Use of this function does -NOT- require that you be presently attached
to the Jack server; however the values will not change unless you are!
>>> jack.check_events()
{'graph_ordering': 0, 'shutdown': 0, 'port_registration': 0, 'hangup': 0}
If graph_ordering == 1:
Then a pair of ports somewhere in the jack graph has been connected or
disconnected.
If port_registration == 1:
Then a port has been added or removed from the jack graph.
If shutdown == 1:
Then the Jack server has shutdown; your client is no longer attached.
If hangup == 1:
Then the Jack server decided to kill the PyJack client for some reason;
the client is no longer attached.
Any flag which is raised is immediatly reset to zero when this
function is called.
------------------------------------------------------------------------