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.

132 lines
4.1KB

  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 <jack/jack.h>
  16. #include <jack/midiport.h>
  17. #include <stdio.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. jack_client_t *client;
  22. jack_port_t *output_port;
  23. unsigned char* note_frqs;
  24. jack_nframes_t* note_starts;
  25. jack_nframes_t* note_lengths;
  26. jack_nframes_t num_notes;
  27. jack_nframes_t loop_nsamp;
  28. jack_nframes_t loop_index;
  29. static void signal_handler(int sig)
  30. {
  31. jack_client_close(client);
  32. fprintf(stderr, "signal received, exiting ...\n");
  33. exit(0);
  34. }
  35. static void usage()
  36. {
  37. fprintf(stderr, "usage: jack_midiseq name nsamp [startindex note nsamp] ...... [startindex note nsamp]\n");
  38. fprintf(stderr, "eg: jack_midiseq Sequencer 24000 0 60 8000 12000 63 8000\n");
  39. fprintf(stderr, "will play a 1/2 sec loop (if srate is 48khz) with a c4 note at the start of the loop\n");
  40. fprintf(stderr, "that lasts for 12000 samples, then a d4# that starts at 1/4 sec that lasts for 800 samples\n");
  41. }
  42. static int process(jack_nframes_t nframes, void *arg)
  43. {
  44. int i,j;
  45. void* port_buf = jack_port_get_buffer(output_port, nframes);
  46. unsigned char* buffer;
  47. jack_midi_clear_buffer(port_buf);
  48. /*memset(buffer, 0, nframes*sizeof(jack_default_audio_sample_t));*/
  49. for (i = 0; i < nframes; i++) {
  50. for (j = 0; j < num_notes; j++) {
  51. if (note_starts[j] == loop_index) {
  52. if ((buffer = jack_midi_event_reserve(port_buf, i, 3))) {
  53. /* printf("wrote a note on, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer); */
  54. buffer[2] = 64; /* velocity */
  55. buffer[1] = note_frqs[j];
  56. buffer[0] = 0x90; /* note on */
  57. }
  58. } else if (note_starts[j] + note_lengths[j] == loop_index) {
  59. if ((buffer = jack_midi_event_reserve(port_buf, i, 3))) {
  60. /* printf("wrote a note off, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer); */
  61. buffer[2] = 64; /* velocity */
  62. buffer[1] = note_frqs[j];
  63. buffer[0] = 0x80; /* note off */
  64. }
  65. }
  66. }
  67. loop_index = loop_index+1 >= loop_nsamp ? 0 : loop_index+1;
  68. }
  69. return 0;
  70. }
  71. int main(int narg, char **args)
  72. {
  73. int i;
  74. if ((narg<6) || ((narg-3)%3 != 0)) {
  75. usage();
  76. exit(1);
  77. }
  78. if ((client = jack_client_open (args[1], JackNullOption, NULL)) == 0) {
  79. fprintf (stderr, "JACK server not running?\n");
  80. return 1;
  81. }
  82. jack_set_process_callback (client, process, 0);
  83. output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  84. loop_index = 0;
  85. num_notes = (narg - 3)/3;
  86. note_frqs = malloc(num_notes*sizeof(unsigned char));
  87. note_starts = malloc(num_notes*sizeof(jack_nframes_t));
  88. note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
  89. loop_nsamp = atoi(args[2]);
  90. for (i = 0; i < num_notes; i++) {
  91. note_starts[i] = atoi(args[3 + 3*i]);
  92. note_frqs[i] = atoi(args[4 + 3*i]);
  93. note_lengths[i] = atoi(args[5 + 3*i]);
  94. }
  95. if (jack_activate(client)) {
  96. fprintf (stderr, "cannot activate client");
  97. return 1;
  98. }
  99. /* install a signal handler to properly quits jack client */
  100. #ifndef WIN32
  101. signal(SIGQUIT, signal_handler);
  102. signal(SIGHUP, signal_handler);
  103. #endif
  104. signal(SIGTERM, signal_handler);
  105. signal(SIGINT, signal_handler);
  106. /* run until interrupted */
  107. while (1) {
  108. #ifdef WIN32
  109. Sleep(1*1000);
  110. #else
  111. sleep(1);
  112. #endif
  113. };
  114. jack_client_close(client);
  115. exit (0);
  116. }