0

I wish to improve quality of beeps, which generated by class AudioTrack. I tried to use stereo format, but it don't help. How can I improve quality of sounds, which generated by AudioTrack?

Below you can see code, which shows, how I generated sound in high quality, but beep unfortunately plays in in a very bad quality.

class beeper {

    private static byte generatedSnd[];
    public static AudioTrack audioTrack;

    public static void createAudioTrack() {
        if (audioTrack == null) {
            audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                    48000,
                    AudioFormat.CHANNEL_OUT_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    generatedSnd.length,
                    AudioTrack.MODE_STREAM);

            if (Build.VERSION.SDK_INT < 21)
                audioTrack.setStereoVolume(AudioTrack.getMaxVolume(),
                        AudioTrack.getMaxVolume());
            else
                audioTrack.setVolume(AudioTrack.getMaxVolume());

            audioTrack.setPlaybackPositionUpdateListener(
                    new OnPlaybackPositionUpdateListener() {
                @Override
                public void onMarkerReached(AudioTrack track) {
                }
                @Override
                public void onPeriodicNotification(AudioTrack track) {
                }
            });
        }
    }

    static void sound(double frequency, double durationMs) {
        generatedSnd = new byte[(int) (2 * 48000 * durationMs / 1000)];
        double sample[] = new double[(int) (48000 * durationMs / 1000)];
        for (int i = 0; i < sample.length; ++i) {
            sample[i] = Math.sin(2 * Math.PI * i / (48000 / frequency));
        }
        int idx = 0;
        for (final double dVal : sample) {
            final short val = (short) ((dVal * 32767));
            generatedSnd[idx++] = (byte) (val & 0x00ff);
            generatedSnd[idx++] = (byte) (val & 0xff00 >>> 8);
        }
        createAudioTrack();
        audioTrack.play();
        audioTrack.write(generatedSnd, 0, generatedSnd.length);
    }

    public static void audioTrackdestroy() {
        if (audioTrack != null) {
            audioTrack.stop();
            audioTrack.flush();
            audioTrack.release();
            audioTrack = null;
        }
    }
}
Harprit Kaur
  • 21
  • 1
  • 6
  • It looks like you're having some problems with your code indentation - would you fix that first please, and repair it in your question too? Thanks. – halfer Nov 20 '16 at 23:50

1 Answers1

0

The shift operator has a higher precedence than the bitwise AND (reference) so you probably mean:

generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8)

UPDATE

Your audio track should be MONO instead of STEREO to match your wave generator function, or else the frequency will be doubled.

Try calling audioTrack.stop() right after audioTrack.write().

nandsito
  • 3,782
  • 2
  • 19
  • 26
  • yes,you right about shift operator,but i can't understand how improve quality of sound usin audiotrack – alexandr kozlovskiy Nov 21 '16 at 11:00
  • @alexandrkozlovskiy does the sound remains bad after the shift operator fix? – nandsito Nov 21 '16 at 11:30
  • The quality of sound is not change,because it was my error,when i copy code in this topic. Also,when i generate a lotof sounds,in differents frequncys,the sound change frequency slow,as i wish,but it choppy. Help me please solve problems with quality of sound,which i generate using audiotrack – alexandr kozlovskiy Nov 22 '16 at 15:29