From e1940b87b005fdaba38e6230551291001f54e99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Wallm=C3=A9nius?= Date: Thu, 4 Oct 2007 19:36:42 +0000 Subject: [PATCH] Optimisation of the midi player, reducing the number of multiplications and memory accesses inside a very frequently executed loop, also does shifting of the whole sample when synthing is done which improves accurracy slightly, ~10% fewer buffer misses git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14983 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugins/midi/midiutil.c | 3 +-- apps/plugins/midi/midiutil.h | 3 +-- apps/plugins/midi/sequencer.c | 11 +---------- apps/plugins/midi/synth.c | 5 ++--- apps/plugins/midi/synth.h | 25 +++++++++++++++---------- 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/apps/plugins/midi/midiutil.c b/apps/plugins/midi/midiutil.c index 7cb128fb84..aba56c5a8c 100644 --- a/apps/plugins/midi/midiutil.c +++ b/apps/plugins/midi/midiutil.c @@ -22,8 +22,7 @@ extern struct plugin_api * rb; int chVol[16] IBSS_ATTR; /* Channel volume */ -int chPanLeft[16] IBSS_ATTR; /* Channel panning */ -int chPanRight[16] IBSS_ATTR; +int chPan[16] IBSS_ATTR; /* Channel panning */ int chPat[16] IBSS_ATTR; /* Channel patch */ int chPW[16] IBSS_ATTR; /* Channel pitch wheel, MSB only */ diff --git a/apps/plugins/midi/midiutil.h b/apps/plugins/midi/midiutil.h index 6968c83c9a..3604f422a5 100644 --- a/apps/plugins/midi/midiutil.h +++ b/apps/plugins/midi/midiutil.h @@ -152,8 +152,7 @@ void * my_malloc(int size); extern struct SynthObject voices[MAX_VOICES]; extern int chVol[16]; /* Channel volume */ -extern int chPanLeft[16]; /* Channel panning */ -extern int chPanRight[16]; +extern int chPan[16]; /* Channel panning */ extern int chPat[16]; /* Channel patch */ extern int chPW[16]; /* Channel pitch wheel, MSB only */ diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c index 82da3efbcf..1a00c078c6 100644 --- a/apps/plugins/midi/sequencer.c +++ b/apps/plugins/midi/sequencer.c @@ -46,15 +46,6 @@ static inline void setVol(int ch, int vol) setVolScale(a); } -static inline void setPan(int ch, int pan) -{ -// printf("\npanning[%d] %d ==> %d", ch, chPanRight[ch], pan); - - chPanLeft[ch]=128-pan; - chPanRight[ch]=pan; -} - - static inline void setPatch(int ch, int pat) { chPat[ch]=pat; @@ -286,7 +277,7 @@ static void sendEvent(struct Event * ev) } case CTRL_PANNING: { - setPan((status_low), d2); + chPan[status_low]=d2; return; } } diff --git a/apps/plugins/midi/synth.c b/apps/plugins/midi/synth.c index 4936afb655..322d0f792d 100644 --- a/apps/plugins/midi/synth.c +++ b/apps/plugins/midi/synth.c @@ -62,10 +62,9 @@ int initSynth(struct MIDIfile * mf, char * filename, char * drumConfig) for(a=0; a<16; a++) { chVol[a]=100; /* Default, not quite full blast.. */ - chPanLeft[a]=64; /* Center */ - chPanRight[a]=64; /* Center */ + chPan[a]=64; /* Center */ chPat[a]=0; /* Ac Gr Piano */ - chPW[a]=256; /* .. not .. bent ? */ + chPW[a]=256; /* .. not .. bent ? */ } for(a=0; a<128; a++) { diff --git a/apps/plugins/midi/synth.h b/apps/plugins/midi/synth.h index 223b5974e2..2b7187e819 100644 --- a/apps/plugins/midi/synth.h +++ b/apps/plugins/midi/synth.h @@ -25,7 +25,7 @@ static inline void synthSample(int * mixL, int * mixR) int i; register int dL=0; register int dR=0; - register short sample = 0; + register int sample = 0; register struct SynthObject *voicept=voices; for(i=MAX_VOICES/2; i > 0; i--) @@ -33,15 +33,17 @@ static inline void synthSample(int * mixL, int * mixR) if(voicept->isUsed==1) { sample = synthVoice(voicept); - dL += (sample*chPanLeft[voicept->ch])>>7; - dR += (sample*chPanRight[voicept->ch])>>7; + dL += sample; + sample *= chPan[voicept->ch]; + dR += sample; } voicept++; if(voicept->isUsed==1) { sample = synthVoice(voicept); - dL += (sample*chPanLeft[voicept->ch])>>7; - dR += (sample*chPanRight[voicept->ch])>>7; + dL += sample; + sample *= chPan[voicept->ch]; + dR += sample; } voicept++; } @@ -51,19 +53,22 @@ static inline void synthSample(int * mixL, int * mixR) if(voicept->isUsed==1) { sample = synthVoice(voicept); - dL += (sample*chPanLeft[voicept->ch])>>7; - dR += (sample*chPanRight[voicept->ch])>>7; + dL += sample; + sample *= chPan[voicept->ch]; + dR += sample; } voicept++; } - *mixL=dL; - *mixR=dR; + dL = (dL << 7) - dR; + + *mixL=dL >> 7; + *mixR=dR >> 7; /* TODO: Automatic Gain Control, anyone? */ /* Or, should this be implemented on the DSP's output volume instead? */ - return; /* No more ghetto lowpass filter.. linear intrpolation works well. */ + return; /* No more ghetto lowpass filter. Linear interpolation works well. */ } static inline struct Event * getEvent(struct Track * tr, int evNum)