Package audio

Source Code of audio.SoundMicroTest2

package audio;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class SoundMicroTest2 {

  /**
   * @param args
   */
  public static void main(String[] args) {
    File outputFile = new File("c:\\test.wav");
    AudioFormat af = new AudioFormat(11025.0F, 16, 2, true, false); // Float-Konstanten
                                    // mit
                                    // Suffix
                                    // (F)
                                    // deklarieren
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
    DataLine.Info sinfo = new DataLine.Info(SourceDataLine.class, af);

    try {
      ByteArrayOutputStream baOut = new ByteArrayOutputStream();
      TargetDataLine tl = (TargetDataLine) AudioSystem.getLine(info);
      tl.open(af);
      tl.start();
      SourceDataLine sl = (SourceDataLine) AudioSystem.getLine(sinfo); // Neu:
                                        // Line
                                        // f�r
                                        // Tonausgabe
      sl.open(af); // �ffnen
      sl.start(); // starten
      int numBytesRead;
      int durchlauf = 0;
      byte[] ba = new byte[256]; // definieren und gleich einmal
                    // initialisieren.
      while (durchlauf < 1000) {
        durchlauf++;
        numBytesRead = tl.read(ba, 0, ba.length);
        baOut.write(ba, 0, numBytesRead);
        sl.write(ba, 0, numBytesRead); // in Tonausgabe schreiben
      }
      baOut.close(); // close() ist wichtig, k�nnten noch Daten gepuffert
              // sein
      tl.stop(); // eingabe stoppen
      sl.stop(); // ausgabe stoppen
      tl.close(); // eingabe schliessen
      sl.close(); // ausgabe schliessen
      ba = baOut.toByteArray(); // ba liegt brach ruhig wieder verwenden
      ByteArrayInputStream baIn = new ByteArrayInputStream(ba);
      AudioInputStream stream = new AudioInputStream(baIn, af, ba.length
          / af.getFrameSize());
       AudioSystem.write(stream, AudioFileFormat.Type.WAVE,outputFile);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
TOP

Related Classes of audio.SoundMicroTest2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.