Example usage for javax.sound.sampled TargetDataLine open

List of usage examples for javax.sound.sampled TargetDataLine open

Introduction

In this page you can find the example usage for javax.sound.sampled TargetDataLine open.

Prototype

void open(AudioFormat format) throws LineUnavailableException;

Source Link

Document

Opens the line with the specified format, causing the line to acquire any required system resources and become operational.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    float sampleRate = 8000;
    int sampleSizeInBits = 8;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = true;
    final AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);
    line.start();//from w  w  w  . ja v a  2 s . com
    Runnable runner = new Runnable() {
        int bufferSize = (int) format.getSampleRate() * format.getFrameSize();

        byte buffer[] = new byte[bufferSize];

        public void run() {
            try {

                int count = line.read(buffer, 0, buffer.length);
                if (count > 0) {
                    out.write(buffer, 0, count);
                }

                out.close();
            } catch (IOException e) {
                System.err.println("I/O problems: " + e);
                System.exit(-1);
            }
        }
    };
    Thread captureThread = new Thread(runner);
    captureThread.start();

    byte audio[] = out.toByteArray();
    InputStream input = new ByteArrayInputStream(audio);
    final SourceDataLine line1 = (SourceDataLine) AudioSystem.getLine(info);
    final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize());
    line1.open(format);
    line1.start();

    runner = new Runnable() {
        int bufferSize = (int) format.getSampleRate() * format.getFrameSize();

        byte buffer[] = new byte[bufferSize];

        public void run() {
            try {
                int count;
                while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
                    if (count > 0) {
                        line1.write(buffer, 0, count);
                    }
                }
                line1.drain();
                line1.close();
            } catch (IOException e) {
                System.err.println("I/O problems: " + e);
                System.exit(-3);
            }
        }
    };
    Thread playThread = new Thread(runner);
    playThread.start();

}

From source file:org.snitko.app.record.SoundRecorder.java

public AudioInputStream record(long durationInSeconds) throws LineUnavailableException {
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info mixer : mixers) {
        System.out.printf("mixer name/desc/vendor/version: %s, %s, %s, %s\n", mixer.getName(),
                mixer.getDescription(), mixer.getVendor(), mixer.getVersion());
    }/*from   w ww .j a v  a2s.c  o m*/

    AudioFileFormat.Type[] types = AudioSystem.getAudioFileTypes();
    for (AudioFileFormat.Type type : types) {
        System.out.println("audio types are: " + type.toString());
    }

    AudioFormat audioFormat = getAudioFormat();

    // the targetDataLine from which audio data is captured
    TargetDataLine targetDataLine = AudioSystem.getTargetDataLine(audioFormat);
    System.out.println("targetDataLine: " + targetDataLine + "\n\tlineInfo" + targetDataLine.getLineInfo()
            + "\n\tformat: " + targetDataLine.getFormat());

    // checks if system supports the data targetDataLine
    if (!AudioSystem.isLineSupported(targetDataLine.getLineInfo())) {
        System.out.println("Line not supported");
        System.exit(0);
    }

    targetDataLine.addLineListener(new TimedLineListener(targetDataLine, durationInSeconds));
    targetDataLine.open(audioFormat);
    targetDataLine.start(); // start capturing

    System.out.println("Start capturing...");
    AudioInputStream audioInputStream = new AudioInputStream(targetDataLine);
    System.out.println("Start recording...");
    return audioInputStream;
}