Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Help with pitch bend formula

  1. #1

    Help with pitch bend formula

    I would like some suggestions of a formula regarding pitch bend.

    Voice 1 plays MIDI note 57 (A3,220Hz).
    Voice 2 plays MIDI note 67 (G4,392Hz).
    Voice 3 plays MIDI note 72 (C5,523Hz).

    For example, say I want to pitch bend these three independent voices up one semitone.

    The difference in frequency for Voice 1 is 13Hz (from 220hz to 233hz)
    The difference in frequency for Voice 2 is 23Hz (from 392hz to 415hz)
    The difference in frequency for Voice 3 is 31Hz (from 523hz to 554hz)

    How would I add a pitch bend value (range from -8192 to +8192) to these formulae so that the frequencies keep their harmonic relationship?

    Voice1Frequency = 8.175 * powf(1.059463,57)
    Voice2Frequency = 8.175 * powf(1.059463,67)
    Voice2Frequency = 8.175 * powf(1.059463,72)

    /Johan

  2. #2
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    416
    Voice1Frequency = 8.175 * powf(1.059463,57+pitchbend/8192.0)
    Voice2Frequency = 8.175 * powf(1.059463,67+pitchbend/8192.0)
    Voice2Frequency = 8.175 * powf(1.059463,72+pitchbend/8192.0)

    alternatively, multiply each voice frequency by the pitch bend factor:

    float pb = powf(1.059463, pitchbend/8192.0)
    VF1 = Voice1Frequency*pb
    VF2 = Voice2Frequency*pb
    VF3 = Voice3Frequency*pb
    Last edited by mlu; 03-19-2021 at 03:02 PM.

  3. #3
    Senior Member
    Join Date
    Jul 2020
    Posts
    985
    powf needs two arguments.

    To convert a linear control value into a geometric factor needs an exponential function, such as
    Code:
    float pb = exp(pitchbend/8192.0);
    VF1 = Voice1Frequency*pb;
    VF2 = Voice2Frequency*pb;
    VF3 = Voice3Frequency*pb ;
    Though perhaps if you want a certain number of octaves this might be better:
    Code:
    float pb = powf(4.0, pitchbend/8192.0);  // +/- 2 octaves as 4 is 2 octave factor

  4. #4
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    416
    sloppy of me, corrected

  5. #5
    Thanks mlu,
    so I guess this would work when bending up/down more than one note (bendRange being for example 1 to 12 notes)?

    Voice1Frequency=8.175*powf(1.059463,57+((pitchbend/8192.0)*bendRange))
    Voice2Frequency=8.175*powf(1.059463,67+((pitchbend/8192.0)*bendRange))
    Voice2Frequency=8.175*powf(1.059463,72+((pitchbend/8192.0)*bendRange))

    /Johan

  6. #6
    Senior Member
    Join Date
    Jul 2020
    Posts
    985
    Two observations:

    1) precalculate the value, don't have the same expression again and again in the code!
    2) use an array if handling multiple notes?

    By the way which of these do you think is more readable:
    Code:
    powf (1.059463, note) ;
    powf (2.0, semitones/12.0) ;
    I know which conveys the meaning more obviously...

    So how about something more like this
    Code:
    float bend = pitchbend / 8192.0 * bendrange ;
    for (int i = 0 ; i < notes ; i++)
      voice_freq[i] = powf (2.0, (note[i] + bend) / 12.0) ;
    With arrays so the code never needs repetition.

    powf (

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •