2
$\begingroup$

I am writing something in the ChucK programming language, which is designed specifically for audio time functions (in this case, hertz). I'm having a really difficult time with a mathematical calculation; I'm not very good at mathematics, I hope I can get help here.

The operation I'm attempting to perform is as follows, given these variables:

F: Input frequency in hertz
I: Incrementation to next F
R: New frequency in hertz

In pseudo-math, it would read like this:

(output) F
R = F + I
F = R

Where I is 1/12 of the power of F, I think. To summarize, if F is 110 hertz, and the same note on the next octave up is 220 hertz, the range between the two is split up in 12 parts, exponentially, which makes up a full musical scale.

I feel my mathematical formula is incorrect, as I am getting F = F *2 instead of what I am aiming to do. But I don't really understand where the problem is.

In code, it looks like this:

110 => float OscFrequency => fbOsc.freq; // 110Hz, or "A -1" musical note
// stuff to set up look
while ( now < stop ) {
    ( OscFrequency + 1 ) - ( Math.pow ( 1, ( OscFrequency / 12 ) ) ) => float stepExponent;
    // other irrelevant things happen here
    OscFrequency + stepExponent => OscFrequency => fbOsc.freq;
}

Full code.

If this were written in a more common language like Java, it would look something like this:

float OscFrequency = 110.00;
// snip
while (now < stop) {
    float stepExponent = (OscFrequency + 1) - (pow(1, (OscFrequency / 12)));
    // other irrelevant things happen here
    OscFrequency = OscFrequency + stepExponent
}

The output looks like this:

[chuck](VM): sporking incoming shred: 1 (fizzbuzz.ck)...
"-----" : (string)
stepExponent:  110.000000 
Result:  1 
OscFrequency:  110.000000 
"-----" : (string)
stepExponent:  220.000000 
Result:  2 
OscFrequency:  220.000000 
"-----" : (string)
stepExponent:  440.000000 
Result:  3 
OscFrequency:  440.000000 
"-----" : (string)
stepExponent:  880.000000 
Result:  4 
OscFrequency:  880.000000

I've been trying to get this thing to calculate properly for a couple days, without success. Can you offer any advice as to where I might be miscalculating the value of F (a.k.a. OscFrequency)?


I've tried a number of other formulas, neither of which yield correct results:

  • OscFrequency + ( ( Math.pow ( OscFrequency, 12 ) % 12 ) - OscFrequency ) => float stepExponent;
  • ( ( OscFrequency + stepExponent ) ) => OscFrequency => float fbOscFreq;
$\endgroup$
1
$\begingroup$

In Java, you can do this one of two ways. You can calculate the increment and add it to the original frequency:

// This is 2^(1/12) - 1
final float HALF_NOTE_STEP = 0.05946309435929526;
float OscFrequency = 110.00;
// snip
while (now < stop) {
    float stepExponent = OscFrequency * HALF_NOTE_STEP;
    // other irrelevant things happen here
    OscFrequency = OscFrequency + stepExponent
}

Or you can multiply directly without calculating the increment:

// This is 2^(1/12)
final float HALF_NOTE_MULTIPLIER = 1.05946309435929526;
float OscFrequency = 110.00;
// snip
while (now < stop) {
    // other irrelevant things happen here
    OscFrequency *= HALF_NOTE_MULTIPLIER;
}

Or you can do it without a loop:

float OscFrequency = 110.00;
OscFrequency = OscFrequency * Math.pow(2.0, (stop-now)/12.0);
| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ This is perfect, thanks for rescuing my sanity, obviously I was over-complicating things! $\endgroup$ – Phrancis Apr 3 '15 at 18:26
  • $\begingroup$ In hindsight, next time I'll ask on StackOverflow if my question is not "pure math" and actually contains code. $\endgroup$ – Phrancis Apr 3 '15 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.