★ TouchArcade needs your help. Click here to support us on Patreon.

how to make a frequency generator?

11-24-2009, 01:01 PM
#1
Joined: Feb 2009
Location: the next whiskey bar
Posts: 5,549
how to make a frequency generator?

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?
11-26-2009, 01:27 AM
#2
Joined: Nov 2008
Location: Munich, Germany
Posts: 754
Send a message via Skype™ to mobile1up
Quote:
Originally Posted by wootbean View Post
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?
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.

// Aaron Ardiri
Mobile 1UP is a proud indie developer - support us!
developer of Caveman / Caveman HD and GW Series