isefire

PlayThatTuneExtended

Jul 14th, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac PlayThatTune.java
  3.  *  Execution:    java PlayThatTune < data.txt
  4.  *  Dependencies: StandardLib, found at http://introcs.cs.princeton.edu/java/stdlib/stdlib.jar
  5.  *
  6.  *  % java PlayThatTune < testaudio.txt
  7.  *
  8.  *
  9.  *  Make sure amplitudes for each line add up to exactly 1
  10.  *  Make sure lengths for each note on the same line are the same #
  11.  *  Data files format:
  12.  *
  13.  *  note length amplitude note length amplitude...
  14.  *  1    .5      .1         5    .5     .9
  15.  *
  16.  *  
  17.  *
  18.  *************************************************************************/
  19.  
  20. public class PlayThatTune
  21. {
  22.     public static double[] sum(double[] a, double[] b,
  23.                                     double awt, double bwt)
  24.     { // superpose a and b, weighted.
  25.         double[] c = new double[a.length];
  26.         assert (a.length == b.length);
  27.         for (int i = 0; i < a.length; i++)
  28.             c[i] = a[i]*awt + b[i]*bwt;
  29.         return c;
  30.     }
  31.    
  32.     public static double[] tone(double hz, double duration, double amplitude)
  33.     {
  34.         int N = (int) (StdAudio.SAMPLE_RATE * duration);
  35.         double[] a = new double[N+1];
  36.             for (int i = 0; i <= N; i++) {
  37.             a[i] = amplitude * Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
  38.         }
  39.         return a;
  40.     }
  41.  
  42.     public static double[] note(int p, double t, double amplitude)
  43.     { //Play note p, with harmonics.
  44.         double hz = 440.0 * Math.pow(2, p/12.0);
  45.         double[] a = tone(hz, t, amplitude);
  46.         double[] hi = tone(2*hz, t, amplitude);
  47.         double[] lo = tone(hz/2, t, amplitude);
  48.         double[] h = sum(hi, lo, .5, .5);
  49.         return sum(a, h , .5, .5);
  50.     }
  51.  
  52.     public static void playmore(int iter, double[] bj, String[] z)
  53.     {
  54.         //iterate with int iter backwards through z
  55.         int pitch = Integer.parseInt(z[(iter*3)-3]);
  56.         double duration = Double.parseDouble(z[(iter*3)-2]);
  57.         double amplitude = Double.parseDouble(z[(iter*3)-1]);
  58.         //Check amplitude value
  59.         assert (amplitude < 1 && amplitude > 0);
  60.         double[] a = note(pitch, duration, amplitude);
  61.         System.out.println(a.length + " is a[]");
  62.         System.out.println(bj.length + " is bj[]");
  63.         iter -= 1;
  64.         for (int i = a.length-1; i >= 0; i--)
  65.         {
  66.             //iterate over a[] backwards, adding a[i] to bj[i]
  67.             //System.out.println("yes"+i);
  68.             bj[i] = (bj[i] + a[i]);
  69.         }
  70.         if (iter == 0)
  71.         {
  72.             StdAudio.play(bj);
  73.         }
  74.         else
  75.         {
  76.             playmore(iter, bj, z);
  77.         }
  78.     }
  79.  
  80.     public static void play()
  81.     {
  82.         String[] z = StdIn.readLine().split(" ");
  83.         int zphi = z.length;
  84.         if (zphi == 1) return;
  85.         if (zphi > 3)
  86.         {
  87.             System.out.println(zphi);
  88.             double duration = Double.parseDouble(z[1]);
  89.             int N = (int) (StdAudio.SAMPLE_RATE * duration);
  90.             double[] bj = new double[N+1];
  91.             playmore((zphi)/3, bj, z);
  92.             return;
  93.         }
  94.         for (int i = 0; i <= z.length-1; i++)
  95.         {
  96.             System.out.println(z[i]);
  97.         }
  98.         int pitch = Integer.parseInt(z[0]);
  99.         double duration = Double.parseDouble(z[1]);
  100.         double[] a = note(pitch, duration, 1);
  101.         //System.out.println(a.length-1);
  102.         StdAudio.play(a);
  103.     }
  104.  
  105.     public static void main(String[] args)
  106.     { //Read and play a tune with harmonics
  107.         while (!StdIn.isEmpty())
  108.         { // Read and play a note with harmonics.
  109.             play();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment