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.

151 lines
3.9KB

  1. /*
  2. Copyright (C) 2004 Ian Esten
  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 <stdio.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <jack/jack.h>
  22. #include <jack/midiport.h>
  23. jack_port_t *input_port;
  24. jack_port_t *output_port;
  25. jack_default_audio_sample_t ramp=0.0;
  26. jack_default_audio_sample_t note_on;
  27. unsigned char note = 0;
  28. jack_default_audio_sample_t note_frqs[128];
  29. jack_client_t *client;
  30. static void signal_handler(int sig)
  31. {
  32. jack_client_close(client);
  33. fprintf(stderr, "signal received, exiting ...\n");
  34. exit(0);
  35. }
  36. static void calc_note_frqs(jack_default_audio_sample_t srate)
  37. {
  38. int i;
  39. for(i=0; i<128; i++)
  40. {
  41. note_frqs[i] = (2.0 * 440.0 / 32.0) * pow(2, (((jack_default_audio_sample_t)i - 9.0) / 12.0)) / srate;
  42. }
  43. }
  44. static int process(jack_nframes_t nframes, void *arg)
  45. {
  46. int i;
  47. void* port_buf = jack_port_get_buffer(input_port, nframes);
  48. jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  49. jack_midi_event_t in_event;
  50. jack_nframes_t event_index = 0;
  51. jack_nframes_t event_count = jack_midi_get_event_count(port_buf);
  52. if(event_count > 1)
  53. {
  54. printf(" midisine: have %d events\n", event_count);
  55. for(i=0; i<event_count; i++)
  56. {
  57. jack_midi_event_get(&in_event, port_buf, i);
  58. printf(" event %d time is %d. 1st byte is 0x%x\n", i, in_event.time, *(in_event.buffer));
  59. }
  60. /* printf("1st byte of 1st event addr is %p\n", in_events[0].buffer);*/
  61. }
  62. jack_midi_event_get(&in_event, port_buf, 0);
  63. for(i=0; i<nframes; i++)
  64. {
  65. if((in_event.time == i) && (event_index < event_count))
  66. {
  67. if( ((*(in_event.buffer) & 0xf0)) == 0x90 )
  68. {
  69. /* note on */
  70. note = *(in_event.buffer + 1);
  71. note_on = 1.0;
  72. }
  73. else if( ((*(in_event.buffer)) & 0xf0) == 0x80 )
  74. {
  75. /* note off */
  76. note = *(in_event.buffer + 1);
  77. note_on = 0.0;
  78. }
  79. event_index++;
  80. if(event_index < event_count)
  81. jack_midi_event_get(&in_event, port_buf, event_index);
  82. }
  83. ramp += note_frqs[note];
  84. ramp = (ramp > 1.0) ? ramp - 2.0 : ramp;
  85. out[i] = note_on*sin(2*M_PI*ramp);
  86. }
  87. return 0;
  88. }
  89. static int srate(jack_nframes_t nframes, void *arg)
  90. {
  91. printf("the sample rate is now %" PRIu32 "/sec\n", nframes);
  92. calc_note_frqs((jack_default_audio_sample_t)nframes);
  93. return 0;
  94. }
  95. static void jack_shutdown(void *arg)
  96. {
  97. exit(1);
  98. }
  99. int main(int narg, char **args)
  100. {
  101. if ((client = jack_client_open("midisine", JackNullOption, NULL)) == 0)
  102. {
  103. fprintf(stderr, "jack server not running?\n");
  104. return 1;
  105. }
  106. calc_note_frqs(jack_get_sample_rate (client));
  107. jack_set_process_callback (client, process, 0);
  108. jack_set_sample_rate_callback (client, srate, 0);
  109. jack_on_shutdown (client, jack_shutdown, 0);
  110. input_port = jack_port_register (client, "midi_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  111. output_port = jack_port_register (client, "audio_out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  112. if (jack_activate (client))
  113. {
  114. fprintf(stderr, "cannot activate client");
  115. return 1;
  116. }
  117. /* install a signal handler to properly quits jack client */
  118. signal(SIGQUIT, signal_handler);
  119. signal(SIGTERM, signal_handler);
  120. signal(SIGHUP, signal_handler);
  121. signal(SIGINT, signal_handler);
  122. /* run until interrupted */
  123. while(1) {
  124. sleep(1);
  125. }
  126. jack_client_close(client);
  127. exit (0);
  128. }