jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
7.1KB

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