2 * HMI midi playing routines by Jani Frilander
4 * External device support by Patrick McCarthy
6 * Ported to d1x/sdl_threads by Matthew Mueller
16 #include <linux/soundcard.h>
17 #include <sys/ioctl.h>
19 #include <sys/types.h>
26 #include <SDL/SDL_thread.h>
28 //#define WANT_AWE32 1
31 #include <linux/awe_voice.h>
34 //#define WANT_MPU401 1
38 unsigned char reset_genmidi[5] =
39 { 0x7e, 0x7f, 0x09, 0x01, 0xf7 };
41 #define MIDI_RESET { \
42 SEQ_MIDIOUT(synth_dev,MIDI_SYSTEM_PREFIX); \
43 SEQ_MIDIOUT(synth_dev,reset_genmidi[0]); \
44 SEQ_MIDIOUT(synth_dev,reset_genmidi[1]); \
45 SEQ_MIDIOUT(synth_dev,reset_genmidi[2]); \
46 SEQ_MIDIOUT(synth_dev,reset_genmidi[3]); \
47 SEQ_MIDIOUT(synth_dev,reset_genmidi[4]); \
50 #define MIDI_MESSAGE2(a,b) { \
51 SEQ_MIDIOUT(synth_dev,a); \
52 SEQ_MIDIOUT(synth_dev,b); \
55 #define MIDI_MESSAGE3(a,b,c) { \
56 SEQ_MIDIOUT(synth_dev,a); \
57 SEQ_MIDIOUT(synth_dev,b); \
58 SEQ_MIDIOUT(synth_dev,c); \
71 int ipc_queue_id = -1;
74 SDL_Thread *player_thread=NULL;
77 unsigned char *data=NULL;
78 char digi_last_midi_song[16] = "";
80 struct synth_info card_info;
86 if (write(seqfd, _seqbuf, _seqbufptr) == -1)
88 perror ("Error writing sequencer device");
89 SDL_KillThread(player_thread);
97 // printf("goodbye\n");//#####
103 int nrmidis,nrsynths,i;
105 if ((seqfd = open(SEQ_DEV, O_WRONLY, 0)) < 0)
107 perror ("Error opening sequencer device");
111 if (ioctl(seqfd, SNDCTL_SEQ_NRSYNTHS, &nrsynths) == -1)
113 perror ("There is no soundcard");
117 if (ioctl(seqfd, SNDCTL_SEQ_NRMIDIS, &nrmidis) == -1)
119 perror ("There is no soundcard");
124 if(nrsynths < 1 && nrmidis < 1)
126 printf("No synth or midi device!\n");
132 //Check if we have wavetable synth device
133 for (i=0; i < nrsynths; i++)
135 card_info.device = i;
136 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
138 perror("cannot get info on soundcard");
142 if (card_info.synth_type == SYNTH_TYPE_SAMPLE)
150 for (i=0; i < nrsynths; i++)
152 card_info.device = i;
153 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
155 perror("cannot get info on soundcard");
159 if (card_info.synth_type==SYNTH_TYPE_SAMPLE
160 &&card_info.synth_subtype==SAMPLE_TYPE_AWE32) {
168 for (i=0; i < nrmidis; i++)
170 struct midi_info cinfo;
172 if (ioctl(seqfd, SNDCTL_MIDI_INFO, &cinfo) == -1)
174 perror("cannot get info on soundcard");
178 // Just take first available for now.
179 card_info.synth_type=SYNTH_TYPE_MIDI;
185 if (card_info.synth_type!=SYNTH_TYPE_MIDI) {
188 card_info.device = synth_dev;
189 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
191 perror("cannot get info on soundcard");
198 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
204 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
205 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
207 AWE_SET_CHANNEL_MODE(synth_dev,1);
208 AWE_DRUM_CHANNELS(synth_dev,drumflag);
213 voices = malloc(sizeof(Voice_info)*card_info.nr_voices);
214 for (i=0;i<card_info.nr_voices;i++)
217 voices[i].channel = -1;
227 ioctl(seqfd,SNDCTL_SEQ_SYNC);
232 void set_program(int channel, int pgm)
235 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
236 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
238 SEQ_SET_PATCH(synth_dev,channel,pgm);
242 program[channel]=pgm;
245 void start_note(int channel, int note, int vel)
250 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
251 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
253 SEQ_START_NOTE(synth_dev,channel,note,vel);
258 for (i=0; i<card_info.nr_voices;i++)
259 if ((voices[i].note == -1) && (voices[i].channel == -1))
261 if (i != card_info.nr_voices)
263 voices[i].note = note;
264 voices[i].channel = channel;
265 if (((1<<channel) & drumflag)) /* drum note */
267 SEQ_SET_PATCH(synth_dev, i, note + 128);
271 SEQ_SET_PATCH(synth_dev, i, program[channel]);
273 SEQ_START_NOTE(synth_dev, i, note, vel);
278 void stop_note(int channel, int note, int vel)
283 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
284 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
286 SEQ_STOP_NOTE(synth_dev,channel,note,vel);
291 for (i=0; i<card_info.nr_voices;i++)
292 if ((voices[i].note == note) && (voices[i].channel == channel))
294 if (i != card_info.nr_voices)
297 voices[i].channel = -1;
298 SEQ_STOP_NOTE(synth_dev,i,note,vel);
303 void set_control(int channel,int ctrl,int value)
308 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
309 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
311 SEQ_CONTROL(synth_dev,channel,ctrl,value);
316 for (i=0; i<card_info.nr_voices;i++)
317 if (voices[i].channel == channel)
318 if (ctrl == CTL_MAIN_VOLUME)
319 SEQ_MAIN_VOLUME(synth_dev,i,value);
323 void set_pitchbend(int channel, int bend)
328 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
329 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
331 SEQ_BENDER(synth_dev,channel,bend);
336 for (i=0; i<card_info.nr_voices;i++)
337 if (voices[i].channel == channel)
339 /*SEQ_BENDER_RANGE(synth_dev, i, 200);*/
340 SEQ_BENDER(synth_dev, i, bend);
345 void set_key_pressure(int channel, int note, int vel)
350 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
351 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
353 AWE_KEY_PRESSURE(synth_dev,channel,note,vel);
358 for (i=0; i<card_info.nr_voices;i++)
359 if ((voices[i].note == note) && (voices[i].channel == channel))
360 SEQ_KEY_PRESSURE(synth_dev,i,note,vel);
364 void set_chn_pressure(int channel, int vel)
369 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
370 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
372 AWE_CHN_PRESSURE(synth_dev,channel,vel);
377 for (i=0; i<card_info.nr_voices;i++)
378 if (voices[i].channel == channel)
379 SEQ_CHN_PRESSURE(synth_dev,i,vel);
389 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
390 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
394 SEQ_STOP_NOTE(synth_dev,i,j,0);
399 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
405 for (i=0; i<card_info.nr_voices;i++)
406 SEQ_STOP_NOTE(synth_dev,i,voices[i].note,0);
410 int get_dtime(unsigned char *data, int *pos)
418 result = (0x7f & buf);
424 result |=(int) (((int) 0x7f & (int) buf)<<7);
429 result |=(int) (((int) 0x7f & (int) buf)<<14);
434 result |=(int) (((int) 0x7f & (int) buf)<<21);
442 int do_track_event(unsigned char *data, int *pos)
445 unsigned char buf[5];
449 channel = buf[0] & 0xf;
451 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
452 switch((buf[0]&0xf0)) {
458 buf[1]=data[*pos]; *pos+=1;
459 buf[2]=data[*pos]; *pos+=1;
460 MIDI_MESSAGE3(buf[0],buf[1],buf[2]);
464 buf[1]=data[*pos]; *pos+=1;
465 MIDI_MESSAGE3(buf[0],0,buf[1]);
477 switch((buf[0] & 0xf0))
484 stop_note((int) channel, (int) buf[1], (int) buf[2]);
493 stop_note((int) channel, (int) buf[1], (int) buf[2]);
497 start_note((int) channel, (int) buf[1], (int) buf[2]);
505 set_key_pressure((int) channel, (int) buf[1], (int) buf[2]);
512 set_control((int) channel, (int) buf[1], (int) buf[2]);
519 set_pitchbend((int) channel, (int) ((buf[2] << 7) + buf[1]));
524 set_program((int) channel, (int) buf[1] );
529 set_chn_pressure((int) channel, (int) buf[1]);
540 void send_ipc(char *message)
542 printf ("sendipc %s\n", message);//##########3
545 ipc_queue_id=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's',
547 snd=malloc(sizeof(long) + 32);
549 player_thread=SDL_CreateThread((int (*)(void *))play_hmi, NULL);
550 // player_pid = play_hmi();
552 if (strlen(message) < 16)
554 sprintf(snd->mtext,"%s",message);
555 msgsnd(ipc_queue_id,snd,16,0);
562 // kill(player_pid,SIGTERM);
563 msgctl( ipc_queue_id, IPC_RMID, 0);
569 int do_ipc(int qid, struct msgbuf *buf, int flags)
573 float last_volume = volume;
576 ipc_read = msgrcv(qid,buf,16,0,flags | MSG_NOERROR);
583 perror("IPC trouble");
588 printf ("do_ipc %s\n", buf->mtext);//##########3
589 switch (buf->mtext[0])
592 volume = (double)(atof(buf->mtext + 1) / 128.0);
593 printf("vol %f->%f\n", last_volume, volume);
594 if (last_volume <= 0 && volume > 0)
596 buf->mtext[0] = 'p'; // start playing again if volume raised above 0
597 strcpy(buf->mtext + 1, digi_last_midi_song);
598 // fall through to case 'p'
600 else if (last_volume > 0 && volume <= 0)
602 strcpy(buf->mtext, "s"); // stop playing if volume reduced to 0
611 strcpy(digi_last_midi_song, buf->mtext + 1);
613 fptr = cfopen((buf->mtext + 1), "rb");
617 l = cfilelength(fptr);
618 data=realloc(data,(size_t) l);
619 cfread(data, l, 1, fptr);
621 printf ("good. fpr=%p l=%i data=%p\n", fptr, l, data);//##########3
626 strcpy(buf->mtext, "s"); // not playing, thus "stop".
634 // SDL_KillThread(player_thread);
637 //printf("do_ipc %s ret %i\n", buf->mtext, ipc_read); // ##########3
643 void play_hmi (void * arg)
660 printf ("play_hmi\n");//#########
677 // signal(SIGTERM, my_quit);
678 rcv=malloc(sizeof(long) + 16);
683 qid=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's', 0660);
691 ipc_read=do_ipc(qid,rcv,0);
693 while(rcv->mtext[0] != 'p');
702 t_info = malloc(sizeof(Track_info)*n_chunks);
707 for(i=0;i<n_chunks;i++)
709 t_info[i].position = pos + 12;
710 t_info[i].status = PLAYING;
711 t_info[i].time = get_dtime(data,&t_info[i].position);
712 pos += (( (0xff & data[pos + 5]) << 8 ) + (0xff & data[pos + 4]));
725 if (t_info[i].status == PLAYING)
729 while((low_chunk <=0) && (i<n_chunks));
734 low_dtime = t_info[low_chunk].time;
736 for(i=1;i<n_chunks;i++)
738 if ((t_info[i].time < low_dtime) &&
739 (t_info[i].status == PLAYING))
741 low_dtime = t_info[i].time;
747 printf("Serious warning: d_time negative!!!!!!\n");
749 csec = 0.86 * low_dtime;
751 //flush sequencer buffer after 20 events
754 ioctl(seqfd, SNDCTL_SEQ_SYNC);
764 t_info[low_chunk].status = do_track_event(data,&t_info[low_chunk].position);
766 if (t_info[low_chunk].status == 3)
768 printf("Error playing data in chunk %d\n",low_chunk);
769 t_info[low_chunk].status = STOPPED;
772 if (t_info[low_chunk].status == PLAYING)
773 t_info[low_chunk].time += get_dtime(data,&t_info[low_chunk].position);
775 //Check if the song has reached the end
776 stop = t_info[0].status;
777 for(i=1;i<n_chunks;i++)
778 stop &= t_info[i].status;
780 if((do_ipc(qid,rcv,IPC_NOWAIT) > 0) && (rcv->mtext[0]=='p'))
783 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
796 ipc_read=do_ipc(qid,rcv,0);
798 while(rcv->mtext[0] != 'p');
801 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
812 void digi_play_midi_song(char *filename, char *melodic_bank, char *drum_bank, int loop)
814 if (FindArg("-nomusic"))
822 sprintf(buf, "p%s", filename);
827 void digi_set_midi_volume( int mvolume ) {
829 if(FindArg("-nomusic"))
832 sprintf(buf,"v%i",mvolume);