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.

186 lines
5.8KB

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