Rename system_midi:* back to system:midi_*
[jack2.git] / example-clients / midiseq.c
blob45085e80a937b65e40b34929a7ca09688fa076a4
1 /*
2 Copyright (C) 2004 Ian Esten
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <jack/jack.h>
20 #include <jack/midiport.h>
21 #include <stdio.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 jack_client_t *client;
27 jack_port_t *output_port;
29 unsigned char* note_frqs;
30 jack_nframes_t* note_starts;
31 jack_nframes_t* note_lengths;
32 jack_nframes_t num_notes;
33 jack_nframes_t loop_nsamp;
34 jack_nframes_t loop_index;
36 static void signal_handler(int sig)
38 jack_client_close(client);
39 fprintf(stderr, "signal received, exiting ...\n");
40 exit(0);
43 static void usage()
45 fprintf(stderr, "usage: jack_midiseq name nsamp [startindex note nsamp] ...... [startindex note nsamp]\n");
46 fprintf(stderr, "eg: jack_midiseq Sequencer 24000 0 60 8000 12000 63 8000\n");
47 fprintf(stderr, "will play a 1/2 sec loop (if srate is 48khz) with a c4 note at the start of the loop\n");
48 fprintf(stderr, "that lasts for 12000 samples, then a d4# that starts at 1/4 sec that lasts for 800 samples\n");
51 static int process(jack_nframes_t nframes, void *arg)
53 int i,j;
54 void* port_buf = jack_port_get_buffer(output_port, nframes);
55 unsigned char* buffer;
56 jack_midi_clear_buffer(port_buf);
57 /*memset(buffer, 0, nframes*sizeof(jack_default_audio_sample_t));*/
59 for (i = 0; i < nframes; i++) {
60 for (j = 0; j < num_notes; j++) {
61 if (note_starts[j] == loop_index) {
62 if ((buffer = jack_midi_event_reserve(port_buf, i, 3))) {
63 /* printf("wrote a note on, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer); */
64 buffer[2] = 64; /* velocity */
65 buffer[1] = note_frqs[j];
66 buffer[0] = 0x90; /* note on */
68 } else if (note_starts[j] + note_lengths[j] == loop_index) {
69 if ((buffer = jack_midi_event_reserve(port_buf, i, 3))) {
70 /* printf("wrote a note off, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer); */
71 buffer[2] = 64; /* velocity */
72 buffer[1] = note_frqs[j];
73 buffer[0] = 0x80; /* note off */
77 loop_index = loop_index+1 >= loop_nsamp ? 0 : loop_index+1;
79 return 0;
82 int main(int narg, char **args)
84 int i;
85 jack_nframes_t nframes;
86 if ((narg<6) || ((narg-3)%3 != 0)) {
87 usage();
88 exit(1);
90 if ((client = jack_client_open (args[1], JackNullOption, NULL)) == 0) {
91 fprintf (stderr, "JACK server not running?\n");
92 return 1;
94 jack_set_process_callback (client, process, 0);
95 output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
96 nframes = jack_get_buffer_size(client);
97 loop_index = 0;
98 num_notes = (narg - 3)/3;
99 note_frqs = malloc(num_notes*sizeof(unsigned char));
100 note_starts = malloc(num_notes*sizeof(jack_nframes_t));
101 note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
102 loop_nsamp = atoi(args[2]);
103 for (i = 0; i < num_notes; i++) {
104 note_starts[i] = atoi(args[3 + 3*i]);
105 note_frqs[i] = atoi(args[4 + 3*i]);
106 note_lengths[i] = atoi(args[5 + 3*i]);
109 if (jack_activate(client)) {
110 fprintf (stderr, "cannot activate client");
111 return 1;
114 /* install a signal handler to properly quits jack client */
115 #ifndef WIN32
116 signal(SIGQUIT, signal_handler);
117 signal(SIGHUP, signal_handler);
118 #endif
119 signal(SIGTERM, signal_handler);
120 signal(SIGINT, signal_handler);
122 /* run until interrupted */
123 while (1) {
124 #ifdef WIN32
125 Sleep(1*1000);
126 #else
127 sleep(1);
128 #endif
131 jack_client_close(client);
132 exit (0);