Fixed Point Math and Pitch Bend

133 views
Skip to first unread message

Glen van Alkemade

unread,
Jul 5, 2017, 5:18:36 PM7/5/17
to Mozzi-users
I have this nice midi pitch bend feature working great, but there's some noise and I am sure the math efficiency can be improved. How can I use fixed point math, or some other ingenious ploy, to offload some of the float math happening here?

PBmax = 1.1225; // retrieved from an array, based on the number of semitones the user wants for maximum bend.
PBmin = 0.8909;

void HandlePitchBend (byte channel, int bend)
{
//bend values from +/-8192. Bend range from 2 to 7 semitones, user-selected
if(bend>0){
shifted = pitch*(1 + ((float(bend/8192.0f)*PBmax)));
else if(bend<0){
shifted = (pitch*(1 + ((float(bend/8192.0f)*PBmin)));
else {
shifted = pitch;       // force to no-bend when bend=0.
              }
aSin.setFreq(shifted);
}

Glen van Alkemade

unread,
Jul 7, 2017, 11:11:29 AM7/7/17
to Mozzi-users
Actually, I see a flaw in the algorith in my post. My question stands, but the code snippet looks like this:

PBmax = 1.1225; // retrieved from an array, based on the number of semitones the user wants for maximum bend.
PBmin = 0.8909;

void HandlePitchBend (byte channel, int bend)
{
//bend values from +/-8192. Bend range from 2 to 7 semitones, user-selected
if(bend>0){
shifted = pitch*(1 + ((float(bend/8192.0f)*PBmax)));
else if(bend<0){
shifted = (pitch*((float(bend/8192.0f)*PBmin));
else {
shifted = pitch;       // force to no-bend when bend=0.
              }
aSin.setFreq(shifted);
}

Glen van Alkemade

unread,
Jul 7, 2017, 11:14:40 AM7/7/17
to Mozzi-users
Or again, this:

PBmax = 1.1225; // retrieved from an array, based on the number of semitones the user wants for maximum bend.
PBmin = 0.8909;

void HandlePitchBend (byte channel, int bend)
{
//bend values from +/-8192. Bend range (PBmax, PBmin) from 2 to 7 semitones, user-selected
if(bend>0){
shifted = pitch*(float(bend/8192.0f)*PBmax));
else if(bend<0){
shifted = (pitch*(float(bend/8192.0f)*PBmin));
else {
shifted = pitch;       // force to no-bend when bend=0.
              }
aSin.setFreq(shifted);
}

Tim Barrass

unread,
Jul 8, 2017, 7:08:09 AM7/8/17
to mozzi...@googlegroups.com
Hi Glen,

factor out things which don't need to be calculated every time... like PBmax/8192.f
... even better if you can make it a constant, or make it calculate only when there's a change.
You really want to avoid division, it is very slow.

Then you might make sure everything is in fixed point.. I can't tell from your code what types your variables are, but you could do this kind of thing...

Q16n16 Q16n16-max-multiplier = float_to_Q16n16(PBmax/8192.f);

Have a look in mozzi_fixmath.h for some fixed point types and conversions, and google fixed point arithmetic.
Try to choose the smallest types you can for the precision you need.

I hope that gets you started.  Ask more if you get stuck.

Tim
-- 
You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
To post to this group, send email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/f53785ca-bf4f-4c05-be6d-5d0aeb0d0a2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages