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.

290 lines
7.2KB

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