예제 #1
0
 @Test
 public void listen() {
   SineString ss440 = new SineString(440.0);
   ss440.pluck();
   for (int i = 0; i < 40000; i++) {
     StdAudio.play(ss440.sample());
     ss440.tic();
   }
 }
  public static void main(String[] args) {

    while (!StdIn.isEmpty()) {
      int pitch = StdIn.readInt();
      double duration = StdIn.readDouble();
      double[] a = note(pitch, duration);
      StdAudio.play(a);
    }

    System.exit(0);
  }
  public static void main(String[] args) {
    StdAudio.play("resources/nbody/2001.mid");
    // setting the input to the file's contents
    String fileName = "3body";
    try {
      System.setIn(new FileInputStream("resources/nbody/" + fileName + ".txt"));
    } catch (Exception e) {
      System.err.printf("Exception caught: %s", e.toString());
      System.exit(0);
    }

    // grab the number of bodies, and the size of the universe as the fist two inputs
    int numOfBodies = StdIn.readInt();
    double sizeOfUniverse = StdIn.readDouble();

    // create a nested array, with the first array holding an array of info for each of the planets
    String[][] nBodyStringArray = new String[numOfBodies][6];
    for (int i = 0; i < numOfBodies; i++) {
      for (int j = 0; j < 6; j++) {
        nBodyStringArray[i][j] = StdIn.readString();
      }
    }

    // set the universe's scale
    StdDraw.setXscale(-sizeOfUniverse, sizeOfUniverse);
    StdDraw.setYscale(-sizeOfUniverse, sizeOfUniverse);

    // initialize delta t
    int timeStep = 25000;

    // create an array from the previous nBodyStringArray, to hold doubles instead of strings, and
    // to hold the acceleration
    double[][] nBodyArray = new double[numOfBodies][7];
    for (int i = 0; i < numOfBodies; i++) {

      double px = Double.parseDouble(nBodyStringArray[i][0]);
      double py = Double.parseDouble(nBodyStringArray[i][1]);
      double vx = Double.parseDouble(nBodyStringArray[i][2]);
      double vy = Double.parseDouble(nBodyStringArray[i][3]);
      double mass = Double.parseDouble(nBodyStringArray[i][4]);
      nBodyArray[i][0] = px;
      nBodyArray[i][1] = py;
      nBodyArray[i][2] = vx;
      nBodyArray[i][3] = vy;
      nBodyArray[i][4] = mass;
      nBodyArray[i][5] = 0.0;
      nBodyArray[i][6] = 0.0;
    }

    // create arrays to hold the force for the planets
    double fx[] = new double[numOfBodies];
    double fy[] = new double[numOfBodies];

    // start the infinite drawing loop
    while (true) {
      // draw the background
      StdDraw.picture(0, 0, "resources/nbody/starfield.jpg");

      // calculate the x-force and y-force for the planets and store them in their respective
      // array's
      for (int i = 0; i < numOfBodies; i++) {
        double px = nBodyArray[i][0];
        double py = nBodyArray[i][1];
        double mass = nBodyArray[i][4];
        fx[i] = 0;
        fy[i] = 0;

        for (int j = 0; j < numOfBodies; j++) {
          if (j != i) {
            double nPx = nBodyArray[j][0];
            double nPy = nBodyArray[j][1];
            double nMass = nBodyArray[j][4];
            double g = 6.67 * Math.pow(10, -11);
            double deltaX = (nPx - px);
            double deltaY = nPy - py;
            double r = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
            double f = (g * mass * nMass) / (Math.pow(r, 2));
            fx[i] += f * (deltaX / r);
            fy[i] += f * deltaY / r;
          }
        }
      }

      // updating the position of the planets
      for (int i = 0; i < numOfBodies; i++) {

        // planet info

        double px = nBodyArray[i][0];
        double py = nBodyArray[i][1];
        double vx = nBodyArray[i][2];
        double vy = nBodyArray[i][3];
        double mass = nBodyArray[i][4];
        double ax = nBodyArray[i][5];
        double ay = nBodyArray[i][6];
        String pic = "resources/nbody/" + nBodyStringArray[i][5];

        // update acceleration
        ax = fx[i] / mass;
        ay = fy[i] / mass;

        // update velocity
        vx = vx + timeStep * ax;
        vy = vy + timeStep * ay;

        // update position
        px = px + timeStep * vx;
        py = py + timeStep * vy;

        // update data array
        nBodyArray[i][0] = px;
        nBodyArray[i][1] = py;
        nBodyArray[i][2] = vx;
        nBodyArray[i][3] = vy;
        nBodyArray[i][5] = ax;
        nBodyArray[i][6] = ay;
        // draw each planet to the screen
        StdDraw.picture(px, py, pic);
      }
      // show that drawing for 30 ms, before restarting the loop
      StdDraw.show(30);
      // t += 25000;
    }
  }
