Java Code Examples for javax.sound.sampled.AudioFormat#isBigEndian()

The following examples show how to use javax.sound.sampled.AudioFormat#isBigEndian() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PCMtoPCMCodec.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 */
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {

    if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {

        AudioFormat sourceFormat = sourceStream.getFormat();
        AudioFormat targetFormat = new AudioFormat( targetEncoding,
                                                    sourceFormat.getSampleRate(),
                                                    sourceFormat.getSampleSizeInBits(),
                                                    sourceFormat.getChannels(),
                                                    sourceFormat.getFrameSize(),
                                                    sourceFormat.getFrameRate(),
                                                    sourceFormat.isBigEndian() );

        return getAudioInputStream( targetFormat, sourceStream );

    } else {
        throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
    }

}
 
Example 2
Source File: AudioFloatFormatConverter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
                                            AudioInputStream sourceStream) {
    if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) {
        throw new IllegalArgumentException(
                "Unsupported conversion: " + sourceStream.getFormat()
                        .toString() + " to " + targetEncoding.toString());
    }
    if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
        return sourceStream;
    AudioFormat format = sourceStream.getFormat();
    int channels = format.getChannels();
    Encoding encoding = targetEncoding;
    float samplerate = format.getSampleRate();
    int bits = format.getSampleSizeInBits();
    boolean bigendian = format.isBigEndian();
    if (targetEncoding.equals(Encoding.PCM_FLOAT))
        bits = 32;
    AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
            channels, channels * bits / 8, samplerate, bigendian);
    return getAudioInputStream(targetFormat, sourceStream);
}
 
Example 3
Source File: AudioFloatConverter.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
AudioFloatLSBFilter(AudioFloatConverter converter, AudioFormat format) {
    int bits = format.getSampleSizeInBits();
    boolean bigEndian = format.isBigEndian();
    this.converter = converter;
    stepsize = (bits + 7) / 8;
    offset = bigEndian ? (stepsize - 1) : 0;
    int lsb_bits = bits % 8;
    if (lsb_bits == 0)
        mask = (byte) 0x00;
    else if (lsb_bits == 1)
        mask = (byte) 0x80;
    else if (lsb_bits == 2)
        mask = (byte) 0xC0;
    else if (lsb_bits == 3)
        mask = (byte) 0xE0;
    else if (lsb_bits == 4)
        mask = (byte) 0xF0;
    else if (lsb_bits == 5)
        mask = (byte) 0xF8;
    else if (lsb_bits == 6)
        mask = (byte) 0xFC;
    else if (lsb_bits == 7)
        mask = (byte) 0xFE;
    else
        mask = (byte) 0xFF;
}
 
Example 4
Source File: PCMtoPCMCodec.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {

    if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {

        AudioFormat sourceFormat = sourceStream.getFormat();
        AudioFormat targetFormat = new AudioFormat( targetEncoding,
                                                    sourceFormat.getSampleRate(),
                                                    sourceFormat.getSampleSizeInBits(),
                                                    sourceFormat.getChannels(),
                                                    sourceFormat.getFrameSize(),
                                                    sourceFormat.getFrameRate(),
                                                    sourceFormat.isBigEndian() );

        return getConvertedStream(targetFormat, sourceStream);

    } else {
        throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
    }

}
 
Example 5
Source File: AudioFloatFormatConverter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
        AudioInputStream sourceStream) {
    if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
        return sourceStream;
    AudioFormat format = sourceStream.getFormat();
    int channels = format.getChannels();
    Encoding encoding = targetEncoding;
    float samplerate = format.getSampleRate();
    int bits = format.getSampleSizeInBits();
    boolean bigendian = format.isBigEndian();
    if (targetEncoding.equals(Encoding.PCM_FLOAT))
        bits = 32;
    AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
            channels, channels * bits / 8, samplerate, bigendian);
    return getAudioInputStream(targetFormat, sourceStream);
}
 
Example 6
Source File: AudioFloatFormatConverter.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
        AudioInputStream sourceStream) {
    if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
        return sourceStream;
    AudioFormat format = sourceStream.getFormat();
    int channels = format.getChannels();
    Encoding encoding = targetEncoding;
    float samplerate = format.getSampleRate();
    int bits = format.getSampleSizeInBits();
    boolean bigendian = format.isBigEndian();
    if (targetEncoding.equals(AudioFloatConverter.PCM_FLOAT))
        bits = 32;
    AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
            channels, channels * bits / 8, samplerate, bigendian);
    return getAudioInputStream(targetFormat, sourceStream);
}
 
Example 7
Source File: AudioFloatFormatConverter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
                                                // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 8
