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.

206 lines
6.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 "external_metro.h"
  16. #include <stdio.h>
  17. typedef jack_default_audio_sample_t sample_t;
  18. const double PI = 3.14;
  19. static void JackSleep(int sec)
  20. {
  21. #ifdef WIN32
  22. Sleep(sec * 1000);
  23. #else
  24. sleep(sec);
  25. #endif
  26. }
  27. int ExternalMetro::process_audio (jack_nframes_t nframes, void* arg)
  28. {
  29. ExternalMetro* metro = (ExternalMetro*)arg;
  30. sample_t *buffer = (sample_t *) jack_port_get_buffer (metro->output_port, nframes);
  31. jack_nframes_t frames_left = nframes;
  32. while (metro->wave_length - metro->offset < frames_left) {
  33. memcpy (buffer + (nframes - frames_left), metro->wave + metro->offset, sizeof (sample_t) * (metro->wave_length - metro->offset));
  34. frames_left -= metro->wave_length - metro->offset;
  35. metro->offset = 0;
  36. }
  37. if (frames_left > 0) {
  38. memcpy (buffer + (nframes - frames_left), metro->wave + metro->offset, sizeof (sample_t) * frames_left);
  39. metro->offset += frames_left;
  40. }
  41. return 0;
  42. }
  43. void ExternalMetro::shutdown (void* arg)
  44. {
  45. printf("shutdown called..\n");
  46. }
  47. ExternalMetro::ExternalMetro(int freq, double max_amp, int dur_arg, int bpm, const char* client_name)
  48. {
  49. sample_t scale;
  50. int i, attack_length, decay_length;
  51. int attack_percent = 1, decay_percent = 10;
  52. const char *bpm_string = "bpm";
  53. jack_options_t options = JackNullOption;
  54. jack_status_t status;
  55. offset = 0;
  56. /* Initial Jack setup, get sample rate */
  57. if ((client = jack_client_open (client_name, options, &status)) == 0) {
  58. fprintf (stderr, "jack server not running?\n");
  59. throw -1;
  60. }
  61. jack_set_process_callback (client, process_audio, this);
  62. jack_on_shutdown (client, shutdown, this);
  63. output_port = jack_port_register (client, bpm_string, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  64. input_port = jack_port_register (client, "metro_in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  65. sr = jack_get_sample_rate (client);
  66. /* setup wave table parameters */
  67. wave_length = 60 * sr / bpm;
  68. tone_length = sr * dur_arg / 1000;
  69. attack_length = tone_length * attack_percent / 100;
  70. decay_length = tone_length * decay_percent / 100;
  71. scale = 2 * PI * freq / sr;
  72. if (tone_length >= wave_length) {
  73. /*
  74. fprintf (stderr, "invalid duration (tone length = %" PRIu32
  75. ", wave length = %" PRIu32 "\n", tone_length,
  76. wave_length);
  77. */
  78. return ;
  79. }
  80. if (attack_length + decay_length > (int)tone_length) {
  81. fprintf (stderr, "invalid attack/decay\n");
  82. return ;
  83. }
  84. /* Build the wave table */
  85. wave = (sample_t *) malloc (wave_length * sizeof(sample_t));
  86. amp = (double *) malloc (tone_length * sizeof(double));
  87. for (i = 0; i < attack_length; i++) {
  88. amp[i] = max_amp * i / ((double) attack_length);
  89. }
  90. for (i = attack_length; i < (int) tone_length - decay_length; i++) {
  91. amp[i] = max_amp;
  92. }
  93. for (i = (int)tone_length - decay_length; i < (int)tone_length; i++) {
  94. amp[i] = - max_amp * (i - (double) tone_length) / ((double) decay_length);
  95. }
  96. for (i = 0; i < (int) tone_length; i++) {
  97. wave[i] = amp[i] * sin (scale * i);
  98. }
  99. for (i = tone_length; i < (int) wave_length; i++) {
  100. wave[i] = 0;
  101. }
  102. if (jack_activate (client)) {
  103. fprintf (stderr, "cannot activate client");
  104. }
  105. }
  106. ExternalMetro::~ExternalMetro()
  107. {
  108. jack_deactivate(client);
  109. jack_port_unregister(client, input_port);
  110. jack_port_unregister(client, output_port);
  111. jack_client_close(client);
  112. free(amp);
  113. free(wave);
  114. }
  115. int main (int argc, char *argv[])
  116. {
  117. ExternalMetro* client1 = NULL;
  118. ExternalMetro* client2 = NULL;
  119. ExternalMetro* client3 = NULL;
  120. ExternalMetro* client4 = NULL;
  121. ExternalMetro* client5 = NULL;
  122. printf("Testing multiple Jack clients in one process: open 5 metro clients...\n");
  123. client1 = new ExternalMetro(1200, 0.4, 20, 80, "t1");
  124. client2 = new ExternalMetro(600, 0.4, 20, 150, "t2");
  125. client3 = new ExternalMetro(1000, 0.4, 20, 110, "t3");
  126. client4 = new ExternalMetro(400, 0.4, 20, 200, "t4");
  127. client5 = new ExternalMetro(1500, 0.4, 20, 150, "t5");
  128. printf("Type 'c' to close all clients and go to next test...\n");
  129. while ((getchar() != 'c')) {
  130. JackSleep(1);
  131. };
  132. delete client1;
  133. delete client2;
  134. delete client3;
  135. delete client4;
  136. delete client5;
  137. printf("Testing quitting the server while a client is running...\n");
  138. client1 = new ExternalMetro(1200, 0.4, 20, 80, "t1");
  139. printf("Now quit the server, shutdown callback should be called...\n");
  140. printf("Type 'c' to move on...\n");
  141. while ((getchar() != 'c')) {
  142. JackSleep(1);
  143. };
  144. printf("Closing client...\n");
  145. delete client1;
  146. printf("Now start the server again...\n");
  147. printf("Type 'c' to move on...\n");
  148. while ((getchar() != 'c')) {
  149. JackSleep(1);
  150. };
  151. printf("Opening a new client....\n");
  152. client1 = new ExternalMetro(1200, 0.4, 20, 80, "t1");
  153. printf("Now quit the server, shutdown callback should be called...\n");
  154. printf("Type 'c' to move on...\n");
  155. while ((getchar() != 'c')) {
  156. JackSleep(1);
  157. };
  158. printf("Simulating client not correctly closed...\n");
  159. printf("Opening a new client....\n");
  160. try {
  161. client1 = new ExternalMetro(1200, 0.4, 20, 80, "t1");
  162. } catch (int num) {
  163. printf("Cannot open a new client since old one was not closed correctly... OK\n");
  164. }
  165. printf("Type 'q' to quit...\n");
  166. while ((getchar() != 'q')) {
  167. JackSleep(1);
  168. };
  169. delete client1;
  170. return 0;
  171. }