-
Notifications
You must be signed in to change notification settings - Fork 5
/
pyjack.c
1314 lines (1105 loc) · 43.9 KB
/
pyjack.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* pyjackc - C module implementation for pyjack
*
* Copyright 2003 Andrew W. Schmeder <[email protected]>
* Copyright 2010 Filipe Coelho (aka 'falkTX') <[email protected]>
*
* This source code is released under the terms of the GNU GPL v2.
* See LICENSE for the full text of these terms.
*/
// Python includes
#include "Python.h"
#include "numpy/arrayobject.h"
// Jack
#include <jack/jack.h>
#include <jack/transport.h>
// C standard
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
/*
TODO's:
- dettach on client on __del__
- free buffers
- close sockets
- hangup callback
- user python callbacks for non-rt events
- make global client a python object
- eventually deprecate the global client api
- remove the attach and detach methods
*/
// Global shared data for jack
/* Uncomment the next line if you have Jack2 */
// #define JACK2 1
#define PYJACK_MAX_PORTS 256
#define W 0
#define R 1
typedef struct {
PyObject_HEAD
jack_client_t* pjc; // Client handle
int buffer_size; // Buffer size
int num_inputs; // Number of input ports registered
int num_outputs; // Number of output ports registered
jack_port_t* input_ports[PYJACK_MAX_PORTS]; // Input ports
jack_port_t* output_ports[PYJACK_MAX_PORTS]; // Output ports
fd_set input_rfd; // fdlist for select
fd_set output_rfd; // fdlist for select
int input_pipe[2]; // socket pair for input port data
int output_pipe[2]; // socket pair for output port data
float* input_buffer_0; // buffer used to transmit audio via slink...
float* output_buffer_0; // buffer used to send audio via slink...
float* input_buffer_1; // buffer used to transmit audio via slink...
float* output_buffer_1; // buffer used to send audio via slink...
int input_buffer_size; // buffer_size * num_inputs * sizeof(sample_t)
int output_buffer_size; // buffer_size * num_outputs * sizeof(sample_t)
int iosync; // true when the python side synchronizing properly...
int event_graph_ordering; // true when a graph ordering event has occured
int event_port_registration; // true when a port registration event has occured
int event_buffer_size; // true when a buffer size change has occured
int event_sample_rate; // true when a sample rate change has occured
int event_xrun; // true when a xrun occurs
int event_shutdown; // true when the jack server is shutdown
int event_hangup; // true when client got hangup signal
int active; // indicates if the client is currently process-enabled
} pyjack_client_t;
pyjack_client_t global_client;
pyjack_client_t * self_or_global_client(PyObject * self) {
if (!self) return & global_client;
return (pyjack_client_t*) self;
}
// Initialize global data
void pyjack_init(pyjack_client_t * client) {
client->pjc = NULL;
client->active = 0;
client->iosync = 0;
client->num_inputs = 0;
client->num_outputs = 0;
client->input_pipe[R] = 0;
client->input_pipe[W] = 0;
client->output_pipe[R] = 0;
client->output_pipe[W] = 0;
// Initialize unamed, raw datagram-type sockets...
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, client->input_pipe) == -1) {
printf("ERROR: Failed to create socketpair input_pipe!!\n");
}
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, client->output_pipe) == -1) {
printf("ERROR: Failed to create socketpair output_pipe!!\n");
}
// Convention is that pipe[W=1] is the "write" end of the pipe, which is always non-blocking.
fcntl(client->input_pipe[W], F_SETFL, O_NONBLOCK);
fcntl(client->output_pipe[W], F_SETFL, O_NONBLOCK);
fcntl(client->output_pipe[R], F_SETFL, O_NONBLOCK);
// The read end, pipe[R=0], is blocking, but we use a select() call to make sure that data is really there.
FD_ZERO(&client->input_rfd);
FD_ZERO(&client->output_rfd);
FD_SET(client->input_pipe[R], &client->input_rfd);
FD_SET(client->output_pipe[R], &client->output_rfd);
// Init buffers to null...
client->input_buffer_size = 0;
client->output_buffer_size = 0;
client->input_buffer_0 = NULL;
client->output_buffer_0 = NULL;
client->input_buffer_1 = NULL;
client->output_buffer_1 = NULL;
}
static void free_and_reset(float ** pointer)
{
if (!*pointer) return;
free(*pointer);
*pointer=0;
}
static void close_and_reset(int * fd)
{
if (!*fd) return;
close(*fd);
*fd=0;
}
// Finalize global data
void pyjack_final(pyjack_client_t * client) {
client->pjc = NULL;
// Free buffers...
client->num_inputs = 0;
client->num_outputs = 0;
client->buffer_size = 0;
free_and_reset(&client->input_buffer_0);
free_and_reset(&client->input_buffer_1);
free_and_reset(&client->output_buffer_0);
free_and_reset(&client->output_buffer_1);
// Close socket...
close_and_reset(&client->input_pipe[R]);
close_and_reset(&client->input_pipe[W]);
close_and_reset(&client->output_pipe[R]);
close_and_reset(&client->output_pipe[W]);
}
// (Re)initialize socketpair buffers
void init_pipe_buffers(pyjack_client_t * client) {
// allocate buffers for send and recv
unsigned new_input_size = client->num_inputs * client->buffer_size * sizeof(float);
if(client->input_buffer_size != new_input_size) {
client->input_buffer_size = new_input_size;
client->input_buffer_0 = realloc(client->input_buffer_0, new_input_size);
client->input_buffer_1 = realloc(client->input_buffer_1, new_input_size);
//printf("Input buffer size %d bytes\n", input_buffer_size);
}
unsigned new_output_size = client->num_outputs * client->buffer_size * sizeof(float);
if(client->output_buffer_size != new_output_size) {
client->output_buffer_size = new_output_size;
client->output_buffer_0 = realloc(client->output_buffer_0, new_output_size);
client->output_buffer_1 = realloc(client->output_buffer_1, new_output_size);
//printf("Output buffer size %d bytes\n", output_buffer_size);
}
// set socket buffers to same size as snd/rcv buffers
setsockopt(client->input_pipe[R], SOL_SOCKET, SO_RCVBUF, &client->input_buffer_size, sizeof(int));
setsockopt(client->input_pipe[R], SOL_SOCKET, SO_SNDBUF, &client->input_buffer_size, sizeof(int));
setsockopt(client->input_pipe[W], SOL_SOCKET, SO_RCVBUF, &client->input_buffer_size, sizeof(int));
setsockopt(client->input_pipe[W], SOL_SOCKET, SO_SNDBUF, &client->input_buffer_size, sizeof(int));
setsockopt(client->output_pipe[R], SOL_SOCKET, SO_RCVBUF, &client->output_buffer_size, sizeof(int));
setsockopt(client->output_pipe[R], SOL_SOCKET, SO_SNDBUF, &client->output_buffer_size, sizeof(int));
setsockopt(client->output_pipe[W], SOL_SOCKET, SO_RCVBUF, &client->output_buffer_size, sizeof(int));
setsockopt(client->output_pipe[W], SOL_SOCKET, SO_SNDBUF, &client->output_buffer_size, sizeof(int));
}
// RT function called by jack
int pyjack_process(jack_nframes_t n, void* arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
int i, r;
// Send input data to python side (non-blocking!)
if (client->num_inputs) {
for(i = 0; i < client->num_inputs; i++) {
memcpy(
&client->input_buffer_0[client->buffer_size * i],
jack_port_get_buffer(client->input_ports[i], n),
(client->buffer_size * sizeof(float))
);
}
r = write(client->input_pipe[W], client->input_buffer_0, client->input_buffer_size);
if(r < 0) {
client->iosync = 0;
} else if(r == client->input_buffer_size) {
client->iosync = 1;
}
}
// Read data from python side (non-blocking!)
if (client->num_outputs) {
r = read(client->output_pipe[R], client->output_buffer_0, client->output_buffer_size);
if(r != client->buffer_size * sizeof(float) * client->num_outputs) {
//printf("not enough data; skipping output\n");
return 0;
}
for(i = 0; i < client->num_outputs; i++) {
memcpy(
jack_port_get_buffer(client->output_ports[i], client->buffer_size),
client->output_buffer_0 + (client->buffer_size * i),
client->buffer_size * sizeof(float)
);
}
}
return 0;
}
// Event notification of buffer size change
int pyjack_buffer_size_changed(jack_nframes_t n, void* arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
client->event_buffer_size = 1;
return 0;
}
// Event notification of sample rate change
int pyjack_sample_rate_changed(jack_nframes_t n, void* arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
client->event_sample_rate = 1;
return 0;
}
// Event notification of graph connect/disconnection
int pyjack_graph_order(void* arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
client->event_graph_ordering = 1;
return 0;
}
// Event notification of xrun
int pyjack_xrun(void* arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
client->event_xrun = 1;
return 0;
}
// Event notification of port registration or drop
void pyjack_port_registration(jack_port_id_t pid, int action, void* arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
client->event_port_registration = 1;
}
// Shutdown handler
void pyjack_shutdown(void * arg) {
pyjack_client_t * client = (pyjack_client_t*) arg;
client->event_shutdown = 1;
client->pjc = NULL;
}
// SIGHUP handler
void pyjack_hangup(int signal) {
// TODO: what to do with non global clients
global_client.event_hangup = 1;
global_client.pjc = NULL;
}
// ------------- Python module stuff ---------------------
// Module exception object
static PyObject* JackError;
static PyObject* JackNotConnectedError;
static PyObject* JackUsageError;
static PyObject* JackInputSyncError;
static PyObject* JackOutputSyncError;
// Attempt to connect to the Jack server
static PyObject* attach(PyObject* self, PyObject* args)
{
char* cname;
if (! PyArg_ParseTuple(args, "s", &cname))
return NULL;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc != NULL) {
PyErr_SetString(JackUsageError, "A connection is already established.");
return NULL;
}
jack_status_t status;
client->pjc = jack_client_open(cname, JackNoStartServer, &status);
if(client->pjc == NULL) {
//TODO check status
PyErr_SetString(JackNotConnectedError, "Failed to connect to Jack audio server.");
return NULL;
}
jack_on_shutdown(client->pjc, pyjack_shutdown, client);
signal(SIGHUP, pyjack_hangup); // TODO: This just works with global clients
if(jack_set_process_callback(client->pjc, pyjack_process, client) != 0) {
PyErr_SetString(JackError, "Failed to set jack process callback.");
return NULL;
}
if(jack_set_buffer_size_callback(client->pjc, pyjack_buffer_size_changed, client) != 0) {
PyErr_SetString(JackError, "Failed to set jack buffer size callback.");
return NULL;
}
if(jack_set_sample_rate_callback(client->pjc, pyjack_sample_rate_changed, client) != 0) {
PyErr_SetString(JackError, "Failed to set jack sample rate callback.");
return NULL;
}
if(jack_set_port_registration_callback(client->pjc, pyjack_port_registration, client) != 0) {
PyErr_SetString(JackError, "Failed to set jack port registration callback.");
return NULL;
}
if(jack_set_graph_order_callback(client->pjc, pyjack_graph_order, client) != 0) {
PyErr_SetString(JackError, "Failed to set jack graph order callback.");
return NULL;
}
if(jack_set_xrun_callback(client->pjc, pyjack_xrun, client) != 0) {
PyErr_SetString(JackError, "Failed to set jack xrun callback.");
return NULL;
}
// Get buffer size
client->buffer_size = jack_get_buffer_size(client->pjc);
// Success!
Py_INCREF(Py_None);
return Py_None;
}
// Detach client from the jack server (also destroys all connections)
static PyObject* detach(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc != NULL) {
jack_client_close(client->pjc);
pyjack_final(client);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* unregister_port(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
char* port_name;
if (! PyArg_ParseTuple(args, "s", &port_name))
return NULL;
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
// if(client->active) {
// PyErr_SetString(JackUsageError, "Cannot unregister ports while client is active.");
// return NULL;
// }
int i = 0;
for (i=0;i<client->num_inputs;i++) {
if (strcmp(port_name, jack_port_short_name(client->input_ports[i]))) continue;
int error = jack_port_unregister(client->pjc, client->input_ports[i]);
if (error) {
PyErr_SetString(JackError, "Unable to unregister input port.");
return NULL;
}
client->num_inputs--;
for (;i<client->num_inputs;i++) {
client->input_ports[i] = client->input_ports[i+1];
}
init_pipe_buffers(client);
Py_INCREF(Py_None);
return Py_None;
}
for (i=0;i<client->num_outputs;i++) {
if (strcmp(port_name, jack_port_short_name(client->output_ports[i]))) continue;
int error = jack_port_unregister(client->pjc, client->output_ports[i]);
if (error) {
PyErr_SetString(JackError, "Unable to unregister output port.");
return NULL;
}
client->num_outputs--;
for (;i<client->num_outputs;i++) {
client->output_ports[i] = client->output_ports[i+1];
}
init_pipe_buffers(client);
Py_INCREF(Py_None);
return Py_None;
}
PyErr_SetString(JackUsageError, "Port not found.");
return NULL;
}
// Create a new port for this client
// Unregistration of ports is not supported; you must disconnect, reconnect, re-reg all ports instead.
static PyObject* register_port(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
int flags;
char* pname;
if (! PyArg_ParseTuple(args, "si", &pname, &flags))
return NULL;
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
// if(client->active) {
// PyErr_SetString(JackUsageError, "Cannot register ports while client is active.");
// return NULL;
// }
if(client->num_inputs >= PYJACK_MAX_PORTS) {
PyErr_SetString(JackUsageError, "Cannot create more than 256 ports. Sorry.");
return NULL;
}
jack_port_t* jp = jack_port_register(client->pjc, pname, JACK_DEFAULT_AUDIO_TYPE, flags, 0);
if(jp == NULL) {
PyErr_SetString(JackError, "Failed to create port.");
return NULL;
}
// Store pointer to this port and increment counter
if(flags & JackPortIsInput) {
client->input_ports[client->num_inputs] = jp;
client->num_inputs++;
}
if(flags & JackPortIsOutput) {
client->output_ports[client->num_outputs] = jp;
client->num_outputs++;
}
init_pipe_buffers(client);
Py_INCREF(Py_None);
return Py_None;
}
// Returns a list of all port names registered in the Jack system
static PyObject* get_ports(PyObject* self, PyObject* args)
{
PyObject* plist;
const char** jplist;
int i;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
jplist = jack_get_ports(client->pjc, NULL, NULL, 0);
i = 0;
plist = PyList_New(0);
if(jplist != NULL) {
while(jplist[i] != NULL) {
PyList_Append(plist, Py_BuildValue("s", jplist[i]));
//free(jplist[i]); // Memory leak or not??
i++;
}
}
Py_INCREF(plist);
return plist;
}
// Return port flags (an integer)
static PyObject* get_port_flags(PyObject* self, PyObject* args)
{
char* pname;
jack_port_t* jp;
int i;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
if (! PyArg_ParseTuple(args, "s", &pname))
return NULL;
jp = jack_port_by_name(client->pjc, pname);
if(jp == NULL) {
PyErr_SetString(JackError, "Bad port name.");
return NULL;
}
i = jack_port_flags(jp);
if(i < 0) {
PyErr_SetString(JackError, "Error getting port flags.");
return NULL;
}
return Py_BuildValue("i", i);
}
// Return a list of full port names connected to the named port
// Port does not need to be owned by this client.
static PyObject* get_connections(PyObject* self, PyObject* args)
{
char* pname;
const char** jplist;
jack_port_t* jp;
PyObject* plist;
int i;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
if (! PyArg_ParseTuple(args, "s", &pname))
return NULL;
jp = jack_port_by_name(client->pjc, pname);
if(jp == NULL) {
PyErr_SetString(JackError, "Bad port name.");
return NULL;
}
jplist = jack_port_get_all_connections(client->pjc, jp);
i = 0;
plist = PyList_New(0);
if(jplist != NULL) {
while(jplist[i] != NULL) {
PyList_Append(plist, Py_BuildValue("s", jplist[i]));
//free(jplist[i]); // memory leak or not?
i++;
}
}
Py_INCREF(plist);
return plist;
}
// connect_port
static PyObject* port_connect(PyObject* self, PyObject* args)
{
char* src_name;
char* dst_name;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
if (! PyArg_ParseTuple(args, "ss", &src_name, &dst_name))
return NULL;
jack_port_t * src = jack_port_by_name(client->pjc, src_name);
if (!src) {
PyErr_SetString(JackUsageError, "Non existing source port.");
return NULL;
}
jack_port_t * dst = jack_port_by_name(client->pjc, dst_name);
if (!dst) {
PyErr_SetString(JackUsageError, "Non existing destination port.");
return NULL;
}
if(! client->active) {
if(jack_port_is_mine(client->pjc, src) || jack_port_is_mine(client->pjc, dst)) {
PyErr_SetString(JackUsageError, "Jack client must be activated to connect own ports.");
return NULL;
}
}
int error = jack_connect(client->pjc, src_name, dst_name);
if (error !=0 && error != EEXIST) {
PyErr_SetString(JackError, "Failed to connect ports.");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static int jack_port_connected_to_extern(const pyjack_client_t * client,
const jack_port_t * src,
const char* dst_name)
{
// finds connections of src, then checks if dst is in there
const char ** existing_connections = jack_port_get_all_connections(client->pjc, src);
if (existing_connections) {
int i; // non C99 nonsense
for (i = 0; existing_connections[i]; i++) {
if (strcmp(existing_connections[i], dst_name) == 0)
return 1;
}
}
return 0;
}
// disconnect_port
static PyObject* port_disconnect(PyObject* self, PyObject* args)
{
char* src_name;
char* dst_name;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
if (! PyArg_ParseTuple(args, "ss", &src_name, &dst_name))
return NULL;
jack_port_t * src = jack_port_by_name(client->pjc, src_name);
if (!src) {
PyErr_SetString(JackUsageError, "Non existing source port.");
return NULL;
}
jack_port_t * dst = jack_port_by_name(client->pjc, dst_name);
if (!dst) {
PyErr_SetString(JackUsageError, "Non existing destination port.");
return NULL;
}
if(jack_port_connected_to_extern(client, src, dst_name)) {
if (jack_disconnect(client->pjc, src_name, dst_name)) {
PyErr_SetString(JackError, "Failed to disconnect ports.");
return NULL;
}
}
Py_INCREF(Py_None);
return Py_None;
}
// get_buffer_size
static PyObject* get_buffer_size(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
int bs = jack_get_buffer_size(client->pjc);
return Py_BuildValue("i", bs);
}
// get_sample_rate
static PyObject* get_sample_rate(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
int sr = jack_get_sample_rate(client->pjc);
return Py_BuildValue("i", sr);
}
// activate
static PyObject* activate(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
if(client->active) {
PyErr_SetString(JackUsageError, "Client is already active.");
return NULL;
}
if(jack_activate(client->pjc) != 0) {
PyErr_SetString(JackUsageError, "Could not activate client.");
return NULL;
}
client->active = 1;
Py_INCREF(Py_None);
return Py_None;
}
// deactivate
static PyObject* deactivate(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
if(! client->active) {
PyErr_SetString(JackUsageError, "Client is not active.");
return NULL;
}
if(jack_deactivate(client->pjc) != 0) {
PyErr_SetString(JackError, "Could not deactivate client.");
return NULL;
}
client->active = 0;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject * get_client_name(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
return Py_BuildValue("s", jack_get_client_name(client->pjc));
}
/** Commit a chunk of audio for the outgoing stream, if any.
* Return the next chunk of audio from the incoming stream, if any
*/
static PyObject* process(PyObject* self, PyObject *args)
{
int j, c, r;
PyArrayObject *input_array;
PyArrayObject *output_array;
pyjack_client_t * client = self_or_global_client(self);
if(! client->active) {
PyErr_SetString(JackUsageError, "Client is not active.");
return NULL;
}
// Import the first and only arg...
if (! PyArg_ParseTuple(args, "O!O!", &PyArray_Type, &output_array, &PyArray_Type, &input_array))
return NULL;
if(input_array->descr->type_num != PyArray_FLOAT || output_array->descr->type_num != PyArray_FLOAT) {
PyErr_SetString(PyExc_ValueError, "arrays must be of type float");
return NULL;
}
if(input_array->nd != 2 || output_array->nd != 2) {
printf("%d, %d\n", input_array->nd, output_array->nd);
PyErr_SetString(PyExc_ValueError, "arrays must be two dimensional");
return NULL;
}
if((client->num_inputs > 0 && input_array->dimensions[1] != client->buffer_size) ||
(client->num_outputs > 0 && output_array->dimensions[1] != client->buffer_size)) {
PyErr_SetString(PyExc_ValueError, "columns of arrays must match buffer size.");
return NULL;
}
if(client->num_inputs > 0 && input_array->dimensions[0] != client->num_inputs) {
PyErr_SetString(PyExc_ValueError, "rows for input array must match number of input ports");
return NULL;
}
if(client->num_outputs > 0 && output_array->dimensions[0] != client->num_outputs) {
PyErr_SetString(PyExc_ValueError, "rows for output array must match number of output ports");
return NULL;
}
// Get input data
// If we are out of sync, there might be bad data in the buffer
// So we have to throw that away first...
if (client->input_buffer_size) {
r = read(client->input_pipe[R], client->input_buffer_1, client->input_buffer_size);
// Copy data into array...
for(c = 0; c < client->num_inputs; c++) {
for(j = 0; j < client->buffer_size; j++) {
memcpy(
input_array->data + (c*input_array->strides[0] + j*input_array->strides[1]),
client->input_buffer_1 + j + (c*client->buffer_size),
sizeof(float)
);
}
}
if(!client->iosync) {
PyErr_SetString(JackInputSyncError, "Input data stream is not synchronized.");
return NULL;
}
}
if (client->output_buffer_size) {
// Copy output data into output buffer...
for(c = 0; c < client->num_outputs; c++) {
for(j = 0; j < client->buffer_size; j++) {
memcpy(&client->output_buffer_1[j + (c*client->buffer_size)],
output_array->data + c*output_array->strides[0] + j*output_array->strides[1],
sizeof(float)
);
}
}
// Send... raise an exception if the output data stream is full.
r = write(client->output_pipe[W], client->output_buffer_1, client->output_buffer_size);
if(r != client->output_buffer_size) {
PyErr_SetString(JackOutputSyncError, "Failed to write output data.");
return NULL;
}
}
// Okay...
Py_INCREF(Py_None);
return Py_None;
}
// Return event status numbers...
static PyObject* check_events(PyObject* self, PyObject *args)
{
pyjack_client_t * client = self_or_global_client(self);
PyObject* d;
d = PyDict_New();
if(d == NULL) return NULL;
PyDict_SetItemString(d, "graph_ordering", Py_BuildValue("i", client->event_graph_ordering));
PyDict_SetItemString(d, "port_registration", Py_BuildValue("i", client->event_port_registration));
PyDict_SetItemString(d, "xrun", Py_BuildValue("i", client->event_xrun));
PyDict_SetItemString(d, "shutdown", Py_BuildValue("i", client->event_shutdown));
PyDict_SetItemString(d, "hangup", Py_BuildValue("i", client->event_hangup));
// Reset all
client->event_graph_ordering = 0;
client->event_port_registration = 0;
client->event_xrun = 0;
client->event_shutdown = 0;
client->event_hangup = 0;
return d;
}
static PyObject* get_frame_time(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
int frt = jack_frame_time(client->pjc);
return Py_BuildValue("i", frt);
}
static PyObject* get_current_transport_frame(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
int ftr = jack_get_current_transport_frame(client->pjc);
return Py_BuildValue("i", ftr);
}
static PyObject* transport_locate (PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
jack_nframes_t newfr;
if (! PyArg_ParseTuple(args, "i", &newfr))
return NULL;
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
jack_transport_locate (client->pjc,newfr);
return Py_None;
}
static PyObject* get_transport_state (PyObject* self, PyObject* args)
{
//int state;
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
jack_transport_state_t transport_state;
transport_state = jack_transport_query (client->pjc, NULL);
return Py_BuildValue("i", transport_state);
}
#ifdef JACK2
static PyObject* get_version(PyObject* self, PyObject* args)
{
int major, minor, micro, proto;
jack_get_version(&major, &minor, µ, &proto);
return Py_BuildValue("iiii", major, minor, micro, proto);
}
#endif
#ifdef JACK2
static PyObject* get_version_string(PyObject* self, PyObject* args)
{
const char* version;
version = jack_get_version_string();
return Py_BuildValue("s", version);
}
#endif
static PyObject* get_cpu_load(PyObject* self, PyObject* args)
{
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
float cpu_load = jack_cpu_load(client->pjc);
return Py_BuildValue("f", cpu_load);
}
static PyObject* get_port_short_name(PyObject* self, PyObject* args)
{
char * port_name;
if (! PyArg_ParseTuple(args, "s", &port_name))
return NULL;
if (port_name == NULL) {
PyErr_SetString(JackError, "Port name cannot be empty.");
return NULL;
}
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
jack_port_t * port = jack_port_by_name(client->pjc, port_name);
if (!port) {
PyErr_SetString(JackError, "Port name cannot be empty.");
return NULL;
}
const char * port_short_name = jack_port_short_name(port);
return Py_BuildValue("s", port_short_name);
}
static PyObject* get_port_type(PyObject* self, PyObject* args)
{
char * port_name;
if (! PyArg_ParseTuple(args, "s", &port_name))
return NULL;
if (port_name == NULL) {
PyErr_SetString(JackError, "Port name cannot be empty.");
return NULL;
}
pyjack_client_t * client = self_or_global_client(self);
if(client->pjc == NULL) {
PyErr_SetString(JackNotConnectedError, "Jack connection has not yet been established.");
return NULL;
}
jack_port_t * port = jack_port_by_name(client->pjc, port_name);
if (!port) {
PyErr_SetString(JackError, "Port name cannot be empty.");
return NULL;
}
const char * port_type = jack_port_type(port);