Example #1
0
 private void checkArgumentsAndRun(String... arguments) {
   if (arguments.length == 0) {
     printError();
   } else {
     try {
       run(arguments);
     } catch (UnsupportedAudioFileException e) {
       printError();
       SharedCommandLineUtilities.printLine();
       System.err.println("Error:");
       System.err.println("\tThe audio file is not supported!");
     } catch (IOException e) {
       printError();
       SharedCommandLineUtilities.printLine();
       System.err.println("Current error:");
       System.err.println("\tIO error, maybe the audio file is not found or not supported!");
     } catch (IllegalArgumentException e) {
       printError();
       SharedCommandLineUtilities.printLine();
       System.err.println("Current error:");
       System.err.println("\tThe algorithm provided is unknown!");
     } catch (LineUnavailableException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }
Example #2
0
 public void play(InputStream source) {
   int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10);
   byte[] buffer = new byte[bufferSize];
   SourceDataLine line;
   try {
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
     line = (SourceDataLine) AudioSystem.getLine(info);
     line.open(format, bufferSize);
   } catch (LineUnavailableException e) {
     e.printStackTrace();
     return;
   }
   line.start();
   try {
     int numBytesRead = 0;
     while (numBytesRead != -1) {
       numBytesRead = source.read(buffer, 0, buffer.length);
       if (numBytesRead != -1) line.write(buffer, 0, numBytesRead);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
   line.drain();
   line.close();
 }
Example #3
0
 SourceDataLine getSourceDataLine(AudioFormat format, int bufferSize) {
   SourceDataLine line = null;
   DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
   if (AudioSystem.isLineSupported(info)) {
     try {
       if (outputMixer == null) {
         line = (SourceDataLine) AudioSystem.getLine(info);
       } else {
         line = (SourceDataLine) outputMixer.getLine(info);
       }
       // remember that time you spent, like, an entire afternoon fussing
       // with this buffer size to try to get the latency decent on Linux?
       // Yah, don't fuss with this anymore, ok?
       line.open(format, bufferSize * format.getFrameSize() * 4);
       if (line.isOpen()) {
         debug(
             "SourceDataLine is "
                 + line.getClass().toString()
                 + "\n"
                 + "Buffer size is "
                 + line.getBufferSize()
                 + " bytes.\n"
                 + "Format is "
                 + line.getFormat().toString()
                 + ".");
         return line;
       }
     } catch (LineUnavailableException e) {
       error("Couldn't open the line: " + e.getMessage());
     }
   }
   error("Unable to return a SourceDataLine: unsupported format - " + format.toString());
   return line;
 }
Example #4
0
  private static void rand(int t, int range) {
    long t0 = System.currentTimeMillis();
    Random r = new Random();
    double hz;
    double n;
    int d;
    int i;

    while (System.currentTimeMillis() <= t0 + t) {
      n = r.nextInt(range * 12 + 1) / 2.0;
      d = 250 * (r.nextInt(8 - 1) + 1);
      if (r.nextDouble() <= 0.1) {
        i = 0;
      } else {
        i = 1;
      }

      hz = 440 * Math.pow(2, n / 6.0);
      try {
        Tone.sound(hz, d, i);
      } catch (LineUnavailableException e) {
        e.printStackTrace();
      }
    }
  }
Example #5
0
  // This is f*****g stupid as hell; if this method makes it into a release, somebody shoot me in
  // the head.
  public void delayTap1(int channel, int volume) {

    try {
      InputStream file_input_stream = p.createInput(modpath);
      delayplayerA = new Player(interpolation);

      delayplayerA.set_module(Player.load_module(file_input_stream));
      file_input_stream.close();
      delayplayerA.set_loop(true);
      delayplayerA.receivebuffer(buffersize);

      for (int i = 0; i < delayplayerA.get_num_channels(); i++) {
        if (i != channel) {
          delayplayerA.ibxm.chanmute[i] = true;
        }
      }
      delayplayerA.ibxm.channels[channel].chanvol_override = volume;

      delayplayerA.play();
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (LineUnavailableException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Example #6
0
  public void beginExecution() {

    AudioFormat audioFormat =
        new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 1, numChannels, 44100.0F, false);
    // System.out.println("AudioPlayer.playAudioInts audio format: " + audioFormat );

    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
    if (!AudioSystem.isLineSupported(dataLineInfo)) {
      System.out.println("AudioPlayer.playAudioInts does not " + " handle this type of audio.");
      return;
    }

    try {
      SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

      sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
    }

    chunkIndex = 0;

    InitialExecution = true;
  }
Example #7
0
 @ActionDoc(text = "plays a sound from the sounds folder")
 public static void playSound(
     @ParamDoc(name = "filename", text = "the filename with extension") String filename) {
   try {
     InputStream is = new FileInputStream(SOUND_DIR + File.separator + filename);
     if (filename.toLowerCase().endsWith(".mp3")) {
       Player player = new Player(is);
       playInThread(player);
     } else {
       AudioInputStream ais = AudioSystem.getAudioInputStream(is);
       Clip clip = AudioSystem.getClip();
       clip.open(ais);
       playInThread(clip);
     }
   } catch (FileNotFoundException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   } catch (JavaLayerException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   } catch (UnsupportedAudioFileException e) {
     logger.error(
         "Format of sound file '{}' is not supported: {}",
         new String[] {filename, e.getMessage()});
   } catch (IOException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   } catch (LineUnavailableException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   }
 }
Example #8
0
    public void run() {
      InputStream ins = GameScreen.class.getResourceAsStream("res/bgmusic.wav");
      AudioInputStream audioIn;
      // System.out.println(ins);

      try {
        clip = AudioSystem.getClip();
        audioIn = AudioSystem.getAudioInputStream(ins);
        clip.open(audioIn);
      } catch (UnsupportedAudioFileException e) {
        // ignore
        e.printStackTrace();
        return;
      } catch (IOException e) {
        // ignore
        e.printStackTrace();
        return;
      } catch (LineUnavailableException e) {
        // ignore
        e.printStackTrace();
        return;
      }
      // System.out.println("woohoo");

      clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
Example #9
0
    protected void startFile(File file) {
      currentFile = file;
      AudioFormat format;
      try {
        format = AudioSystem.getAudioFileFormat(file).getFormat();
        float samplerate = format.getSampleRate();
        int size = 1024;
        int overlap = 0;

        PitchResyntheziser prs = new PitchResyntheziser(samplerate);
        estimationGain = new GainProcessor(estimationGainSlider.getValue() / 100.0);
        estimationDispatcher = AudioDispatcher.fromFile(file, size, overlap);
        estimationDispatcher.addAudioProcessor(new PitchProcessor(algo, samplerate, size, prs));
        estimationDispatcher.addAudioProcessor(estimationGain);
        estimationDispatcher.addAudioProcessor(new AudioPlayer(format));

        sourceGain = new GainProcessor(sourceGainSlider.getValue() / 100.0);
        sourceDispatcher = AudioDispatcher.fromFile(file, size, overlap);
        sourceDispatcher.addAudioProcessor(sourceGain);
        sourceDispatcher.addAudioProcessor(new AudioPlayer(format));

        new Thread(estimationDispatcher).start();
        new Thread(sourceDispatcher).start();

      } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (LineUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
Example #10
0
  public void run() {

    File soundFile = new File(filename);
    if (!soundFile.exists()) {
      System.err.println("Wave file not found: " + filename);
      Dialog.erreur(null, "Le fichier " + filename + " n'a pas été trouver.");
      return;
    }

    AudioInputStream audioInputStream = null;
    try {
      audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) {
      e1.printStackTrace();
      return;
    } catch (IOException e1) {
      e1.printStackTrace();
      return;
    }

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try {
      auline = (SourceDataLine) AudioSystem.getLine(info);
      auline.open(format);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      return;
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    if (auline.isControlSupported(FloatControl.Type.PAN)) {
      FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
      if (curPosition == Position.RIGHT) pan.setValue(1.0f);
      else if (curPosition == Position.LEFT) pan.setValue(-1.0f);
    }

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try {
      while (nBytesRead != -1) {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
        if (nBytesRead >= 0) auline.write(abData, 0, nBytesRead);
      }
    } catch (IOException e) {
      e.printStackTrace();
      return;
    } finally {
      auline.drain();
      auline.close();
    }
  }
Example #11
0
 public ThreadSound() {
   try {
     clip0 = AudioSystem.getClip();
     clip1 = AudioSystem.getClip();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
     canPlayAudio = false;
   }
 }
 /** @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */
 @Override
 public void actionPerformed(ActionEvent e) {
   if (e.getSource() == play) {
     try {
       ReJava.playSample(waveData, sps);
     } catch (LineUnavailableException e1) {
       e1.printStackTrace();
     }
   }
 }
Example #13
0
  public void run() {

    File soundFile = new File(this.filename);
    if (!soundFile.exists()) {
      System.err.println("nicht gefunden: " + filename);
      return;
    }

    AudioInputStream audioInputStream = null;
    try {
      audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) {
      e1.printStackTrace();
      return;
    } catch (IOException e1) {
      e1.printStackTrace();
      return;
    }

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try {
      auline = (SourceDataLine) AudioSystem.getLine(info);
      auline.open(format);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      return;
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    FloatControl rate = (FloatControl) auline.getControl(FloatControl.Type.SAMPLE_RATE);

    rate.setValue(rate.getValue() * 5f);

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try {
      while (nBytesRead != -1) {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
        if (nBytesRead >= 0) auline.write(abData, 0, nBytesRead);
      }
    } catch (IOException e) {
      e.printStackTrace();
      return;
    } finally {
      auline.drain();
      auline.close();
    }
  }
 // Constructor takes the name to save decoded track as in the form of a string ex:"track1.wav"
 public FlacTrackPlayer(String trackName) {
   player = null;
   track = new File(trackName);
   Mixer mixer = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]);
   try {
     mixer.open();
   } catch (LineUnavailableException e) {
     // TODO Auto-generated catch block
     System.out.println("huh?");
     e.printStackTrace();
   }
 }
Example #15
0
 @Override
 public void actionPerformed(final ActionEvent e) {
   String name = e.getActionCommand();
   PitchEstimationAlgorithm newAlgo = PitchEstimationAlgorithm.valueOf(name);
   algo = newAlgo;
   try {
     setNewMixer(currentMixer);
   } catch (LineUnavailableException e1) {
     e1.printStackTrace();
   } catch (UnsupportedAudioFileException e1) {
     e1.printStackTrace();
   }
 }
Example #16
0
 public Oscilator(int sampleRate) {
   this.sampleRate = sampleRate;
   // オーディオ形式を指定
   AudioFormat af = new AudioFormat(sampleRate, 8, 1, true, true);
   try {
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
     line = (SourceDataLine) AudioSystem.getLine(info);
     line.open();
     line.start();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Example #17
0
    /** @param filename the name of the file that is going to be played */
    public void playSound(String filename) {

      String strFilename = filename;

      try {
        soundFile = new File(strFilename);
      } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
      }

      try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
      } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
      }

      audioFormat = audioStream.getFormat();

      DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
      try {
        sourceLine = (SourceDataLine) AudioSystem.getLine(info);
        sourceLine.open(audioFormat);
      } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
      } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
      }

      sourceLine.start();

      int nBytesRead = 0;
      byte[] abData = new byte[BUFFER_SIZE];
      while (nBytesRead != -1) {
        try {
          nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (nBytesRead >= 0) {
          @SuppressWarnings("unused")
          int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
      }

      sourceLine.drain();
      sourceLine.close();
    }
Example #18
0
 public MakoVM(int[] m) {
   this.m = m;
   try {
     AudioFormat format = new AudioFormat(8000f, 8, 1, false, false);
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
     soundLine = (SourceDataLine) AudioSystem.getLine(info);
     soundLine.open(format, 670);
     soundLine.start();
   } catch (IllegalArgumentException e) {
     System.out.println("Unable to initialize sound.");
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Example #19
0
 private static void scale(int f0, int range) {
   double hz;
   for (double n = 0; n <= 6 * range; n += 0.5) {
     if ((int) (n / 6) != n / 6 && (int) ((n - 2.5) / 6) != (n - 2.5) / 6) {
       n += 0.5;
     }
     hz = f0 * Math.pow(2, n / 6.0);
     try {
       Tone.sound(hz, 100, 1);
     } catch (LineUnavailableException e) {
       e.printStackTrace();
     }
   }
 }
Example #20
0
 /**
  * Sets the value of output to newOutput. The output variable specifies whether or not to play the
  * audio of this source on the server.
  *
  * @param newOutput The new value for output.
  */
 public void setOutput(boolean newOutput) {
   output = newOutput;
   if (output) {
     try {
       serverOutput.open();
     } catch (LineUnavailableException e) {
       e.printStackTrace();
     }
     serverOutput.start();
   } else {
     serverOutput.stop();
     serverOutput.close();
   }
 }
Example #21
0
 /** Load all sounds */
 private static void load() {
   loaded = true;
   try {
     startSound = Resources.loadSound("/sounds/start.wav");
     backgroundSound = Resources.loadSound("/sounds/background.wav");
     placeholderSound = Resources.loadSound("/sounds/placeholder.wav");
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Example #22
0
  public WavEffect(String fileName) {
    URL url = getClass().getResource("/audios/" + fileName);
    try {
      clip = AudioSystem.getClip();
      AudioInputStream input = AudioSystem.getAudioInputStream(url);
      clip.open(input);

    } catch (LineUnavailableException e) {
      e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #23
0
  private void sound(String nameSound) {
    Clip clip;
    try {

      clip = AudioSystem.getClip();
      clip.open(AudioSystem.getAudioInputStream(new File("resource/fileSound/" + nameSound)));
      clip.start();

    } catch (UnsupportedAudioFileException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (LineUnavailableException e) {
      e.printStackTrace();
    }
  }
Example #24
0
 public static void ScoreUp() {
   try {
     AudioInputStream audioIn;
     Clip clip;
     clip = AudioSystem.getClip();
     audioIn = AudioSystem.getAudioInputStream(Main.class.getResource("/Sounds/Score.wav"));
     clip.open(audioIn);
     clip.start();
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Example #25
0
  private void init() {

    AudioFormat format = new AudioFormat((float) 44100, 16, 2, true, false);
    try {
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
      mainLine = (SourceDataLine) AudioSystem.getLine(info);
      mainLine.open(format);
      mainLine.start();

      bufferLine = (SourceDataLine) AudioSystem.getLine(info);
      bufferLine.open(format);
      bufferLine.start();

    } catch (LineUnavailableException e) {
      e.printStackTrace();
    }
  }
Example #26
0
 public void run() {
   try {
     b = new byte[6300];
     line.open(new AudioFormat(44100, 16, 1, true, true), 6300);
     line.start();
     while (true) {
       line.read(b, 0, b.length);
       server.writeByteBuffers(b, sourceIndex);
       if (output) serverOutput.write(b, 0, b.length);
     }
   } catch (LineUnavailableException e) {
     e.printStackTrace();
     System.out.println(sourceIndex);
   } finally {
     line.stop();
     line.close();
   }
 }
Example #27
0
 @SuppressWarnings("static-access")
 private void PlayMusic() {
   try {
     AudioInputStream audioIn;
     Clip clip;
     clip = AudioSystem.getClip();
     audioIn = AudioSystem.getAudioInputStream(this.getClass().getResource("/Sounds/Main.wav"));
     clip.open(audioIn);
     clip.start();
     clip.loop(clip.LOOP_CONTINUOUSLY);
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Example #28
0
 // Constructor to construct each element of the enum with its own sound file.
 SoundEffect(String soundFileName) {
   try {
     // Use URL (instead of File) to read from disk and JAR.
     URL url = this.getClass().getClassLoader().getResource(soundFileName);
     // Set up an audio input stream piped from the sound file.
     AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
     // Get a clip resource.
     clip = AudioSystem.getClip();
     // Open audio clip and load samples from the audio input stream.
     clip.open(audioInputStream);
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
 public static void Play() {
   try {
     // Open an audio input stream.
     File soundFile = new File("foo.wav");
     AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
     // Get a sound clip resource.
     Clip clip = AudioSystem.getClip();
     // Open audio clip and load samples from the audio input stream.
     clip.open(audioIn);
     clip.start();
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Example #30
0
  // ctor
  public DataInThread(DatagramSocket socket) {
    try {
      dSocket = socket;

      // sampl Rate & bits, #Channels, signed?, bigEndian?
      AudioFormat format = new AudioFormat(16000.0f, 16, 1, true, true);
      microphone = AudioSystem.getTargetDataLine(format);
      micData = new byte[MAXBUFSIZE];
      microphone.open(format);
      inputThread = new Thread(this);
    } catch (LineUnavailableException e) {
      // TODO is there anything else we should do if the microphone
      //		can't be connected?

      // http://stackoverflow.com/questions/14348169/understanding-java-sound-api-finding-mic-on-mixer
      e.printStackTrace();
    }
  }