Ejemplo n.º 1
0
  /*
   * Return data as a byte array.
   */
  private static byte[] readByte(String filename) {
    byte[] data = null;
    AudioInputStream ais = null;
    try {

      // try to read from file
      File file = new File(filename);
      if (file.exists()) {
        ais = AudioSystem.getAudioInputStream(file);
        data = new byte[ais.available()];
        ais.read(data);
      }

      // try to read from URL
      else {
        URL url = StdAudio.class.getResource(filename);
        ais = AudioSystem.getAudioInputStream(url);
        data = new byte[ais.available()];
        ais.read(data);
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
      throw new RuntimeException("Could not read " + filename);
    }

    return data;
  }
Ejemplo n.º 2
0
  /** Save the double array as a sound file (using .wav or .au format). */
  public static void save(String filename, double[] input) {
    // assumes 44,100 samples per second
    // use 16-bit audio, mono, signed PCM, little Endian
    AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
    byte[] data = new byte[2 * input.length];
    for (int i = 0; i < input.length; i++) {
      int temp = (short) (input[i] * MAX_16_BIT);
      data[2 * i + 0] = (byte) temp;
      data[2 * i + 1] = (byte) (temp >> 8);
    }

    // now save the file
    try {
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      AudioInputStream ais = new AudioInputStream(bais, format, input.length);
      if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
        AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
      } else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
        AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
      } else {
        throw new RuntimeException("File format not supported: " + filename);
      }
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
    }
  }
Ejemplo n.º 3
0
 public void openSound() {
   if (!sound.isOpen()) {
     try {
       AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundPath);
       sound = AudioSystem.getClip();
       sound.open(audioInputStream);
     } catch (Exception e) {
       System.out.println("The sound from file " + String.valueOf(soundPath) + "was not found!");
       System.out.println(e);
     }
   }
 }
Ejemplo n.º 4
0
 Sound(URL soundPath, float gain) {
   this.soundPath = soundPath;
   try {
     AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundPath);
     sound = AudioSystem.getClip();
     sound.open(audioInputStream);
     gainControl = (FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN);
     gainControl.setValue(gain);
   } catch (Exception e) {
     System.out.println("The sound from file " + String.valueOf(soundPath) + "was not found!");
     System.out.println(e);
   } // Satisfy the catch
 }
Ejemplo n.º 5
0
  // open up an audio stream
  private static void init() {
    try {
      // 44,100 samples per second, 16-bit audio, mono, signed PCM, little
      // Endian
      AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);

      // the internal buffer is a fraction of the actual buffer size, this
      // choice is arbitrary
      // it gets divided because we can't expect the buffered data to line
      // up exactly with when
      // the sound card decides to push out its samples.
      buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE / 3];
      listeners = new HashSet<AudioEventListener>();
    } catch (Exception e) {
      System.err.println("Error initializing StdAudio audio system:");
      e.printStackTrace();
      System.exit(1);
    }

    // no sound gets made before this call
    line.start();
  }