i am creating a small hearing test application for a university project. I have been using some of the code found in: Playing an arbitrary tone with Android
So far i have a button set with a switch statement, so each time it is clicked, it will play tones of increasing frequency. however, when it reaches 4000Hz the sound will no longer play, does anyone have any ideas? Thanks!
public class AutoTest extends MainActivity {
private final int duration = 5; // seconds
private final int sampleRate = 8000;
private final int numSamples = duration * sampleRate;
private final double sample[] = new double[numSamples];
private double freqOfTone = 250; // hz
private int inc=0;
int count = 0;
private final byte generatedSnd[] = new byte[2 * numSamples];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_auto_test);
}
void genTone(){
// fill out the array
for (int i = 0; i < numSamples; ++i) {
// sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/(freqOfTone+inc)));
sample[i] = Math.sin((freqOfTone+inc) * 2 * Math.PI * i / (sampleRate));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
int ramp = numSamples / 20;
for (int i = 0; i < ramp; i++) {
// scale to maximum amplitude
final short val = (short) ((sample[i] * 32767) * i / ramp);
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
for (int i = ramp; i < numSamples - ramp; i++) {
// scale to maximum amplitude
final short val = (short) ((sample[i] * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
for (int i = numSamples - ramp; i < numSamples; i++) {
// scale to maximum amplitude
final short val = (short) ((sample[i] * 32767) * (numSamples - i) / ramp);
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
void playSound(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STREAM);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
public void playTone(View view)
{
//switch statement - auto - increment
switch(count) {
case 0:
break;
case 1:
inc+=250;
break;
case 2:
inc+=500;
break;
case 3:
inc+=1000;
break;
case 4:
inc+=2000;
break;
case 5:
inc+=2000;
break;
case 6:
inc+=2000;
break;
default:
Log.d("Values","error message");
break;
}
genTone();
playSound();
Log.d("Values","This is the value of count"+ count);
count++;
here is my code, i would greatly appreciate any help!