In JackCoreAudio driver, clock drift compensation in aggregated devices working.
[jack2.git] / example-clients / midiseq.c
blob10a37b2a4d12ec42a8d0d05c6201ae96616c9413
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++)
61 for(j=0; j<num_notes; j++)
63 if(note_starts[j] == loop_index)
65 buffer = jack_midi_event_reserve(port_buf, i, 3);
66 /* printf("wrote a note on, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
67 buffer[2] = 64; /* velocity */
68 buffer[1] = note_frqs[j];
69 buffer[0] = 0x90; /* note on */
71 else if(note_starts[j] + note_lengths[j] == loop_index)
73 buffer = jack_midi_event_reserve(port_buf, i, 3);
74 /* printf("wrote a note off, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
75 buffer[2] = 64; /* velocity */
76 buffer[1] = note_frqs[j];
77 buffer[0] = 0x80; /* note off */
80 loop_index = loop_index+1 >= loop_nsamp ? 0 : loop_index+1;
82 return 0;
85 int main(int narg, char **args)
87 int i;
88 jack_nframes_t nframes;
89 if((narg<6) || ((narg-3)%3 !=0))
91 usage();
92 exit(1);
94 if((client = jack_client_open (args[1], JackNullOption, NULL)) == 0)
96 fprintf (stderr, "jack server not running?\n");
97 return 1;
99 jack_set_process_callback (client, process, 0);
100 output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
101 nframes = jack_get_buffer_size(client);
102 loop_index = 0;
103 num_notes = (narg - 3)/3;
104 note_frqs = malloc(num_notes*sizeof(unsigned char));
105 note_starts = malloc(num_notes*sizeof(unsigned char));
106 note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
107 loop_nsamp = atoi(args[2]);
108 for(i=0; i<num_notes; i++)
110 note_starts[i] = atoi(args[3 + 3*i]);
111 note_frqs[i] = atoi(args[4 + 3*i]);
112 note_lengths[i] = atoi(args[5 + 3*i]);
115 if (jack_activate(client))
117 fprintf (stderr, "cannot activate client");
118 return 1;
121 /* install a signal handler to properly quits jack client */
122 signal(SIGQUIT, signal_handler);
123 signal(SIGTERM, signal_handler);
124 signal(SIGHUP, signal_handler);
125 signal(SIGINT, signal_handler);
127 /* run until interrupted */
128 while (1) {
129 sleep(1);
132 jack_client_close(client);
133 exit (0);