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

The following examples show how to use javax.sound.sampled.AudioFormat#getFrameSize() . 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: Toolkit.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static boolean isFullySpecifiedPCMFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
        return false;
    }
    if ((format.getFrameRate() <= 0)
        || (format.getSampleRate() <= 0)
        || (format.getSampleSizeInBits() <= 0)
        || (format.getFrameSize() <= 0)
        || (format.getChannels() <= 0)) {
        return false;
    }
    return true;
}
 
Example 2
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 3
Source File: Toolkit.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static boolean isFullySpecifiedPCMFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
        return false;
    }
    if ((format.getFrameRate() <= 0)
        || (format.getSampleRate() <= 0)
        || (format.getSampleSizeInBits() <= 0)
        || (format.getFrameSize() <= 0)
        || (format.getChannels() <= 0)) {
        return false;
    }
    return true;
}
 
Example 4
Source File: AuFileWriter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private InputStream getFileStream(AuFileFormat auFileFormat, AudioInputStream audioStream) throws IOException {

        // private method ... assumes auFileFormat is a supported file type

        AudioFormat format            = auFileFormat.getFormat();

        int headerSize     = AuFileFormat.AU_HEADERSIZE;
        long dataSize       = auFileFormat.getFrameLength();
        //$$fb fix for Bug 4351296
        //int dataSizeInBytes = dataSize * format.getFrameSize();
        long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();
        if (dataSizeInBytes>0x7FFFFFFFl) {
            dataSizeInBytes=UNKNOWN_SIZE;
        }
        int auType = auFileFormat.getAuType();
        int sampleRate     = (int)format.getSampleRate();
        int channels       = format.getChannels();

        // if we need to do any format conversion, we do it here.
        //$$ fb 2001-07-13: Bug 4391108
        audioStream = AudioSystem.getAudioInputStream(format, audioStream);

        final byte[] header;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             DataOutputStream dos = new DataOutputStream(baos)) {
            dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
            dos.writeInt(headerSize);
            dos.writeInt((int) dataSizeInBytes);
            dos.writeInt(auType);
            dos.writeInt(sampleRate);
            dos.writeInt(channels);
            header = baos.toByteArray();
        }
        // Now create a new InputStream from headerStream and the InputStream
        // in audioStream
        return new SequenceInputStream(new ByteArrayInputStream(header),
                                       new NoCloseInputStream(audioStream));
    }
 
Example 5
Source File: Toolkit.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static boolean isFullySpecifiedPCMFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
        return false;
    }
    if ((format.getFrameRate() <= 0)
        || (format.getSampleRate() <= 0)
        || (format.getSampleSizeInBits() <= 0)
        || (format.getFrameSize() <= 0)
        || (format.getChannels() <= 0)) {
        return false;
    }
    return true;
}
 
Example 6
Source File: WaveFloatFileWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private AudioInputStream toLittleEndian(AudioInputStream ais) {
    AudioFormat format = ais.getFormat();
    AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(), format
            .getChannels(), format.getFrameSize(), format.getFrameRate(),
            false);
    return AudioSystem.getAudioInputStream(targetFormat, ais);
}
 
Example 7
Source File: AudioFloatFormatConverter.java    From jdk8u-dev-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 8
Source File: SoftMixingSourceDataLine.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public void open(AudioFormat format) throws LineUnavailableException {
    if (bufferSize == -1)
        bufferSize = ((int) (format.getFrameRate() / 2))
                * format.getFrameSize();
    open(format, bufferSize);
}
 
Example 9
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 10
Source File: AudioFloatInputStream.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static AudioFloatInputStream getInputStream(AudioFormat format,
        byte[] buffer, int offset, int len) {
    AudioFloatConverter converter = AudioFloatConverter
            .getConverter(format);
    if (converter != null)
        return new BytaArrayAudioFloatInputStream(converter, buffer,
                offset, len);

    InputStream stream = new ByteArrayInputStream(buffer, offset, len);
    long aLen = format.getFrameSize() == AudioSystem.NOT_SPECIFIED
            ? AudioSystem.NOT_SPECIFIED : len / format.getFrameSize();
    AudioInputStream astream = new AudioInputStream(stream, format, aLen);
    return getInputStream(astream);
}
 
Example 11
Source File: DummySourceDataLine.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void open(AudioFormat format, int bufferSize)
        throws LineUnavailableException {
    this.format = format;
    this.bufferSize = bufferSize;
    this.framesize = format.getFrameSize();
    opened = true;
}
 
