From 7f1fbe4af2d7e44e808418a969f5d2301b002c61 Mon Sep 17 00:00:00 2001 From: Stepan Moskovchenko Date: Sat, 17 Nov 2007 06:39:02 +0000 Subject: MIDI: Increase percision of synthesizer by a factor of 4 - makes certain parts (guitar bends, mostly) sound more natural. Also, completely rearrange the order of operations in the delta computation. Had to use long longs. Probably not a good idea for speed, but the order can be optimized more later. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15652 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugins/midi/midiutil.h | 2 +- apps/plugins/midi/sequencer.c | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/plugins/midi/midiutil.h b/apps/plugins/midi/midiutil.h index 18d493b23a..c3c5d91f47 100644 --- a/apps/plugins/midi/midiutil.h +++ b/apps/plugins/midi/midiutil.h @@ -17,7 +17,7 @@ * ****************************************************************************/ -#define FRACTSIZE 10 +#define FRACTSIZE 12 #define BUF_SIZE 8192 /* 32 kB output buffers */ #define NBUF 2 diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c index bd2f33b365..e2a9f7dd5c 100644 --- a/apps/plugins/midi/sequencer.c +++ b/apps/plugins/midi/sequencer.c @@ -144,10 +144,25 @@ static void findDelta(struct SynthObject * so, int ch, int note) { struct GWaveform * wf = patchSet[chPat[ch]]->waveforms[patchSet[chPat[ch]]->noteTable[note]]; so->wf=wf; - unsigned int delta= 0; + + /* Used to be unsigned int, but math had to be done in different order to avoid overflow */ + unsigned long long delta= 0; + +/* + Old formula: delta = (((gustable[note+chPBNoteOffset[ch]]<rootFreq)) * wf->sampRate / (SAMPLE_RATE)); - delta = (delta * chPBFractBend[ch])>> 16; + Plus some pitch stuff. See old SVN for how it used to be +*/ + + delta = (((gustable[note+chPBNoteOffset[ch]]))); /* anywhere from 8000 to 8000000 */ + delta = delta * wf->sampRate; /* approx 20000 - 44000 but can vary with tuning */ + delta = (delta * chPBFractBend[ch]); /* approx 60000 - 70000 */ + delta = delta / (SAMPLE_RATE); /* 44100 or 22050 */ + delta = delta / (wf->rootFreq); /* anywhere from 8000 to 8000000 */ + + /* Pitch bend is encoded as a fractional of 16 bits, hence the 16 */ + delta = delta >> (16 - FRACTSIZE); /* a shift of approx 4 bits */ so->delta = delta; } @@ -250,7 +265,7 @@ inline void pressNote(int ch, int note, int vol) struct GWaveform * wf = drumSet[note]->waveforms[0]; voices[a].wf=wf; - voices[a].delta = (((gustable[note]<<10) / wf->rootFreq) * wf->sampRate / SAMPLE_RATE); + voices[a].delta = (((gustable[note]<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); -- cgit v1.2.3