Problems with note-frequenzy algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Asbjørn Ulsberg

    #1

    Problems with note-frequenzy algorithm

    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?

    --
    Asbjørn Ulsberg -=|=- X-No-Archive: No
    "He's a loathsome offensive brute, yet I can't look away"
  • Bret Mulvey

    #2
    Re: Problems with note-frequenzy algorithm

    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.

    "Asbjørn Ulsberg" <asbjorn@tigers taden.no> wrote in message
    news:Xns9400DA3 AC1953asbjornti gerstadenno@new s.online.no...[color=blue]
    > 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?
    >
    > --
    > Asbjørn Ulsberg -=|=- X-No-Archive: No
    > "He's a loathsome offensive brute, yet I can't look away"[/color]


    Comment

    • ozbear

      #3
      Re: Problems with note-frequenzy algorithm

      On 24 Sep 2003 21:40:38 GMT, "=?ISO-8859-1?Q?Asbj=F8rn?= Ulsberg"
      <asbjorn@tigers taden.no> wrote:
      [color=blue]
      >I'm writing a little C# / DirectSound piano which uses
      >SecondaryBuffe r 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?[/color]

      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

      Comment

      • ozbear

        #4
        Re: Problems with note-frequenzy algorithm

        On 24 Sep 2003 21:40:38 GMT, "=?ISO-8859-1?Q?Asbj=F8rn?= Ulsberg"
        <asbjorn@tigers taden.no> wrote:
        [color=blue]
        >I'm writing a little C# / DirectSound piano which uses
        >SecondaryBuffe r 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?[/color]

        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

        Comment

        Working...