예제 #4
0
 public static void main(String[] args) {
   double[] soundData = StdAudio.read("ee20.wav");
   StdAudio.play(soundData);
 }
예제 #5
0
 public static void main(String[] args) {
   synthesizer.GuitarString[] melody = new synthesizer.GuitarString[37];
   synthesizer.GuitarString[] cmaj = new synthesizer.GuitarString[8];
   synthesizer.GuitarString[] gmaj = new synthesizer.GuitarString[8];
   synthesizer.GuitarString[] amin = new synthesizer.GuitarString[8];
   synthesizer.GuitarString[] fmaj = new synthesizer.GuitarString[8];
   for (int i = 0; i < 37; i += 1) {
     melody[i] = new synthesizer.GuitarString(440.0 * Math.pow(2, (i - 24.0) / 12.0));
   }
   int cmaj_index = 15;
   int gmaj_index = 10;
   int amin_index = 12;
   int fmaj_index = 8;
   int[] majorscale = {2, 2, 1, 2, 2, 2, 1, 2};
   int[] minorscale = {2, 1, 2, 2, 1, 2, 2, 2};
   int m = 0;
   while (m < 8) {
     cmaj[m] = new synthesizer.GuitarString(440.0 * Math.pow(2, (cmaj_index - 24.0) / 12.0));
     cmaj_index += majorscale[m];
     gmaj[m] = new synthesizer.GuitarString(440.0 * Math.pow(2, (gmaj_index - 24.0) / 12.0));
     gmaj_index += majorscale[m];
     amin[m] = new synthesizer.GuitarString(440.0 * Math.pow(2, (amin_index - 24.0) / 12.0));
     amin_index += minorscale[m];
     fmaj[m] = new synthesizer.GuitarString(440.0 * Math.pow(2, (fmaj_index - 24.0) / 12.0));
     fmaj_index += majorscale[m];
     m += 1;
   }
   synthesizer.GuitarString[] chords = new synthesizer.GuitarString[12];
   chords[0] = new synthesizer.GuitarString(440.0 * Math.pow(2, (3 - 24.0) / 12.0));
   chords[1] = new synthesizer.GuitarString(440.0 * Math.pow(2, (7 - 24.0) / 12.0));
   chords[2] = new synthesizer.GuitarString(440.0 * Math.pow(2, (10 - 24.0) / 12.0));
   // gmaj
   chords[3] = new synthesizer.GuitarString(440.0 * Math.pow(2, (2 - 24.0) / 12.0));
   chords[4] = new synthesizer.GuitarString(440.0 * Math.pow(2, (5 - 24.0) / 12.0));
   chords[5] = new synthesizer.GuitarString(440.0 * Math.pow(2, (10 - 24.0) / 12.0));
   // aminor
   chords[6] = new synthesizer.GuitarString(440.0 * Math.pow(2, (3 - 24.0) / 12.0));
   chords[7] = new synthesizer.GuitarString(440.0 * Math.pow(2, (7 - 24.0) / 12.0));
   chords[8] = new synthesizer.GuitarString(440.0 * Math.pow(2, (12 - 24.0) / 12.0));
   // fmaj
   chords[9] = new synthesizer.GuitarString(440.0 * Math.pow(2, (3 - 24.0) / 12.0));
   chords[10] = new synthesizer.GuitarString(440.0 * Math.pow(2, (8 - 24.0) / 12.0));
   chords[11] = new synthesizer.GuitarString(440.0 * Math.pow(2, (12 - 24.0) / 12.0));
   int z = 10000;
   int c_index = 0;
   int before = 0;
   int total_time = 0;
   while (total_time < 8) {
     if (c_index == 12) {
       c_index = 0;
     }
     chords[c_index].pluck();
     c_index += 1;
     chords[c_index].pluck();
     c_index += 1;
     chords[c_index].pluck();
     c_index += 1;
     int time = 0;
     while (time < 8) {
       int[] x = {0, 1, 2, 4, 5};
       int c = x[((int) (Math.random() * 5))];
       while (before == c) {
         c = x[((int) (Math.random() * 5))];
       }
       before = c;
       if (c_index == 3) {
         cmaj[c].pluck();
       } else if (c_index == 6) {
         gmaj[c].pluck();
       } else if (c_index == 9) {
         amin[c].pluck();
       } else {
         fmaj[c].pluck();
       }
       while (z > 0) {
         double sample = 0;
         if (c_index == 3) {
           sample = cmaj[c].sample();
         } else if (c_index == 6) {
           sample = gmaj[c].sample();
         } else if (c_index == 9) {
           sample = amin[c].sample();
         } else {
           sample = fmaj[c].sample();
         }
         for (int i = 0; i < 12; i += 1) {
           sample += chords[i].sample();
         }
         StdAudio.play(sample);
         for (int i = 0; i < 8; i += 1) {
           if (i % 2 == 0) {
             cmaj[i].tic();
             gmaj[i].tic();
             fmaj[i].tic();
             amin[i].tic();
           }
         }
         for (int i = 0; i < 12; i += 1) {
           chords[i].tic();
         }
         z -= 1;
       }
       z = 10000;
       time += 1;
     }
     total_time += 1;
   }
 }
  public void StartSession() {

    try {
      seqformat entry;

      LEDFlasher ledthread = null;
      ScreenGraphicsThread sgthread = null;

      // init threads
      if (Main.useusb) {
        LEDCtrl.init();
        ledthread = new LEDFlasher("LEDFlasher1");
        // ledthread.setPriority(1);
      }
      if (Main.screengraphics) {
        sgthread = new ScreenGraphicsThread("ScreenGraphicsThread1");
      }

      // --loop entries
      for (int j = 0; j < sequence.entrynum; j++) {
        entry = sequence.entry[j];

        System.out.println("entry:" + entry.type + entry.duration + entry.binfreq);
        // --special handling for entries
        if (entry.type.compareToIgnoreCase("sleep") == 0) {
          if (Main.screengraphics) sgthread.status = 1;
          if (Main.useusb) ledthread.status = 1;
          RainLib.Sleep(1000 * 60 * entry.duration);
          continue;
        }
        if (entry.type.contains(".mp3")) {
          if (Main.screengraphics) sgthread.status = 1;
          if (Main.useusb) ledthread.status = 1;
          mp3playThread mp3thread = new mp3playThread(entry.type, entry.volume);
          mp3thread.start();
          continue;
        }
        if (entry.duration == 0) {
          System.out.println("end of sequence reached");
          break;
        }

        // --give entry parameters to flasher threads
        if (Main.useusb) {
          if (j == 0) ledthread.binfreq = entry.binfreq;
          ledthread.setActive(entry.binfreq, entry.duration);
        }
        if (Main.screengraphics) {
          if (j == 0) sgthread.binfreq = entry.binfreq;
          sgthread.setActive(entry.binfreq, entry.duration);
        }

        // --play sound sample
        for (int sample = 0; sample < entry.duration; sample++) {
          // fill array with 1 sec of stereo 16-bit tonedata
          for (int i = 0; i < SAMPLE_RATE * 2; i++) {
            a[i] = Math.sin(2 * Math.PI * i / SAMPLE_RATE * entry.basefreq / 2);
            a[i + 1] =
                Math.sin(2 * Math.PI * i / SAMPLE_RATE * (entry.basefreq - entry.binfreq) / 2);
            i++;
          }

          // System.out.println("playing sample: "+sample);
          // StdAudio.toneplayled(0,a,SAMPLE_RATE*2,entry[j].binfreq);
          // StdAudio.play(a);

          StdAudio.toneplay(0, entry.volume, a, SAMPLE_RATE * 2);
          if (Main.status == 0) break;
        }
        if (Main.status == 0) break;
      }

      if (Main.useusb) {
        ledthread.status = 0;
        LEDCtrl.LEDoff();
      }
      if (Main.screengraphics) {
        sgthread.status = 0;
      }
      Main.status = 0;

    } catch (Throwable e) {
      System.out.println("Exception: " + e.toString());
    }
  }