javax.sound.sampled.AudioFormat.Encoding Java Examples

The following examples show how to use javax.sound.sampled.AudioFormat.Encoding. 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: AiffData.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Convert the audio bytes into the stream
 * 
 * @param format The audio format being decoded
 * @param audio_bytes The audio byts
 * @param two_bytes_data True if we using double byte data
 * @return The byte bufer of data
 */
private static ByteBuffer convertAudioBytes(AudioFormat format, byte[] audio_bytes, boolean two_bytes_data) {
	ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
	dest.order(ByteOrder.nativeOrder());
	ByteBuffer src = ByteBuffer.wrap(audio_bytes);
	src.order(ByteOrder.BIG_ENDIAN);
	if (two_bytes_data) {
		ShortBuffer dest_short = dest.asShortBuffer();
		ShortBuffer src_short = src.asShortBuffer();
		while (src_short.hasRemaining())
			dest_short.put(src_short.get());
	} else {
		while (src.hasRemaining()) {
			byte b = src.get();
			if (format.getEncoding() == Encoding.PCM_SIGNED) {
				b = (byte) (b + 127);
			}
			dest.put(b);
		}
	}
	dest.rewind();
	return dest;
}
 
Example #2
Source File: AudioFloatFormatConverter.java    From openjdk-jdk8u-backup 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 #3
Source File: TGMixer.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TGMixer() {
	super(new Line.Info(Mixer.class));
	
	this.lines = new HashMap<Class<?>, Line>();
	this.sourceLineInfo = new ArrayList<DataLine.Info>();
	this.targetLineInfo = new ArrayList<DataLine.Info>();
	
	List<AudioFormat> formats = new ArrayList<AudioFormat>();
	for (int channels = 1; channels <= 2; channels++) {
		formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false)); 
		formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false));
			for (int bits = 16; bits < 32; bits += 8) {
				formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false));
				formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false));
				formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true));
				formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true));
			}
			formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, false));
			formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, true));
			formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, false));
			formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, true));
		}
		
		this.sourceLineInfo.add(new DataLine.Info(SourceDataLine.class, formats.toArray(new AudioFormat[formats.size()]), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED));
}
 
Example #4
Source File: PCM_FLOAT_support.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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.");
}
 
Example #5
Source File: PCM_FLOAT_support.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
}
 
Example #6
Source File: StretchedAudioInputStream.java    From pumpernickel with MIT License 6 votes vote down vote up
/**
 * Create a StretchedAudioInputStream that distorts the incoming audio so it
 * matches a fixed number of frames.
 * 
 * @param in
 *            the AudioInputStream to stretch.
 * @param frames
 *            the number of frames the input stream should be stretched to.
 * @throws IOException
 *             if an IO problem occurs.
 */
public static StretchedAudioInputStream create(AudioInputStream in,
		long frames) throws IOException {
	AudioFormat format = in.getFormat();
	if (!(format.getEncoding().equals(Encoding.PCM_SIGNED) || format
			.getEncoding().equals(Encoding.PCM_UNSIGNED)))
		throw new IllegalArgumentException(
				"the audio input must be PCM-encoded data (found "
						+ format.getEncoding() + ")");

	PipedInputStream pipedIn = new PipedInputStream();
	PipedOutputStream pipedOut = new PipedOutputStream(pipedIn);

	/**
	 * One flaw with this model is that we always generate ALL the
	 * transformed data: even if the entity working with pipedIn is trying
	 * to skip large chunks of data.
	 */

	Thread thread = new StretchThread(in, format, frames, pipedOut);
	thread.start();
	return new StretchedAudioInputStream(pipedIn, format, frames);
}
 
Example #7
Source File: PCM_FLOAT_support.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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.");
}
 
Example #8
Source File: Bits16ToFromFloatArray.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void test(final Encoding enc, final byte[] expected,
                         boolean end) {
    System.err.println("enc = " + enc);
    AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,
                                     end);
    byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];
    AudioFloatConverter conv = AudioFloatConverter.getConverter(af);

    conv.toByteArray(FLOATS, bytes);

    if (!Arrays.equals(bytes, expected)) {
        System.err.println("Actual: " + Arrays.toString(bytes));
        System.err.println("Expected: " + Arrays.toString(expected));
        throw new RuntimeException();
    }

    float[] floats = new float[bytes.length / af.getFrameSize()];
    conv.toFloatArray(bytes, floats);

    if (!Arrays.equals(floats, FLOATS)) {
        System.err.println("Actual: " + Arrays.toString(floats));
        System.err.println("Expected: " + Arrays.toString(FLOATS));
        throw new RuntimeException();
    }
}
 
Example #9
Source File: AudioFloatFormatConverter.java    From hottub 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 #10
Source File: PCM_FLOAT_support.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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.");
}
 
