Example usage for javax.sound.sampled TargetDataLine getFormat

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

Introduction

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

Prototype

AudioFormat getFormat();

Source Link

Document

Obtains the current format (encoding, sample rate, number of channels, etc.) of the data line's audio data.

Usage

From source file:com.arkatay.yada.codec.AudioEncoder.java

public void startModule(TargetDataLine inputLine, int audioFormatIndex) throws LineUnavailableException {
    capturedFrameSizeInNanos = 20L * millisToNanos;

    if (state != STATE_OFF)
        throw new IllegalStateException("Trying to re-start the encoder");

    // Check bounds
    AudioFormat[] audioFormats = getSupportedAudioFormats();
    if (audioFormatIndex < 0 || audioFormatIndex >= audioFormats.length)
        throw new LineUnavailableException("Audio format array out of bounds");

    // Get format
    AudioFormat audioFormat = audioFormats[audioFormatIndex];

    // Create line if created internally
    if (inputLine == null) {
        inputLine = createLine(audioFormat);
    }/*  w w  w  .  ja  v  a2 s  .  co  m*/

    // Validate the audio format if external
    else if (!audioFormat.matches(inputLine.getFormat())) {
        throw new LineUnavailableException("Audio format not supported");
    }

    this.inputLine = inputLine;
    this.audioFormatIndex = audioFormatIndex;

    // Call init on the sub-class implementation
    init();

    // Calculate stuff
    capturedFrameSizeInBytes = (int) (audioFormat.getFrameRate() * audioFormat.getFrameSize()
            * capturedFrameSizeInNanos / (1000 * millisToNanos));
    diffTimeNanosLimit = diffTimeMillisLimit * millisToNanos;

    // Open the input line, the wanted buffer size is N times as big as the frame size
    inputLine.open(audioFormat, 4 * capturedFrameSizeInBytes);
    inputLineBufferSize = inputLine.getBufferSize();
    log.debug("Input line is open with buffer size " + inputLineBufferSize);

    // Create a buffer for the captured frame
    captureBuffer = new byte[capturedFrameSizeInBytes];

    // Go to state idle
    state = STATE_IDLE;

    // Start the capturing thread, it will block until startProcessing is called
    thread.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());
    }//ww w.  ja  v a 2s .  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;
}