cleanup...
[jack.git] / example-clients / midisine.c
blob871d4e8b71afa9d68bf457d96cecbf05f31b6591
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 <stdio.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
26 #include <jack/jack.h>
27 #include <jack/midiport.h>
29 jack_port_t *input_port;
30 jack_port_t *output_port;
31 jack_default_audio_sample_t ramp=0.0;
32 jack_default_audio_sample_t note_on;
33 unsigned char note = 0;
34 jack_default_audio_sample_t note_frqs[128];
36 void calc_note_frqs(jack_default_audio_sample_t srate)
38 int i;
39 for(i=0; i<128; i++)
41 note_frqs[i] = (2.0 * 440.0 / 32.0) * pow(2, (((jack_default_audio_sample_t)i - 9.0) / 12.0)) / srate;
45 int process(jack_nframes_t nframes, void *arg)
47 int i;
48 void* port_buf = jack_port_get_buffer(input_port, nframes);
49 jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
50 jack_midi_event_t in_event;
51 jack_nframes_t event_index = 0;
52 jack_nframes_t event_count = jack_midi_get_event_count(port_buf);
53 if(event_count > 1)
55 printf(" midisine: have %d events\n", event_count);
56 for(i=0; i<event_count; i++)
58 jack_midi_event_get(&in_event, port_buf, i);
59 printf(" event %d time is %d. 1st byte is 0x%x\n", i, in_event.time, *(in_event.buffer));
61 /* printf("1st byte of 1st event addr is %p\n", in_events[0].buffer);*/
63 jack_midi_event_get(&in_event, port_buf, 0);
64 for(i=0; i<nframes; i++)
66 if((in_event.time == i) && (event_index < event_count))
68 if( ((*(in_event.buffer) & 0xf0)) == 0x90 )
70 /* note on */
71 note = *(in_event.buffer + 1);
72 note_on = 1.0;
74 else if( ((*(in_event.buffer)) & 0xf0) == 0x80 )
76 /* note off */
77 note = *(in_event.buffer + 1);
78 note_on = 0.0;
80 event_index++;
81 if(event_index < event_count)
82 jack_midi_event_get(&in_event, port_buf, event_index);
84 ramp += note_frqs[note];
85 ramp = (ramp > 1.0) ? ramp - 2.0 : ramp;
86 out[i] = note_on*sin(2*M_PI*ramp);
88 return 0;
91 int srate(jack_nframes_t nframes, void *arg)
93 printf("the sample rate is now %" PRIu32 "/sec\n", nframes);
94 calc_note_frqs((jack_default_audio_sample_t)nframes);
95 return 0;
98 void jack_shutdown(void *arg)
100 exit(1);
103 int main(int narg, char **args)
105 jack_client_t *client;
107 if ((client = jack_client_new("midisine")) == 0)
109 fprintf(stderr, "jack server not running?\n");
110 return 1;
113 calc_note_frqs(jack_get_sample_rate (client));
115 jack_set_process_callback (client, process, 0);
117 jack_set_sample_rate_callback (client, srate, 0);
119 jack_on_shutdown (client, jack_shutdown, 0);
121 input_port = jack_port_register (client, "midi_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
122 output_port = jack_port_register (client, "audio_out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
124 if (jack_activate (client))
126 fprintf(stderr, "cannot activate client");
127 return 1;
130 /* run until interrupted */
131 while(1)
133 sleep(1);
135 jack_client_close(client);
136 exit (0);