Example #11
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 #12
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 #13
Source File: Foo.java    From coming with MIT License 5 votes vote down vote up
public AudioFormat colorized(Book b1, Book b2, int random) throws Exception, IllegalArgumentException {
    LinkedList<Point2D> points = new LinkedList<Point2D>();
    points.add(new Point(10, 20));
    points.add(new Point(20, 20));
    points.add(new Point(30, 20));

    if(random > 100) {
        Bar barInstance = new Bar();
        barInstance.returnField(new JLabel("I am a JLabel object"));

        if(random < 150) {
            for(int i = 0 ; i < 10 ; ++i) {
                JButton button = new JButton("I am a JButton object");
            }

            barInstance = new Bar(new JTextArea());

            return new AudioFormat(Encoding.ALAW, (float)1.0, 8, 2, 1, (float)1.0, true, new HashMap<String, Object>());
        }
        else {
            throw new Exception("a test exception");
        }
    }
    else {
        throw new IllegalArgumentException("a test illegal format exception");
    }
}
 
Example #14
Source File: AudioFloatFormatConverter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean isConversionSupported(Encoding targetEncoding,
        AudioFormat sourceFormat) {
    if (AudioFloatConverter.getConverter(sourceFormat) == null)
        return false;
    for (int i = 0; i < formats.length; i++) {
        if (targetEncoding.equals(formats[i]))
            return true;
    }
    return false;
}
 
Example #15
Source File: AudioFloatFormatConverter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean isConversionSupported(Encoding targetEncoding,
        AudioFormat sourceFormat) {
    if (AudioFloatConverter.getConverter(sourceFormat) == null)
        return false;
    for (int i = 0; i < formats.length; i++) {
        if (targetEncoding.equals(formats[i]))
            return true;
    }
    return false;
}
 
Example #16
Source File: WaveFloatFileWriter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #17
Source File: PCM_FLOAT_support.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #18
Source File: ExpectedNPEOnNull.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public AudioInputStream getAudioInputStream(Encoding encoding,
                                            AudioInputStream stream) {
    Objects.requireNonNull(encoding);
    Objects.requireNonNull(stream);
    return null;
}
 
Example #19
Source File: AudioFloatFormatConverter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isConversionSupported(Encoding targetEncoding,
                                     AudioFormat sourceFormat) {
    Objects.requireNonNull(targetEncoding);
    if (AudioFloatConverter.getConverter(sourceFormat) == null)
        return false;
    for (int i = 0; i < formats.length; i++) {
        if (targetEncoding.equals(formats[i]))
            return true;
    }
    return false;
}
 
Example #20
Source File: AudioFloatFormatConverter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean isConversionSupported(Encoding targetEncoding,
        AudioFormat sourceFormat) {
    if (AudioFloatConverter.getConverter(sourceFormat) == null)
        return false;
    for (int i = 0; i < formats.length; i++) {
        if (targetEncoding.equals(formats[i]))
            return true;
    }
    return false;
}
 
Example #21
Source File: WaveFloatFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #22
Source File: PCM_FLOAT_support.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: PCM_FLOAT_support.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #24
Source File: AudioFloatFormatConverter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isConversionSupported(Encoding targetEncoding,
                                     AudioFormat sourceFormat) {
    Objects.requireNonNull(targetEncoding);
    if (AudioFloatConverter.getConverter(sourceFormat) == null)
        return false;
    for (int i = 0; i < formats.length; i++) {
        if (targetEncoding.equals(formats[i]))
            return true;
    }
    return false;
}
 
Example #25
Source File: PCM_FLOAT_support.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #26
Source File: WavPCMAudioInputStreamFilter.java    From pumpernickel with MIT License 5 votes vote down vote up
private static Set<Conversion> getRequiredConversions(AudioFormat format) {
	Set<Conversion> set = new HashSet<Conversion>();
	if (format.isBigEndian())
		set.add(Conversion.TO_LITTLE_ENDIAN);
	if (format.getEncoding().equals(Encoding.PCM_SIGNED)
			&& format.getSampleSizeInBits() == 8)
		set.add(Conversion.TO_UNSIGNED);
	if (format.getEncoding().equals(Encoding.PCM_UNSIGNED)
			&& format.getSampleSizeInBits() == 16)
		set.add(Conversion.TO_SIGNED);
	return set;
}
 
Example #27
Source File: AudioFloatFormatConverter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Encoding[] getTargetEncodings() {
    return new Encoding[] { Encoding.PCM_SIGNED, Encoding.PCM_UNSIGNED,
            Encoding.PCM_FLOAT };
}
 
Example #28
Source File: UlawCodec.java    From openjdk-jdk9 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 = stream.getFrameLength();
    }
    // set framePos to zero
    framePos = 0;
    frameSize = inputFormat.getFrameSize();
    if (frameSize == AudioSystem.NOT_SPECIFIED) {
        frameSize = 1;
    }
}
 
Example #29
Source File: PCMtoPCMCodec.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public AudioFormat.Encoding[] getSourceEncodings() {
    return new Encoding[]{Encoding.PCM_SIGNED, Encoding.PCM_UNSIGNED};
}
 
Example #30
Source File: UlawCodec.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public AudioFormat.Encoding[] getSourceEncodings() {
    return new Encoding[]{Encoding.ULAW, Encoding.PCM_SIGNED};
}