1 /* $Id: hmiplay.c,v 1.6 2004-05-19 07:00:57 btb Exp $ */
3 * HMI midi playing routines by Jani Frilander
5 * External device support by Patrick McCarthy
7 * Ported to d1x/sdl_threads by Matthew Mueller
17 #include <linux/soundcard.h>
18 #include <sys/ioctl.h>
20 #include <sys/types.h>
22 #define __USE_GNU //in recent glibc versions msgbuf is only defined if you have __USE_GNU defined
27 #include <SDL/SDL_thread.h>
29 //#define WANT_AWE32 1
32 #include <linux/awe_voice.h>
35 //#define WANT_MPU401 1
39 unsigned char reset_genmidi[5] =
40 { 0x7e, 0x7f, 0x09, 0x01, 0xf7 };
42 #define MIDI_RESET { \
43 SEQ_MIDIOUT(synth_dev,MIDI_SYSTEM_PREFIX); \
44 SEQ_MIDIOUT(synth_dev,reset_genmidi[0]); \
45 SEQ_MIDIOUT(synth_dev,reset_genmidi[1]); \
46 SEQ_MIDIOUT(synth_dev,reset_genmidi[2]); \
47 SEQ_MIDIOUT(synth_dev,reset_genmidi[3]); \
48 SEQ_MIDIOUT(synth_dev,reset_genmidi[4]); \
51 #define MIDI_MESSAGE2(a,b) { \
52 SEQ_MIDIOUT(synth_dev,a); \
53 SEQ_MIDIOUT(synth_dev,b); \
56 #define MIDI_MESSAGE3(a,b,c) { \
57 SEQ_MIDIOUT(synth_dev,a); \
58 SEQ_MIDIOUT(synth_dev,b); \
59 SEQ_MIDIOUT(synth_dev,c); \
72 int ipc_queue_id = -1;
75 SDL_Thread *player_thread=NULL;
78 unsigned char *data=NULL;
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)
575 ipc_read = msgrcv(qid,buf,16,0,flags | MSG_NOERROR);
582 perror("IPC trouble");
587 printf ("do_ipc %s\n", buf->mtext);//##########3
588 switch (buf->mtext[0])
591 volume=(double) ((double) buf->mtext[0]/127.0);
594 fptr=cfopen((buf->mtext+1),"rb");
597 l = cfilelength(fptr);
598 data=realloc(data,(size_t) l);
599 cfread(data, l, 1, fptr);
601 printf ("good. fpr=%p l=%i data=%p\n", fptr, l, data);//##########3
609 // SDL_KillThread(player_thread);
617 void play_hmi (void * arg)
634 printf ("play_hmi\n");//#########
651 // signal(SIGTERM, my_quit);
652 rcv=malloc(sizeof(long) + 16);
657 qid=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's', 0660);
665 ipc_read=do_ipc(qid,rcv,0);
667 while(rcv->mtext[0] != 'p');
676 t_info = malloc(sizeof(Track_info)*n_chunks);
681 for(i=0;i<n_chunks;i++)
683 t_info[i].position = pos + 12;
684 t_info[i].status = PLAYING;
685 t_info[i].time = get_dtime(data,&t_info[i].position);
686 pos += (( (0xff & data[pos + 5]) << 8 ) + (0xff & data[pos + 4]));
699 if (t_info[i].status == PLAYING)
703 while((low_chunk <=0) && (i<n_chunks));
708 low_dtime = t_info[low_chunk].time;
710 for(i=1;i<n_chunks;i++)
712 if ((t_info[i].time < low_dtime) &&
713 (t_info[i].status == PLAYING))
715 low_dtime = t_info[i].time;
721 printf("Serious warning: d_time negative!!!!!!\n");
723 csec = 0.86 * low_dtime;
725 //flush sequencer buffer after 20 events
728 ioctl(seqfd, SNDCTL_SEQ_SYNC);
738 t_info[low_chunk].status = do_track_event(data,&t_info[low_chunk].position);
740 if (t_info[low_chunk].status == 3)
742 printf("Error playing data in chunk %d\n",low_chunk);
743 t_info[low_chunk].status = STOPPED;
746 if (t_info[low_chunk].status == PLAYING)
747 t_info[low_chunk].time += get_dtime(data,&t_info[low_chunk].position);
749 //Check if the song has reached the end
750 stop = t_info[0].status;
751 for(i=1;i<n_chunks;i++)
752 stop &= t_info[i].status;
754 if((do_ipc(qid,rcv,IPC_NOWAIT) > 0) && (rcv->mtext[0]=='p'))
757 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
770 ipc_read=do_ipc(qid,rcv,0);
772 while(rcv->mtext[0] != 'p');
775 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
786 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {
789 sprintf(buf,"p%s",filename);
792 void digi_set_midi_volume( int mvolume ) {
795 sprintf(buf,"v%i",mvolume);