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.

134 lines
4.2KB

  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. jack_nframes_t nframes;
  75. if ((narg<6) || ((narg-3)%3 != 0)) {
  76. usage();
  77. exit(1);
  78. }
  79. if ((client = jack_client_open (args[1], JackNullOption, NULL)) == 0) {
  80. fprintf (stderr, "JACK server not running?\n");
  81. return 1;
  82. }
  83. jack_set_process_callback (client, process, 0);
  84. output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  85. nframes = jack_get_buffer_size(client);
  86. loop_index = 0;
  87. num_notes = (narg - 3)/3;
  88. note_frqs = malloc(num_notes*sizeof(unsigned char));
  89. note_starts = malloc(num_notes*sizeof(jack_nframes_t));
  90. note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
  91. loop_nsamp = atoi(args[2]);
  92. for (i = 0; i < num_notes; i++) {
  93. note_starts[i] = atoi(args[3 + 3*i]);
  94. note_frqs[i] = atoi(args[4 + 3*i]);
  95. note_lengths[i] = atoi(args[5 + 3*i]);
  96. }
  97. if (jack_activate(client)) {
  98. fprintf (stderr, "cannot activate client");
  99. return 1;
  100. }
  101. /* install a signal handler to properly quits jack client */
  102. #ifndef WIN32
  103. signal(SIGQUIT, signal_handler);
  104. signal(SIGHUP, signal_handler);
  105. #endif
  106. signal(SIGTERM, signal_handler);
  107. signal(SIGINT, signal_handler);
  108. /* run until interrupted */
  109. while (1) {
  110. #ifdef WIN32
  111. Sleep(1*1000);
  112. #else
  113. sleep(1);
  114. #endif
  115. };
  116. jack_client_close(client);
  117. exit (0);
  118. }