Source File: SelfSimilarity.java    From jipes with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void deriveOutputFormat(final AudioFormat inputFormat) {
    final int frameDiff = secondFrame - firstFrame;
    final float sampleRate = inputFormat.getSampleRate() / frameDiff;
    audioFormat = new AudioFormat(
            inputFormat.getEncoding(), sampleRate, inputFormat.getSampleSizeInBits(),
            inputFormat.getChannels(), inputFormat.getFrameSize(), sampleRate,
            inputFormat.isBigEndian());
}
 
Example 9
Source File: Resample.java    From jipes with GNU Lesser General Public License v2.1 5 votes vote down vote up
private AudioFormat getProcessedAudioFormat(final AudioBuffer buffer) {
    final AudioFormat sourceAudioFormat = buffer.getAudioFormat();
    return new AudioFormat(sourceAudioFormat.getSampleRate() * resampler.getFactor(),
            sourceAudioFormat.getSampleSizeInBits(),
            sourceAudioFormat.getChannels(),
            AudioFormat.Encoding.PCM_SIGNED.equals(sourceAudioFormat.getEncoding()),
            sourceAudioFormat.isBigEndian());
}
 
Example 10
Source File: AudioFloatFormatConverter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
AudioFloatInputStreamChannelMixer(AudioFloatInputStream ais,
        int targetChannels) {
    this.sourceChannels = ais.getFormat().getChannels();
    this.targetChannels = targetChannels;
    this.ais = ais;
    AudioFormat format = ais.getFormat();
    targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(),
            targetChannels, (format.getFrameSize() / sourceChannels)
                    * targetChannels, format.getFrameRate(), format
                    .isBigEndian());
}
 
Example 11
Source File: SoftMixingDataLine.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
    // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 12
Source File: SoftMixingDataLine.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
    // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 13
Source File: SoftMixingDataLine.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
    // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 14
Source File: SoftMixingDataLine.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
    // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 15
Source File: SoftMixingDataLine.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
    // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 16
Source File: AlawCodec.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
AlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {

            super(stream, outputFormat, -1);

            AudioFormat inputFormat = stream.getFormat();

            // throw an IllegalArgumentException if not ok
            if ( ! (isConversionSupported(outputFormat, inputFormat)) ) {

                throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
            }

            //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
            boolean PCMIsBigEndian;

            // determine whether we are encoding or decoding
            if (AudioFormat.Encoding.ALAW.equals(inputFormat.getEncoding())) {
                encode = false;
                encodeFormat = inputFormat;
                decodeFormat = outputFormat;
                PCMIsBigEndian = outputFormat.isBigEndian();
            } else {
                encode = true;
                encodeFormat = outputFormat;
                decodeFormat = inputFormat;
                PCMIsBigEndian = inputFormat.isBigEndian();
                tempBuffer = new byte[tempBufferSize];
            }

            if (PCMIsBigEndian) {
                tabByte1 = ALAW_TABH;
                tabByte2 = ALAW_TABL;
                highByte = 0;
                lowByte  = 1;
            } else {
                tabByte1 = ALAW_TABL;
                tabByte2 = ALAW_TABH;
                highByte = 1;
                lowByte  = 0;
            }

            // set the AudioInputStream length in frames if we know it
            if (stream instanceof AudioInputStream) {
                frameLength = ((AudioInputStream)stream).getFrameLength();
            }

            // set framePos to zero
            framePos = 0;
            frameSize = inputFormat.getFrameSize();
            if( frameSize==AudioSystem.NOT_SPECIFIED ) {
                frameSize=1;
            }
        }
 
