title: Generating sine, square, triangle, sawtooth audio signals using Android's AudioTrack Class
body: <p>My objective is to generate <strong>sine</strong>, <strong>square</strong>, <strong>triangle</strong> and <strong>sawtooth</strong> signals with Android's <strong>AudioTrack</strong> Class. </p>
  
  <p>I have made an attempt based on a <a href="http://www.badlogicgames.com/wordpress/?p=228" rel="nofollow">tutorial</a>. It works relatively well, although I'm not always sure how accurate the frequency generated corresponds with the frequency aimed at.</p>
  
  <p>I would like to make square, triangle etc. functions. The tutorial only implements sine. My question is therefore:</p>
  
  <p>How exactly does the sample work</p>
  
  <pre><code>  new Thread( new Runnable( ) 
    {
       public void run( )
       {                
          final float frequency = 440;
          float increment = (float)(2*Math.PI) * frequency / 44100; // angular increment for each sample
          float angle = 0;
          AudioDevice device = new AndroidAudioDevice( );
          float samples[] = new float[1024];
  
          while( true )
          {
             for( int i = 0; i &lt; samples.length; i++ )
             {
                samples[i] = (float)Math.sin( angle );
                angle += increment;
             }
  
             device.writeSamples( samples );
          }            
       }
    } ).start();
  </code></pre>
  
  <p>Can I make the sine into a square like follows (using the signum function)?</p>
  
  <pre><code>samples[i] = (float)Math.signum(Math.sin( angle ));
  </code></pre>
  
  <p>Basically, I would like to fundamentally understand the samples being written, so I can generate various signals and also eventually <strong>superposition</strong> them.</p>
  
  <p>Thank you for your time!</p>