Improve performance of musical note synthesis using AudioTrack
I am trying to build an android app which synthesizes and plays musical notes. I am using AudioTrack. It seems that my program is unable to fill the track buffer fast enough due to the latency in generating the audio information. I am generating the audio info from the fourier coefficients of the note. Hence before each write, the program has to create and sample 2048 sine waves each with a sample size of 2400 and sample rate of 44100!
So instead of a continuous sound I hear intermittent beeps. The Logcat gives the following warning in between beeps:
W/AudioTrack﹕ obtainBuffer() track 0x177ff80 disabled, restarting
My code is given below. Can anyone identify ways in which I could optimize the code?
package com.alantaar.alantaar;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import java.util.Arrays;
public class AudioGenerator {
private AudioTrack audio;
private double[] coefficients;
private int bufferSize;
private int sampleRate;
private double[] phases;
private double frequency;
public AudioGenerator(double[] coefficients, double frequency, int sampleRate, int bufferSize){
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
this.bufferSize = bufferSize > minBufferSize ? bufferSize : minBufferSize;
audio = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, this.bufferSize, AudioTrack.MODE_STREAM);
this.coefficients = coefficients;
this.sampleRate = sampleRate;
this.frequency = frequency;
this.phases = new double[coefficients.length];
Arrays.fill(this.phases, 0.0);
Log.i("length", Integer.toString(coefficients.length));
audio.play();
}
public void playNote(){
while(true){
short[] waveSample = sampleWave();
audio.write(waveSample, 0, waveSample.length);
}
}
private short[] sampleWave(){
short [] waveSample = new short[bufferSize];
Arrays.fill(waveSample, (short)0);
for(int i = 0; i < coefficients.length; i++){
double coefficient = coefficients[i];
short[] sineSamples = sampleSineWave(coefficient, i);
for(int j = 0; j < waveSample.length; j++){
waveSample[j] += sineSamples[j];
}
}
return waveSample;
}
private short [] sampleSineWave(double coefficient, int index){
double freq = frequency * index;
short [] samples = new short[bufferSize];
for(int i = 0; i < bufferSize; i++){
samples[i] = (short) (coefficient * Short.MAX_VALUE * Math.cos(phases[index]));
phases[index] += 2 * Math.PI * freq/sampleRate;
}
return samples;
}
public void pauseNote(){
audio.stop();
}
public void stopNote(){
audio.release();
}
}
Here are a few things that pop out at me:
2*pi*freq/sampleRate
could be done right before the for
. Same with coefficient * Short.MAX_VALUE
. audio.write
is blocking to generate more samples. I don't know the behavior of AudioTrack
's streaming mode but there is a good chance that your program is spending most of its time in a blocking call.