Esempio n. 1
0
 /*
  * Create a new empty sound
  */
 public ArrayList<Integer> newSound(int soundLength) {
   myHelper = new Helper(soundLength);
   myHelper.setViewer(this);
   data = myHelper.getData();
   createMainFrame();
   return data;
 }
Esempio n. 2
0
  /*
   * Load a sound given a specific file name
   */
  public ArrayList<Integer> load(String filename) {
    if (filename == null) {
      System.err.println("Please specify a valid file name");
      return null;
    }

    File file = new File(filename);
    if (!file.isFile()) {
      System.err.println("File not found");
      return null;
    }

    String name = file.getName();
    if (!(name.endsWith(".au")
        || name.endsWith(".wav")
        || name.endsWith(".aiff")
        || name.endsWith(".aif"))) {
      System.err.println(
          "Audio format not supported. Please use either files ending with '.au', '.wav', '.aiff', '.aif'");
      return null;
    }

    myHelper = new Helper(file);
    myHelper.setViewer(this);
    createMainFrame();
    data = myHelper.getData();
    return data;
  }
Esempio n. 3
0
  /*
   * Load some sound data from through a file chooser
   * We restrict the allowed file formats to .wav, .au .aif .aiff.
   * We only check agains the file name. The file may actually be encoded
   * differently.
   */
  public ArrayList<Integer> load() {
    JFileChooser fileChooser = new JFileChooser();
    File thisFile = new File(".");
    try {
      fileChooser = new JFileChooser(thisFile.getCanonicalPath());
    } catch (Exception e) {
      System.out.println("Cannot find current directory.");
      // Will simply load from default path instead.
    }
    fileChooser.setFileFilter(
        new javax.swing.filechooser.FileFilter() {
          public boolean accept(File f) {
            if (f.isDirectory()) {
              return true;
            }
            String name = f.getName();
            if (name.endsWith(".au")
                || name.endsWith(".wav")
                || name.endsWith(".aiff")
                || name.endsWith(".aif")) {
              return true;
            }
            return false;
          }

          public String getDescription() {
            return ".au, .wav, .aif";
          }
        });

    int returnVal = fileChooser.showOpenDialog(new JFrame());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fileChooser.getSelectedFile();
      myHelper = new Helper(file);
      myHelper.setViewer(this);
      data = myHelper.getData();
      createMainFrame();
      return data;
    }
    return null;
  }
Esempio n. 4
0
  private ArrayList<Integer> findMinMax(int beginFrame, int endFrame) {
    // System.err.println(beginFrame + " " + endFrame);
    // System.err.println(data);

    ArrayList<Integer> minMax = new ArrayList<Integer>();
    int length = endFrame - beginFrame + 1;
    float maxValue = myHelper.getMaxSampleValue();

    float max = (-1) * maxValue;
    ;
    float min = maxValue;
    for (int sampleIndex = beginFrame; sampleIndex < endFrame; sampleIndex++) {
      int sampleValue = data.get(sampleIndex);
      if (max < sampleValue) {
        max = sampleValue;
      }
      if (min > sampleValue) {
        min = sampleValue;
      }
    }
    minMax.add((int) min);
    minMax.add((int) max);
    return minMax;
  }
Esempio n. 5
0
 /*
  * Return the sampling rate of the sound.  Only works if the sound has been
  * created (i.e., the helper)
  */
 public int getSamplingRate() {
   return ((int) myHelper.getSamplingRate());
 }
