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.

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