/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2005 Stepan Moskovchenko * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "plugin.h" #include "midiutil.h" #include "guspat.h" #include "synth.h" extern struct plugin_api * rb; long tempo=375000; /* Sets the volume scaling by channel volume and note volume */ /* This way we can do the multiplication/indexing once per */ /* MIDI event at the most, instead of once per sample. */ static inline void setVolScale(int a) { struct SynthObject * so = &voices[a]; so->volscale = ((signed short int)so->vol*(signed short int)chVol[so->ch]); } static inline void setVol(int ch, int vol) { int a=0; chVol[ch]=vol; /* If channel volume changes, we need to recalculate the volume scale */ /* factor for all voices active on this channel */ for(a=0; awaveforms[patchSet[chPat[ch]]->noteTable[note]]; so->wf=wf; unsigned int delta= 0; delta = (((gustable[note]<rootFreq)) * wf->sampRate / (SAMPLE_RATE)); delta = (delta * pitchTbl[chPW[ch]])>> 16; so->delta = delta; } static inline void setPW(int ch, int msb, int lsb) { chPW[ch] = msb<<2|lsb>>5; int a=0; for(a = 0; awaveforms[0]; voices[a].wf=wf; voices[a].delta = (((gustable[note]<<10) / wf->rootFreq) * wf->sampRate / SAMPLE_RATE); if(wf->mode & 28) // printf("\nWoah, a drum patch has a loop. Stripping the loop..."); wf->mode = wf->mode & (255-28); /* Turn it on */ voices[a].isUsed=1; setPoint(&voices[a], 0); } else { /* printf("\nWarning: drum %d does not have a patch defined... Ignoring it", note); */ } } } static void releaseNote(int ch, int note) { if(ch==9) return; int a=0; for(a=0; amode & 28)) { setPoint(&voices[a], 3); } } } } static void sendEvent(struct Event * ev) { const unsigned char status_low = ev->status & 0x0F; const unsigned char d1 = ev->d1; const unsigned char d2 = ev->d2; switch(ev->status & 0xF0) { case MIDI_CONTROL: switch(d1) { case CTRL_VOLUME: { setVol((status_low), d2); return; } case CTRL_PANNING: { chPan[status_low]=d2; return; } } break; case MIDI_PITCHW: setPW((status_low), d2, d1); return; case MIDI_NOTE_ON: switch(d2) { case 0: /* Release by vol=0 */ releaseNote(status_low, d1); return; default: pressNote(status_low, d1, d2); return; } case MIDI_NOTE_OFF: releaseNote(status_low, d1); return; case MIDI_PRGM: if((status_low) != 9) setPatch(status_low, d1); } } int tick(void) ICODE_ATTR; int tick(void) { if(mf==NULL) return 0; int a=0; int tracksAdv=0; for(a=0; anumTracks; a++) { struct Track * tr = mf->tracks[a]; if(tr == NULL) printf("NULL TRACK: %d", a); //BIG DEBUG STATEMENT //printf("\nTrack %2d, Event = %4d of %4d, Delta = %5d, Next = %4d", a, tr->pos, tr->numEvents, tr->delta, getEvent(tr, tr->pos)->delta); if(tr != NULL && (tr->pos < tr->numEvents)) { tr->delta++; tracksAdv++; while(getEvent(tr, tr->pos)->delta <= tr->delta) { struct Event * e = getEvent(tr, tr->pos); if(e->status != 0xFF) { sendEvent(e); if(((e->status&0xF0) == MIDI_PRGM)) { /* printf("\nPatch Event, patch[%d] ==> %d", e->status&0xF, e->d1); */ } } else { if(e->d1 == 0x51) { tempo = (((short)e->evData[0])<<16)|(((short)e->evData[1])<<8)|(e->evData[2]); /* printf("\nMeta-Event: Tempo Set = %d", tempo); */ bpm=mf->div*1000000/tempo; numberOfSamples=SAMPLE_RATE/bpm; } } tr->delta = 0; tr->pos++; if(tr->pos>=(tr->numEvents-1)) break; } } } if(tracksAdv != 0) return 1; else return 0; }