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.

147 lines
4.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 "external_metro.h"
  16. typedef jack_default_audio_sample_t sample_t;
  17. const double PI = 3.14;
  18. static int process_audio (jack_nframes_t nframes, void* arg)
  19. {
  20. ExternalMetro* metro = (ExternalMetro*)arg;
  21. sample_t *buffer = (sample_t *) jack_port_get_buffer (metro->output_port, nframes);
  22. jack_nframes_t frames_left = nframes;
  23. while (metro->wave_length - metro->offset < frames_left) {
  24. memcpy (buffer + (nframes - frames_left), metro->wave + metro->offset, sizeof (sample_t) * (metro->wave_length - metro->offset));
  25. frames_left -= metro->wave_length - metro->offset;
  26. metro->offset = 0;
  27. }
  28. if (frames_left > 0) {
  29. memcpy (buffer + (nframes - frames_left), metro->wave + metro->offset, sizeof (sample_t) * frames_left);
  30. metro->offset += frames_left;
  31. }
  32. return 0;
  33. }
  34. ExternalMetro::ExternalMetro(int freq, double max_amp, int dur_arg, int bpm, char* client_name)
  35. {
  36. sample_t scale;
  37. int i, attack_length, decay_length;
  38. double *amp;
  39. int attack_percent = 1, decay_percent = 10;
  40. char *bpm_string = "bpm";
  41. offset = 0;
  42. /* Initial Jack setup, get sample rate */
  43. if (!client_name) {
  44. client_name = (char *) malloc (9 * sizeof (char));
  45. strcpy (client_name, "metro");
  46. }
  47. if ((client = jack_client_new (client_name)) == 0) {
  48. fprintf (stderr, "jack server not running?\n");
  49. return ;
  50. }
  51. jack_set_process_callback (client, process_audio, this);
  52. output_port = jack_port_register (client, bpm_string, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  53. input_port = jack_port_register (client, "metro_in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  54. sr = jack_get_sample_rate (client);
  55. /* setup wave table parameters */
  56. wave_length = 60 * sr / bpm;
  57. tone_length = sr * dur_arg / 1000;
  58. attack_length = tone_length * attack_percent / 100;
  59. decay_length = tone_length * decay_percent / 100;
  60. scale = 2 * PI * freq / sr;
  61. if (tone_length >= wave_length) {
  62. /*
  63. fprintf (stderr, "invalid duration (tone length = %" PRIu32
  64. ", wave length = %" PRIu32 "\n", tone_length,
  65. wave_length);
  66. */
  67. return ;
  68. }
  69. if (attack_length + decay_length > (int)tone_length) {
  70. fprintf (stderr, "invalid attack/decay\n");
  71. return ;
  72. }
  73. /* Build the wave table */
  74. wave = (sample_t *) malloc (wave_length * sizeof(sample_t));
  75. amp = (double *) malloc (tone_length * sizeof(double));
  76. for (i = 0; i < attack_length; i++) {
  77. amp[i] = max_amp * i / ((double) attack_length);
  78. }
  79. for (i = attack_length; i < (int) tone_length - decay_length; i++) {
  80. amp[i] = max_amp;
  81. }
  82. for (i = (int)tone_length - decay_length; i < (int)tone_length; i++) {
  83. amp[i] = - max_amp * (i - (double) tone_length) / ((double) decay_length);
  84. }
  85. for (i = 0; i < (int) tone_length; i++) {
  86. wave[i] = amp[i] * sin (scale * i);
  87. }
  88. for (i = tone_length; i < (int) wave_length; i++) {
  89. wave[i] = 0;
  90. }
  91. if (jack_activate (client)) {
  92. fprintf (stderr, "cannot activate client");
  93. }
  94. }
  95. ExternalMetro::~ExternalMetro()
  96. {
  97. jack_deactivate(client);
  98. jack_port_unregister(client, input_port);
  99. jack_port_unregister(client, output_port);
  100. jack_client_close(client);
  101. }
  102. int main (int argc, char *argv[])
  103. {
  104. ExternalMetro* client1 = NULL;
  105. ExternalMetro* client2 = NULL;
  106. ExternalMetro* client3 = NULL;
  107. ExternalMetro* client4 = NULL;
  108. ExternalMetro* client5 = NULL;
  109. client1 = new ExternalMetro(1200, 0.4, 20, 80, "t1");
  110. client2 = new ExternalMetro(600, 0.4, 20, 150, "t2");
  111. client3 = new ExternalMetro(1000, 0.4, 20, 110, "t3");
  112. client4 = new ExternalMetro(400, 0.4, 20, 200, "t4");
  113. client5 = new ExternalMetro(1500, 0.4, 20, 150, "t5");
  114. while ((getchar() != 'q')) {
  115. sleep(1);
  116. };
  117. delete client1;
  118. delete client2;
  119. delete client3;
  120. delete client4;
  121. delete client5;
  122. }