aboutsummaryrefslogtreecommitdiff
path: root/jack_transport/jack_transport.c
blob: f78134637f95ccbb5a793bc45d6d4e05445d4579 (plain)
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
/* 
* 
* jack_transport Copyright (C) 2005 Tim Blechmann
* 
* 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.
* 
* 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; see the file COPYING.  If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include "m_pd.h"
#include "jack/jack.h"
#include "stdio.h"

static t_class *jack_transport_class;


typedef struct _jack_transport
{
    t_object x_obj;
	t_outlet * x_outlet;
	t_outlet * x_outlet_info;
	jack_client_t* x_jack_client;
	jack_position_t* x_pos;
} jack_transport_t;

/* connect to jack server */
static int jack_transport_connect(jack_transport_t * x)
{
	char port_name[80] = "";
	static int client = 0;
	do 
	{
		sprintf(port_name,"pure_data_jack_transport_%d",client);
		client++;
	} 
	while (((x->x_jack_client = jack_client_open (port_name, (jack_options_t)0, NULL)) == 0) &&
		   client < 100);
	client = 0;

	if (!x->x_jack_client)
	{
		post("jack_transport: can't connect to jack server");
		return 1;
	}

	post("jack_transport: connecting as %s", port_name);
	
	jack_activate(x->x_jack_client);
	
	return 0;
}


static jack_transport_t * jack_transport_new(void)
{
	int status = 0;
	jack_transport_t *x = (jack_transport_t*) pd_new(jack_transport_class);
	

	x->x_outlet = outlet_new(&x->x_obj, NULL);
	x->x_outlet_info = outlet_new(&x->x_obj, NULL);
	x->x_pos = (jack_position_t*) getbytes(sizeof(jack_position_t));
	
	status = jack_transport_connect(x);

	return x;
}

static void jack_transport_starter(jack_transport_t * x)
{
	jack_transport_start(x->x_jack_client);
	return;
}

static void jack_transport_stoper(jack_transport_t * x)
{
	jack_transport_stop(x->x_jack_client);
	return;
}

static void jack_transport_timebase(jack_transport_t * x)
{
	jack_position_t pos;
	t_atom ap[8];
	const int rolling = (jack_transport_query(x->x_jack_client, &pos) == JackTransportRolling);
	SETFLOAT(ap+0, pos.bar);
	SETFLOAT(ap+1, pos.beat);
	SETSYMBOL(ap+2, gensym("type"));
	SETFLOAT(ap+3, pos.beat_type);
	SETSYMBOL(ap+4, gensym("beats/bar"));
	SETFLOAT(ap+5, pos.beats_per_bar);
	SETSYMBOL(ap+6, gensym("bpm"));
	SETFLOAT(ap+7, pos.beats_per_minute);

	outlet_anything(x->x_outlet_info, gensym("beat"), 2, ap+2);
	outlet_anything(x->x_outlet_info, gensym("beat"), 2, ap+4);
	outlet_anything(x->x_outlet_info, gensym("beat"), 2, ap+6);
	outlet_anything(x->x_outlet_info, gensym("beat"), 1, ap+1);
	outlet_anything(x->x_outlet_info, gensym("bar"),  1, ap+0);
}

static void jack_transport_bang(jack_transport_t * x)
{
	float f;
	if (!x->x_jack_client)
		return;
	
	f = (float)jack_get_current_transport_frame(x->x_jack_client);
	jack_transport_timebase(x);
	outlet_float(x->x_outlet, f);
}

static void jack_transport_float(jack_transport_t * x, float f)
{
	if (!x->x_jack_client)
		return;
	
	jack_transport_locate(x->x_jack_client, (jack_nframes_t)f);
}


void jack_transport_setup(void)
{
	jack_transport_class = class_new(gensym("jack_transport"), 
									 (t_newmethod)jack_transport_new,
									 NULL, sizeof(jack_transport_t),
									 CLASS_DEFAULT, 0);
	class_addmethod(jack_transport_class, (t_method)jack_transport_starter,
					gensym("start"),0,0);
	class_addmethod(jack_transport_class, (t_method)jack_transport_stoper,
					gensym("stop"),0,0);
	class_addbang(jack_transport_class, (t_method)jack_transport_bang);
	class_addfloat(jack_transport_class, (t_method)jack_transport_float);

}