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, int bufferSize) throws LineUnavailableException;

Source Link

Document

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

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);
    }/*from  w ww .  ja va2  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();
}