JACK example clients
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.

119 lines
3.4KB

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