1
0
Fork 0
forked from len0rd/rockbox

Rearrange logic in the synthVoice loop to do less tests and remove need of a struct member for a small speedup, move some memory lookups out of the loop for a small speedup, further cosmetic changes to the synthVoice function. Change isUsed to a bool for clearer logic and also a tiny speedup

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15563 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Nils Wallménius 2007-11-11 01:02:45 +00:00
parent d185f9eba8
commit 0fd4c2e455
4 changed files with 43 additions and 45 deletions

View file

@ -263,7 +263,7 @@ static int midimain(void * filename)
{ {
notesUsed = 0; notesUsed = 0;
for(a=0; a<MAX_VOICES; a++) for(a=0; a<MAX_VOICES; a++)
if(voices[a].isUsed == 1) if(voices[a].isUsed)
notesUsed++; notesUsed++;
tick(); tick();
} while(notesUsed == 0); } while(notesUsed == 0);

View file

@ -109,11 +109,12 @@ struct SynthObject
int delta; int delta;
int decay; int decay;
unsigned int cp; /* unsigned int */ unsigned int cp; /* unsigned int */
int state, loopState; int state;
int note, vol, ch, isUsed; int note, vol, ch;
int curRate, curOffset, targetOffset; int curRate, curOffset, targetOffset;
int curPoint; int curPoint;
signed short int volscale; signed short int volscale;
bool isUsed;
}; };
struct Event struct Event

View file

@ -156,7 +156,7 @@ static inline void computeDeltas(int ch)
int a=0; int a=0;
for(a = 0; a<MAX_VOICES; a++) for(a = 0; a<MAX_VOICES; a++)
{ {
if(voices[a].isUsed==1 && voices[a].ch == ch) if(voices[a].isUsed && voices[a].ch == ch)
{ {
findDelta(&voices[a], ch, voices[a].note); findDelta(&voices[a], ch, voices[a].note);
} }
@ -202,7 +202,7 @@ inline void pressNote(int ch, int note, int vol)
if(voices[a].ch == ch && voices[a].note == note) if(voices[a].ch == ch && voices[a].note == note)
break; break;
if(voices[a].isUsed==0) if(!voices[a].isUsed)
break; break;
} }
if(a==MAX_VOICES) if(a==MAX_VOICES)
@ -227,7 +227,6 @@ inline void pressNote(int ch, int note, int vol)
setVolScale(a); setVolScale(a);
voices[a].loopState=STATE_NONLOOPING;
/* /*
* OKAY. Gt = Gus Table value * OKAY. Gt = Gus Table value
* rf = Root Frequency of wave * rf = Root Frequency of wave
@ -239,7 +238,7 @@ inline void pressNote(int ch, int note, int vol)
{ {
findDelta(&voices[a], ch, note); findDelta(&voices[a], ch, note);
/* Turn it on */ /* Turn it on */
voices[a].isUsed=1; voices[a].isUsed=true;
setPoint(&voices[a], 0); setPoint(&voices[a], 0);
} else } else
{ {
@ -256,7 +255,7 @@ inline void pressNote(int ch, int note, int vol)
wf->mode = wf->mode & (255-28); wf->mode = wf->mode & (255-28);
/* Turn it on */ /* Turn it on */
voices[a].isUsed=1; voices[a].isUsed=true;
setPoint(&voices[a], 0); setPoint(&voices[a], 0);
} else } else
@ -411,7 +410,7 @@ void seekBackward(int nsec)
{ {
notesUsed = 0; notesUsed = 0;
for(a=0; a<MAX_VOICES; a++) for(a=0; a<MAX_VOICES; a++)
if(voices[a].isUsed == 1) if(voices[a].isUsed)
notesUsed++; notesUsed++;
tick(); tick();
} while(notesUsed == 0); } while(notesUsed == 0);

View file

