File: intime.c

package info (click to toggle)
jack-audio-connection-kit 1%3A0.125.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,368 kB
  • sloc: ansic: 40,493; cpp: 1,426; makefile: 678; python: 327; sh: 89
file content (157 lines) | stat: -rw-r--r-- 4,953 bytes parent folder | download | duplicates (5)
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
/*
 *  intime.c -- JACK internal timebase master example client.
 *
 *  To run: first start `jackd', then `jack_load intime intime 6/8,180bpm'.
 */

/*  Copyright (C) 2003 Jack O'Quin.
 *  
 *  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; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include <jack/jack.h>

/* Time and tempo variables, global to the entire transport timeline.
 * There is no attempt to keep a true tempo map.  The default time
 * signature is "march time": 4/4, 120bpm
 */
float time_beats_per_bar = 4.0;
float time_beat_type = 4.0;
double time_ticks_per_beat = 1920.0;
double time_beats_per_minute = 120.0;

/* BBT timebase callback.
 *
 * Runs in the process thread.  Realtime, must not wait.
 */
void 
timebbt (jack_transport_state_t state, jack_nframes_t nframes, 
	 jack_position_t *pos, int new_pos, void *arg)
{
	double min;			/* minutes since frame 0 */
	long abs_tick;			/* ticks since frame 0 */
	long abs_beat;			/* beats since frame 0 */

	if (new_pos) {

		pos->valid = JackPositionBBT;
		pos->beats_per_bar = time_beats_per_bar;
		pos->beat_type = time_beat_type;
		pos->ticks_per_beat = time_ticks_per_beat;
		pos->beats_per_minute = time_beats_per_minute;

		/* Compute BBT info from frame number.  This is
		 * relatively simple here, but would become complex if
		 * we supported tempo or time signature changes at
		 * specific locations in the transport timeline.  I
		 * make no claims for the numerical accuracy or
		 * efficiency of these calculations. */

		min = pos->frame / ((double) pos->frame_rate * 60.0);
		abs_tick = min * pos->beats_per_minute * pos->ticks_per_beat;
		abs_beat = abs_tick / pos->ticks_per_beat;

		pos->bar = abs_beat / pos->beats_per_bar;
		pos->beat = abs_beat - (pos->bar * pos->beats_per_bar) + 1;
		pos->tick = abs_tick - (abs_beat * pos->ticks_per_beat);
		pos->bar_start_tick = pos->bar * pos->beats_per_bar *
			pos->ticks_per_beat;
		pos->bar++;		/* adjust start to bar 1 */

		/* some debug code... */
		fprintf(stderr, "\nnew position: %" PRIu32 "\tBBT: %3"
			PRIi32 "|%" PRIi32 "|%04" PRIi32 "\n",
			pos->frame, pos->bar, pos->beat, pos->tick);

	} else {

		/* Compute BBT info based on previous period. */
		pos->tick += (nframes * pos->ticks_per_beat *
			      pos->beats_per_minute / (pos->frame_rate * 60));

		while (pos->tick >= pos->ticks_per_beat) {
			pos->tick -= pos->ticks_per_beat;
			if (++pos->beat > pos->beats_per_bar) {
				pos->beat = 1;
				++pos->bar;
				pos->bar_start_tick += (pos->beats_per_bar *
							pos->ticks_per_beat);
			}
		}
	}
}

/* experimental timecode callback
 *
 * Fill in extended timecode fields using the trivial assumption that
 * we are running at nominal speed, hence with no drift.
 *
 * It would probably be faster to compute frame_time without the
 * conditional expression.  But, this demonstrates the invariant:
 * next_time[i] == frame_time[i+1], unless a reposition occurs.
 *
 * Runs in the process thread.  Realtime, must not wait.
 */
void 
timecode (jack_transport_state_t state, jack_nframes_t nframes, 
	  jack_position_t *pos, int new_pos, void *arg)
{
	/* nominal transport speed */
	double seconds_per_frame = 1.0 / (double) pos->frame_rate;

	pos->valid = JackPositionTimecode;
	pos->frame_time = (new_pos?
			   pos->frame * seconds_per_frame:
			   pos->next_time);
	pos->next_time = (pos->frame + nframes) * seconds_per_frame;
}

/* after internal client loaded */
int
jack_initialize (jack_client_t *client, const char *load_init)
{
	JackTimebaseCallback callback = timebbt;

	int rc = sscanf(load_init, " %f/%f, %lf bpm ", &time_beats_per_bar,
			&time_beat_type, &time_beats_per_minute);

	if (rc > 0) {
		fprintf (stderr, "counting %.1f/%.1f at %.2f bpm\n",
			 time_beats_per_bar, time_beat_type,
			 time_beats_per_minute);
	} else {
		int len = strlen(load_init);
		if ((len > 0) && (strncmp(load_init, "timecode", len) == 0))
			callback = timecode;
	}

	if (jack_set_timebase_callback(client, 0, callback, NULL) != 0) {
		fprintf (stderr, "Unable to take over timebase.\n");
		return 1;		/* terminate */
	}

	fprintf (stderr, "Internal timebase master defined.\n");
	jack_activate (client);
	return 0;			/* success */
}

/* before unloading */
void
jack_finish (void *arg)
{
	fprintf (stderr, "Internal timebase client exiting.\n");
}