generate Tone - Android Media

Android examples for Media:Audio

Description

generate Tone

Demo Code


//package com.java2s;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

public class Main {
    public static AudioTrack generateTone(double freqHz, int durationMs) {
        int count = (int) (44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
        short[] samples = new short[count];
        for (int i = 0; i < count; i += 2) {
            short sample = (short) (Math.sin(2 * Math.PI * i
                    / (44100.0 / freqHz)) * 0x7FFF);
            samples[i + 0] = sample;//w  ww. j  a  v  a 2  s . co  m
            samples[i + 1] = sample;
        }
        AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT, count * (Short.SIZE / 8),
                AudioTrack.MODE_STATIC);
        track.write(samples, 0, count);
        return track;
    }
}

Related Tutorials