Esempio n. 6
0
  private ArrayList<SoundSample> getSampleAsArrayList(Selection selection, Component c) {
    ArrayList<SoundSample> points = new ArrayList<SoundSample>();

    // System.err.println(c.getHeight());
    int sampleBeginIndex = selection.getBegin();
    int sampleEndIndex = selection.getEnd();

    if (!checkRange(sampleBeginIndex, sampleEndIndex)) {
      System.err.println(
          "The range of sample ("
              + sampleBeginIndex
              + " , "
              + sampleEndIndex
              + ") you are trying to visualize is invalid");
    } else {
      int selectionLength = sampleEndIndex - sampleBeginIndex + 1;
      float maxValue = myHelper.getMaxSampleValue();
      samplePerPixel = selectionLength * 1.0 / frameWidth;
      if (DEBUG) System.err.println("getSamples: sample per pixel " + samplePerPixel);

      if (selectionLength
          < frameWidth) { // if every sample can be displayed (i.e at least 1 pixel per sample)
        if (DEBUG) System.err.println("getSamples: Everything can be displayed " + selectionLength);
        int ratio = (int) Math.floor(frameWidth / selectionLength);
        if (DEBUG) System.err.println("getSamples: ratio " + ratio);
        int occupyPixels = ratio * selectionLength;
        selectionLength =
            selectionLength
                + (frameWidth - occupyPixels)
                    / ratio; // fill up the display space with a few more samples

        if (DEBUG) System.err.println("getSamples: selection length " + selectionLength);

        if (selectionLength + sampleBeginIndex - 1 > data.size()) {
          selectionLength -= selectionLength + sampleBeginIndex - 1 - data.size();
        }
        if (DEBUG) System.err.println("getSamples: Final selection length " + selectionLength);
        for (int i = 0; i < selectionLength; i++) {
          float sampleValue = data.get(sampleBeginIndex + i - 1);
          float y = ((float) Math.floor(c.getHeight() / 2) * (1 - sampleValue / maxValue));
          points.add(
              new SoundSample(new Point2D.Float(i * ratio, y), sampleValue, sampleBeginIndex + i));
        }
      } else if (selectionLength / 2 < frameWidth) { // we can sample at Nyquist rate
        // System.err.println("Sample begin " + sampleBeginIndex );

        int start = sampleBeginIndex;
        for (int pixel = 0; pixel < frameWidth; pixel++) {
          // System.err.println("Sample index " + (start+ samplePerPixel * pixel) );

          int sampleValue = data.get((int) Math.floor(start + samplePerPixel * pixel));
          // int sampleValue = findMax(start, end);
          float y =
              ((float) Math.floor(c.getHeight() / 2)
                  - (sampleValue * ((float) Math.floor(c.getHeight() / 2) / maxValue)));
          points.add(new SoundSample(new Point2D.Float(pixel, y), sampleValue, pixel));
        }
      } else { // show general contour
        int start = sampleBeginIndex;
        int end = (int) (sampleBeginIndex + this.samplePerPixel);
        int baseLine = c.getHeight() / 2;
        for (int pixel = 0; pixel < frameWidth; pixel++) {
          ArrayList<Integer> minMax = findMinMax(start, end);
          // System.err.println(minMax);
          float y = baseLine - (minMax.get(0) * 0.9f / maxValue * baseLine);
          points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(0), pixel));

          y = baseLine - (minMax.get(0) * 0.7f / maxValue * baseLine);
          points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(0) / 2, pixel));

          // System.err.print("Min y: " + y + " ");

          y = baseLine - (minMax.get(1) * 0.9f / maxValue * baseLine);
          points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(1) / 2, pixel));
          y = baseLine - (minMax.get(1) * 0.7f / maxValue * baseLine);
          points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(1), pixel));

          //					if (y > c.getHeight())
          //						System.err.println("not right");
          start = end + 1;
          end = (int) (end + samplePerPixel);
        }
      }
    }
    return points;
  }
Esempio n. 7
0
 /*
  * Write the sound to a specific file
  */
 public void writeToFile(String fileName) {
   if (myHelper != null) myHelper.writeToFile(fileName);
   else System.err.println("No sound has been loaded. Please load a new sound");
 }
Esempio n. 8
0
 /*
  * Play the sound
  */
 public void play() {
   if (myHelper != null) myHelper.play();
   else System.err.println("No sound has been loaded. Please load a new sound");
 }
Esempio n. 9
0
 /*
  * Takes a new ArrayList to use as the data for the sound.
  * Will keep the same audio format (sampling rate, depth (which should be 16!), filename, etc).
  */
 public void setData(ArrayList<Integer> data) {
   this.data = data;
   myHelper.setData(data);
   refresh(false); // byte array is updated in the Helper.setData(), no need to do it again
   zoomReset();
 }