Example 17
Source File: JavaSoundSampleLoader.java    From jsyn with Apache License 2.0 4 votes vote down vote up
private float[] loadSignedPCM(AudioInputStream audioInputStream) throws IOException,
        UnsupportedAudioFileException {
    int totalSamplesRead = 0;
    AudioFormat format = audioInputStream.getFormat();
    int numFrames = (int) audioInputStream.getFrameLength();
    int numSamples = format.getChannels() * numFrames;
    float[] data = new float[numSamples];
    final int bytesPerFrame = format.getFrameSize();
    // Set an arbitrary buffer size of 1024 frames.
    int numBytes = 1024 * bytesPerFrame;
    byte[] audioBytes = new byte[numBytes];
    int numBytesRead = 0;
    int numFramesRead = 0;
    // Try to read numBytes bytes from the file.
    while ((numBytesRead = audioInputStream.read(audioBytes)) != -1) {
        int bytesRemainder = numBytesRead % bytesPerFrame;
        if (bytesRemainder != 0) {
            // TODO Read until you get enough data.
            throw new IOException("Read partial block of sample data!");
        }

        if (audioInputStream.getFormat().getSampleSizeInBits() == 16) {
            if (format.isBigEndian()) {
                SampleLoader.decodeBigI16ToF32(audioBytes, 0, numBytesRead, data,
                        totalSamplesRead);
            } else {
                SampleLoader.decodeLittleI16ToF32(audioBytes, 0, numBytesRead, data,
                        totalSamplesRead);
            }
        } else if (audioInputStream.getFormat().getSampleSizeInBits() == 24) {
            if (format.isBigEndian()) {
                SampleLoader.decodeBigI24ToF32(audioBytes, 0, numBytesRead, data,
                        totalSamplesRead);
            } else {
                SampleLoader.decodeLittleI24ToF32(audioBytes, 0, numBytesRead, data,
                        totalSamplesRead);
            }
        } else if (audioInputStream.getFormat().getSampleSizeInBits() == 32) {
            if (format.isBigEndian()) {
                SampleLoader.decodeBigI32ToF32(audioBytes, 0, numBytesRead, data,
                        totalSamplesRead);
            } else {
                SampleLoader.decodeLittleI32ToF32(audioBytes, 0, numBytesRead, data,
                        totalSamplesRead);
            }
        } else {
            throw new UnsupportedAudioFileException(
                    "Only 16, 24 or 32 bit PCM samples supported.");
        }

        // Calculate the number of frames actually read.
        numFramesRead = numBytesRead / bytesPerFrame;
        totalSamplesRead += numFramesRead * format.getChannels();
    }
    return data;
}
 
Example 18
Source File: AudioFloatConverter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static AudioFloatConverter getConverter(AudioFormat format) {
    AudioFloatConverter conv = null;
    if (format.getFrameSize() == 0)
        return null;
    if (format.getFrameSize() !=
            ((format.getSampleSizeInBits() + 7) / 8) * format.getChannels()) {
        return null;
    }
    if (format.getEncoding().equals(Encoding.PCM_SIGNED)) {
        if (format.isBigEndian()) {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8S();
            } else if (format.getSampleSizeInBits() > 8 &&
                  format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16SB();
            } else if (format.getSampleSizeInBits() > 16 &&
                  format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24SB();
            } else if (format.getSampleSizeInBits() > 24 &&
                  format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32SB();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xSB(((format
                        .getSampleSizeInBits() + 7) / 8) - 4);
            }
        } else {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8S();
            } else if (format.getSampleSizeInBits() > 8 &&
                     format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16SL();
            } else if (format.getSampleSizeInBits() > 16 &&
                     format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24SL();
            } else if (format.getSampleSizeInBits() > 24 &&
                     format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32SL();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xSL(((format
                        .getSampleSizeInBits() + 7) / 8) - 4);
            }
        }
    } else if (format.getEncoding().equals(Encoding.PCM_UNSIGNED)) {
        if (format.isBigEndian()) {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8U();
            } else if (format.getSampleSizeInBits() > 8 &&
                    format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16UB();
            } else if (format.getSampleSizeInBits() > 16 &&
                    format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24UB();
            } else if (format.getSampleSizeInBits() > 24 &&
                    format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32UB();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xUB(((
                        format.getSampleSizeInBits() + 7) / 8) - 4);
            }
        } else {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8U();
            } else if (format.getSampleSizeInBits() > 8 &&
                    format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16UL();
            } else if (format.getSampleSizeInBits() > 16 &&
                    format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24UL();
            } else if (format.getSampleSizeInBits() > 24 &&
                    format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32UL();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xUL(((
                        format.getSampleSizeInBits() + 7) / 8) - 4);
            }
        }
    } else if (format.getEncoding().equals(Encoding.PCM_FLOAT)) {
        if (format.getSampleSizeInBits() == 32) {
            if (format.isBigEndian())
                conv = new AudioFloatConversion32B();
            else
                conv = new AudioFloatConversion32L();
        } else if (format.getSampleSizeInBits() == 64) {
            if (format.isBigEndian())
                conv = new AudioFloatConversion64B();
            else
                conv = new AudioFloatConversion64L();
        }

    }

    if ((format.getEncoding().equals(Encoding.PCM_SIGNED) ||
            format.getEncoding().equals(Encoding.PCM_UNSIGNED)) &&
            (format.getSampleSizeInBits() % 8 != 0)) {
        conv = new AudioFloatLSBFilter(conv, format);
    }

    if (conv != null)
        conv.format = format;
    return conv;
}
 
