@Override
 public void actionPerformed(final ActionEvent e) {
   String name = e.getActionCommand();
   PitchEstimationAlgorithm newAlgo = PitchEstimationAlgorithm.valueOf(name);
   algo = newAlgo;
   if (currentFile != null) {
     estimationDispatcher.stop();
     sourceDispatcher.stop();
     startFile(currentFile);
   }
 }
    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();
      }
    }
Beispiel #3
0
  private void setNewMixer(Mixer mixer)
      throws LineUnavailableException, UnsupportedAudioFileException {

    if (dispatcher != null) {
      dispatcher.stop();
    }
    if (fileName == null) {
      final AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
      final DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format);
      TargetDataLine line;
      line = (TargetDataLine) mixer.getLine(dataLineInfo);
      final int numberOfSamples = bufferSize;
      line.open(format, numberOfSamples);
      line.start();
      final AudioInputStream stream = new AudioInputStream(line);

      // create a new dispatcher
      dispatcher = new AudioDispatcher(stream, bufferSize, overlap);
    } else {
      try {
        File audioFile = new File(fileName);
        dispatcher = AudioDispatcher.fromFile(audioFile, bufferSize, overlap);
        AudioFormat format = AudioSystem.getAudioFileFormat(audioFile).getFormat();
        dispatcher.addAudioProcessor(new AudioPlayer(format));
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    currentMixer = mixer;

    // add a processor, handle pitch event.
    dispatcher.addAudioProcessor(new PitchProcessor(algo, sampleRate, bufferSize, this));
    dispatcher.addAudioProcessor(fftProcessor);

    // run the dispatcher (on a new thread).
    new Thread(dispatcher, "Audio dispatching").start();
  }
  @Ignore
  @Test
  public void testSilenceWriter()
      throws UnsupportedAudioFileException, InterruptedException, LineUnavailableException,
          FileNotFoundException {
    float sampleRate = 44100;
    int bufferSize = 1024;
    int overlap = 0;

    // available mixers
    int index = 0;
    int selectedMixerIndex = 4;
    for (Mixer.Info mixer : AudioSystem.getMixerInfo()) {
      System.out.println(index + ": " + Shared.toLocalString(mixer));
      index++;
    }
    Mixer.Info selectedMixer = AudioSystem.getMixerInfo()[selectedMixerIndex];
    System.out.println("Selected mixer: " + Shared.toLocalString(selectedMixer));

    // open a line
    final Mixer mixer = AudioSystem.getMixer(selectedMixer);
    final AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, true);
    final DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format);
    TargetDataLine line;
    line = (TargetDataLine) mixer.getLine(dataLineInfo);
    final int numberOfSamples = bufferSize;
    line.open(format, numberOfSamples);
    line.start();
    final AudioInputStream stream = new AudioInputStream(line);

    // create a new dispatcher
    AudioDispatcher dispatcher = new AudioDispatcher(stream, bufferSize, overlap);

    WaveformWriter writer = new WaveformWriter(format, "01.file.wav");
    // add a processor, handle percussion event.
    dispatcher.addAudioProcessor(new SilenceDetector());
    dispatcher.addAudioProcessor(writer);

    // run the dispatcher (on the same thread, use start() to run it on
    // another thread).
    new Thread(dispatcher).start();

    Thread.sleep(3000);

    dispatcher.removeAudioProcessor(writer);
    writer = new WaveformWriter(format, "02.file.wav");
    dispatcher.addAudioProcessor(writer);

    Thread.sleep(3000);

    dispatcher.stop();
  }
    private void run(String[] args)
        throws UnsupportedAudioFileException, IOException, IllegalArgumentException,
            LineUnavailableException {
      PitchEstimationAlgorithm algo = PitchEstimationAlgorithm.FFT_YIN;
      String inputFile = args[0];
      String outputFile = null;
      String combinedFile = null;
      if (args.length == 3 && args[0].equalsIgnoreCase("--detector")) {
        algo = PitchEstimationAlgorithm.valueOf(args[1].toUpperCase());
        inputFile = args[2];
      } else if (args.length == 3 && args[0].equalsIgnoreCase("--output")) {
        outputFile = args[1];
        inputFile = args[2];
      } else if (args.length == 5
          && args[0].equalsIgnoreCase("--detector")
          && args[2].equalsIgnoreCase("--output")) {
        algo = PitchEstimationAlgorithm.valueOf(args[1].toUpperCase());
        outputFile = args[3];
        inputFile = args[4];
      } else if (args.length == 7
          && args[0].equalsIgnoreCase("--detector")
          && args[2].equalsIgnoreCase("--output")
          && args[4].equalsIgnoreCase("--combined")) {
        algo = PitchEstimationAlgorithm.valueOf(args[1].toUpperCase());
        outputFile = args[3];
        combinedFile = args[5];
        inputFile = args[6];
      } else if (args.length != 1) {
        printError();
        SharedCommandLineUtilities.printLine();
        System.err.println("Current error:");
        System.err.println(
            "\tThe command expects the options in the specified order, the current command is not parsed correctly!");
        return;
      }
      File audioFile = new File(inputFile);
      AudioFormat format = AudioSystem.getAudioFileFormat(audioFile).getFormat();
      float samplerate = format.getSampleRate();
      int size = 1024;
      int overlap = 0;
      PitchResyntheziser prs = new PitchResyntheziser(samplerate);
      AudioDispatcher dispatcher = AudioDispatcher.fromFile(audioFile, size, overlap);
      dispatcher.addAudioProcessor(new PitchProcessor(algo, samplerate, size, prs));
      if (outputFile != null) {
        dispatcher.addAudioProcessor(new WaveformWriter(format, outputFile));
      } else {
        dispatcher.addAudioProcessor(new AudioPlayer(format));
      }
      dispatcher.addAudioProcessor(
          new AudioProcessor() {
            @Override
            public void processingFinished() {}

            @Override
            public boolean process(AudioEvent audioEvent) {
              System.err.print(String.format("%3.0f %%", audioEvent.getProgress() * 100));
              System.err.print(String.format("\b\b\b\b\b", audioEvent.getProgress()));
              return true;
            }
          });
      dispatcher.run();

      if (combinedFile != null) {
        combineTwoMonoAudioFilesInTwoChannels(inputFile, outputFile, combinedFile);
      }
    }