warn_tonebank[current_set[j]] = 1; } current_set[j] = 0; }  
warn_tonebank[newbank] = 1; } newbank = 0; } current_set[j] = newbank; 
static MidiEvent *groom_list(int32 divisions, int32 *eventsp, int32 *samplesp){    MidiEvent *groomed_list, *lp;    MidiEventList *meep;    int32 i, j, our_event_count, tempo, skip_this_event;    int32 sample_cum, samples_to_do, at, st, dt, counting_time;    int ch, gch;    uint8 current_set[MAX_CHANNELS],	warn_tonebank[128], warn_drumset[128];    int8 bank_lsb[MAX_CHANNELS], bank_msb[MAX_CHANNELS], mapID[MAX_CHANNELS];    int current_program[MAX_CHANNELS];    int wrd_args[WRD_MAXPARAM];    int wrd_argc;    int chidx[256];    int newbank, newprog;    move_channels(chidx);    COPY_CHANNELMASK(drumchannels, current_file_info->drumchannels);    COPY_CHANNELMASK(drumchannel_mask, current_file_info->drumchannel_mask);    /* Move drumchannels */    for(ch = REDUCE_CHANNELS; ch < MAX_CHANNELS; ch++)    {	i = chidx[ch];	if(i != -1 && i != ch && !IS_SET_CHANNELMASK(drumchannel_mask, i))	{	    if(IS_SET_CHANNELMASK(drumchannels, ch))		SET_CHANNELMASK(drumchannels, i);	    else		UNSET_CHANNELMASK(drumchannels, i);	}    }    for(j = 0; j < MAX_CHANNELS; j++)    {	if(ISDRUMCHANNEL(j))	    current_set[j] = 0;	else	{	    if(special_tonebank >= 0)		current_set[j] = special_tonebank;	    else		current_set[j] = default_tonebank;	    if(tonebank[current_set[j]] == NULL)	    {		if(warn_tonebank[current_set[j]] == 0)		{		    ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,			      "Tone bank %d is undefined", newbank);		    warn_tonebank[current_set[j]] = 1;		}		current_set[j] = 0;	    }	}	bank_lsb[j] = bank_msb[j] = 0;	if(play_system_mode == XG_SYSTEM_MODE && j % 16 == 9)	    bank_msb[j] = 127; /* Use MSB=127 for XG */	current_program[j] = default_program[j];    }    memset(warn_tonebank, 0, sizeof(warn_tonebank));    memset(warn_drumset, 0, sizeof(warn_drumset));    tempo = 500000;    compute_sample_increment(tempo, divisions);    /* This may allocate a bit more than we need */    groomed_list = lp =	(MidiEvent *)safe_malloc(sizeof(MidiEvent) * (event_count + 1));    meep = evlist;    our_event_count = 0;    st = at = sample_cum = 0;    counting_time = 2; /* We strip any silence before the first NOTE ON. */    wrd_argc = 0;    change_system_mode(DEFAULT_SYSTEM_MODE);    for(j = 0; j < MAX_CHANNELS; j++)	mapID[j] = get_default_mapID(j);    for(i = 0; i < event_count; i++)    {	skip_this_event = 0;	ch = meep->event.channel;	gch = GLOBAL_CHANNEL_EVENT_TYPE(meep->event.type);	if(!gch && ch >= MAX_CHANNELS) /* For safety */	    meep->event.channel = ch = ch % MAX_CHANNELS;	if(!gch && IS_SET_CHANNELMASK(quietchannels, ch))	    skip_this_event = 1;	else switch(meep->event.type)	{	  case ME_NONE:	    skip_this_event = 1;	    break;	  case ME_RESET:	    change_system_mode(meep->event.a);	    ctl->cmsg(CMSG_INFO, VERB_NOISY, "MIDI reset at %d sec",		      (int)((double)st / play_mode->rate + 0.5));	    for(j = 0; j < MAX_CHANNELS; j++)	    {		if(play_system_mode == XG_SYSTEM_MODE && j % 16 == 9)		    mapID[j] = XG_DRUM_MAP;		else		    mapID[j] = get_default_mapID(j);		if(ISDRUMCHANNEL(j))		    current_set[j] = 0;		else		{		    if(special_tonebank >= 0)			current_set[j] = special_tonebank;		    else			current_set[j] = default_tonebank;		    if(tonebank[current_set[j]] == NULL)			current_set[j] = 0;		}		bank_lsb[j] = bank_msb[j] = 0;		if(play_system_mode == XG_SYSTEM_MODE && j % 16 == 9)		{		    bank_msb[j] = 127; /* Use MSB=127 for XG */		}		current_program[j] = default_program[j];	    }	    break;	  case ME_PROGRAM:	    if(ISDRUMCHANNEL(ch))		newbank = current_program[ch];	    else		newbank = current_set[ch];	    newprog = meep->event.a;	    switch(play_system_mode)	    {	      case GS_SYSTEM_MODE:		switch(bank_lsb[ch])		{		  case 0:	/* No change */		    break;		  case 1:		    ctl->cmsg(CMSG_INFO, VERB_DEBUG,			      "(GS ch=%d SC-55 MAP)", ch);		    mapID[ch] = (!ISDRUMCHANNEL(ch) ? SC_55_TONE_MAP				 : SC_55_DRUM_MAP);		    break;		  case 2:		    ctl->cmsg(CMSG_INFO, VERB_DEBUG,			      "(GS ch=%d SC-88 MAP)", ch);		    mapID[ch] = (!ISDRUMCHANNEL(ch) ? SC_88_TONE_MAP				 : SC_88_DRUM_MAP);		    break;		  case 3:		    ctl->cmsg(CMSG_INFO, VERB_DEBUG,			      "(GS ch=%d SC-88Pro MAP)", ch);		    mapID[ch] = (!ISDRUMCHANNEL(ch) ? SC_88PRO_TONE_MAP				 : SC_88PRO_DRUM_MAP);		    break;		  default:		    ctl->cmsg(CMSG_INFO, VERB_DEBUG,			      "(GS: ch=%d Strange bank LSB %d)",			      ch, bank_lsb[ch]);		    break;		}		newbank = bank_msb[ch];		break;	      case XG_SYSTEM_MODE: /* XG */		switch(bank_msb[ch])		{		  case 0: /* Normal */		    if(ch == 9  && bank_lsb[ch] == 127 && mapID[ch] == XG_DRUM_MAP) {		      /* FIXME: Why this part is drum?  Is this correct? */		      ctl->cmsg(CMSG_WARNING, VERB_NORMAL,				"Warning: XG bank 0/127 is found. It may be not correctly played.");		      ;		    } else {		      ctl->cmsg(CMSG_INFO, VERB_DEBUG, "(XG ch=%d Normal voice)",				ch);		      midi_drumpart_change(ch, 0);		      mapID[ch] = XG_NORMAL_MAP;		    }		    break;		  case 64: /* SFX voice */		    ctl->cmsg(CMSG_INFO, VERB_DEBUG, "(XG ch=%d SFX voice)",			      ch);		    midi_drumpart_change(ch, 0);		    mapID[ch] = XG_SFX64_MAP;		    break;		  case 126: /* SFX kit */		    ctl->cmsg(CMSG_INFO, VERB_DEBUG, "(XG ch=%d SFX kit)", ch);		    midi_drumpart_change(ch, 1);		    mapID[ch] = XG_SFX126_MAP;		    break;		  case 127: /* Drum kit */		    ctl->cmsg(CMSG_INFO, VERB_DEBUG,			      "(XG ch=%d Drum kit)", ch);		    midi_drumpart_change(ch, 1);		    mapID[ch] = XG_DRUM_MAP;		    break;		  default:		    ctl->cmsg(CMSG_INFO, VERB_DEBUG,			      "(XG: ch=%d Strange bank MSB %d)",			      ch, bank_msb[ch]);		    break;		}		newbank = bank_lsb[ch];		break;	      default:		newbank = bank_msb[ch];		break;	    }	    if(ISDRUMCHANNEL(ch))		current_set[ch] = newprog;	    else	    {		if(special_tonebank >= 0)		    newbank = special_tonebank;		if(current_program[ch] == SPECIAL_PROGRAM)		    skip_this_event = 1;		current_set[ch] = newbank;	    }	    current_program[ch] = newprog;	    break;	  case ME_NOTEON:	    if(counting_time)		counting_time = 1;	    if(ISDRUMCHANNEL(ch))	    {		newbank = current_set[ch];		newprog = meep->event.a;		instrument_map(mapID[ch], &newbank, &newprog);		if(!drumset[newbank]) /* Is this a defined drumset? */		{		    if(warn_drumset[newbank] == 0)		    {			ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,				  "Drum set %d is undefined", newbank);			warn_drumset[newbank] = 1;		    }		    newbank = 0;		}		/* Mark this instrument to be loaded */		if(!(drumset[newbank]->tone[newprog].instrument))		    drumset[newbank]->tone[newprog].instrument =			MAGIC_LOAD_INSTRUMENT;	    }	    else	    {		if(current_program[ch] == SPECIAL_PROGRAM)		    break;		newbank = current_set[ch];		newprog = current_program[ch];		instrument_map(mapID[ch], &newbank, &newprog);		if(tonebank[newbank] == NULL)		{		    if(warn_tonebank[newbank] == 0)		    {			ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,				  "Tone bank %d is undefined", newbank);			warn_tonebank[newbank] = 1;		    }		    newbank = 0;		}		/* Mark this instrument to be loaded */		if(!(tonebank[newbank]->tone[newprog].instrument))		    tonebank[newbank]->tone[newprog].instrument =			MAGIC_LOAD_INSTRUMENT;	    }	    break;	  case ME_TONE_BANK_MSB:	    bank_msb[ch] = meep->event.a;	    break;	  case ME_TONE_BANK_LSB:	    bank_lsb[ch] = meep->event.a;	    break;	  case ME_CHORUS_TEXT:	  case ME_LYRIC:	  case ME_MARKER:	  case ME_INSERT_TEXT:	  case ME_TEXT:	  case ME_KARAOKE_LYRIC:	    if((meep->event.a | meep->event.b) == 0)		skip_this_event = 1;	    else if(counting_time && ctl->trace_playing)		counting_time = 1;	    break;	  case ME_DRUMPART:	    midi_drumpart_change(ch, meep->event.a);	    break;	  case ME_WRD:	    if(readmidi_wrd_mode == WRD_TRACE_MIMPI)	    {		wrd_args[wrd_argc++] = meep->event.a | 256 * meep->event.b;		if(ch != WRD_ARG)		{		    if(ch == WRD_MAG) {			wrdt->apply(WRD_MAGPRELOAD, wrd_argc, wrd_args);		    }		    else if(ch == WRD_PLOAD)			wrdt->apply(WRD_PHOPRELOAD, wrd_argc, wrd_args);		    else if(ch == WRD_PATH)			wrdt->apply(WRD_PATH, wrd_argc, wrd_args);		    wrd_argc = 0;		}	    }	    if(counting_time == 2 && readmidi_wrd_mode != WRD_TRACE_NOTHING)		counting_time = 1;	    break;	  case ME_SHERRY:	    if(counting_time == 2)		counting_time = 1;	    break;	  case ME_NOTE_STEP:	    if(counting_time == 2)		skip_this_event = 1;	    break;        }	/* Recompute time in samples*/	if((dt = meep->event.time - at) && !counting_time)	{	    samples_to_do = sample_increment * dt;	    sample_cum += sample_correction * dt;	    if(sample_cum & 0xFFFF0000)	    {		samples_to_do += ((sample_cum >> 16) & 0xFFFF);		sample_cum &= 0x0000FFFF;	    }	    st += samples_to_do;	    if(st < 0)	    {		ctl->cmsg(CMSG_ERROR, VERB_NORMAL,			  "Overflow the sample counter");		free(groomed_list);		return NULL;	    }	}	else if(counting_time == 1)	    counting_time = 0;	if(meep->event.type == ME_TEMPO)	{	    tempo = ch + meep->event.b * 256 + meep->event.a * 65536;	    tempo *= tempo_adjust;	    compute_sample_increment(tempo, divisions);	}	if(!skip_this_event)	{	    /* Add the event to the list */	    *lp = meep->event;	    lp->time = st;	    lp++;	    our_event_count++;	}	at = meep->event.time;	meep = meep->next;    }    /* Add an End-of-Track event */    lp->time = st;    lp->type = ME_EOT;    our_event_count++;    free_midi_list();        *eventsp = our_event_count;    *samplesp = st;    return groomed_list;}
4357	/local/tlutelli/issta_data/temp/c/2005_temp/2005/4357/1d0bf6e5e338ca74d843070b5cf43f17d8401495/readmidi.c/clean/timidity/readmidi.c