Example 19
Source File: AudioFloatConverter.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static AudioFloatConverter getConverter(AudioFormat format) {
    AudioFloatConverter conv = null;
    if (format.getFrameSize() == 0)
        return null;
    if (format.getFrameSize() !=
            ((format.getSampleSizeInBits() + 7) / 8) * format.getChannels()) {
        return null;
    }
    if (format.getEncoding().equals(Encoding.PCM_SIGNED)) {
        if (format.isBigEndian()) {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8S();
            } else if (format.getSampleSizeInBits() > 8 &&
                  format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16SB();
            } else if (format.getSampleSizeInBits() > 16 &&
                  format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24SB();
            } else if (format.getSampleSizeInBits() > 24 &&
                  format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32SB();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xSB(((format
                        .getSampleSizeInBits() + 7) / 8) - 4);
            }
        } else {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8S();
            } else if (format.getSampleSizeInBits() > 8 &&
                     format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16SL();
            } else if (format.getSampleSizeInBits() > 16 &&
                     format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24SL();
            } else if (format.getSampleSizeInBits() > 24 &&
                     format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32SL();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xSL(((format
                        .getSampleSizeInBits() + 7) / 8) - 4);
            }
        }
    } else if (format.getEncoding().equals(Encoding.PCM_UNSIGNED)) {
        if (format.isBigEndian()) {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8U();
            } else if (format.getSampleSizeInBits() > 8 &&
                    format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16UB();
            } else if (format.getSampleSizeInBits() > 16 &&
                    format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24UB();
            } else if (format.getSampleSizeInBits() > 24 &&
                    format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32UB();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xUB(((
                        format.getSampleSizeInBits() + 7) / 8) - 4);
            }
        } else {
            if (format.getSampleSizeInBits() <= 8) {
                conv = new AudioFloatConversion8U();
            } else if (format.getSampleSizeInBits() > 8 &&
                    format.getSampleSizeInBits() <= 16) {
                conv = new AudioFloatConversion16UL();
            } else if (format.getSampleSizeInBits() > 16 &&
                    format.getSampleSizeInBits() <= 24) {
                conv = new AudioFloatConversion24UL();
            } else if (format.getSampleSizeInBits() > 24 &&
                    format.getSampleSizeInBits() <= 32) {
                conv = new AudioFloatConversion32UL();
            } else if (format.getSampleSizeInBits() > 32) {
                conv = new AudioFloatConversion32xUL(((
                        format.getSampleSizeInBits() + 7) / 8) - 4);
            }
        }
    } else if (format.getEncoding().equals(Encoding.PCM_FLOAT)) {
        if (format.getSampleSizeInBits() == 32) {
            if (format.isBigEndian())
                conv = new AudioFloatConversion32B();
            else
                conv = new AudioFloatConversion32L();
        } else if (format.getSampleSizeInBits() == 64) {
            if (format.isBigEndian())
                conv = new AudioFloatConversion64B();
            else
                conv = new AudioFloatConversion64L();
        }

    }

    if ((format.getEncoding().equals(Encoding.PCM_SIGNED) ||
            format.getEncoding().equals(Encoding.PCM_UNSIGNED)) &&
            (format.getSampleSizeInBits() % 8 != 0)) {
        conv = new AudioFloatLSBFilter(conv, format);
    }

    if (conv != null)
        conv.format = format;
    return conv;
}
 
Example 20
Source File: UlawCodec.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
    super(stream, outputFormat, AudioSystem.NOT_SPECIFIED);

    AudioFormat inputFormat = stream.getFormat();

    // throw an IllegalArgumentException if not ok
    if (!(isConversionSupported(outputFormat, inputFormat))) {
        throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
    }

    //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
    boolean PCMIsBigEndian;

    // determine whether we are encoding or decoding
    if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
        encode = false;
        encodeFormat = inputFormat;
        decodeFormat = outputFormat;
        PCMIsBigEndian = outputFormat.isBigEndian();
    } else {
        encode = true;
        encodeFormat = outputFormat;
        decodeFormat = inputFormat;
        PCMIsBigEndian = inputFormat.isBigEndian();
        tempBuffer = new byte[tempBufferSize];
    }

    // setup tables according to byte order
    if (PCMIsBigEndian) {
        tabByte1 = ULAW_TABH;
        tabByte2 = ULAW_TABL;
        highByte = 0;
        lowByte  = 1;
    } else {
        tabByte1 = ULAW_TABL;
        tabByte2 = ULAW_TABH;
        highByte = 1;
        lowByte  = 0;
    }

    // set the AudioInputStream length in frames if we know it
    if (stream instanceof AudioInputStream) {
        frameLength = ((AudioInputStream)stream).getFrameLength();
    }
    // set framePos to zero
    framePos = 0;
    frameSize = inputFormat.getFrameSize();
    if (frameSize == AudioSystem.NOT_SPECIFIED) {
        frameSize = 1;
    }
}