Mercurial > hg > index.fcgi > lsonify > lsonify-1pba
view lsonify.c @ 3:3dd48f06493e
add sample rate change callback
author | paulo@localhost |
---|---|
date | Thu, 21 May 2009 00:56:16 -0700 |
parents | ff028323c114 |
children |
line source
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.
17 */
19 // 20090516 PBA: started modifying metro.c for lsonify
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <math.h>
26 #include <getopt.h>
27 #include <string.h>
29 #include <jack/jack.h>
30 #include <jack/transport.h>
32 typedef jack_default_audio_sample_t sample_t;
34 const double PI = 3.14;
36 jack_client_t *client;
37 jack_port_t *output_port;
38 unsigned long sr;
39 int freq = 880;
40 int bpm;
41 jack_nframes_t tone_length, wave_length;
42 sample_t *wave;
43 long offset = 0;
44 int transport_aware = 0;
45 jack_transport_state_t transport_state;
47 void
48 usage ()
50 {
51 fprintf (stderr, "\n"
52 "usage: lsonify \n"
53 " [ --frequency OR -f frequency (in Hz) ]\n"
54 " [ --amplitude OR -A maximum amplitude (between 0 and 1) ]\n"
55 " [ --duration OR -D duration (in ms) ]\n"
56 " [ --attack OR -a attack (in percent of duration) ]\n"
57 " [ --decay OR -d decay (in percent of duration) ]\n"
58 " [ --name OR -n jack name for metronome client ]\n"
59 " [ --transport OR -t transport aware ]\n"
60 " [ --nophysical OR -p to prevent auto-connect to all physical ports]\n"
61 " --bpm OR -b beats per minute\n"
62 );
63 }
65 void
66 process_silence (jack_nframes_t nframes)
67 {
68 sample_t *buffer = (sample_t *) jack_port_get_buffer (output_port, nframes);
69 memset (buffer, 0, sizeof (jack_default_audio_sample_t) * nframes);
70 }
72 void
73 process_audio (jack_nframes_t nframes)
74 {
76 sample_t *buffer = (sample_t *) jack_port_get_buffer (output_port, nframes);
77 jack_nframes_t frames_left = nframes;
79 while (wave_length - offset < frames_left) {
80 memcpy (buffer + (nframes - frames_left), wave + offset, sizeof (sample_t) * (wave_length - offset));
81 frames_left -= wave_length - offset;
82 offset = 0;
83 }
84 if (frames_left > 0) {
85 memcpy (buffer + (nframes - frames_left), wave + offset, sizeof (sample_t) * frames_left);
86 offset += frames_left;
87 }
88 }
90 int
91 process (jack_nframes_t nframes, void *arg)
92 {
93 if (transport_aware) {
94 jack_position_t pos;
96 if (jack_transport_query (client, &pos)
97 != JackTransportRolling) {
99 process_silence (nframes);
100 return 0;
101 }
102 offset = pos.frame % wave_length;
103 }
104 process_audio (nframes);
105 return 0;
106 }
108 int
109 sample_rate_change (jack_nframes_t nframes, void *arg) {
110 if (nframes != sr) {
111 printf("Sample rate has changed! Exiting...\n");
112 exit(-1);
113 }
114 return 0;
115 }
117 int
118 main (int argc, char *argv[])
119 {
121 sample_t scale;
122 int i, attack_length, decay_length;
123 double *amp;
124 double max_amp = 0.5;
125 int opt;
126 int got_bpm = 0;
127 int attack_percent = 1, decay_percent = 10, dur_arg = 100;
128 char *client_name = 0;
129 char *port_string = "out";
130 int verbose = 0;
131 int connect_physical_ports = 1;
132 jack_status_t status;
134 const char *options = "f:A:D:a:d:b:n:tphv";
135 struct option long_options[] =
136 {
137 {"frequency", 1, 0, 'f'},
138 {"amplitude", 1, 0, 'A'},
139 {"duration", 1, 0, 'D'},
140 {"attack", 1, 0, 'a'},
141 {"decay", 1, 0, 'd'},
142 {"bpm", 1, 0, 'b'},
143 {"name", 1, 0, 'n'},
144 {"transport", 0, 0, 't'},
145 {"nophysical", 0, 0, 'p'},
146 {"help", 0, 0, 'h'},
147 {"verbose", 0, 0, 'v'},
148 {0, 0, 0, 0}
149 };
151 while ((opt = getopt_long (argc, argv, options, long_options, NULL)) != EOF) {
152 switch (opt) {
153 case 'f':
154 if ((freq = atoi (optarg)) <= 0) {
155 fprintf (stderr, "invalid frequency\n");
156 return -1;
157 }
158 break;
159 case 'A':
160 if (((max_amp = atof (optarg)) <= 0)|| (max_amp > 1)) {
161 fprintf (stderr, "invalid amplitude\n");
162 return -1;
163 }
164 break;
165 case 'D':
166 dur_arg = atoi (optarg);
167 fprintf (stderr, "durarg = %u\n", dur_arg);
168 break;
169 case 'a':
170 if (((attack_percent = atoi (optarg)) < 0) || (attack_percent > 100)) {
171 fprintf (stderr, "invalid attack percent\n");
172 return -1;
173 }
174 break;
175 case 'd':
176 if (((decay_percent = atoi (optarg)) < 0) || (decay_percent > 100)) {
177 fprintf (stderr, "invalid decay percent\n");
178 return -1;
179 }
180 break;
181 case 'b':
182 got_bpm = 1;
183 if ((bpm = atoi (optarg)) < 0) {
184 fprintf (stderr, "invalid bpm\n");
185 return -1;
186 }
187 break;
188 case 'n':
189 client_name = (char *) malloc (strlen (optarg) * sizeof (char));
190 strcpy (client_name, optarg);
191 break;
192 case 'v':
193 verbose = 1;
194 break;
195 case 't':
196 transport_aware = 1;
197 break;
198 case 'p':
199 connect_physical_ports = 0;
200 break;
201 default:
202 fprintf (stderr, "unknown option %c\n", opt);
203 case 'h':
204 usage ();
205 return -1;
206 }
207 }
208 if (!got_bpm) {
209 fprintf (stderr, "bpm not specified\n");
210 usage ();
211 return -1;
212 }
214 /* Initial Jack setup, get sample rate */
215 if (!client_name) {
216 pid_t pid = getpid();
217 client_name = (char *) malloc (32 * sizeof (char));
218 snprintf (client_name, 32, "metro_%d", pid);
219 }
220 if ((client = jack_client_open (client_name, JackNoStartServer, &status)) == 0) {
221 fprintf (stderr, "jack server not running?\n");
222 return 1;
223 }
224 jack_set_process_callback (client, process, 0);
225 output_port = jack_port_register (client, port_string, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
227 sr = jack_get_sample_rate (client);
228 jack_set_sample_rate_callback (client, sample_rate_change, 0);
230 /* setup wave table parameters */
231 wave_length = 60 * sr / bpm;
232 tone_length = sr * dur_arg / 1000;
233 attack_length = tone_length * attack_percent / 100;
234 decay_length = tone_length * decay_percent / 100;
235 scale = 2 * PI * freq / sr;
237 if (tone_length >= wave_length) {
238 fprintf (stderr, "invalid duration (tone length = %" PRIu32
239 ", wave length = %" PRIu32 "\n", tone_length,
240 wave_length);
241 return -1;
242 }
243 if (attack_length + decay_length > (int)tone_length) {
244 fprintf (stderr, "invalid attack/decay\n");
245 return -1;
246 }
248 /* Build the wave table */
249 wave = (sample_t *) malloc (wave_length * sizeof(sample_t));
250 amp = (double *) malloc (tone_length * sizeof(double));
252 for (i = 0; i < attack_length; i++) {
253 amp[i] = max_amp * i / ((double) attack_length);
254 }
255 for (i = attack_length; i < (int)tone_length - decay_length; i++) {
256 amp[i] = max_amp;
257 }
258 for (i = (int)tone_length - decay_length; i < (int)tone_length; i++) {
259 amp[i] = - max_amp * (i - (double) tone_length) / ((double) decay_length);
260 }
261 for (i = 0; i < (int)tone_length; i++) {
262 wave[i] = amp[i] * sin (scale * i);
263 }
264 for (i = tone_length; i < (int)wave_length; i++) {
265 wave[i] = 0;
266 }
268 if (jack_activate (client)) {
269 fprintf (stderr, "cannot activate client");
270 return 1;
271 }
273 /* connect to physical ports */
274 if (connect_physical_ports) {
275 const char **ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
276 const char **c;
277 for (c = ports; c && *c; c++)
278 jack_connect (client, jack_port_name(output_port), *c);
279 }
281 while (1) {
282 sleep(1);
283 };
285 }