jack1 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.

118 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 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, nframes);
  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, nframes);
  49. /* printf("wrote a note on, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
  50. buffer[1] = note_frqs[j];
  51. buffer[0] = 0x90; /* note on */
  52. }
  53. else if(note_starts[j] + note_lengths[j] == loop_index)
  54. {
  55. buffer = jack_midi_event_reserve(port_buf, i, 3, nframes);
  56. /* printf("wrote a note off, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
  57. buffer[1] = note_frqs[j];
  58. buffer[0] = 0x80; /* note off */
  59. }
  60. }
  61. loop_index = loop_index+1 >= loop_nsamp ? 0 : loop_index+1;
  62. }
  63. return 0;
  64. }
  65. int main(int narg, char **args)
  66. {
  67. int i;
  68. jack_nframes_t nframes;
  69. if((narg<6) || ((narg-3)%3 !=0))
  70. {
  71. usage();
  72. exit(1);
  73. }
  74. if((client = jack_client_new (args[1])) == 0)
  75. {
  76. fprintf (stderr, "jack server not running?\n");
  77. return 1;
  78. }
  79. jack_set_process_callback (client, process, 0);
  80. output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  81. nframes = jack_get_buffer_size(client);
  82. jack_midi_reset_new_port(jack_port_get_buffer(output_port, nframes), nframes);
  83. loop_index = 0;
  84. num_notes = (narg - 3)/3;
  85. note_frqs = malloc(num_notes*sizeof(unsigned char));
  86. note_starts = malloc(num_notes*sizeof(unsigned char));
  87. note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
  88. loop_nsamp = atoi(args[2]);
  89. for(i=0; i<num_notes; i++)
  90. {
  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. {
  97. fprintf (stderr, "cannot activate client");
  98. return 1;
  99. }
  100. while (1)
  101. {
  102. sleep(1);
  103. };
  104. }