Disabled a debug message.
[epichord.git] / src / jack.cpp
blobee7001307dbbed8556dca1c658cb16cb374548da
1 /*
2 Epichord - a midi sequencer
3 Copyright (C) 2008 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
23 #include <stdio.h>
24 #include <string.h>
26 #include <vector>
27 #include <string>
29 #include <pthread.h>
30 #include <jack/jack.h>
31 #include <jack/types.h>
32 #include <jack/ringbuffer.h>
33 #include <jack/midiport.h>
34 #include <jack/transport.h>
36 #define HAVE_LASH
38 #ifdef HAVE_LASH
39 #include <lash/lash.h>
40 lash_client_t* lash_client;
41 #endif
43 #include "seq.h"
44 #include "backend.h"
47 //#include "ui.h"
49 #define PORT_COUNT 8
51 //#define HAVE_LASH
53 extern std::vector<track*> tracks;
55 jack_client_t* client;
57 jack_port_t* outport[PORT_COUNT];
58 jack_port_t* inport;
59 void* pbo[PORT_COUNT];
60 void* pbi;
62 jack_ringbuffer_t* outbuf;
63 jack_ringbuffer_t* inbuf;
65 static int playing = 0;
66 static int looping = 0;
67 static int recording=0;
68 static int loop_start = 0;
69 static int loop_end = 512;
70 static int passthru = 1;
71 static int rec_port = 0;
73 static int init_chans = 1;
75 static uint64_t cur_frame = 0;
76 static uint64_t last_frame = 0;
77 static int frame_jump = 0;
78 static int cur_tick;
79 static int last_tick;
80 static int bpm = 120;
81 static int new_bpm = 120;
82 static int tpb = TICKS_PER_BEAT;
83 static int sample_rate = 0;
84 static int frame_count = 0;
86 #define t2f(X) ((uint64_t)X*60*sample_rate/(bpm*tpb))
87 #define f2t(X) ((uint64_t)X*bpm*tpb/(sample_rate*60))
91 /* callback for seq.c and play.c */
92 void dispatch_event(mevent* e, int track, int tick_base){
94 jack_midi_data_t* md;
96 int p = tracks[track]->port;
97 int c = tracks[track]->chan;
99 //uint64_t base = t2f(tick_base);
100 //uint64_t frame = t2f(e->tick) + base - last_frame + frame_jump;
101 uint64_t eframe = t2f((uint64_t)(e->tick+tick_base));
102 uint64_t frame = eframe - last_frame + frame_jump;
104 if(e->type == -1){//dummy events
105 return;
108 if(looping && e->tick+tick_base == loop_end && e->type != MIDI_NOTE_OFF){
109 //dispatch only note off events on a loop_end
110 return;
113 //printf("%d %d %d %llu %llu %llu\n",e->tick,tick_base,e->tick+tick_base, eframe, last_frame, frame);
115 if(eframe < last_frame){
116 printf("dispatch: %llu %llu negative frame index. BOOM segfault.\n", t2f(e->tick+tick_base), last_frame);
117 return;
120 if(frame == frame_count){
121 //printf("dispatch: scheduling bug. frame index == frame count.\n");
122 frame--;
125 unsigned char buf[3];
126 size_t n;
127 if(midi_encode(e,c,buf,&n) < 0){
128 return;
131 md = jack_midi_event_reserve(pbo[p],frame,n);
132 if(md == NULL){
133 md = jack_midi_event_reserve(pbo[p],frame_count-1,n);
134 if(md == NULL){
135 printf("dispatch: can't reserve midi event.\n");
136 return;
138 printf("dispatch: send midi using scheduling kludge.\n");
140 for(int i=0; i<n; i++){
141 md[i] = buf[i];
148 /* jack callbacks */
150 static int H = 1;
151 static int process(jack_nframes_t nframes, void* arg){
153 frame_count = nframes;
155 //handle incoming midi events
156 for(int i=0; i<PORT_COUNT; i++){
157 pbo[i] = jack_port_get_buffer(outport[i],nframes);
158 jack_midi_clear_buffer(pbo[i]);
160 pbi = jack_port_get_buffer(inport,nframes);
162 jack_midi_data_t* md1;
163 jack_midi_data_t* md2;
165 jack_midi_event_t me;
167 uint32_t rec_tick;
169 for(int i=0; i<jack_midi_get_event_count(pbi); i++){
170 jack_midi_event_get(&me,pbi,i);
171 md1 = me.buffer;
172 //printf("%d got midi event, type 0x%x, value 0x%x 0x%x\n",i,md1[0],md1[1],md1[2]);
173 if(passthru){
174 md2 = jack_midi_event_reserve(pbo[rec_port],0,me.size);
175 if(md2 == NULL){
176 printf("passthru: can't reserve midi event\n");
178 else{
179 memcpy(md2,md1,me.size);
182 rec_tick = last_tick + (me.time*bpm*tpb)/(sample_rate*60);
184 //if(playing && recording){
185 uint16_t size = me.size;
186 jack_ringbuffer_write(inbuf,(char*)&rec_tick,4);
187 jack_ringbuffer_write(inbuf,(char*)&size,2);
188 jack_ringbuffer_write(inbuf,(char*)md1,me.size);
189 // }
192 //init chans
193 if(init_chans && playing){
194 init_chans=0;
195 char buf[3];
196 for(int j=0; j<tracks.size(); j++){
197 int ch = tracks[j]->chan;
198 int pt = tracks[j]->port;
200 //bank select
201 buf[0] = 0xB0 | ch;
202 buf[1] = 0;
203 buf[2] = tracks[j]->bank;
204 send_midi(buf,3,pt);
206 //program change
207 buf[0] = 0xC0 | ch;
208 buf[1] = tracks[j]->prog;
209 send_midi(buf,2,pt);
211 //channel volume
212 buf[0] = 0xB0 | ch;
213 buf[1] = 7;
214 buf[2] = tracks[j]->vol;
215 send_midi(buf,3,pt);
217 //channel pan
218 buf[0] = 0xB0 | ch;
219 buf[1] = 10;
220 buf[2] = tracks[j]->pan;
221 send_midi(buf,3,pt);
226 //handle outgoing immediate midi events
227 uint16_t n;
228 char p;
229 char buf[1024];
230 while(jack_ringbuffer_read_space(outbuf) > 0){
231 jack_ringbuffer_read(outbuf,&p,1);
232 jack_ringbuffer_read(outbuf,(char*)&n,2);
233 jack_ringbuffer_read(outbuf,buf,n);
234 md1 = jack_midi_event_reserve(pbo[p],0,n);
235 if(md1 == NULL){
236 printf("gui: can't reserve midi event\n");
237 break;
239 for(int i=0; i<n; i++){
240 md1[i]=buf[i];
245 //play
246 if(playing){
247 if(bpm != new_bpm){
248 if(new_bpm != 0){
249 bpm = new_bpm;
251 else{
252 printf("process: someone set the bpm to zero!\n");
253 bpm = 120;
255 cur_frame = t2f(cur_tick);
256 last_frame = t2f(last_tick);
259 last_frame = cur_frame;
260 cur_frame += nframes;
261 last_tick = cur_tick;
262 cur_tick = f2t(cur_frame);
263 frame_jump = 0;
264 int le = loop_end;
265 int ls = loop_start;
266 int lf = looping;
269 /* this only handles the case where the nframes covers the
270 loop boundary once. a more robust way would split it into
271 before covering n loop boundaries, a loop for n looping regions,
272 and a final section covering part of the loop region. this
273 would only need to be used for insanely high bpm */
274 if(lf && last_tick < le && cur_tick > le ){
275 //printf("split l%d m%d r%d\n",last_tick,loop_end,cur_tick);
276 int split_frame = t2f(le) - last_frame + 1;
277 int left_over = nframes - split_frame;
278 int jump = split_frame;
279 cur_frame = last_frame + jump;
280 cur_tick = f2t(cur_frame);
281 //printf("cur tick %d\n",cur_tick);
282 play_seq(cur_tick);
284 reset_backend(ls);
285 //printf("cur tick %d\n",cur_tick);
286 frame_jump = jump;
287 last_frame = t2f(ls);
288 cur_frame = last_frame + left_over;
289 cur_tick = f2t(cur_frame);
291 play_seq(cur_tick);
293 else if(lf && cur_tick > le){
294 reset_backend(ls);
295 last_frame = t2f(ls);
296 cur_frame = last_frame + nframes;
297 cur_tick = f2t(cur_frame);
298 //printf("%llu %llu %d %d\n",last_frame,cur_frame,last_tick,cur_tick);
299 play_seq(cur_tick);
301 else{
302 //printf("%llu %llu %d %d\n",last_frame,cur_frame,last_tick,cur_tick);
303 play_seq(cur_tick);
307 return 0;
314 /* backend api wrapper */
316 int init_backend(int* argc, char*** argv){
318 client = jack_client_open("Epichord",(jack_options_t)0,NULL);
319 if(client == NULL){
320 printf("init_backend: failure to open jack client\n");
321 return -1;
324 jack_set_process_callback(client, process, 0);
326 char buf[64];
327 for(int i=0; i<PORT_COUNT; i++){
328 sprintf(buf,"midi out %d",i);
329 outport[i]=jack_port_register(client,buf,JACK_DEFAULT_MIDI_TYPE,JackPortIsOutput,0);
332 inport=jack_port_register(client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
334 outbuf = jack_ringbuffer_create(1024);
335 inbuf = jack_ringbuffer_create(1024);
337 sample_rate = jack_get_sample_rate(client);
339 jack_activate(client);
341 #ifdef HAVE_LASH
343 lash_client = lash_init(lash_extract_args(argc, argv), "Epichord",
344 LASH_Config_File, LASH_PROTOCOL( 2, 0 ));
345 if(!lash_client){
346 printf("init_backend: lash failed to initialize\n");
348 else{
349 lash_jack_client_name(lash_client, "Epichord");
352 #endif
354 return 0;
360 int shutdown_backend(){
361 pause_backend();
362 all_notes_off();
363 sleep(1);
364 jack_deactivate(client);
365 //sleep(1);
366 return 0;
370 int start_backend(){
371 playing = 1;
374 int pause_backend(){
375 playing = 0;
378 int reset_backend(int tick){
379 if(tick==0){
380 init_chans = 1;
382 cur_frame = t2f(tick);
383 last_frame = cur_frame;
384 last_tick = tick;
385 cur_tick = tick;
386 set_seq_pos(tick);
390 void toggle_backend_recording(){
391 if(recording){
392 recording = 0;
394 else{
395 recording = 1;
400 //send a midi event from the gui thread
401 void send_midi(char* raw, uint16_t n, uint8_t port){
402 if(n>1024){
403 printf("send_midi: cant send, immediate message too big\n");
404 return;
406 char buf[2048];
407 buf[0] = port;
408 memcpy(buf+1,&n,2);
409 memcpy(buf+3,raw,n);
410 //FIXME check return value
411 jack_ringbuffer_write(outbuf,buf,3+n);
414 void send_midi_local(char* raw, uint16_t n){
415 uint32_t tick = cur_tick;
416 uint16_t size = n;
417 //FIXME chec return value
418 jack_ringbuffer_write(inbuf,(char*)&tick,4);
419 jack_ringbuffer_write(inbuf,(char*)&size,2);
420 jack_ringbuffer_write(inbuf,raw,n);
425 std::string sysexbuf;
426 //get next incoming midi event for gui thread
427 int recv_midi(int* chan, int* tick, int* type, int* val1, int* val2){
428 uint16_t n;
429 uint32_t t;
430 unsigned char* buf;
431 if(jack_ringbuffer_read(inbuf,(char*)&t,4) == 0){
432 return 0;
434 jack_ringbuffer_read(inbuf,(char*)&n,2);
435 buf = (unsigned char*)malloc(n);
436 jack_ringbuffer_read(inbuf,(char*)buf,n);
438 *tick = t;
439 *chan = buf[0]&0x0f;
440 *type = buf[0]&0xf0;
441 *val1 = buf[1];
442 *val2 = buf[2];
444 char hbuf[8];
446 if (buf[0]==0xf0){
447 sysexbuf = "";
448 for(int i=2; i<n-1; i++){
449 snprintf(hbuf,8,"%02x ",buf[i]);
450 sysexbuf.append(hbuf);
454 free(buf);
455 return 1;
458 const char* getsysexbuf(){
459 return sysexbuf.c_str();
463 void all_notes_off(){
464 for(int i=0; i<tracks.size(); i++){
465 midi_track_off(i);
469 void program_change(int track, int prog){
470 int port = tracks[track]->port;
471 int chan = tracks[track]->chan;
473 char buf[2];
474 buf[0] = 0xC0 | chan;
475 buf[1] = prog;
476 send_midi(buf, 2, port);
479 void midi_bank_controller(int track, int bank){
480 int port = tracks[track]->port;
481 int chan = tracks[track]->chan;
483 char buf[3];
484 buf[0] = 0xB0 | chan;
485 buf[1] = 0;
486 buf[2] = bank;
487 send_midi(buf, 3, port);
490 void midi_volume_controller(int track, int vol){
491 int port = tracks[track]->port;
492 int chan = tracks[track]->chan;
494 char buf[3];
495 buf[0] = 0xB0 | chan;
496 buf[1] = 7;
497 buf[2] = vol;
498 send_midi(buf, 3, port);
501 void midi_pan_controller(int track, int pan){
502 int port = tracks[track]->port;
503 int chan = tracks[track]->chan;
505 char buf[3];
506 buf[0] = 0xB0 | chan;
507 buf[1] = 10;
508 buf[2] = pan;
509 send_midi(buf, 3, port);
512 void midi_expression_controller(int track, int expr){
513 int port = tracks[track]->port;
514 int chan = tracks[track]->chan;
516 char buf[3];
517 buf[0] = 0xB0 | chan;
518 buf[1] = 11;
519 buf[2] = expr;
520 send_midi(buf, 3, port);
524 void midi_note_off(int note, int chan, int port){
525 char buf[3];
526 buf[0] = 0x80 | chan;
527 buf[1] = note;
528 buf[2] = 0x00;
529 send_midi(buf,3,port);
532 void midi_track_off(int track){
533 int chan = tracks[track]->chan;
534 int port = tracks[track]->port;
536 midi_channel_off(chan,port);
539 void midi_channel_off(int chan, int port){
540 char buf[3] = {0xB0,123,0};
541 buf[0] = 0xB0 | chan;
542 send_midi(buf,3,port);
544 buf[0] = 0xB0 | chan;
545 buf[1] = 120;
546 send_midi(buf,3,port);
550 void backend_set_passthru(int v){
551 passthru = v;
554 void set_loop_start(int tick){
555 loop_start = tick;
558 void set_loop_end(int tick){
559 loop_end = tick;
562 int get_loop_start(){
563 return loop_start;
566 int get_loop_end(){
567 return loop_end;
570 int get_play_position(){
571 return cur_tick;
574 int solo;
575 void set_solo(int s){
576 solo = s;
579 int get_solo(){
580 return solo;
583 int is_backend_playing(){
584 return playing;
587 int is_backend_recording(){
588 return recording&&playing;
591 void toggle_loop(){
592 if(looping){
593 looping = 0;
595 else{
596 looping = 1;
601 void set_bpm(int n){
602 new_bpm = n;
606 char* session_string;
607 int backend_session_process(){
608 int ret = SESSION_NOMORE;
609 #ifdef HAVE_LASH
610 lash_event_t *e;
612 char *name;
614 e = lash_get_event(lash_client);
615 if(!e){
616 return SESSION_NOMORE;
619 asprintf(&session_string,"%s",lash_event_get_string(e));
620 const int t = lash_event_get_type (e);
622 switch(t)
624 case LASH_Save_File:
625 printf("session_process: LASH save\n");
626 lash_send_event(lash_client, lash_event_new_with_type(LASH_Save_File));
627 ret = SESSION_SAVE;
628 break;
629 case LASH_Restore_File:
630 printf("session_process: LASH load\n");
631 lash_send_event(lash_client, lash_event_new_with_type(LASH_Restore_File));
632 ret = SESSION_LOAD;
633 break;
634 case LASH_Quit:
635 printf("session_process: LASH quit\n");
636 ret = SESSION_QUIT;
637 break;
638 default:
639 printf("session_process: unhandled LASH event (%d)\n", t);
640 switch(t){
641 case LASH_Client_Name: printf("LASH_Client_Name\n"); break;
642 case LASH_Jack_Client_Name: printf("LASH_Jack_Client_Name\n"); break;
643 case LASH_Alsa_Client_ID: printf("LASH_Alsa_Client_ID\n"); break;
644 case LASH_Save_File: printf("LASH_Save_File\n"); break;
645 case LASH_Save_Data_Set: printf("LASH_Save_Data_Set\n"); break;
646 case LASH_Restore_Data_Set: printf("LASH_Restore_Data_Set\n"); break;
647 case LASH_Save: printf("LASH_Save\n"); break;
649 ret = SESSION_UNHANDLED;
650 break;
652 //lash_event_destroy(e);
653 #endif
654 return ret;
657 char* get_session_string(){
658 return session_string;