Example usage for javax.sound.sampled AudioFormat matches

List of usage examples for javax.sound.sampled AudioFormat matches

Introduction

In this page you can find the example usage for javax.sound.sampled AudioFormat matches.

Prototype

public boolean matches(AudioFormat format) 

Source Link

Document

Indicates whether this format matches the one specified.

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 ww w  .  j  ava 2  s  .com*/

    // 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.sipfoundry.sipxconfig.site.common.AssetSelector.java

public static boolean isAcceptedAudioFormat(InputStream stream) {
    try {/*from   w w w .j a va  2 s.c om*/
        InputStream testedStream = stream;
        // HACK: looks like in openjdk InputStream does not support mark/reset
        if (!stream.markSupported()) {
            // getAudioInputStream depends on mark reset do we wrap buffered input stream
            // around passed stream
            testedStream = new BufferedInputStream(stream);
        }
        AudioInputStream audio = AudioSystem.getAudioInputStream(new BufferedInputStream(testedStream));
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000, // sample
                // rate
                16, // bits per sample
                1, // channels
                2, // frame rate
                8000, // frame size
                false); // isBigEndian)
        return format.matches(audio.getFormat());
    } catch (IOException e) {
        LOG.warn("Uploaded file problems.", e);
    } catch (UnsupportedAudioFileException e) {
        LOG.info("Unsupported format", e);
    }
    return false;
}