how to make a frequency generator?

Discussion in 'Public Game Developers Forum' started by wootbean, Nov 24, 2009.

  1. wootbean

    wootbean Well-Known Member

    Feb 8, 2009
    5,549
    1
    36
    the next whiskey bar
    I found an app called "FreqGen" that's a frequency generator, I'm wondering if anybody here knows if there are any audio APIs or something along those lines that can be used to make one? Basically how do you generate a continuous tone and how can you vary the frequency of the tone?
     
  2. mobile1up

    mobile1up Well-Known Member

    Nov 6, 2008
    754
    0
    16
    Technical Director
    Munich, Germany
    you need to roll your own wave generator. its not that difficult to do - google for "PCM tone generation" - as you can see in the application, there are many ways, square wave, sine wave, saw wave.. you programmatically adjust the PCM output for this. no API that i am aware of exists for this - but it isn't hard to write up.

    Code:
    ... (initialization) ...
    
      g_pcm_sfx -> tonegen.min   = 0;
      g_pcm_sfx -> tonegen.max   = 0;
      g_pcm_sfx -> tonegen.cycle = 1;
      g_pcm_sfx -> tonegen.index = 0;
      g_pcm_sfx -> tonegen.count = 0;
      g_pcm_sfx -> tonegen.state = false;
    
    ... (tone configuration) ...
    
      g_pcm_sfx -> tonegen.index = 0;
      g_pcm_sfx -> tonegen.count = ((cyc_freq * duration) / 1000);
      g_pcm_sfx -> tonegen.cycle = (int16)(min_freq / freq);
    
      val_s = (int16)((32767L*volume*volume)/10000L);
      g_pcm_sfx -> tonegen.max = (int16) val_s;
      g_pcm_sfx -> tonegen.min = (int16)-val_s;
    
    ... (audio call back) ...
    
      // simple square wave, write value
      valu = (g_pcm_sfx -> tonegen.state)
              ? g_pcm_sfx -> tonegen.max : g_pcm_sfx -> tonegen.min;
    
    piece of cake :) - this is c+p from our code base for doing this. we dont use tone generation often tho; because you can get better sampled audio.
     

Share This Page