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.

124 lines
4.0KB

  1. /*
  2. Copyright (C) 2002 Anthony Van Groningen
  3. Copyright (C) 2005 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "internal_metro.h"
  17. typedef jack_default_audio_sample_t sample_t;
  18. const double PI = 3.14;
  19. static int process_audio (jack_nframes_t nframes, void* arg)
  20. {
  21. InternalMetro* metro = (InternalMetro*)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. InternalMetro::InternalMetro(int freq, double max_amp, int dur_arg, int bpm, char* client_name)
  36. {
  37. sample_t scale;
  38. int i, attack_length, decay_length;
  39. double *amp;
  40. int attack_percent = 1, decay_percent = 10;
  41. const char *bpm_string = "bpm";
  42. offset = 0;
  43. /* Initial Jack setup, get sample rate */
  44. if (!client_name) {
  45. client_name = (char *) malloc (9 * sizeof (char));
  46. strcpy (client_name, "metro");
  47. }
  48. if ((client = jack_client_new (client_name)) == 0) {
  49. fprintf (stderr, "jack server not running?\n");
  50. return ;
  51. }
  52. jack_set_process_callback (client, process_audio, this);
  53. output_port = jack_port_register (client, bpm_string, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  54. input_port = jack_port_register (client, "metro_in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  55. sr = jack_get_sample_rate (client);
  56. /* setup wave table parameters */
  57. wave_length = 60 * sr / bpm;
  58. tone_length = sr * dur_arg / 1000;
  59. attack_length = tone_length * attack_percent / 100;
  60. decay_length = tone_length * decay_percent / 100;
  61. scale = 2 * PI * freq / sr;
  62. if (tone_length >= wave_length) {
  63. /*
  64. fprintf (stderr, "invalid duration (tone length = %" PRIu32
  65. ", wave length = %" PRIu32 "\n", tone_length,
  66. wave_length);
  67. */
  68. return ;
  69. }
  70. if (attack_length + decay_length > (int)tone_length) {
  71. fprintf (stderr, "invalid attack/decay\n");
  72. return ;
  73. }
  74. /* Build the wave table */
  75. wave = (sample_t *) malloc (wave_length * sizeof(sample_t));
  76. amp = (double *) malloc (tone_length * sizeof(double));
  77. for (i = 0; i < attack_length; i++) {
  78. amp[i] = max_amp * i / ((double) attack_length);
  79. }
  80. for (i = attack_length; i < (int) tone_length - decay_length; i++) {
  81. amp[i] = max_amp;
  82. }
  83. for (i = (int)tone_length - decay_length; i < (int)tone_length; i++) {
  84. amp[i] = - max_amp * (i - (double) tone_length) / ((double) decay_length);
  85. }
  86. for (i = 0; i < (int) tone_length; i++) {
  87. wave[i] = amp[i] * sin (scale * i);
  88. }
  89. for (i = tone_length; i < (int) wave_length; i++) {
  90. wave[i] = 0;
  91. }
  92. if (jack_activate (client)) {
  93. fprintf(stderr, "cannot activate client");
  94. return;
  95. }
  96. }
  97. InternalMetro::~InternalMetro()
  98. {
  99. jack_deactivate(client);
  100. jack_port_unregister(client, input_port);
  101. jack_port_unregister(client, output_port);
  102. jack_client_close(client);
  103. }