1 /* $Id: hmiplay.c,v 1.8 2004-05-19 19:14:52 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;
79 char digi_last_midi_song[16] = "";
81 struct synth_info card_info;
87 if (write(seqfd, _seqbuf, _seqbufptr) == -1)
89 perror ("Error writing sequencer device");
90 SDL_KillThread(player_thread);
98 // printf("goodbye\n");//#####
104 int nrmidis,nrsynths,i;
106 if ((seqfd = open(SEQ_DEV, O_WRONLY, 0)) < 0)
108 perror ("Error opening sequencer device");
112 if (ioctl(seqfd, SNDCTL_SEQ_NRSYNTHS, &nrsynths) == -1)
114 perror ("There is no soundcard");
118 if (ioctl(seqfd, SNDCTL_SEQ_NRMIDIS, &nrmidis) == -1)
120 perror ("There is no soundcard");
125 if(nrsynths < 1 && nrmidis < 1)
127 printf("No synth or midi device!\n");
133 //Check if we have wavetable synth device
134 for (i=0; i < nrsynths; i++)
136 card_info.device = i;
137 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
139 perror("cannot get info on soundcard");
143 if (card_info.synth_type == SYNTH_TYPE_SAMPLE)
151 for (i=0; i < nrsynths; i++)
153 card_info.device = i;
154 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
156 perror("cannot get info on soundcard");
160 if (card_info.synth_type==SYNTH_TYPE_SAMPLE
161 &&card_info.synth_subtype==SAMPLE_TYPE_AWE32) {
169 for (i=0; i < nrmidis; i++)
171 struct midi_info cinfo;
173 if (ioctl(seqfd, SNDCTL_MIDI_INFO, &cinfo) == -1)
175 perror("cannot get info on soundcard");
179 // Just take first available for now.
180 card_info.synth_type=SYNTH_TYPE_MIDI;
186 if (card_info.synth_type!=SYNTH_TYPE_MIDI) {
189 card_info.device = synth_dev;
190 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
192 perror("cannot get info on soundcard");
199 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
205 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
206 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
208 AWE_SET_CHANNEL_MODE(synth_dev,1);
209 AWE_DRUM_CHANNELS(synth_dev,drumflag);
214 voices = malloc(sizeof(Voice_info)*card_info.nr_voices);
215 for (i=0;i<card_info.nr_voices;i++)
218 voices[i].channel = -1;
228 ioctl(seqfd,SNDCTL_SEQ_SYNC);
233 void set_program(int channel, int pgm)
236 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
237 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
239 SEQ_SET_PATCH(synth_dev,channel,pgm);
243 program[channel]=pgm;
246 void start_note(int channel, int note, int vel)
251 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
252 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
254 SEQ_START_NOTE(synth_dev,channel,note,vel);
259 for (i=0; i<card_info.nr_voices;i++)
260 if ((voices[i].note == -1) && (voices[i].channel == -1))
262 if (i != card_info.nr_voices)
264 voices[i].note = note;
265 voices[i].channel = channel;
266 if (((1<<channel) & drumflag)) /* drum note */
268 SEQ_SET_PATCH(synth_dev, i, note + 128);
272 SEQ_SET_PATCH(synth_dev, i, program[channel]);
274 SEQ_START_NOTE(synth_dev, i, note, vel);
279 void stop_note(int channel, int note, int vel)
284 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
285 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
287 SEQ_STOP_NOTE(synth_dev,channel,note,vel);
292 for (i=0; i<card_info.nr_voices;i++)
293 if ((voices[i].note == note) && (voices[i].channel == channel))
295 if (i != card_info.nr_voices)
298 voices[i].channel = -1;
299 SEQ_STOP_NOTE(synth_dev,i,note,vel);
304 void set_control(int channel,int ctrl,int value)
309 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
310 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
312 SEQ_CONTROL(synth_dev,channel,ctrl,value);
317 for (i=0; i<card_info.nr_voices;i++)
318 if (voices[i].channel == channel)
319 if (ctrl == CTL_MAIN_VOLUME)
320 SEQ_MAIN_VOLUME(synth_dev,i,value);
324 void set_pitchbend(int channel, int bend)
329 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
330 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
332 SEQ_BENDER(synth_dev,channel,bend);
337 for (i=0; i<card_info.nr_voices;i++)
338 if (voices[i].channel == channel)
340 /*SEQ_BENDER_RANGE(synth_dev, i, 200);*/
341 SEQ_BENDER(synth_dev, i, bend);
346 void set_key_pressure(int channel, int note, int vel)
351 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
352 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
354 AWE_KEY_PRESSURE(synth_dev,channel,note,vel);
359 for (i=0; i<card_info.nr_voices;i++)
360 if ((voices[i].note == note) && (voices[i].channel == channel))
361 SEQ_KEY_PRESSURE(synth_dev,i,note,vel);
365 void set_chn_pressure(int channel, int vel)
370 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
371 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
373 AWE_CHN_PRESSURE(synth_dev,channel,vel);
378 for (i=0; i<card_info.nr_voices;i++)
379 if (voices[i].channel == channel)
380 SEQ_CHN_PRESSURE(synth_dev,i,vel);
390 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
391 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
395 SEQ_STOP_NOTE(synth_dev,i,j,0);
400 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
406 for (i=0; i<card_info.nr_voices;i++)
407 SEQ_STOP_NOTE(synth_dev,i,voices[i].note,0);
411 int get_dtime(unsigned char *data, int *pos)
419 result = (0x7f & buf);
425 result |=(int) (((int) 0x7f & (int) buf)<<7);
430 result |=(int) (((int) 0x7f & (int) buf)<<14);
435 result |=(int) (((int) 0x7f & (int) buf)<<21);
443 int do_track_event(unsigned char *data, int *pos)
446 unsigned char buf[5];
450 channel = buf[0] & 0xf;
452 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
453 switch((buf[0]&0xf0)) {
459 buf[1]=data[*pos]; *pos+=1;
460 buf[2]=data[*pos]; *pos+=1;
461 MIDI_MESSAGE3(buf[0],buf[1],buf[2]);
465 buf[1]=data[*pos]; *pos+=1;
466 MIDI_MESSAGE3(buf[0],0,buf[1]);
478 switch((buf[0] & 0xf0))
485 stop_note((int) channel, (int) buf[1], (int) buf[2]);
494 stop_note((int) channel, (int) buf[1], (int) buf[2]);
498 start_note((int) channel, (int) buf[1], (int) buf[2]);
506 set_key_pressure((int) channel, (int) buf[1], (int) buf[2]);
513 set_control((int) channel, (int) buf[1], (int) buf[2]);
520 set_pitchbend((int) channel, (int) ((buf[2] << 7) + buf[1]));
525 set_program((int) channel, (int) buf[1] );
530 set_chn_pressure((int) channel, (int) buf[1]);
541 void send_ipc(char *message)
543 printf ("sendipc %s\n", message);//##########3
546 ipc_queue_id=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's',
548 snd=malloc(sizeof(long) + 32);
550 player_thread=SDL_CreateThread((int (*)(void *))play_hmi, NULL);
551 // player_pid = play_hmi();
553 if (strlen(message) < 16)
555 sprintf(snd->mtext,"%s",message);
556 msgsnd(ipc_queue_id,snd,16,0);
563 // kill(player_pid,SIGTERM);
564 msgctl( ipc_queue_id, IPC_RMID, 0);
570 int do_ipc(int qid, struct msgbuf *buf, int flags)
574 float last_volume = volume;
577 ipc_read = msgrcv(qid,buf,16,0,flags | MSG_NOERROR);
584 perror("IPC trouble");
589 printf ("do_ipc %s\n", buf->mtext);//##########3
590 switch (buf->mtext[0])
593 volume = (double)(atof(buf->mtext + 1) / 128.0);
594 printf("vol %f->%f\n", last_volume, volume);
595 if (last_volume <= 0 && volume > 0)
597 buf->mtext[0] = 'p'; // start playing again if volume raised above 0
598 strcpy(buf->mtext + 1, digi_last_midi_song);
599 // fall through to case 'p'
601 else if (last_volume > 0 && volume <= 0)
603 strcpy(buf->mtext, "s"); // stop playing if volume reduced to 0
612 strcpy(digi_last_midi_song, buf->mtext + 1);
614 fptr = cfopen((buf->mtext + 1), "rb");
618 l = cfilelength(fptr);
619 data=realloc(data,(size_t) l);
620 cfread(data, l, 1, fptr);
622 printf ("good. fpr=%p l=%i data=%p\n", fptr, l, data);//##########3
627 strcpy(buf->mtext, "s"); // not playing, thus "stop".
635 // SDL_KillThread(player_thread);
638 //printf("do_ipc %s ret %i\n", buf->mtext, ipc_read); // ##########3
644 void play_hmi (void * arg)
661 printf ("play_hmi\n");//#########
678 // signal(SIGTERM, my_quit);
679 rcv=malloc(sizeof(long) + 16);
684 qid=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's', 0660);
692 ipc_read=do_ipc(qid,rcv,0);
694 while(rcv->mtext[0] != 'p');
703 t_info = malloc(sizeof(Track_info)*n_chunks);
708 for(i=0;i<n_chunks;i++)
710 t_info[i].position = pos + 12;
711 t_info[i].status = PLAYING;
712 t_info[i].time = get_dtime(data,&t_info[i].position);
713 pos += (( (0xff & data[pos + 5]) << 8 ) + (0xff & data[pos + 4]));
726 if (t_info[i].status == PLAYING)
730 while((low_chunk <=0) && (i<n_chunks));
735 low_dtime = t_info[low_chunk].time;
737 for(i=1;i<n_chunks;i++)
739 if ((t_info[i].time < low_dtime) &&
740 (t_info[i].status == PLAYING))
742 low_dtime = t_info[i].time;
748 printf("Serious warning: d_time negative!!!!!!\n");
750 csec = 0.86 * low_dtime;
752 //flush sequencer buffer after 20 events
755 ioctl(seqfd, SNDCTL_SEQ_SYNC);
765 t_info[low_chunk].status = do_track_event(data,&t_info[low_chunk].position);
767 if (t_info[low_chunk].status == 3)
769 printf("Error playing data in chunk %d\n",low_chunk);
770 t_info[low_chunk].status = STOPPED;
773 if (t_info[low_chunk].status == PLAYING)
774 t_info[low_chunk].time += get_dtime(data,&t_info[low_chunk].position);
776 //Check if the song has reached the end
777 stop = t_info[0].status;
778 for(i=1;i<n_chunks;i++)
779 stop &= t_info[i].status;
781 if((do_ipc(qid,rcv,IPC_NOWAIT) > 0) && (rcv->mtext[0]=='p'))
784 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
797 ipc_read=do_ipc(qid,rcv,0);
799 while(rcv->mtext[0] != 'p');
802 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
813 void digi_play_midi_song(char *filename, char *melodic_bank, char *drum_bank, int loop)
815 if (FindArg("-nomusic"))
823 sprintf(buf, "p%s", filename);
828 void digi_set_midi_volume( int mvolume ) {
830 if(FindArg("-nomusic"))
833 sprintf(buf,"v%i",mvolume);