Merge branch 'master' into server_no_deadlock
[jack2.git] / example-clients / metro.c
blob9afc58b2fce3ebca85383455d3f4caa5fd0c6f86
1 /*
2 Copyright (C) 2002 Anthony Van Groningen
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #ifndef WIN32
23 #include <unistd.h>
24 #endif
25 #include <math.h>
26 #include <signal.h>
27 #include <getopt.h>
28 #include <string.h>
30 #include <jack/jack.h>
31 #include <jack/transport.h>
33 typedef jack_default_audio_sample_t sample_t;
35 const double PI = 3.14;
37 jack_client_t *client;
38 jack_port_t *output_port;
39 unsigned long sr;
40 int freq = 880;
41 int bpm;
42 jack_nframes_t tone_length, wave_length;
43 sample_t *wave;
44 long offset = 0;
45 int transport_aware = 0;
46 jack_transport_state_t transport_state;
48 static void signal_handler(int sig)
50 jack_client_close(client);
51 fprintf(stderr, "signal received, exiting ...\n");
52 exit(0);
55 static void
56 usage ()
58 fprintf (stderr, "\n"
59 "usage: jack_metro \n"
60 " [ --frequency OR -f frequency (in Hz) ]\n"
61 " [ --amplitude OR -A maximum amplitude (between 0 and 1) ]\n"
62 " [ --duration OR -D duration (in ms) ]\n"
63 " [ --attack OR -a attack (in percent of duration) ]\n"
64 " [ --decay OR -d decay (in percent of duration) ]\n"
65 " [ --name OR -n jack name for metronome client ]\n"
66 " [ --transport OR -t transport aware ]\n"
67 " --bpm OR -b beats per minute\n"
71 static void
72 process_silence (jack_nframes_t nframes)
74 sample_t *buffer = (sample_t *) jack_port_get_buffer (output_port, nframes);
75 memset (buffer, 0, sizeof (jack_default_audio_sample_t) * nframes);
78 static void
79 process_audio (jack_nframes_t nframes)
81 sample_t *buffer = (sample_t *) jack_port_get_buffer (output_port, nframes);
82 jack_nframes_t frames_left = nframes;
84 while (wave_length - offset < frames_left) {
85 memcpy (buffer + (nframes - frames_left), wave + offset, sizeof (sample_t) * (wave_length - offset));
86 frames_left -= wave_length - offset;
87 offset = 0;
89 if (frames_left > 0) {
90 memcpy (buffer + (nframes - frames_left), wave + offset, sizeof (sample_t) * frames_left);
91 offset += frames_left;
95 static int
96 process (jack_nframes_t nframes, void *arg)
98 if (transport_aware) {
99 jack_position_t pos;
101 if (jack_transport_query (client, &pos)
102 != JackTransportRolling) {
104 process_silence (nframes);
105 return 0;
107 offset = pos.frame % wave_length;
109 process_audio (nframes);
110 return 0;
114 main (int argc, char *argv[])
116 sample_t scale;
117 int i, attack_length, decay_length;
118 double *amp;
119 double max_amp = 0.5;
120 int option_index;
121 int opt;
122 int got_bpm = 0;
123 int attack_percent = 1, decay_percent = 10, dur_arg = 100;
124 char *client_name = 0;
125 char *bpm_string = "bpm";
126 int verbose = 0;
127 jack_status_t status;
129 const char *options = "f:A:D:a:d:b:n:thv";
130 struct option long_options[] =
132 {"frequency", 1, 0, 'f'},
133 {"amplitude", 1, 0, 'A'},
134 {"duration", 1, 0, 'D'},
135 {"attack", 1, 0, 'a'},
136 {"decay", 1, 0, 'd'},
137 {"bpm", 1, 0, 'b'},
138 {"name", 1, 0, 'n'},
139 {"transport", 0, 0, 't'},
140 {"help", 0, 0, 'h'},
141 {"verbose", 0, 0, 'v'},
142 {0, 0, 0, 0}
145 while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
146 switch (opt) {
147 case 'f':
148 if ((freq = atoi (optarg)) <= 0) {
149 fprintf (stderr, "invalid frequency\n");
150 return -1;
152 break;
153 case 'A':
154 if (((max_amp = atof (optarg)) <= 0)|| (max_amp > 1)) {
155 fprintf (stderr, "invalid amplitude\n");
156 return -1;
158 break;
159 case 'D':
160 dur_arg = atoi (optarg);
161 fprintf (stderr, "durarg = %u\n", dur_arg);
162 break;
163 case 'a':
164 if (((attack_percent = atoi (optarg)) < 0) || (attack_percent > 100)) {
165 fprintf (stderr, "invalid attack percent\n");
166 return -1;
168 break;
169 case 'd':
170 if (((decay_percent = atoi (optarg)) < 0) || (decay_percent > 100)) {
171 fprintf (stderr, "invalid decay percent\n");
172 return -1;
174 break;
175 case 'b':
176 got_bpm = 1;
177 if ((bpm = atoi (optarg)) < 0) {
178 fprintf (stderr, "invalid bpm\n");
179 return -1;
181 bpm_string = (char *) malloc ((strlen (optarg) + 4) * sizeof (char));
182 strcpy (bpm_string, optarg);
183 strcat (bpm_string, "_bpm");
184 break;
185 case 'n':
186 client_name = (char *) malloc (strlen (optarg) * sizeof (char));
187 strcpy (client_name, optarg);
188 break;
189 case 'v':
190 verbose = 1;
191 break;
192 case 't':
193 transport_aware = 1;
194 break;
195 default:
196 fprintf (stderr, "unknown option %c\n", opt);
197 case 'h':
198 usage ();
199 return -1;
202 if (!got_bpm) {
203 fprintf (stderr, "bpm not specified\n");
204 usage ();
205 return -1;
208 /* Initial Jack setup, get sample rate */
209 if (!client_name) {
210 client_name = (char *) malloc (9 * sizeof (char));
211 strcpy (client_name, "metro");
213 if ((client = jack_client_open (client_name, JackNoStartServer, &status)) == 0) {
214 fprintf (stderr, "jack server not running?\n");
215 return 1;
217 jack_set_process_callback (client, process, 0);
218 output_port = jack_port_register (client, bpm_string, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
220 sr = jack_get_sample_rate (client);
222 /* setup wave table parameters */
223 wave_length = 60 * sr / bpm;
224 tone_length = sr * dur_arg / 1000;
225 attack_length = tone_length * attack_percent / 100;
226 decay_length = tone_length * decay_percent / 100;
227 scale = 2 * PI * freq / sr;
229 if (tone_length >= wave_length) {
230 fprintf (stderr, "invalid duration (tone length = %u, wave length = %u\n", tone_length, wave_length);
231 return -1;
233 if (attack_length + decay_length > (int)tone_length) {
234 fprintf (stderr, "invalid attack/decay\n");
235 return -1;
238 /* Build the wave table */
239 wave = (sample_t *) malloc (wave_length * sizeof(sample_t));
240 amp = (double *) malloc (tone_length * sizeof(double));
242 for (i = 0; i < attack_length; i++) {
243 amp[i] = max_amp * i / ((double) attack_length);
245 for (i = attack_length; i < (int)tone_length - decay_length; i++) {
246 amp[i] = max_amp;
248 for (i = (int)tone_length - decay_length; i < (int)tone_length; i++) {
249 amp[i] = - max_amp * (i - (double) tone_length) / ((double) decay_length);
251 for (i = 0; i < (int)tone_length; i++) {
252 wave[i] = amp[i] * sin (scale * i);
254 for (i = tone_length; i < (int)wave_length; i++) {
255 wave[i] = 0;
258 if (jack_activate (client)) {
259 fprintf (stderr, "cannot activate client\n");
260 return 1;
263 /* install a signal handler to properly quits jack client */
264 #ifdef WIN32
265 signal(SIGINT, signal_handler);
266 signal(SIGABRT, signal_handler);
267 signal(SIGTERM, signal_handler);
268 #else
269 signal(SIGQUIT, signal_handler);
270 signal(SIGTERM, signal_handler);
271 signal(SIGHUP, signal_handler);
272 signal(SIGINT, signal_handler);
273 #endif
275 /* run until interrupted */
276 while (1) {
277 #ifdef WIN32
278 Sleep(1000);
279 #else
280 sleep(1);
281 #endif
284 jack_client_close(client);
285 exit (0);