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.

135 lines
3.8KB

  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. {
  51. for(j=0; j<num_notes; j++)
  52. {
  53. if(note_starts[j] == loop_index)
  54. {
  55. buffer = jack_midi_event_reserve(port_buf, i, 3);
  56. /* printf("wrote a note on, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
  57. buffer[2] = 64; /* velocity */
  58. buffer[1] = note_frqs[j];
  59. buffer[0] = 0x90; /* note on */
  60. }
  61. else if(note_starts[j] + note_lengths[j] == loop_index)
  62. {
  63. buffer = jack_midi_event_reserve(port_buf, i, 3);
  64. /* printf("wrote a note off, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
  65. buffer[2] = 64; /* velocity */
  66. buffer[1] = note_frqs[j];
  67. buffer[0] = 0x80; /* note off */
  68. }
  69. }
  70. loop_index = loop_index+1 >= loop_nsamp ? 0 : loop_index+1;
  71. }
  72. return 0;
  73. }
  74. int main(int narg, char **args)
  75. {
  76. int i;
  77. jack_nframes_t nframes;
  78. if((narg<6) || ((narg-3)%3 !=0))
  79. {
  80. usage();
  81. exit(1);
  82. }
  83. if((client = jack_client_open (args[1], JackNullOption, NULL)) == 0)
  84. {
  85. fprintf (stderr, "JACK server not running?\n");
  86. return 1;
  87. }
  88. jack_set_process_callback (client, process, 0);
  89. output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  90. nframes = jack_get_buffer_size(client);
  91. loop_index = 0;
  92. num_notes = (narg - 3)/3;
  93. note_frqs = malloc(num_notes*sizeof(unsigned char));
  94. note_starts = malloc(num_notes*sizeof(jack_nframes_t));
  95. note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
  96. loop_nsamp = atoi(args[2]);
  97. for(i=0; i<num_notes; i++)
  98. {
  99. note_starts[i] = atoi(args[3 + 3*i]);
  100. note_frqs[i] = atoi(args[4 + 3*i]);
  101. note_lengths[i] = atoi(args[5 + 3*i]);
  102. }
  103. if (jack_activate(client))
  104. {
  105. fprintf (stderr, "cannot activate client");
  106. return 1;
  107. }
  108. /* install a signal handler to properly quits jack client */
  109. signal(SIGQUIT, signal_handler);
  110. signal(SIGTERM, signal_handler);
  111. signal(SIGHUP, signal_handler);
  112. signal(SIGINT, signal_handler);
  113. /* run until interrupted */
  114. while (1) {
  115. sleep(1);
  116. };
  117. jack_client_close(client);
  118. exit (0);
  119. }