From 28b5afd05a075cce1f46e5cc85c7e2caae3fc6ee Mon Sep 17 00:00:00 2001 From: Stepan Moskovchenko Date: Wed, 3 May 2006 19:32:22 +0000 Subject: Optimize synth code by pre-computing the volume scaling for each note. Scaling is now calculated once per MIDI event at the most, instead of once per sample. Increase voice ramping speed, increase number of active voices a little. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@9870 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugins/midi/sequencer.c | 47 +++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'apps/plugins/midi/sequencer.c') diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c index b6fb592294..ebd5b76405 100644 --- a/apps/plugins/midi/sequencer.c +++ b/apps/plugins/midi/sequencer.c @@ -16,6 +16,7 @@ * ****************************************************************************/ +void setVolScale(int a); extern struct plugin_api * rb; @@ -24,8 +25,14 @@ long tempo=375000; inline void setVol(int ch, int vol) { -// printf("\nvolume[%d] %d ==> %d", ch, chVol[ch], 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; // \|/ was 10 - so->delta = (((gustable[note]<<10) / (wf->rootFreq)) * wf->sampRate / (SAMPLE_RATE)); - so->delta = (so->delta * pitchTbl[chPW[ch]])>> 16; -} -*/ - void findDelta(struct SynthObject * so, int ch, int note) { struct GWaveform * wf = patchSet[chPat[ch]]->waveforms[patchSet[chPat[ch]]->noteTable[note]]; - so->wf=wf; // \|/ was 10 - - unsigned long delta= 0 ; + so->wf=wf; + unsigned long delta= 0; /* More percision- extra bit - not so off-key as before */ delta = (((gustable[note]<rootFreq)) * wf->sampRate / (SAMPLE_RATE)); delta = (delta * pitchTbl[chPW[ch]])>> 16; @@ -139,7 +134,6 @@ void findDelta(struct SynthObject * so, int ch, int note) inline void setPW(int ch, int msb, int lsb) { -// printf("\npitchw[%d] %d ==> %d", ch, chPW[ch], msb); chPW[ch] = msb<<2|lsb>>5; int a=0; @@ -152,16 +146,26 @@ inline void setPW(int ch, int msb, int lsb) } } + +/* 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. */ +void setVolScale(int a) +{ + struct SynthObject * so = &voices[a]; + so->volscale = ((signed short int)so->vol*(signed short int)chVol[so->ch]); +} + void pressNote(int ch, int note, int vol) { static int lastKill = 0; -//Silences all channels but one, for easy debugging, for me. +/* Silences all channels but one, for easy debugging, for me. */ /* if(ch == 0) return; if(ch == 1) return; if(ch == 2) return; if(ch == 3) return; -// if(ch == 4) return; + if(ch == 4) return; if(ch == 5) return; if(ch == 6) return; if(ch == 7) return; @@ -203,6 +207,7 @@ void pressNote(int ch, int note, int vol) voices[a].state=STATE_ATTACK; voices[a].decay=255; + setVolScale(a); voices[a].loopState=STATE_NONLOOPING; voices[a].loopDir = LOOPDIR_FORWARD; @@ -304,9 +309,7 @@ void sendEvent(struct Event * ev) if((ev->status & 0xF0) == MIDI_PRGM) { - if((ev->status & 0x0F) == 9) - printf("\nNOT PATCHING: Someone tried patching Channel 9 onto something?"); - else + if((ev->status & 0x0F) != 9) setPatch(ev->status & 0x0F, ev->d1); } } @@ -351,7 +354,7 @@ int tick(void) 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); +/* printf("\nMeta-Event: Tempo Set = %d", tempo); */ bpm=mf->div*1000000/tempo; numberOfSamples=SAMPLE_RATE/bpm; -- cgit v1.2.3