Create Sound wave form amplitude values calculated for the frequency, sample rate and duration. - Android Media

Android examples for Media:Sound

Description

Create Sound wave form amplitude values calculated for the frequency, sample rate and duration.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w  ww .jav  a 2 s.c o m*/
     * Create waveform amplitude values calculated for the frequency, sample rate and duration.
     *
     * @param frequency The frequency in hertz of the tone to be generated.
     * @param duration The duration of the tone.
     * @param sampleRate The number of samples per second.
     * @return An array of amplitude values.
     */
    public static final double[] toAmplitudeValues(final double frequency,
            final int duration, final int sampleRate) {
        final double[] amplitudes = new double[sampleRate * duration];
        final double wavePerSample = sampleRate / frequency;
        final double twoPi = Math.PI * 2;
        for (int index = 0; index < amplitudes.length; ++index) {
            amplitudes[index] = Math.sin((twoPi * index) / wavePerSample);
        }
        return amplitudes;
    }
}

Related Tutorials