Example 12
Source File: AudioFloatFormatConverter.java    From hottub 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 13
Source File: DummySourceDataLine.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void open(AudioFormat format) throws LineUnavailableException {
    if (bufferSize == -1)
        bufferSize = ((int) (format.getFrameRate() / 2))
                * format.getFrameSize();
    open(format, bufferSize);
}
 
Example 14
Source File: Toolkit.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static long millis2bytes(AudioFormat format, long millis) {
    long result = (long) (millis * format.getFrameRate() / 1000.0f * format.getFrameSize());
    return align(result, format.getFrameSize());
}
 
Example 15
Source File: SoftMixingSourceDataLine.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void open(AudioFormat format) throws LineUnavailableException {
    if (bufferSize == -1)
        bufferSize = ((int) (format.getFrameRate() / 2))
                * format.getFrameSize();
    open(format, bufferSize);
}
 
Example 16
Source File: WaveFloatFileReader.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
        throws UnsupportedAudioFileException, IOException {

    RIFFReader riffiterator = new RIFFReader(stream);
    if (!riffiterator.getFormat().equals("RIFF"))
        throw new UnsupportedAudioFileException();
    if (!riffiterator.getType().equals("WAVE"))
        throw new UnsupportedAudioFileException();

    boolean fmt_found = false;
    boolean data_found = false;

    int channels = 1;
    long samplerate = 1;
    int framesize = 1;
    int bits = 1;
    long dataSize = 0;

    while (riffiterator.hasNextChunk()) {
        RIFFReader chunk = riffiterator.nextChunk();
        if (chunk.getFormat().equals("fmt ")) {
            fmt_found = true;

            int format = chunk.readUnsignedShort();
            if (format != WaveFileFormat.WAVE_FORMAT_IEEE_FLOAT) {
                throw new UnsupportedAudioFileException();
            }
            channels = chunk.readUnsignedShort();
            samplerate = chunk.readUnsignedInt();
            /* framerate = */chunk.readUnsignedInt();
            framesize = chunk.readUnsignedShort();
            bits = chunk.readUnsignedShort();
        }
        if (chunk.getFormat().equals("data")) {
            dataSize = chunk.getSize();
            data_found = true;
            break;
        }
    }
    if (!fmt_found || !data_found) {
        throw new UnsupportedAudioFileException();
    }
    AudioFormat audioformat = new AudioFormat(
            Encoding.PCM_FLOAT, samplerate, bits, channels,
            framesize, samplerate, false);
    return new StandardFileFormat(AudioFileFormat.Type.WAVE, audioformat,
                                  dataSize / audioformat.getFrameSize());
}
 
Example 17
Source File: Toolkit.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static long micros2bytes(AudioFormat format, long micros) {
    long result = (long) (micros * format.getFrameRate() / 1000000.0f * format.getFrameSize());
    return align(result, format.getFrameSize());
}
 
Example 18
Source File: SoftMixingClip.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void open(AudioFormat format, byte[] data, int offset, int bufferSize)
        throws LineUnavailableException {
    synchronized (control_mutex) {
        if (isOpen()) {
            throw new IllegalStateException(
                    "Clip is already open with format " + getFormat()
                            + " and frame lengh of " + getFrameLength());
        }
        if (AudioFloatConverter.getConverter(format) == null)
            throw new IllegalArgumentException("Invalid format : "
                    + format.toString());
        if (bufferSize % format.getFrameSize() != 0)
            throw new IllegalArgumentException(
                    "Buffer size does not represent an integral number of sample frames!");

        if (data != null) {
            this.data = Arrays.copyOf(data, data.length);
        }
        this.offset = offset;
        this.bufferSize = bufferSize;
        this.format = format;
        this.framesize = format.getFrameSize();

        loopstart = 0;
        loopend = -1;
        loop_sg = true;

        if (!mixer.isOpen()) {
            mixer.open();
            mixer.implicitOpen = true;
        }

        outputformat = mixer.getFormat();
        out_nrofchannels = outputformat.getChannels();
        in_nrofchannels = format.getChannels();

        open = true;

        mixer.getMainMixer().openLine(this);
    }

}
 
Example 19
Source File: EmergencySoundbank.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static public byte[] toBytes(float[] in, AudioFormat format) {
    byte[] out = new byte[in.length * format.getFrameSize()];
    return AudioFloatConverter.getConverter(format).toByteArray(in, out);
}
 
Example 20
Source File: UlawCodec.java    From TencentKona-8 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;
    }
}