cleanup...
[jack.git] / example-clients / midiseq.c
blob072b00e99140a0b3a4b89a6ef43fe40cd45a6006
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 <stdlib.h>
23 #include <unistd.h>
25 jack_client_t *client;
26 jack_port_t *output_port;
28 unsigned char* note_frqs;
29 jack_nframes_t* note_starts;
30 jack_nframes_t* note_lengths;
31 jack_nframes_t num_notes;
32 jack_nframes_t loop_nsamp;
33 jack_nframes_t loop_index;
35 void usage()
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");
43 int process(jack_nframes_t nframes, void *arg)
45 int i,j;
46 void* port_buf = jack_port_get_buffer(output_port, nframes);
47 unsigned char* buffer;
48 jack_midi_clear_buffer(port_buf);
49 /*memset(buffer, 0, nframes*sizeof(jack_default_audio_sample_t));*/
51 for(i=0; i<nframes; i++)
53 for(j=0; j<num_notes; j++)
55 if(note_starts[j] == loop_index)
57 buffer = jack_midi_event_reserve(port_buf, i, 3);
58 /* printf("wrote a note on, port buffer = 0x%x, event buffer = 0x%x\n", port_buf, buffer);*/
59 buffer[2] = 64; /* velocity */
60 buffer[1] = note_frqs[j];
61 buffer[0] = 0x90; /* note on */
63 else if(note_starts[j] + note_lengths[j] == loop_index)
65 buffer = jack_midi_event_reserve(port_buf, i, 3);
66 /* printf("wrote a note off, 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] = 0x80; /* note off */
72 loop_index = loop_index+1 >= loop_nsamp ? 0 : loop_index+1;
74 return 0;
77 int main(int narg, char **args)
79 int i;
80 jack_nframes_t nframes;
81 if((narg<6) || ((narg-3)%3 !=0))
83 usage();
84 exit(1);
86 if((client = jack_client_new (args[1])) == 0)
88 fprintf (stderr, "jack server not running?\n");
89 return 1;
91 jack_set_process_callback (client, process, 0);
92 output_port = jack_port_register (client, "out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
93 nframes = jack_get_buffer_size(client);
94 loop_index = 0;
95 num_notes = (narg - 3)/3;
96 note_frqs = malloc(num_notes*sizeof(unsigned char));
97 note_starts = malloc(num_notes*sizeof(unsigned char));
98 note_lengths = malloc(num_notes*sizeof(jack_nframes_t));
99 loop_nsamp = atoi(args[2]);
100 for(i=0; i<num_notes; i++)
102 note_starts[i] = atoi(args[3 + 3*i]);
103 note_frqs[i] = atoi(args[4 + 3*i]);
104 note_lengths[i] = atoi(args[5 + 3*i]);
107 if (jack_activate(client))
109 fprintf (stderr, "cannot activate client");
110 return 1;
113 while (1)
115 sleep(1);