Problems with note-frequenzy algorithm

  • Thread starter =?ISO-8859-1?Q?Asbj=F8rn?= Ulsberg
  • Start date
?

=?ISO-8859-1?Q?Asbj=F8rn?= Ulsberg

I'm writing a little C# / DirectSound piano which uses
SecondaryBuffer to play .wav-files. The SecondaryBuffer has a
Frequenzy property which I set to modulate the .wav file into
playing the right note.

The algorithm for a given frequenzy is «x * 2 ^ (y / 12)», where
'x' is the start frequenzy (I use 440 which is A) and 'y' is the
number of half notes from x the given note is. If I am going to
get the frequenzy of a C, the formula would look like this: «440
* 2 ^ (3 / 12)», and the result should be 523,251130601[...]Hz.

But I can't figure out a way to get this algorithm to work. If I
only use integers in the algorithm, it works, but I need a
_much_ higher precision level than int. I would like to use
decimal, but neither that, nor float or double works. I've tried
everything it seems, but nothing works.

How can I apply the ^ operator to other types than integer?
 
B

Bret Mulvey

The "^" operator is the "bitwise exlusive OR" operator and has nothing to do
with exponentiation, which I assume is what you want. x * 2 ^ (y / 12) may
compile, but it won't give you the result you're looking for.

Try x * Math.Pow(2.0, y / 12.0) instead.
 
O

ozbear

I'm writing a little C# / DirectSound piano which uses
SecondaryBuffer to play .wav-files. The SecondaryBuffer has a
Frequenzy property which I set to modulate the .wav file into
playing the right note.

The algorithm for a given frequenzy is «x * 2 ^ (y / 12)», where
'x' is the start frequenzy (I use 440 which is A) and 'y' is the
number of half notes from x the given note is. If I am going to
get the frequenzy of a C, the formula would look like this: «440
* 2 ^ (3 / 12)», and the result should be 523,251130601[...]Hz.

But I can't figure out a way to get this algorithm to work. If I
only use integers in the algorithm, it works, but I need a
_much_ higher precision level than int. I would like to use
decimal, but neither that, nor float or double works. I've tried
everything it seems, but nothing works.

How can I apply the ^ operator to other types than integer?

You are confused about the ^ operator. ^ is an exclusive or (XOR)
operator, not an exponentiation/raise to a power operator.

C# does not have an exponentiation operator, but it does have a
Pow function in the Math class. Your proper expression should be:

440.0 * Math.Pow(2.0,3.0/12.0)

Oz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top