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.

293 lines
7.6KB

  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 <getopt.h>
  23. #include <string.h>
  24. #include <jack/jack.h>
  25. typedef jack_default_audio_sample_t sample_t;
  26. const double PI = 3.14;
  27. jack_client_t *client;
  28. jack_port_t *input_port;
  29. jack_port_t *output_port;
  30. unsigned long sr;
  31. int freq = 880;
  32. int bpm;
  33. jack_nframes_t tone_length, wave_length;
  34. sample_t *wave;
  35. long offset = 0;
  36. int transport_aware = 0;
  37. jack_transport_state_t transport_state;
  38. void
  39. usage ()
  40. {
  41. fprintf (stderr, "\n"
  42. "usage: jack_metro \n"
  43. " [ --frequency OR -f frequency (in Hz) ]\n"
  44. " [ --amplitude OR -A maximum amplitude (between 0 and 1) ]\n"
  45. " [ --duration OR -D duration (in ms) ]\n"
  46. " [ --attack OR -a attack (in percent of duration) ]\n"
  47. " [ --decay OR -d decay (in percent of duration) ]\n"
  48. " [ --name OR -n jack name for metronome client ]\n"
  49. " [ --transport OR -t transport aware ]\n"
  50. " --bpm OR -b beats per minute\n"
  51. );
  52. }
  53. void
  54. process_silence (jack_nframes_t nframes)
  55. {
  56. sample_t *buffer = (sample_t *) jack_port_get_buffer (output_port, nframes);
  57. memset (buffer, 0, sizeof (jack_default_audio_sample_t) * nframes);
  58. }
  59. int
  60. process_audio (jack_nframes_t nframes, void *arg)
  61. {
  62. sample_t *buffer = (sample_t *) jack_port_get_buffer (output_port, nframes);
  63. jack_nframes_t frames_left = nframes;
  64. while (wave_length - offset < frames_left) {
  65. memcpy (buffer + (nframes - frames_left), wave + offset, sizeof (sample_t) * (wave_length - offset));
  66. frames_left -= wave_length - offset;
  67. offset = 0;
  68. }
  69. if (frames_left > 0) {
  70. memcpy (buffer + (nframes - frames_left), wave + offset, sizeof (sample_t) * frames_left);
  71. offset += frames_left;
  72. }
  73. return 0;
  74. }
  75. /*
  76. int
  77. process (jack_nframes_t nframes, void *arg)
  78. {
  79. if (transport_aware) {
  80. jack_position_t pos;
  81. if (jack_transport_query (client, &pos)
  82. != JackTransportRolling) {
  83. process_silence (nframes);
  84. return 0;
  85. }
  86. offset = pos.frame % wave_length;
  87. }
  88. process_audio (nframes);
  89. return 0;
  90. }
  91. */
  92. int
  93. sample_rate_change () {
  94. printf("Sample rate has changed! Exiting...\n");
  95. exit(-1);
  96. }
  97. int
  98. main (int argc, char *argv[])
  99. {
  100. sample_t scale;
  101. int i, attack_length, decay_length;
  102. double *amp;
  103. double max_amp = 0.5;
  104. int option_index;
  105. int opt;
  106. int got_bpm = 0;
  107. int attack_percent = 1, decay_percent = 10, dur_arg = 100;
  108. char *client_name = 0;
  109. char *bpm_string = "bpm";
  110. int verbose = 0;
  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_new (client_name)) == 0) {
  194. fprintf (stderr, "jack server not running?\n");
  195. return 1;
  196. }
  197. jack_set_process_callback (client, process_audio, 0);
  198. output_port = jack_port_register (client, bpm_string, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  199. input_port = jack_port_register (client, "metro_in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  200. sr = jack_get_sample_rate (client);
  201. /* setup wave table parameters */
  202. wave_length = 60 * sr / bpm;
  203. tone_length = sr * dur_arg / 1000;
  204. attack_length = tone_length * attack_percent / 100;
  205. decay_length = tone_length * decay_percent / 100;
  206. scale = 2 * PI * freq / sr;
  207. if (tone_length >= wave_length) {
  208. //fprintf (stderr, "invalid duration (tone length = %" PRIu32
  209. // ", wave length = %" PRIu32 "\n", tone_length,
  210. // wave_length);
  211. return -1;
  212. }
  213. if (attack_length + decay_length > (int)tone_length) {
  214. fprintf (stderr, "invalid attack/decay\n");
  215. return -1;
  216. }
  217. /* Build the wave table */
  218. wave = (sample_t *) malloc (wave_length * sizeof(sample_t));
  219. amp = (double *) malloc (tone_length * sizeof(double));
  220. for (i = 0; i < attack_length; i++) {
  221. amp[i] = max_amp * i / ((double) attack_length);
  222. }
  223. for (i = attack_length; i < (int) tone_length - decay_length; i++) {
  224. amp[i] = max_amp;
  225. }
  226. for (i = (int)tone_length - decay_length; i < (int)tone_length; i++) {
  227. amp[i] = - max_amp * (i - (double) tone_length) / ((double) decay_length);
  228. }
  229. for (i = 0; i < (int) tone_length; i++) {
  230. wave[i] = amp[i] * sin (scale * i);
  231. }
  232. for (i = tone_length; i < (int) wave_length; i++) {
  233. wave[i] = 0;
  234. }
  235. if (jack_activate (client)) {
  236. fprintf (stderr, "cannot activate client");
  237. return 1;
  238. }
  239. #ifdef WIN32
  240. // Connection can only be done after activation
  241. jack_connect(client,jack_port_name(output_port), "portaudio:winmme:in1");
  242. jack_connect(client,jack_port_name(output_port), "portaudio:winmme:in2");
  243. jack_connect(client,"portaudio:winmme:out2", jack_port_name(input_port));
  244. #else
  245. // Connection can only be done after activation
  246. jack_connect(client,jack_port_name(output_port), "coreaudio:Built-in Audio:in2");
  247. jack_connect(client,"coreaudio:Built-in Audio:out2", jack_port_name(input_port));
  248. #endif
  249. while ((getchar() != 'q')) {
  250. //while (1) {
  251. //sleep(1);
  252. //printf("jack_frame_time %ld\n", (long)jack_frame_time(client));
  253. //usleep(2000);
  254. };
  255. if (jack_deactivate (client)) {
  256. fprintf (stderr, "cannot deactivate client");
  257. return 1;
  258. }
  259. jack_client_close(client);
  260. return 0;
  261. }