summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNils Wallménius <nils@rockbox.org>2007-11-11 01:02:45 +0000
committerNils Wallménius <nils@rockbox.org>2007-11-11 01:02:45 +0000
commit0fd4c2e455957ec078425361972003e8c3c50fe4 (patch)
treedbd1e43836bf567cf8c5bc1f54c9eb4e3c2a77be
parentd185f9eba8706fb2415eb6406a0ffd90113e95da (diff)
downloadrockbox-0fd4c2e455957ec078425361972003e8c3c50fe4.tar.gz
rockbox-0fd4c2e455957ec078425361972003e8c3c50fe4.zip
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
-rw-r--r--apps/plugins/midi/midiplay.c2
-rw-r--r--apps/plugins/midi/midiutil.h5
-rw-r--r--apps/plugins/midi/sequencer.c11
-rw-r--r--apps/plugins/midi/synth.c70
4 files changed, 43 insertions, 45 deletions
diff --git a/apps/plugins/midi/midiplay.c b/apps/plugins/midi/midiplay.c
index cc8746308f..934cea7695 100644
--- a/apps/plugins/midi/midiplay.c
+++ b/apps/plugins/midi/midiplay.c
@@ -263,7 +263,7 @@ static int midimain(void * filename)
263 { 263 {
264 notesUsed = 0; 264 notesUsed = 0;
265 for(a=0; a<MAX_VOICES; a++) 265 for(a=0; a<MAX_VOICES; a++)
266 if(voices[a].isUsed == 1) 266 if(voices[a].isUsed)
267 notesUsed++; 267 notesUsed++;
268 tick(); 268 tick();
269 } while(notesUsed == 0); 269 } while(notesUsed == 0);
diff --git a/apps/plugins/midi/midiutil.h b/apps/plugins/midi/midiutil.h
index dfffe39dd6..f26f1208e3 100644
--- a/apps/plugins/midi/midiutil.h
+++ b/apps/plugins/midi/midiutil.h
@@ -109,11 +109,12 @@ struct SynthObject
109 int delta; 109 int delta;
110 int decay; 110 int decay;
111 unsigned int cp; /* unsigned int */ 111 unsigned int cp; /* unsigned int */
112 int state, loopState; 112 int state;
113 int note, vol, ch, isUsed; 113 int note, vol, ch;
114 int curRate, curOffset, targetOffset; 114 int curRate, curOffset, targetOffset;
115 int curPoint; 115 int curPoint;
116 signed short int volscale; 116 signed short int volscale;
117 bool isUsed;
117}; 118};
118 119
119struct Event 120struct Event
diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c
index 4e6c15fba6..7847c375ba 100644
--- a/apps/plugins/midi/sequencer.c
+++ b/apps/plugins/midi/sequencer.c
@@ -156,7 +156,7 @@ static inline void computeDeltas(int ch)
156 int a=0; 156 int a=0;
157 for(a = 0; a<MAX_VOICES; a++) 157 for(a = 0; a<MAX_VOICES; a++)
158 { 158 {
159 if(voices[a].isUsed==1 && voices[a].ch == ch) 159 if(voices[a].isUsed && voices[a].ch == ch)
160 { 160 {
161 findDelta(&voices[a], ch, voices[a].note); 161 findDelta(&voices[a], ch, voices[a].note);
162 } 162 }
@@ -202,7 +202,7 @@ inline void pressNote(int ch, int note, int vol)
202 if(voices[a].ch == ch && voices[a].note == note) 202 if(voices[a].ch == ch && voices[a].note == note)
203 break; 203 break;
204 204
205 if(voices[a].isUsed==0) 205 if(!voices[a].isUsed)
206 break; 206 break;
207 } 207 }
208 if(a==MAX_VOICES) 208 if(a==MAX_VOICES)
@@ -227,7 +227,6 @@ inline void pressNote(int ch, int note, int vol)
227 227
228 setVolScale(a); 228 setVolScale(a);
229 229
230 voices[a].loopState=STATE_NONLOOPING;
231 /* 230 /*
232 * OKAY. Gt = Gus Table value 231 * OKAY. Gt = Gus Table value
233 * rf = Root Frequency of wave 232 * rf = Root Frequency of wave
@@ -239,7 +238,7 @@ inline void pressNote(int ch, int note, int vol)
239 { 238 {
240 findDelta(&voices[a], ch, note); 239 findDelta(&voices[a], ch, note);
241 /* Turn it on */ 240 /* Turn it on */
242 voices[a].isUsed=1; 241 voices[a].isUsed=true;
243 setPoint(&voices[a], 0); 242 setPoint(&voices[a], 0);
244 } else 243 } else
245 { 244 {
@@ -256,7 +255,7 @@ inline void pressNote(int ch, int note, int vol)
256 wf->mode = wf->mode & (255-28); 255 wf->mode = wf->mode & (255-28);
257 256
258 /* Turn it on */ 257 /* Turn it on */
259 voices[a].isUsed=1; 258 voices[a].isUsed=true;
260 setPoint(&voices[a], 0); 259 setPoint(&voices[a], 0);
261 260
262 } else 261 } else
@@ -411,7 +410,7 @@ void seekBackward(int nsec)
411 { 410 {
412 notesUsed = 0; 411 notesUsed = 0;
413 for(a=0; a<MAX_VOICES; a++) 412 for(a=0; a<MAX_VOICES; a++)
414 if(voices[a].isUsed == 1) 413 if(voices[a].isUsed)
415 notesUsed++; 414 notesUsed++;
416 tick(); 415 tick();
417 } while(notesUsed == 0); 416 } while(notesUsed == 0);
diff --git a/apps/plugins/midi/synth.c b/apps/plugins/midi/synth.c
index ca59c76a8b..b2efce1f7d 100644
--- a/apps/plugins/midi/synth.c
+++ b/apps/plugins/midi/synth.c
@@ -51,7 +51,7 @@ void resetControllers()
51 voices[a].cp=0; 51 voices[a].cp=0;
52 voices[a].vol=0; 52 voices[a].vol=0;
53 voices[a].ch=0; 53 voices[a].ch=0;
54 voices[a].isUsed=0; 54 voices[a].isUsed=false;
55 voices[a].note=0; 55 voices[a].note=0;
56 } 56 }
57 57
@@ -271,7 +271,6 @@ inline void stopVoice(struct SynthObject * so)
271static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned int samples) 271static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned int samples)
272{ 272{
273 struct GWaveform * wf; 273 struct GWaveform * wf;
274 register int s;
275 register int s1; 274 register int s1;
276 register int s2; 275 register int s2;
277 276
@@ -279,6 +278,9 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
279 278
280 wf = so->wf; 279 wf = so->wf;
281 280
281 const unsigned int pan = chPan[so->ch];
282 const int volscale = so->volscale;
283
282 const int mode_mask24 = wf->mode&24; 284 const int mode_mask24 = wf->mode&24;
283 const int mode_mask28 = wf->mode&28; 285 const int mode_mask28 = wf->mode&28;
284 const int mode_mask_looprev = wf->mode&LOOP_REVERSE; 286 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
289 const unsigned int start_loop = wf->startLoop << FRACTSIZE; 291 const unsigned int start_loop = wf->startLoop << FRACTSIZE;
290 const int diff_loop = end_loop-start_loop; 292 const int diff_loop = end_loop-start_loop;
291 293
292 while(samples > 0) 294 while(samples-- > 0)
293 { 295 {
294 samples--;
295 /* Is voice being ramped? */ 296 /* Is voice being ramped? */
296 if(so->state == STATE_RAMPDOWN) 297 if(so->state == STATE_RAMPDOWN)
297 { 298 {
@@ -300,12 +301,12 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
300 so->decay = so->decay / 2; 301 so->decay = so->decay / 2;
301 302
302 if(so->decay < 10 && so->decay > -10) 303 if(so->decay < 10 && so->decay > -10)
303 so->isUsed = 0; 304 so->isUsed = false;
304 305
305 s1=so->decay; 306 s1=so->decay;
306 s2 = s1*chPan[so->ch]; 307 s2 = s1*pan;
307 s1 = (s1<<7) -s2; 308 s1 = (s1<<7) -s2;
308 *(out++)+=(((s1&0x7FFF80) << 9) | ((s2&0x7FFF80) >> 7)); 309 *(out++)+=((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
309 continue; 310 continue;
310 } 311 }
311 } else /* OK to advance voice */ 312 } else /* OK to advance voice */
@@ -315,23 +316,8 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
315 316
316 s2 = getSample((cp_temp >> FRACTSIZE)+1, wf); 317 s2 = getSample((cp_temp >> FRACTSIZE)+1, wf);
317 318
318 /* LOOP_REVERSE|LOOP_PINGPONG = 24 */ 319 if(mode_mask28 && cp_temp >= end_loop)
319 if(mode_mask24 && so->loopState == STATE_LOOPING && (cp_temp < start_loop))
320 { 320 {
321 if(mode_mask_looprev)
322 {
323 cp_temp += diff_loop;
324 s2=getSample((cp_temp >> FRACTSIZE), wf);
325 }
326 else
327 {
328 so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */
329 }
330 }
331
332 if(mode_mask28 && (cp_temp >= end_loop))
333 {
334 so->loopState = STATE_LOOPING;
335 if(!mode_mask24) 321 if(!mode_mask24)
336 { 322 {
337 cp_temp -= diff_loop; 323 cp_temp -= diff_loop;
@@ -340,6 +326,20 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
340 else 326 else
341 { 327 {
342 so->delta = -so->delta; 328 so->delta = -so->delta;
329
330 /* LOOP_REVERSE|LOOP_PINGPONG = 24 */
331 if(cp_temp < start_loop) /* this appears to never be true in here */
332 {
333 if(mode_mask_looprev)
334 {
335 cp_temp += diff_loop;
336 s2=getSample((cp_temp >> FRACTSIZE), wf);
337 }
338 else
339 {
340 so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */
341 }
342 }
343 } 343 }
344 } 344 }
345 345
@@ -354,12 +354,12 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
354 /* Better, working, linear interpolation */ 354 /* Better, working, linear interpolation */
355 s1=getSample((cp_temp >> FRACTSIZE), wf); 355 s1=getSample((cp_temp >> FRACTSIZE), wf);
356 356
357 s = s1 + ((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE); 357 s1 +=((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE);
358 358
359 if(so->curRate == 0) 359 if(so->curRate == 0)
360 { 360 {
361 stopVoice(so); 361 stopVoice(so);
362// so->isUsed = 0; 362// so->isUsed = false;
363 363
364 } 364 }
365 365
@@ -404,25 +404,23 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i
404 stopVoice(so); 404 stopVoice(so);
405 } 405 }
406 406
407 s = (s * (so->curOffset >> 22) >> 8); 407 s1 = s1 * (so->curOffset >> 22) >> 8;
408
409 /* Scaling by channel volume and note volume is done in sequencer.c */
410 /* That saves us some multiplication and pointer operations */
411 s1 = s1 * volscale >> 14;
408 412
409 /* need to set ramp beginning */ 413 /* need to set ramp beginning */
410 if(so->state == STATE_RAMPDOWN && so->decay == 0) 414 if(so->state == STATE_RAMPDOWN && so->decay == 0)
411 { 415 {
412 so->decay = s*so->volscale>>14; 416 so->decay = s1;
413 if(so->decay == 0) 417 if(so->decay == 0)
414 so->decay = 1; /* stupid junk.. */ 418 so->decay = 1; /* stupid junk.. */
415 } 419 }
416 420
417 421 s2 = s1*pan;
418 /* Scaling by channel volume and note volume is done in sequencer.c */
419 /* That saves us some multiplication and pointer operations */
420 s1=s*so->volscale>>14;
421
422 s2 = s1*chPan[so->ch];
423 s1 = (s1<<7) - s2; 422 s1 = (s1<<7) - s2;
424 *(out++)+=(((s1&0x7FFF80) << 9) | ((s2&0x7FFF80) >> 7)); 423 *(out++)+=((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
425
426 } 424 }
427 425
428 so->cp=cp_temp; /* store this again */ 426 so->cp=cp_temp; /* store this again */
@@ -451,7 +449,7 @@ void synthSamples(int32_t *buf_ptr, unsigned int num_samples)
451 for(i=0; i < MAX_VOICES; i++) 449 for(i=0; i < MAX_VOICES; i++)
452 { 450 {
453 voicept=&voices[i]; 451 voicept=&voices[i];
454 if(voicept->isUsed==1) 452 if(voicept->isUsed)
455 { 453 {
456 synthVoice(voicept, samp_buf, num_samples); 454 synthVoice(voicept, samp_buf, num_samples);
457 } 455 }