-
Notifications
You must be signed in to change notification settings - Fork 0
/
jackclock.c
284 lines (224 loc) · 8.32 KB
/
jackclock.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
/*
*
* Written by Nicolas Bouillot <[email protected]>
*
* 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 (at your option) any later version.
*
* See file LICENSE for further informations on licensing terms.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Based on PureData by Miller Puckette and others.
*
*/
#include "m_pd.h"
#include <pthread.h>
#include <jack/jack.h>
#include <jack/transport.h>
#include <jack/types.h>
#include <unistd.h>
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif
//from s_stuff.h, pd-0.42-5
#define API_JACK 5
extern int sys_audioapi;
/* ------------------------ jackclock ----------------------------- */
static t_class *jackclock_class;
typedef struct _jackclock
{
t_object x_obj;
t_int is_connected_to_jack;
t_int jack_sample_rate;
jack_client_t *client;
jack_position_t current; //from jack
t_int looping;
jack_nframes_t loop_begin;
t_float loop_end;
t_int loopeventlock;
jack_transport_state_t transport_state;
t_outlet *x_outlet; //jack clock in ms when bang
t_outlet *x_outlet0; //jack samplerate
t_outlet *x_outlet1; //jack state
t_outlet *x_outlet2; //bang when reaching the end of the loop (only in loop mode)
pthread_attr_t attr;
pthread_t jack_connect_thread;
} t_jackclock;
void jackclock_free(t_jackclock *x)
{
jack_release_timebase (x->client);
jack_client_close (x->client);
}
void jackclock_locate(t_jackclock *x, t_floatarg pos_ms){
jack_transport_locate (x->client, (jack_nframes_t) (pos_ms*x->jack_sample_rate));
}
void jackclock_freewheel(t_jackclock *x, t_floatarg onoff){
if(sys_audioapi == API_JACK)
jack_set_freewheel (x->client, (int)onoff);
else
post("jackclock: freewheel mode require to set jack as audio api");
}
void jackclock_locateframe(t_jackclock *x, t_floatarg pos_frame){
jack_transport_locate (x->client, (jack_nframes_t) pos_frame);
}
void jackclock_start(t_jackclock *x){
jack_transport_start(x->client);
}
void jackclock_stop(t_jackclock *x){
jack_transport_stop(x->client);
}
void jackclock_setloop(t_jackclock *x, jack_nframes_t begin_frame, jack_nframes_t end_frame){
if(begin_frame >= end_frame){
post("jackclock: reverse playing not supported\n");}
else{
x->loop_begin=begin_frame;
x->loop_end=end_frame;
x->looping=1;
jack_transport_locate (x->client, (jack_nframes_t) begin_frame);
}
}
void jackclock_loop(t_jackclock *x, t_floatarg begin_ms, t_floatarg end_ms ){
jackclock_setloop(x, (jack_nframes_t)(begin_ms*x->jack_sample_rate),
(jack_nframes_t)(end_ms*x->jack_sample_rate));
}
void jackclock_loopframe(t_jackclock *x, t_floatarg begin_frame, t_floatarg end_frame ){
jackclock_setloop(x, (jack_nframes_t)begin_frame,
(jack_nframes_t)end_frame);
}
void jackclock_unloop(t_jackclock *x){
x->looping=0;
}
void jackclock_bang(t_jackclock *x)
{
if(x->is_connected_to_jack)
{
jack_transport_state_t old_transport_state=x->transport_state;
x->transport_state = jack_transport_query (x->client, &x->current);
float samplerate=(float)jack_get_sample_rate (x->client);
float currenttime=(float)x->current.frame;
outlet_float(x->x_outlet, currenttime);
if(samplerate != (float)x->jack_sample_rate)
{
outlet_float(x->x_outlet0, samplerate);
x->jack_sample_rate=samplerate;
}
if(old_transport_state != x->transport_state)
{
switch (x->transport_state) {
case JackTransportStopped:
outlet_float(x->x_outlet1,0);
break;
case JackTransportRolling:
outlet_float(x->x_outlet1,1);
break;
case JackTransportStarting:
outlet_float(x->x_outlet1,2);
break;
default:
outlet_float(x->x_outlet1,3);
}
}
}
}
void jack_shutdown (void *arg)
{
t_jackclock *x=(t_jackclock *)arg;
x->is_connected_to_jack=0;
}
void jack_timebase_callback (jack_transport_state_t state, jack_nframes_t nframes, jack_position_t* pos, int new_position, void *arg)
{
t_jackclock *x=(t_jackclock *)arg;
if(x->looping)
{
jack_transport_state_t current_transport_state = jack_transport_query (x->client, &x->current);
if(x->current.frame >= x->loop_end){
if(!x->loopeventlock)
{
jack_transport_locate (x->client, (jack_nframes_t) x->loop_begin);
outlet_bang(x->x_outlet2);
x->loopeventlock=1;
}
else x->loopeventlock=0;
}
}
}
void *jackconnect (void *v_param)
{
t_jackclock *x=(t_jackclock *)v_param;
while(!x->is_connected_to_jack)
{
if ((x->client = jack_client_open ("jackclock", JackNullOption, NULL)) == 0) {
post ("jackclock: jack server not running? retry...\n");
}
else
{
jack_on_shutdown (x->client, jack_shutdown, (void *)x);
jack_set_timebase_callback(x->client, 0, jack_timebase_callback, (void *)x);
if (jack_activate (x->client)) post ("jackclock: cannot activate client");
else x->is_connected_to_jack=1;
}
sleep(1);
}
return NULL;
}
static void *jackclock_new(void)
{
t_jackclock *x = (t_jackclock *)pd_new(jackclock_class);
x->is_connected_to_jack=0;
x->jack_sample_rate=0;
x->looping=0;
x->loop_begin=0;
x->loop_end=0;
x->loopeventlock=0;
x->x_outlet = outlet_new(&x->x_obj, &s_float);
x->x_outlet0 = outlet_new(&x->x_obj, &s_float);
x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
pthread_attr_init(&(x->attr));
pthread_attr_setdetachstate(&(x->attr), PTHREAD_CREATE_JOINABLE);
pthread_create(&x->jack_connect_thread,
&(x->attr),
jackconnect,
(void*) x);
pthread_attr_destroy(&(x->attr));
return (x);
}
void jackclock_setup(void)
{
jackclock_class = class_new(gensym("jackclock"),
(t_newmethod)jackclock_new,
(t_method) jackclock_free,
sizeof(t_jackclock),
0,
A_DEFFLOAT,
0);
class_addmethod(jackclock_class, (t_method)jackclock_locate,
gensym("locate"), A_FLOAT, 0);
class_addmethod(jackclock_class, (t_method)jackclock_locateframe,
gensym("locateframe"), A_FLOAT, 0);
class_addmethod(jackclock_class, (t_method)jackclock_start,
gensym("start"), 0);
class_addmethod(jackclock_class, (t_method)jackclock_stop,
gensym("stop"), 0);
class_addmethod(jackclock_class, (t_method)jackclock_loop,
gensym("loop"), A_FLOAT, A_FLOAT,0);
class_addmethod(jackclock_class, (t_method)jackclock_loopframe,
gensym("loopframe"), A_FLOAT, A_FLOAT,0);
class_addmethod(jackclock_class, (t_method)jackclock_unloop,
gensym("unloop"),0);
class_addmethod(jackclock_class, (t_method)jackclock_freewheel,
gensym("freewheel"), A_FLOAT,0);
class_addbang(jackclock_class, jackclock_bang);
class_sethelpsymbol(jackclock_class, gensym("help-jackclock"));
}