Tone.java


Below is the syntax highlighted version of Tone.java from §2.1 Static Methods.


/******************************************************************************
 *  Compilation:  javac Tone.java
 *  Execution:    java Tone hz duration
 *
 *  Play a note of the given freqency for the given duration.
 *
 *  % java Tone 440 1.5
 *
 ******************************************************************************/

public class Tone {

  // create a pure tone of the given frequency for the given duration
    public static double[] tone(double hz, double duration) {
        int n = (int) (StdAudio.SAMPLE_RATE * duration);
        double[] a = new double[n+1];
        for (int i = 0; i <= n; i++) {
            a[i] = Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
        }
        return a;
    }


    public static void main(String[] args) {

        // frequency
        double hz = Double.parseDouble(args[0]);

        // number of seconds to play the note
        double duration = Double.parseDouble(args[1]);

        // create the array
        double[] a = tone(hz, duration);

        // play it using standard audio
        StdAudio.play(a);
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:15:13 EDT 2022.