title: Android Audio - Streaming sine-tone generator odd behaviour
body: <p>first time poster here. I usually like to find the answer myself (be it through research or trial-and-error), but I'm stumped here.</p>
  
  <p><strong>What I'm trying to do:</strong>
  I'm building a simple android audio synthesizer. Right now, I'm just playing a sine-tone in real time, with a slider in the UI that changes the tone's frequency as the user adjusts it.</p>
  
  <p><strong>How I've built it:</strong>
  Basically, I have two threads - a worker thread and an output thread. The worker thread simply fills a buffer with the sine wave data every time its tick() method is called. Once the buffer is filled, it alerts the output thread that the data is ready to be written to the audio track. The reason I am using two threads is because audiotrack.write() blocks, and I want the worker thread to be able to begin processing its data as soon as possible (rather than waiting for the audio track to finish writing). The slider on the UI simply changes a variable in the worker thread, so that any changes to the frequency (via the slider) will be read by the worker thread's tick() method.</p>
  
  <p><strong>What works:</strong>
  Almost everything; The threads communicate well, there don't seem to be any gaps or clicks in the playback. Despite the large buffer size (thanks android), the responsiveness is OK. The frequency variable does change, as do the intermediate values used during the buffer calculations in the tick() method (verified by Log.i()).</p>
  
  <p><strong>What doesn't work:</strong>
  For some reason, I can't seem to get a continuous change in audible frequency. When I adjust the slider, the frequency changes in steps, often as wide as fourths or fifths. Theoretically, I should be hearing changes as minute as 1Hz, but I'm not. Oddly enough, it seems as if changes to the slider is causing the sine wave to play through intervals in the harmonic series; However, I can verify that the frequency variable is NOT snapping to integral multiples of the default frequency.</p>
  
  <p>My Audio track is set up as such:</p>
  
  <pre><code>_buffSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
  _audioTrackOut = new AudioTrack(AudioManager.STREAM_MUSIC, _sampleRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, _buffSize, AudioTrack.MODE_STREAM);
  </code></pre>
  
  <p>The worker thread's buffer is being populated (via tick()) as such:</p>
  
  <pre><code>public short[] tick()
  {
      short[] outBuff = new short[_outBuffSize/2]; // (buffer size in Bytes) / 2
      for (int i = 0; i &lt; _outBuffSize/2; i++) 
      {
          outBuff[i] = (short) (Short.MAX_VALUE * ((float) Math.sin(_currentAngle)));
  
          //Update angleIncrement, as the frequency may have changed by now
          _angleIncrement = (float) (2.0f * Math.PI) * _freq / _sampleRate;
          _currentAngle = _currentAngle + _angleIncrement;    
      }
      return outBuff;     
  }
  </code></pre>
  
  <p>The audio data is being written like this:</p>
  
  <pre><code>_audioTrackOut.write(fromWorker, 0, fromWorker.length);
  </code></pre>
  
  <p>Any help would be greatly appreciated. How can I get more gradual changes in frequency? I'm pretty confident that my logic in tick() is sound, as Log.i() verifies that the variables angleIncrement and currentAngle are being updated properly.</p>
  
  <p>Thank you!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I found a similar problem here: <a href="https://stackoverflow.com/questions/3305598/android-audiotrack-buffering-problems">Android AudioTrack buffering problems</a>
  The solution proposed that one must be able to produce samples fast enough for the audioTrack, which makes sense. I lowered my sample rate to 22050Hz, and ran some empirical tests - I can fill my buffer (via tick()) in approximately 6ms in the worst case. This is more than adequate. At 22050Hz, the audioTrack gives me a buffer size of 2048 samples (or 4096 Bytes). So, each filled buffer lasts for ~0.0928 seconds of audio, which is much longer than it takes to create the data (1~6 ms). SO, I know that I don't have any problems producing samples fast enough.</p>
  
  <p>I should also note that for about the first 3 seconds of the applications lifecycle, it works fine - a smooth sweep of the slider produces a smooth sweep in the audio output. After this, it starts to get really choppy (sound only changes about every 100Mhz), and after that, it stops responding to slider input at all.</p>
  
  <p>I also fixed one bug, but I don't think it has an effect. AudioTrack.getMinBufferSize() returns the smallest allowable buffer size in BYTES, and I was using this number as the length of the buffer in tick() - I now use half this number (2 Bytes per sample).</p>