I think the bad sound is due to the audio format: AudioFormat.ENCODING_PCM_8BIT uses unsigned samples, so the sine between 1 and -1 should be converted to 0-255 bytes, try the following:
for (final double dVal : sample) { final short val = (short) ((dVal + 1) / 2 * 255) ; generatedSnd[idx++] = (byte) val; }
Try also changing the sample rate to 11025, because the 4200 may not be supported on some devices:
private final int sampleRate = 11025;
Alberto alonso ruibal
source share