views:

33

answers:

1

Hi,

My objective is to generate sine, square, triangle and sawtooth signals with Android's AudioTrack Class.

I have made an attempt based on a tutorial. It works relatively well, although I'm not always sure how accurate the frequency generated corresponds with the frequency aimed at.

I would like to make square, triangle etc. functions. The tutorial only implements sine. My question is therefore:

How exactly does the sample work

  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 < samples.length; i++ )
           {
              samples[i] = (float)Math.sin( angle );
              angle += increment;
           }

           device.writeSamples( samples );
        }            
     }
  } ).start();

Can I make the sine into a square like follows (using the signum function)?

samples[i] = (float)Math.signum(Math.sin( angle ));

Basically, I would like to fundamentally understand the samples being written, so I can generate various signals and also eventually superposition them.

Thank you for your time!