Added unclone tool. Adjusted GUI. Added icons.
[epichord.git] / src / jack.cpp
blobb69026fc82c0256b52e2ee9a6c32438ead98e4de
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 //void (*update_display)() = NULL;
93 /* callback for seq.c and play.c */
94 /* THIS BARELY WORKS */
95 void dispatch_event(mevent* e, int track, int tick_base){
97 jack_midi_data_t* md;
99 int p = tracks[track]->port;
100 int c = tracks[track]->chan;
102 uint64_t base = t2f(tick_base);
103 uint64_t frame = t2f(e->tick) + base - last_frame + frame_jump;
105 if(e->type == -1){
106 return;
109 if(looping && e->tick+tick_base == loop_end && e->type != MIDI_NOTE_OFF){
110 //dispatch only note off events on a loop_end
111 return;
114 //printf("0x%x %d %llu %llu %llu %d %llu %d\n", e->type, e->tick, t2f(e->tick), base, last_frame, frame_jump, frame, bpm);
116 if(t2f(e->tick)+base < last_frame){
117 printf("playing a note in the past?? lucky it didnt segfault!\n");
118 return;
121 if(frame == frame_count){
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;
139 for(int i=0; i<n; i++){
140 md[i] = buf[i];
147 /* jack callbacks */
149 static int H = 1;
150 static int process(jack_nframes_t nframes, void* arg){
152 frame_count = nframes;
154 //handle incoming midi events
155 for(int i=0; i<PORT_COUNT; i++){
156 pbo[i] = jack_port_get_buffer(outport[i],nframes);
157 jack_midi_clear_buffer(pbo[i]);
159 pbi = jack_port_get_buffer(inport,nframes);
161 jack_midi_data_t* md1;
162 jack_midi_data_t* md2;
164 jack_midi_event_t me;
166 uint32_t rec_tick;
168 for(int i=0; i<jack_midi_get_event_count(pbi); i++){
169 jack_midi_event_get(&me,pbi,i);
170 md1 = me.buffer;
171 //printf("%d got midi event, type 0x%x, value 0x%x 0x%x\n",i,md1[0],md1[1],md1[2]);
172 if(passthru){
173 md2 = jack_midi_event_reserve(pbo[rec_port],0,me.size);
174 if(md2 == NULL){
175 printf("passthru: can't reserve midi event\n");
177 else{
178 memcpy(md2,md1,me.size);
181 rec_tick = last_tick + (me.time*bpm*tpb)/(sample_rate*60);
183 //if(playing && recording){
184 uint16_t size = me.size;
185 jack_ringbuffer_write(inbuf,(char*)&rec_tick,4);
186 jack_ringbuffer_write(inbuf,(char*)&size,2);
187 jack_ringbuffer_write(inbuf,(char*)md1,me.size);
188 // }
191 //init chans
192 if(init_chans && playing){
193 init_chans=0;
194 char buf[3];
195 for(int j=0; j<tracks.size(); j++){
196 int ch = tracks[j]->chan;
197 int pt = tracks[j]->port;
199 //bank select
200 buf[0] = 0xB0 | ch;
201 buf[1] = 0;
202 buf[2] = tracks[j]->bank;
203 send_midi(buf,3,pt);
205 //program change
206 buf[0] = 0xC0 | ch;
207 buf[1] = tracks[j]->prog;
208 send_midi(buf,2,pt);
210 //channel volume
211 buf[0] = 0xB0 | ch;
212 buf[1] = 7;
213 buf[2] = tracks[j]->vol;
214 send_midi(buf,3,pt);
216 //channel pan
217 buf[0] = 0xB0 | ch;
218 buf[1] = 10;
219 buf[2] = tracks[j]->pan;
220 send_midi(buf,3,pt);
225 //handle outgoing immediate midi events
226 uint16_t n;
227 char p;
228 char buf[1024];
229 while(jack_ringbuffer_read_space(outbuf) > 0){
230 jack_ringbuffer_read(outbuf,&p,1);
231 jack_ringbuffer_read(outbuf,(char*)&n,2);
232 jack_ringbuffer_read(outbuf,buf,n);
233 md1 = jack_midi_event_reserve(pbo[p],0,n);
234 if(md1 == NULL){
235 printf("gui: can't reserve midi event\n");
236 break;
238 for(int i=0; i<n; i++){
239 md1[i]=buf[i];
244 //play
245 if(playing){
246 if(bpm != new_bpm){
247 bpm = new_bpm;
248 cur_frame = t2f(cur_tick);
249 last_frame = t2f(last_tick);
252 last_frame = cur_frame;
253 cur_frame += nframes;
254 last_tick = cur_tick;
255 cur_tick = f2t(cur_frame);
256 frame_jump = 0;
257 int le = loop_end;
258 int ls = loop_start;
259 int lf = looping;
262 /* this only handles the case where the nframes covers the
263 loop boundary once. a more robust way would split it into
264 before covering n loop boundaries, a loop for n looping regions,
265 and a final section covering part of the loop region. this
266 would only need to be used for insanely high bpm */
267 if(lf && last_tick < le && cur_tick > le ){
268 //printf("split l%d m%d r%d\n",last_tick,loop_end,cur_tick);
269 int split_frame = t2f(le) - last_frame + 1;
270 int left_over = nframes - split_frame;
271 int jump = split_frame;
272 cur_frame = last_frame + jump;
273 cur_tick = f2t(cur_frame);
274 //printf("cur tick %d\n",cur_tick);
275 play_seq(cur_tick, dispatch_event);
277 reset_backend(ls);
278 //printf("cur tick %d\n",cur_tick);
279 frame_jump = jump;
280 last_frame = t2f(ls);
281 cur_frame = last_frame + left_over;
282 cur_tick = f2t(cur_frame);
284 play_seq(cur_tick, dispatch_event);
286 else if(lf && cur_tick > le){
287 reset_backend(ls);
288 last_frame = t2f(ls);
289 cur_frame = last_frame + nframes;
290 cur_tick = f2t(cur_frame);
291 //printf("%llu %llu %d %d\n",last_frame,cur_frame,last_tick,cur_tick);
292 play_seq(cur_tick, dispatch_event);
294 else{
295 //printf("%llu %llu %d %d\n",last_frame,cur_frame,last_tick,cur_tick);
296 play_seq(cur_tick, dispatch_event);
300 return 0;
307 /* backend api wrapper */
309 int init_backend(int* argc, char*** argv){
311 client = jack_client_open("Epichord",(jack_options_t)0,NULL);
312 if(client == NULL){
313 printf("failure to open jack client\n");
314 return -1;
317 jack_set_process_callback(client, process, 0);
319 char buf[64];
320 for(int i=0; i<PORT_COUNT; i++){
321 sprintf(buf,"midi out %d",i);
322 outport[i]=jack_port_register(client,buf,JACK_DEFAULT_MIDI_TYPE,JackPortIsOutput,0);
325 inport=jack_port_register(client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
327 outbuf = jack_ringbuffer_create(1024);
328 inbuf = jack_ringbuffer_create(1024);
330 sample_rate = jack_get_sample_rate(client);
332 jack_activate(client);
334 #ifdef HAVE_LASH
336 lash_client = lash_init(lash_extract_args(argc, argv), "Epichord",
337 LASH_Config_File, LASH_PROTOCOL( 2, 0 ));
338 if(!lash_client){
339 printf("lash failed to initialize\n");
341 else{
342 lash_jack_client_name(lash_client, "Epichord");
345 #endif
347 return 0;
353 int shutdown_backend(){
354 pause_backend();
355 all_notes_off();
356 sleep(1);
357 jack_deactivate(client);
358 //sleep(1);
359 return 0;
363 int start_backend(){
364 playing = 1;
367 int pause_backend(){
368 playing = 0;
371 int reset_backend(int tick){
372 if(tick==0){
373 init_chans = 1;
375 cur_frame = t2f(tick);
376 last_frame = cur_frame;
377 last_tick = tick;
378 cur_tick = tick;
379 set_seq_pos(tick);
383 void toggle_backend_recording(){
384 if(recording){
385 recording = 0;
387 else{
388 recording = 1;
393 //send a midi event from the gui thread
394 void send_midi(char* raw, uint16_t n, uint8_t port){
395 if(n>1024){
396 printf("send_midi: cant send, immediate message too big\n");
397 return;
399 char buf[2048];
400 buf[0] = port;
401 memcpy(buf+1,&n,2);
402 memcpy(buf+3,raw,n);
403 //FIXME check return value
404 jack_ringbuffer_write(outbuf,buf,3+n);
407 void send_midi_local(char* raw, uint16_t n){
408 uint32_t tick = cur_tick;
409 uint16_t size = n;
410 //FIXME chec return value
411 jack_ringbuffer_write(inbuf,(char*)&tick,4);
412 jack_ringbuffer_write(inbuf,(char*)&size,2);
413 jack_ringbuffer_write(inbuf,raw,n);
418 std::string sysexbuf;
419 //get next incoming midi event for gui thread
420 int recv_midi(int* chan, int* tick, int* type, int* val1, int* val2){
421 uint16_t n;
422 uint32_t t;
423 unsigned char* buf;
424 if(jack_ringbuffer_read(inbuf,(char*)&t,4) == 0){
425 return 0;
427 jack_ringbuffer_read(inbuf,(char*)&n,2);
428 buf = (unsigned char*)malloc(n);
429 jack_ringbuffer_read(inbuf,(char*)buf,n);
431 *tick = t;
432 *chan = buf[0]&0x0f;
433 *type = buf[0]&0xf0;
434 *val1 = buf[1];
435 *val2 = buf[2];
437 char hbuf[8];
439 if (buf[0]==0xf0){
440 sysexbuf = "";
441 for(int i=2; i<n-1; i++){
442 snprintf(hbuf,8,"%02x ",buf[i]);
443 sysexbuf.append(hbuf);
447 free(buf);
448 return 1;
451 const char* getsysexbuf(){
452 return sysexbuf.c_str();
456 void all_notes_off(){
457 for(int i=0; i<tracks.size(); i++){
458 midi_track_off(i);
462 void program_change(int track, int prog){
463 int port = tracks[track]->port;
464 int chan = tracks[track]->chan;
466 char buf[2];
467 buf[0] = 0xC0 | chan;
468 buf[1] = prog;
469 send_midi(buf, 2, port);
472 void midi_bank_controller(int track, int bank){
473 int port = tracks[track]->port;
474 int chan = tracks[track]->chan;
476 char buf[3];
477 buf[0] = 0xB0 | chan;
478 buf[1] = 0;
479 buf[2] = bank;
480 send_midi(buf, 3, port);
483 void midi_volume_controller(int track, int vol){
484 int port = tracks[track]->port;
485 int chan = tracks[track]->chan;
487 char buf[3];
488 buf[0] = 0xB0 | chan;
489 buf[1] = 7;
490 buf[2] = vol;
491 send_midi(buf, 3, port);
494 void midi_pan_controller(int track, int pan){
495 int port = tracks[track]->port;
496 int chan = tracks[track]->chan;
498 char buf[3];
499 buf[0] = 0xB0 | chan;
500 buf[1] = 10;
501 buf[2] = pan;
502 send_midi(buf, 3, port);
505 void midi_expression_controller(int track, int expr){
506 int port = tracks[track]->port;
507 int chan = tracks[track]->chan;
509 char buf[3];
510 buf[0] = 0xB0 | chan;
511 buf[1] = 11;
512 buf[2] = expr;
513 send_midi(buf, 3, port);
517 void midi_note_off(int note, int chan, int port){
518 char buf[3];
519 buf[0] = 0x80 | chan;
520 buf[1] = note;
521 buf[2] = 0x00;
522 send_midi(buf,3,port);
525 void midi_track_off(int track){
526 int chan = tracks[track]->chan;
527 int port = tracks[track]->port;
529 midi_channel_off(chan,port);
532 void midi_channel_off(int chan, int port){
533 char buf[3] = {0xB0,123,0};
534 buf[0] = 0xB0 | chan;
535 send_midi(buf,3,port);
537 buf[0] = 0xB0 | chan;
538 buf[1] = 120;
539 send_midi(buf,3,port);
543 void backend_set_passthru(int v){
544 passthru = v;
547 void set_loop_start(int tick){
548 loop_start = tick;
551 void set_loop_end(int tick){
552 loop_end = tick;
555 int get_loop_start(){
556 return loop_start;
559 int get_loop_end(){
560 return loop_end;
563 int get_play_position(){
564 return cur_tick;
567 int solo;
568 void set_solo(int s){
569 solo = s;
572 int get_solo(){
573 return solo;
576 int is_backend_playing(){
577 return playing;
580 int is_backend_recording(){
581 return recording&&playing;
584 void toggle_loop(){
585 if(looping){
586 looping = 0;
588 else{
589 looping = 1;
594 void set_bpm(int n){
595 new_bpm = n;
599 char* session_string;
600 int backend_session_process(){
601 int ret = SESSION_NOMORE;
602 #ifdef HAVE_LASH
603 lash_event_t *e;
605 char *name;
607 e = lash_get_event(lash_client);
608 if(!e){
609 return SESSION_NOMORE;
612 asprintf(&session_string,"%s",lash_event_get_string(e));
613 const int t = lash_event_get_type (e);
615 switch(t)
617 case LASH_Save_File:
618 printf("session_process: LASH save\n");
619 lash_send_event(lash_client, lash_event_new_with_type(LASH_Save_File));
620 ret = SESSION_SAVE;
621 break;
622 case LASH_Restore_File:
623 printf("session_process: LASH load\n");
624 lash_send_event(lash_client, lash_event_new_with_type(LASH_Restore_File));
625 ret = SESSION_LOAD;
626 break;
627 case LASH_Quit:
628 printf("session_process: LASH quit\n");
629 ret = SESSION_QUIT;
630 break;
631 default:
632 printf("session_process: unhandled LASH event (%d)\n", t);
633 switch(t){
634 case LASH_Client_Name: printf("LASH_Client_Name\n"); break;
635 case LASH_Jack_Client_Name: printf("LASH_Jack_Client_Name\n"); break;
636 case LASH_Alsa_Client_ID: printf("LASH_Alsa_Client_ID\n"); break;
637 case LASH_Save_File: printf("LASH_Save_File\n"); break;
638 case LASH_Save_Data_Set: printf("LASH_Save_Data_Set\n"); break;
639 case LASH_Restore_Data_Set: printf("LASH_Restore_Data_Set\n"); break;
640 case LASH_Save: printf("LASH_Save\n"); break;
642 ret = SESSION_UNHANDLED;
643 break;
645 //lash_event_destroy(e);
646 #endif
647 return ret;
650 char* get_session_string(){
651 return session_string;