@ -51,7 +51,7 @@ void resetControllers()
voices[a].cp=0; voices[a].cp=0;
voices[a].vol=0; voices[a].vol=0;
voices[a].ch=0; voices[a].ch=0;
voices[a].isUsed=0; voices[a].isUsed=false;
voices[a].note=0; voices[a].note=0;
} }
@ -271,7 +271,6 @@ inline void stopVoice(struct SynthObject * so)
static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned int samples) static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned int samples)
{ {
struct GWaveform * wf; struct GWaveform * wf;
register int s;
register int s1; register int s1;
register int s2; register int s2;
@ -279,6 +278,9 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
wf = so->wf; wf = so->wf;
const unsigned int pan = chPan[so->ch];
const int volscale = so->volscale;
const int mode_mask24 = wf->mode&24; const int mode_mask24 = wf->mode&24;
const int mode_mask28 = wf->mode&28; const int mode_mask28 = wf->mode&28;
const int mode_mask_looprev = wf->mode&LOOP_REVERSE; const int mode_mask_looprev = wf->mode&LOOP_REVERSE;
@ -289,9 +291,8 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
const unsigned int start_loop = wf->startLoop << FRACTSIZE; const unsigned int start_loop = wf->startLoop << FRACTSIZE;
const int diff_loop = end_loop-start_loop; const int diff_loop = end_loop-start_loop;
while(samples > 0) while(samples-- > 0)
{ {
samples--;
/* Is voice being ramped? */ /* Is voice being ramped? */
if(so->state == STATE_RAMPDOWN) if(so->state == STATE_RAMPDOWN)
{ {
@ -300,12 +301,12 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
so->decay = so->decay / 2; so->decay = so->decay / 2;
if(so->decay < 10 && so->decay > -10) if(so->decay < 10 && so->decay > -10)
so->isUsed = 0; so->isUsed = false;
s1=so->decay; s1=so->decay;
s2 = s1*chPan[so->ch]; s2 = s1*pan;
s1 = (s1<<7) -s2; s1 = (s1<<7) -s2;
*(out++)+=(((s1&0x7FFF80) << 9) | ((s2&0x7FFF80) >> 7)); *(out++)+=((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
continue; continue;
} }
} else /* OK to advance voice */ } else /* OK to advance voice */
@ -315,8 +316,19 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
s2 = getSample((cp_temp >> FRACTSIZE)+1, wf); s2 = getSample((cp_temp >> FRACTSIZE)+1, wf);
if(mode_mask28 && cp_temp >= end_loop)
{
if(!mode_mask24)
{
cp_temp -= diff_loop;
s2=getSample((cp_temp >> FRACTSIZE), wf);
}
else
{
so->delta = -so->delta;
/* LOOP_REVERSE|LOOP_PINGPONG = 24 */ /* LOOP_REVERSE|LOOP_PINGPONG = 24 */
if(mode_mask24 && so->loopState == STATE_LOOPING && (cp_temp < start_loop)) if(cp_temp < start_loop) /* this appears to never be true in here */
{ {
if(mode_mask_looprev) if(mode_mask_looprev)
{ {
@ -328,18 +340,6 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */ so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */
} }
} }
if(mode_mask28 && (cp_temp >= end_loop))
{
so->loopState = STATE_LOOPING;
if(!mode_mask24)
{
cp_temp -= diff_loop;
s2=getSample((cp_temp >> FRACTSIZE), wf);
}
else
{
so->delta = -so->delta;
} }
} }
@ -354,12 +354,12 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
/* Better, working, linear interpolation */ /* Better, working, linear interpolation */
s1=getSample((cp_temp >> FRACTSIZE), wf); s1=getSample((cp_temp >> FRACTSIZE), wf);
s = s1 + ((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE); s1 +=((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE);
if(so->curRate == 0) if(so->curRate == 0)
{ {
stopVoice(so); stopVoice(so);
// so->isUsed = 0; // so->isUsed = false;
} }
@ -404,25 +404,23 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
stopVoice(so); stopVoice(so);
} }
s = (s * (so->curOffset >> 22) >> 8); s1 = s1 * (so->curOffset >> 22) >> 8;
/* Scaling by channel volume and note volume is done in sequencer.c */
/* That saves us some multiplication and pointer operations */
s1 = s1 * volscale >> 14;
/* need to set ramp beginning */ /* need to set ramp beginning */
if(so->state == STATE_RAMPDOWN && so->decay == 0) if(so->state == STATE_RAMPDOWN && so->decay == 0)
{ {
so->decay = s*so->volscale>>14; so->decay = s1;
if(so->decay == 0) if(so->decay == 0)
so->decay = 1; /* stupid junk.. */ so->decay = 1; /* stupid junk.. */
} }
s2 = s1*pan;
/* Scaling by channel volume and note volume is done in sequencer.c */
/* That saves us some multiplication and pointer operations */
s1=s*so->volscale>>14;
s2 = s1*chPan[so->ch];
s1 = (s1<<7) - s2; s1 = (s1<<7) - s2;
*(out++)+=(((s1&0x7FFF80) << 9) | ((s2&0x7FFF80) >> 7)); *(out++)+=((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
} }
so->cp=cp_temp; /* store this again */ so->cp=cp_temp; /* store this again */
@ -451,7 +449,7 @@ void synthSamples(int32_t *buf_ptr, unsigned int num_samples)
for(i=0; i < MAX_VOICES; i++) for(i=0; i < MAX_VOICES; i++)
{ {
voicept=&voices[i]; voicept=&voices[i];
if(voicept->isUsed==1) if(voicept->isUsed)
{ {
synthVoice(voicept, samp_buf, num_samples); synthVoice(voicept, samp_buf, num_samples);
} }