829N/A/*
6321N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/Aimport java.io.BufferedInputStream;
829N/Aimport java.io.File;
829N/Aimport java.io.FileInputStream;
829N/Aimport java.io.IOException;
829N/Aimport java.io.InputStream;
829N/Aimport java.net.URL;
829N/Aimport java.util.HashMap;
829N/Aimport java.util.Map;
829N/A
829N/Aimport javax.sound.sampled.AudioFileFormat;
829N/Aimport javax.sound.sampled.AudioFormat;
829N/Aimport javax.sound.sampled.AudioInputStream;
829N/Aimport javax.sound.sampled.AudioSystem;
829N/Aimport javax.sound.sampled.UnsupportedAudioFileException;
829N/Aimport javax.sound.sampled.AudioFormat.Encoding;
829N/Aimport javax.sound.sampled.spi.AudioFileReader;
829N/A
829N/A/**
829N/A * WAVE file reader for files using format WAVE_FORMAT_EXTENSIBLE (0xFFFE).
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class WaveExtensibleFileReader extends AudioFileReader {
829N/A
829N/A static private class GUID {
829N/A long i1;
829N/A
829N/A int s1;
829N/A
829N/A int s2;
829N/A
829N/A int x1;
829N/A
829N/A int x2;
829N/A
829N/A int x3;
829N/A
829N/A int x4;
829N/A
829N/A int x5;
829N/A
829N/A int x6;
829N/A
829N/A int x7;
829N/A
829N/A int x8;
829N/A
829N/A private GUID() {
829N/A }
829N/A
6321N/A GUID(long i1, int s1, int s2, int x1, int x2, int x3, int x4,
829N/A int x5, int x6, int x7, int x8) {
829N/A this.i1 = i1;
829N/A this.s1 = s1;
829N/A this.s2 = s2;
829N/A this.x1 = x1;
829N/A this.x2 = x2;
829N/A this.x3 = x3;
829N/A this.x4 = x4;
829N/A this.x5 = x5;
829N/A this.x6 = x6;
829N/A this.x7 = x7;
829N/A this.x8 = x8;
829N/A }
829N/A
829N/A public static GUID read(RIFFReader riff) throws IOException {
829N/A GUID d = new GUID();
829N/A d.i1 = riff.readUnsignedInt();
829N/A d.s1 = riff.readUnsignedShort();
829N/A d.s2 = riff.readUnsignedShort();
829N/A d.x1 = riff.readUnsignedByte();
829N/A d.x2 = riff.readUnsignedByte();
829N/A d.x3 = riff.readUnsignedByte();
829N/A d.x4 = riff.readUnsignedByte();
829N/A d.x5 = riff.readUnsignedByte();
829N/A d.x6 = riff.readUnsignedByte();
829N/A d.x7 = riff.readUnsignedByte();
829N/A d.x8 = riff.readUnsignedByte();
829N/A return d;
829N/A }
829N/A
829N/A public int hashCode() {
829N/A return (int) i1;
829N/A }
829N/A
829N/A public boolean equals(Object obj) {
829N/A if (!(obj instanceof GUID))
829N/A return false;
829N/A GUID t = (GUID) obj;
829N/A if (i1 != t.i1)
829N/A return false;
829N/A if (s1 != t.s1)
829N/A return false;
829N/A if (s2 != t.s2)
829N/A return false;
829N/A if (x1 != t.x1)
829N/A return false;
829N/A if (x2 != t.x2)
829N/A return false;
829N/A if (x3 != t.x3)
829N/A return false;
829N/A if (x4 != t.x4)
829N/A return false;
829N/A if (x5 != t.x5)
829N/A return false;
829N/A if (x6 != t.x6)
829N/A return false;
829N/A if (x7 != t.x7)
829N/A return false;
829N/A if (x8 != t.x8)
829N/A return false;
829N/A return true;
829N/A }
829N/A
829N/A }
829N/A
6321N/A private static final String[] channelnames = { "FL", "FR", "FC", "LF",
829N/A "BL",
829N/A "BR", // 5.1
829N/A "FLC", "FLR", "BC", "SL", "SR", "TC", "TFL", "TFC", "TFR", "TBL",
829N/A "TBC", "TBR" };
829N/A
6321N/A private static final String[] allchannelnames = { "w1", "w2", "w3", "w4", "w5",
829N/A "w6", "w7", "w8", "w9", "w10", "w11", "w12", "w13", "w14", "w15",
829N/A "w16", "w17", "w18", "w19", "w20", "w21", "w22", "w23", "w24",
829N/A "w25", "w26", "w27", "w28", "w29", "w30", "w31", "w32", "w33",
829N/A "w34", "w35", "w36", "w37", "w38", "w39", "w40", "w41", "w42",
829N/A "w43", "w44", "w45", "w46", "w47", "w48", "w49", "w50", "w51",
829N/A "w52", "w53", "w54", "w55", "w56", "w57", "w58", "w59", "w60",
829N/A "w61", "w62", "w63", "w64" };
829N/A
6321N/A private static final GUID SUBTYPE_PCM = new GUID(0x00000001, 0x0000, 0x0010,
829N/A 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
829N/A
6321N/A private static final GUID SUBTYPE_IEEE_FLOAT = new GUID(0x00000003, 0x0000,
829N/A 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
829N/A
829N/A private String decodeChannelMask(long channelmask) {
829N/A StringBuffer sb = new StringBuffer();
829N/A long m = 1;
829N/A for (int i = 0; i < allchannelnames.length; i++) {
829N/A if ((channelmask & m) != 0L) {
829N/A if (i < channelnames.length) {
829N/A sb.append(channelnames[i] + " ");
829N/A } else {
829N/A sb.append(allchannelnames[i] + " ");
829N/A }
829N/A }
829N/A m *= 2L;
829N/A }
829N/A if (sb.length() == 0)
829N/A return null;
829N/A return sb.substring(0, sb.length() - 1);
829N/A
829N/A }
829N/A
829N/A public AudioFileFormat getAudioFileFormat(InputStream stream)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A
829N/A stream.mark(200);
829N/A AudioFileFormat format;
829N/A try {
829N/A format = internal_getAudioFileFormat(stream);
829N/A } finally {
829N/A stream.reset();
829N/A }
829N/A return format;
829N/A }
829N/A
829N/A private AudioFileFormat internal_getAudioFileFormat(InputStream stream)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A
829N/A RIFFReader riffiterator = new RIFFReader(stream);
829N/A if (!riffiterator.getFormat().equals("RIFF"))
829N/A throw new UnsupportedAudioFileException();
829N/A if (!riffiterator.getType().equals("WAVE"))
829N/A throw new UnsupportedAudioFileException();
829N/A
829N/A boolean fmt_found = false;
829N/A boolean data_found = false;
829N/A
829N/A int channels = 1;
829N/A long samplerate = 1;
829N/A // long framerate = 1;
829N/A int framesize = 1;
829N/A int bits = 1;
829N/A int validBitsPerSample = 1;
829N/A long channelMask = 0;
829N/A GUID subFormat = null;
829N/A
829N/A while (riffiterator.hasNextChunk()) {
829N/A RIFFReader chunk = riffiterator.nextChunk();
829N/A
829N/A if (chunk.getFormat().equals("fmt ")) {
829N/A fmt_found = true;
829N/A
829N/A int format = chunk.readUnsignedShort();
829N/A if (format != 0xFFFE)
829N/A throw new UnsupportedAudioFileException(); // WAVE_FORMAT_EXTENSIBLE
829N/A // only
829N/A channels = chunk.readUnsignedShort();
829N/A samplerate = chunk.readUnsignedInt();
829N/A /* framerate = */chunk.readUnsignedInt();
829N/A framesize = chunk.readUnsignedShort();
829N/A bits = chunk.readUnsignedShort();
829N/A int cbSize = chunk.readUnsignedShort();
829N/A if (cbSize != 22)
829N/A throw new UnsupportedAudioFileException();
829N/A validBitsPerSample = chunk.readUnsignedShort();
829N/A if (validBitsPerSample > bits)
829N/A throw new UnsupportedAudioFileException();
829N/A channelMask = chunk.readUnsignedInt();
829N/A subFormat = GUID.read(chunk);
829N/A
829N/A }
829N/A if (chunk.getFormat().equals("data")) {
829N/A data_found = true;
829N/A break;
829N/A }
829N/A }
829N/A
829N/A if (!fmt_found)
829N/A throw new UnsupportedAudioFileException();
829N/A if (!data_found)
829N/A throw new UnsupportedAudioFileException();
829N/A
829N/A Map<String, Object> p = new HashMap<String, Object>();
829N/A String s_channelmask = decodeChannelMask(channelMask);
829N/A if (s_channelmask != null)
829N/A p.put("channelOrder", s_channelmask);
829N/A if (channelMask != 0)
829N/A p.put("channelMask", channelMask);
829N/A // validBitsPerSample is only informational for PCM data,
829N/A // data is still encode according to SampleSizeInBits.
829N/A p.put("validBitsPerSample", validBitsPerSample);
829N/A
829N/A AudioFormat audioformat = null;
829N/A if (subFormat.equals(SUBTYPE_PCM)) {
829N/A if (bits == 8) {
829N/A audioformat = new AudioFormat(Encoding.PCM_UNSIGNED,
829N/A samplerate, bits, channels, framesize, samplerate,
829N/A false, p);
829N/A } else {
829N/A audioformat = new AudioFormat(Encoding.PCM_SIGNED, samplerate,
829N/A bits, channels, framesize, samplerate, false, p);
829N/A }
829N/A } else if (subFormat.equals(SUBTYPE_IEEE_FLOAT)) {
2716N/A audioformat = new AudioFormat(Encoding.PCM_FLOAT,
829N/A samplerate, bits, channels, framesize, samplerate, false, p);
829N/A } else
829N/A throw new UnsupportedAudioFileException();
829N/A
829N/A AudioFileFormat fileformat = new AudioFileFormat(
829N/A AudioFileFormat.Type.WAVE, audioformat,
829N/A AudioSystem.NOT_SPECIFIED);
829N/A return fileformat;
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(InputStream stream)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A
829N/A AudioFileFormat format = getAudioFileFormat(stream);
829N/A RIFFReader riffiterator = new RIFFReader(stream);
829N/A if (!riffiterator.getFormat().equals("RIFF"))
829N/A throw new UnsupportedAudioFileException();
829N/A if (!riffiterator.getType().equals("WAVE"))
829N/A throw new UnsupportedAudioFileException();
829N/A while (riffiterator.hasNextChunk()) {
829N/A RIFFReader chunk = riffiterator.nextChunk();
829N/A if (chunk.getFormat().equals("data")) {
829N/A return new AudioInputStream(chunk, format.getFormat(), chunk
829N/A .getSize());
829N/A }
829N/A }
829N/A throw new UnsupportedAudioFileException();
829N/A }
829N/A
829N/A public AudioFileFormat getAudioFileFormat(URL url)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A InputStream stream = url.openStream();
829N/A AudioFileFormat format;
829N/A try {
829N/A format = getAudioFileFormat(new BufferedInputStream(stream));
829N/A } finally {
829N/A stream.close();
829N/A }
829N/A return format;
829N/A }
829N/A
829N/A public AudioFileFormat getAudioFileFormat(File file)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A InputStream stream = new FileInputStream(file);
829N/A AudioFileFormat format;
829N/A try {
829N/A format = getAudioFileFormat(new BufferedInputStream(stream));
829N/A } finally {
829N/A stream.close();
829N/A }
829N/A return format;
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(URL url)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A return getAudioInputStream(new BufferedInputStream(url.openStream()));
829N/A }
829N/A
829N/A public AudioInputStream getAudioInputStream(File file)
829N/A throws UnsupportedAudioFileException, IOException {
829N/A return getAudioInputStream(new BufferedInputStream(new FileInputStream(
829N/A file)));
829N/A }
829N/A
829N/A}