Play That Tune

2
9/18/2015 Pl ayThatTune.j ava http://introcs.cs.princeton.edu/j ava/15inout/PlayThatTune.j ava.html 1/2 PlayThatTune.java Below is the syntax highlighted version of PlayThatTune.java  from §1.5 Input and Output.  /******** ********* ********* ********* ******** ********* ********* ********* ********  * Compilation: javac PlayThatTune.java  * Execution: java PlayThatTune < input.txt  * Dependencies: StdAudio.java StdAudio.java  *  * This is a datadriven program that plays pure tones from  * the notes on the chromatic scale, specified on standard input  * by their distance from concert A.  *  * % java PlayThatTune < elise.txt  *  *  * Data files  * ‐‐‐‐‐‐‐‐‐‐  * http://www.cs.princeton.edu/introcs/21function/elise.txt  * http://www.cs.princeton.edu/introcs/21function/freebird.txt  * http://www.cs.princeton.edu/introcs/21function/Ascale.txt  * http://www.cs.princeton.edu/introcs/21function/National_Anthem.txt  * http://www.cs.princeton.edu/introcs/21function/looney.txt  * http://www.cs.princeton.edu/introcs/21function/StairwayToHeaven.txt  * http://www.cs.princeton.edu/introcs/21function/entertainer.txt  * http://www.cs.princeton.edu/introcs/21function/oldnassau.txt  * http://www.cs.princeton.edu/introcs/21function/arabesque.txt  * http://www.cs.princeton.edu/introcs/21function/firstcut.txt  * http://www.cs.princeton.edu/introcs/21function/tomsdiner.txt  *  ******************************************************************************/ public class PlayThatTune {  public static void main(String[] args ) {   // repeat as long as there are more integers to read in  while (!StdIn.isEmpty()) {   // read in the pitch, where 0 = Concert A (A4)  int pitch = StdIn .readInt();   // read in duration in seconds  double duration = StdIn .readDouble();   // build sine wave with desired frequency  double hz = 440 * Math .pow(2, pitch / 12.0); 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 );  }   // play it using standard audio  StdAudio .play(a);  }  } }

description

java code

Transcript of Play That Tune

7/17/2019 Play That Tune

http://slidepdf.com/reader/full/play-that-tune 1/2

9/18/2015 PlayThatTune.java

http://introcs.cs.princeton.edu/java/15inout/PlayThatTune.java.html 1/2

PlayThatTune.java

Below is the syntax highlighted version of PlayThatTune.java from §1.5 Input and Output.

 /******************************************************************************  * Compilation: javac PlayThatTune.java  * Execution: java PlayThatTune < input.txt  * Dependencies: StdAudio.java StdAudio.java  *  * This is a data‐driven program that plays pure tones from  * the notes on the chromatic scale, specified on standard input  * by their distance from concert A.  *  * % java PlayThatTune < elise.txt  *  *

  * Data files  * ‐‐‐‐‐‐‐‐‐‐  * http://www.cs.princeton.edu/introcs/21function/elise.txt  * http://www.cs.princeton.edu/introcs/21function/freebird.txt  * http://www.cs.princeton.edu/introcs/21function/Ascale.txt  * http://www.cs.princeton.edu/introcs/21function/National_Anthem.txt  * http://www.cs.princeton.edu/introcs/21function/looney.txt  * http://www.cs.princeton.edu/introcs/21function/StairwayToHeaven.txt  * http://www.cs.princeton.edu/introcs/21function/entertainer.txt  * http://www.cs.princeton.edu/introcs/21function/old‐nassau.txt  * http://www.cs.princeton.edu/introcs/21function/arabesque.txt  * http://www.cs.princeton.edu/introcs/21function/firstcut.txt  

* http://www.cs.princeton.edu/introcs/21function/tomsdiner.txt  *  ******************************************************************************/ 

public class  PlayThatTune {

  public static void main(String[]  args)  {

   // repeat as long as there are more integers to read in  while (!StdIn.isEmpty())  {

   // read in the pitch, where 0 = Concert A (A4)  int  pitch =  StdIn.readInt();

   // read in duration in seconds  double  duration =  StdIn.readDouble();

   // build sine wave with desired frequency  double  hz = 440 *  Math.pow(2,  pitch / 12.0);  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);  }

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

7/17/2019 Play That Tune

http://slidepdf.com/reader/full/play-that-tune 2/2

9/18/2015 PlayThatTune.java

http://introcs.cs.princeton.edu/java/15inout/PlayThatTune.java.html 2/2

Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.

 Last updated: S un Aug 2 18:43:37 EDT 2015.