1 /* $Id: hmiplay.c,v 1.2 2003-03-13 00:20:21 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>
26 #include <SDL_thread.h>
28 //#define WANT_AWE32 1
31 #include <linux/awe_voice.h>
34 //#define WANT_MPU401 1
37 #define MIDI_MESSAGE2(a,b) { \
38 SEQ_MIDIOUT(synth_dev,a); \
39 SEQ_MIDIOUT(synth_dev,b); \
42 #define MIDI_MESSAGE3(a,b,c) { \
43 SEQ_MIDIOUT(synth_dev,a); \
44 SEQ_MIDIOUT(synth_dev,b); \
45 SEQ_MIDIOUT(synth_dev,c); \
58 int ipc_queue_id = -1;
61 SDL_Thread *player_thread=NULL;
64 unsigned char *data=NULL;
66 struct synth_info card_info;
72 if (write(seqfd, _seqbuf, _seqbufptr) == -1)
74 perror ("Error writing sequencer device");
75 SDL_KillThread(player_thread);
83 // printf("goodbye\n");//#####
89 int nrmidis,nrsynths,i;
91 if ((seqfd = open(SEQ_DEV, O_WRONLY, 0)) < 0)
93 perror ("Error opening sequencer device");
97 if (ioctl(seqfd, SNDCTL_SEQ_NRSYNTHS, &nrsynths) == -1)
99 perror ("There is no soundcard");
103 if (ioctl(seqfd, SNDCTL_SEQ_NRMIDIS, &nrmidis) == -1)
105 perror ("There is no soundcard");
110 if(nrsynths < 1 && nrmidis < 1)
112 printf("No synth or midi device!\n");
118 //Check if we have wavetable synth device
119 for (i=0; i < nrsynths; i++)
121 card_info.device = i;
122 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
124 perror("cannot get info on soundcard");
128 if (card_info.synth_type == SYNTH_TYPE_SAMPLE)
136 for (i=0; i < nrsynths; i++)
138 card_info.device = i;
139 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
141 perror("cannot get info on soundcard");
145 if (card_info.synth_type==SYNTH_TYPE_SAMPLE
146 &&card_info.synth_subtype==SAMPLE_TYPE_AWE32) {
154 for (i=0; i < nrmidis; i++)
156 struct midi_info cinfo;
158 if (ioctl(seqfd, SNDCTL_MIDI_INFO, &cinfo) == -1)
160 perror("cannot get info on soundcard");
164 // Just take first available for now.
165 card_info.synth_type=SYNTH_TYPE_MIDI;
171 if (card_info.synth_type!=SYNTH_TYPE_MIDI) {
174 card_info.device = synth_dev;
175 if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info) == -1)
177 perror("cannot get info on soundcard");
184 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
185 // Insert some sort of midi reset here later.
189 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
190 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
192 AWE_SET_CHANNEL_MODE(synth_dev,1);
193 AWE_DRUM_CHANNELS(synth_dev,drumflag);
198 voices = malloc(sizeof(Voice_info)*card_info.nr_voices);
199 for (i=0;i<card_info.nr_voices;i++)
202 voices[i].channel = -1;
212 ioctl(seqfd,SNDCTL_SEQ_SYNC);
217 void set_program(int channel, int pgm)
220 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
221 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
223 SEQ_SET_PATCH(synth_dev,channel,pgm);
227 program[channel]=pgm;
230 void start_note(int channel, int note, int vel)
235 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
236 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
238 SEQ_START_NOTE(synth_dev,channel,note,vel);
243 for (i=0; i<card_info.nr_voices;i++)
244 if ((voices[i].note == -1) && (voices[i].channel == -1))
246 if (i != card_info.nr_voices)
248 voices[i].note = note;
249 voices[i].channel = channel;
250 if (((1<<channel) & drumflag)) /* drum note */
252 SEQ_SET_PATCH(synth_dev, i, note + 128);
256 SEQ_SET_PATCH(synth_dev, i, program[channel]);
258 SEQ_START_NOTE(synth_dev, i, note, vel);
263 void stop_note(int channel, int note, int vel)
268 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
269 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
271 SEQ_STOP_NOTE(synth_dev,channel,note,vel);
276 for (i=0; i<card_info.nr_voices;i++)
277 if ((voices[i].note == note) && (voices[i].channel == channel))
279 if (i != card_info.nr_voices)
282 voices[i].channel = -1;
283 SEQ_STOP_NOTE(synth_dev,i,note,vel);
288 void set_control(int channel,int ctrl,int value)
293 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
294 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
296 SEQ_CONTROL(synth_dev,channel,ctrl,value);
301 for (i=0; i<card_info.nr_voices;i++)
302 if (voices[i].channel == channel)
303 if (ctrl == CTL_MAIN_VOLUME)
304 SEQ_MAIN_VOLUME(synth_dev,i,value);
308 void set_pitchbend(int channel, int bend)
313 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
314 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
316 SEQ_BENDER(synth_dev,channel,bend);
321 for (i=0; i<card_info.nr_voices;i++)
322 if (voices[i].channel == channel)
324 /*SEQ_BENDER_RANGE(synth_dev, i, 200);*/
325 SEQ_BENDER(synth_dev, i, bend);
330 void set_key_pressure(int channel, int note, int vel)
335 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
336 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
338 AWE_KEY_PRESSURE(synth_dev,channel,note,vel);
343 for (i=0; i<card_info.nr_voices;i++)
344 if ((voices[i].note == note) && (voices[i].channel == channel))
345 SEQ_KEY_PRESSURE(synth_dev,i,note,vel);
349 void set_chn_pressure(int channel, int vel)
354 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
355 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
357 AWE_CHN_PRESSURE(synth_dev,channel,vel);
362 for (i=0; i<card_info.nr_voices;i++)
363 if (voices[i].channel == channel)
364 SEQ_CHN_PRESSURE(synth_dev,i,vel);
374 if (card_info.synth_type == SYNTH_TYPE_SAMPLE
375 && card_info.synth_subtype == SAMPLE_TYPE_AWE32)
379 SEQ_STOP_NOTE(synth_dev,i,j,0);
384 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
388 for (i=0; i<card_info.nr_voices;i++)
389 SEQ_STOP_NOTE(synth_dev,i,voices[i].note,0);
393 int get_dtime(unsigned char *data, int *pos)
401 result = (0x7f & buf);
407 result |=(int) (((int) 0x7f & (int) buf)<<7);
412 result |=(int) (((int) 0x7f & (int) buf)<<14);
417 result |=(int) (((int) 0x7f & (int) buf)<<21);
425 int do_track_event(unsigned char *data, int *pos)
428 unsigned char buf[5];
432 channel = buf[0] & 0xf;
434 if (card_info.synth_type==SYNTH_TYPE_MIDI) {
435 switch((buf[0]&0xf0)) {
441 buf[1]=data[*pos]; *pos+=1;
442 buf[2]=data[*pos]; *pos+=1;
443 MIDI_MESSAGE3(buf[0],buf[1],buf[2]);
447 buf[1]=data[*pos]; *pos+=1;
448 MIDI_MESSAGE3(buf[0],0,buf[1]);
460 switch((buf[0] & 0xf0))
467 stop_note((int) channel, (int) buf[1], (int) buf[2]);
476 stop_note((int) channel, (int) buf[1], (int) buf[2]);
480 start_note((int) channel, (int) buf[1], (int) buf[2]);
488 set_key_pressure((int) channel, (int) buf[1], (int) buf[2]);
495 set_control((int) channel, (int) buf[1], (int) buf[2]);
502 set_pitchbend((int) channel, (int) ((buf[2] << 7) + buf[1]));
507 set_program((int) channel, (int) buf[1] );
512 set_chn_pressure((int) channel, (int) buf[1]);
523 void send_ipc(char *message)
525 printf ("sendipc %s\n", message);//##########3
528 ipc_queue_id=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's',
530 snd=malloc(sizeof(long) + 32);
532 player_thread=SDL_CreateThread(play_hmi, NULL);
533 // player_pid = play_hmi();
535 if (strlen(message) < 16)
537 sprintf(snd->mtext,"%s",message);
538 msgsnd(ipc_queue_id,snd,16,0);
545 // kill(player_pid,SIGTERM);
546 msgctl( ipc_queue_id, IPC_RMID, 0);
552 int do_ipc(int qid, struct msgbuf *buf, int flags)
558 ipc_read = msgrcv(qid,buf,16,0,flags | MSG_NOERROR);
565 perror("IPC trouble");
570 printf ("do_ipc %s\n", buf->mtext);//##########3
571 switch (buf->mtext[0])
574 volume=(double) ((double) buf->mtext[0]/127.0);
577 fptr=cfopen((buf->mtext+1),"rb");
580 l = cfilelength(fptr);
581 data=realloc(data,(size_t) l);
582 cfread(data, l, 1, fptr);
584 printf ("good. fpr=%p l=%i data=%p\n", fptr, l, data);//##########3
592 // SDL_KillThread(player_thread);
600 void play_hmi (void * arg)
617 printf ("play_hmi\n");//#########
634 // signal(SIGTERM, my_quit);
635 rcv=malloc(sizeof(long) + 16);
642 qid=msgget ((key_t) ('l'<<24) | ('d'<<16) | ('e'<<8) | 's', 0660);
650 ipc_read=do_ipc(qid,rcv,0);
652 while(rcv->mtext[0] != 'p');
661 t_info = malloc(sizeof(Track_info)*n_chunks);
666 for(i=0;i<n_chunks;i++)
668 t_info[i].position = pos + 12;
669 t_info[i].status = PLAYING;
670 t_info[i].time = get_dtime(data,&t_info[i].position);
671 pos += (( (0xff & data[pos + 5]) << 8 ) + (0xff & data[pos + 4]));
682 if (t_info[i].status == PLAYING)
686 while((low_chunk <=0) && (i<n_chunks));
691 low_dtime = t_info[low_chunk].time;
693 for(i=1;i<n_chunks;i++)
695 if ((t_info[i].time < low_dtime) &&
696 (t_info[i].status == PLAYING))
698 low_dtime = t_info[i].time;
704 printf("Serious warning: d_time negative!!!!!!\n");
706 csec = 0.86 * low_dtime;
708 //flush sequencer buffer after 20 events
711 ioctl(seqfd, SNDCTL_SEQ_SYNC);
717 t_info[low_chunk].status = do_track_event(data,&t_info[low_chunk].position);
719 if (t_info[low_chunk].status == 3)
721 printf("Error playing data in chunk %d\n",low_chunk);
722 t_info[low_chunk].status = STOPPED;
725 if (t_info[low_chunk].status == PLAYING)
726 t_info[low_chunk].time += get_dtime(data,&t_info[low_chunk].position);
728 //Check if the song has reached the end
729 stop = t_info[0].status;
730 for(i=1;i<n_chunks;i++)
731 stop &= t_info[i].status;
733 if((do_ipc(qid,rcv,IPC_NOWAIT) > 0) && (rcv->mtext[0]=='p'))
736 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
749 ipc_read=do_ipc(qid,rcv,0);
751 while(rcv->mtext[0] != 'p');
754 t_info = realloc(t_info,sizeof(Track_info)*n_chunks);
765 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {
768 sprintf(buf,"p%s",filename);
771 void digi_set_midi_volume( int mvolume ) {
774 sprintf(buf,"v%i",mvolume);