Search in sources :

Example 1 with Encoding

use of javax.sound.sampled.AudioFormat.Encoding in project jdk8u_jdk by JetBrains.

the class AudioFloatFormatConverter method getAudioInputStream.

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);
}
Also used : Encoding(javax.sound.sampled.AudioFormat.Encoding) AudioFormat(javax.sound.sampled.AudioFormat)

Example 2 with Encoding

use of javax.sound.sampled.AudioFormat.Encoding in project jdk8u_jdk by JetBrains.

the class DLSSoundbank method writeSample.

private void writeSample(RIFFWriter writer, DLSSample sample) throws IOException {
    AudioFormat audioformat = sample.getFormat();
    Encoding encoding = audioformat.getEncoding();
    float sampleRate = audioformat.getSampleRate();
    int sampleSizeInBits = audioformat.getSampleSizeInBits();
    int channels = audioformat.getChannels();
    int frameSize = audioformat.getFrameSize();
    float frameRate = audioformat.getFrameRate();
    boolean bigEndian = audioformat.isBigEndian();
    boolean convert_needed = false;
    if (audioformat.getSampleSizeInBits() == 8) {
        if (!encoding.equals(Encoding.PCM_UNSIGNED)) {
            encoding = Encoding.PCM_UNSIGNED;
            convert_needed = true;
        }
    } else {
        if (!encoding.equals(Encoding.PCM_SIGNED)) {
            encoding = Encoding.PCM_SIGNED;
            convert_needed = true;
        }
        if (bigEndian) {
            bigEndian = false;
            convert_needed = true;
        }
    }
    if (convert_needed) {
        audioformat = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, frameRate, bigEndian);
    }
    // fmt
    RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
    int sampleformat = 0;
    if (audioformat.getEncoding().equals(Encoding.PCM_UNSIGNED))
        sampleformat = 1;
    else if (audioformat.getEncoding().equals(Encoding.PCM_SIGNED))
        sampleformat = 1;
    else if (audioformat.getEncoding().equals(Encoding.PCM_FLOAT))
        sampleformat = 3;
    fmt_chunk.writeUnsignedShort(sampleformat);
    fmt_chunk.writeUnsignedShort(audioformat.getChannels());
    fmt_chunk.writeUnsignedInt((long) audioformat.getSampleRate());
    long srate = ((long) audioformat.getFrameRate()) * audioformat.getFrameSize();
    fmt_chunk.writeUnsignedInt(srate);
    fmt_chunk.writeUnsignedShort(audioformat.getFrameSize());
    fmt_chunk.writeUnsignedShort(audioformat.getSampleSizeInBits());
    fmt_chunk.write(0);
    fmt_chunk.write(0);
    writeSampleOptions(writer.writeChunk("wsmp"), sample.sampleoptions);
    if (convert_needed) {
        RIFFWriter data_chunk = writer.writeChunk("data");
        AudioInputStream stream = AudioSystem.getAudioInputStream(audioformat, (AudioInputStream) sample.getData());
        byte[] buff = new byte[1024];
        int ret;
        while ((ret = stream.read(buff)) != -1) {
            data_chunk.write(buff, 0, ret);
        }
    } else {
        RIFFWriter data_chunk = writer.writeChunk("data");
        ModelByteBuffer databuff = sample.getDataBuffer();
        databuff.writeTo(data_chunk);
    /*
            data_chunk.write(databuff.array(),
            databuff.arrayOffset(),
            databuff.capacity());
             */
    }
    writeInfo(writer.writeList("INFO"), sample.info);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Encoding(javax.sound.sampled.AudioFormat.Encoding) AudioFormat(javax.sound.sampled.AudioFormat)

Example 3 with Encoding

use of javax.sound.sampled.AudioFormat.Encoding in project jdk8u_jdk by JetBrains.

the class PCM_FLOAT_support method test.

static boolean test(Encoding enc) {
    out("conversion " + enc + " -> PCM_FLOAT:");
    Encoding[] encodings = AudioSystem.getTargetEncodings(enc);
    for (Encoding e : encodings) {
        if (e.equals(pcmFloatEnc)) {
            out("  - OK");
            return true;
        }
    }
    out("  - FAILED (not supported)");
    testFailed = true;
    return false;
}
Also used : Encoding(javax.sound.sampled.AudioFormat.Encoding)

Example 4 with Encoding

use of javax.sound.sampled.AudioFormat.Encoding in project jdk8u_jdk by JetBrains.

the class PCM_FLOAT_support method main.

public static void main(String[] args) throws Exception {
    // 1st checks Encoding.PCM_FLOAT is available
    pcmFloatEnc = Encoding.PCM_FLOAT;
    Encoding[] encodings = AudioSystem.getTargetEncodings(pcmFloatEnc);
    out("conversion from PCM_FLOAT to " + encodings.length + " encodings:");
    for (Encoding e : encodings) {
        out("  - " + e);
    }
    if (encodings.length == 0) {
        testFailed = true;
    }
    test(Encoding.PCM_SIGNED);
    test(Encoding.PCM_UNSIGNED);
    if (testFailed) {
        throw new Exception("test failed");
    }
    out("test passed.");
}
Also used : Encoding(javax.sound.sampled.AudioFormat.Encoding)

Aggregations

Encoding (javax.sound.sampled.AudioFormat.Encoding)4 AudioFormat (javax.sound.sampled.AudioFormat)2 AudioInputStream (javax.sound.sampled.AudioInputStream)1