8147407: Provide support of WaveExtensible sound format
authorserb
Thu, 14 Jan 2016 23:14:01 +0300
changeset 35687 78e8e8fea09b
parent 35686 1c21a27682a5
child 35688 744b6cf60397
8147407: Provide support of WaveExtensible sound format Reviewed-by: prr, amenkov
jdk/src/java.desktop/share/classes/META-INF/services/javax.sound.sampled.spi.AudioFileReader
jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveExtensibleFileReader.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileFormat.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileReader.java
jdk/test/javax/sound/sampled/spi/AudioFileReader/RecognizeWaveExtensible.java
--- a/jdk/src/java.desktop/share/classes/META-INF/services/javax.sound.sampled.spi.AudioFileReader	Thu Jan 07 23:50:00 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/META-INF/services/javax.sound.sampled.spi.AudioFileReader	Thu Jan 14 23:14:01 2016 +0300
@@ -3,4 +3,5 @@
 com.sun.media.sound.AiffFileReader
 com.sun.media.sound.WaveFileReader
 com.sun.media.sound.WaveFloatFileReader
+com.sun.media.sound.WaveExtensibleFileReader
 com.sun.media.sound.SoftMidiAudioFileReader
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveExtensibleFileReader.java	Thu Jan 07 23:50:00 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveExtensibleFileReader.java	Thu Jan 14 23:14:01 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -124,7 +124,6 @@
                 return false;
             return true;
         }
-
     }
 
     private static final String[] channelnames = { "FL", "FR", "FC", "LF",
@@ -196,9 +195,9 @@
                 fmt_found = true;
 
                 int format = chunk.readUnsignedShort();
-                if (format != 0xFFFE)
-                    throw new UnsupportedAudioFileException(); // WAVE_FORMAT_EXTENSIBLE
-                // only
+                if (format != WaveFileFormat.WAVE_FORMAT_EXTENSIBLE) {
+                    throw new UnsupportedAudioFileException();
+                }
                 channels = chunk.readUnsignedShort();
                 samplerate = chunk.readUnsignedInt();
                 /* framerate = */chunk.readUnsignedInt();
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileFormat.java	Thu Jan 07 23:50:00 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileFormat.java	Thu Jan 14 23:14:01 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,13 +28,11 @@
 import javax.sound.sampled.AudioFileFormat;
 import javax.sound.sampled.AudioFormat;
 
-
 /**
  * WAVE file format class.
  *
  * @author Jan Borgersen
  */
-
 final class WaveFileFormat extends AudioFileFormat {
 
     /**
@@ -53,15 +51,16 @@
     private static final int STANDARD_FMT_CHUNK_SIZE = 16;
 
     // magic numbers
-    static  final int RIFF_MAGIC         = 1380533830;
-    static  final int WAVE_MAGIC         = 1463899717;
-    static  final int FMT_MAGIC                  = 0x666d7420; // "fmt "
-    static  final int DATA_MAGIC                 = 0x64617461; // "data"
+    static final int RIFF_MAGIC = 1380533830;
+    static final int WAVE_MAGIC = 1463899717;
+    static final int FMT_MAGIC  = 0x666d7420; // "fmt "
+    static final int DATA_MAGIC = 0x64617461; // "data"
 
     // encodings
     static final int WAVE_FORMAT_UNKNOWN   = 0x0000;
     static final int WAVE_FORMAT_PCM       = 0x0001;
     static final int WAVE_FORMAT_ADPCM     = 0x0002;
+    static final int WAVE_FORMAT_IEEE_FLOAT= 0x0003;
     static final int WAVE_FORMAT_ALAW      = 0x0006;
     static final int WAVE_FORMAT_MULAW     = 0x0007;
     static final int WAVE_FORMAT_OKI_ADPCM = 0x0010;
@@ -72,12 +71,7 @@
     static final int WAVE_IBM_FORMAT_ADPCM = 0x0103;
     static final int WAVE_FORMAT_DVI_ADPCM = 0x0011;
     static final int WAVE_FORMAT_SX7383    = 0x1C07;
-
-
-    WaveFileFormat( AudioFileFormat aff ) {
-
-        this( aff.getType(), aff.getByteLength(), aff.getFormat(), aff.getFrameLength() );
-    }
+    static final int WAVE_FORMAT_EXTENSIBLE= 0xFFFE;
 
     WaveFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {
 
@@ -98,7 +92,6 @@
     }
 
     int getWaveType() {
-
         return waveType;
     }
 
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java	Thu Jan 07 23:50:00 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java	Thu Jan 14 23:14:01 2016 +0300
@@ -52,27 +52,6 @@
  */
 public final class WaveFileWriter extends SunFileWriter {
 
-    // magic numbers
-    static  final int RIFF_MAGIC = 1380533830;
-    static  final int WAVE_MAGIC = 1463899717;
-    static  final int FMT_MAGIC  = 0x666d7420; // "fmt "
-    static  final int DATA_MAGIC = 0x64617461; // "data"
-
-    // encodings
-    static final int WAVE_FORMAT_UNKNOWN   = 0x0000;
-    static final int WAVE_FORMAT_PCM       = 0x0001;
-    static final int WAVE_FORMAT_ADPCM     = 0x0002;
-    static final int WAVE_FORMAT_ALAW      = 0x0006;
-    static final int WAVE_FORMAT_MULAW     = 0x0007;
-    static final int WAVE_FORMAT_OKI_ADPCM = 0x0010;
-    static final int WAVE_FORMAT_DIGISTD   = 0x0015;
-    static final int WAVE_FORMAT_DIGIFIX   = 0x0016;
-    static final int WAVE_IBM_FORMAT_MULAW = 0x0101;
-    static final int WAVE_IBM_FORMAT_ALAW  = 0x0102;
-    static final int WAVE_IBM_FORMAT_ADPCM = 0x0103;
-    static final int WAVE_FORMAT_DVI_ADPCM = 0x0011;
-    static final int WAVE_FORMAT_SX7383    = 0x1C07;
-
     /**
      * Constructs a new WaveFileWriter object.
      */
@@ -80,10 +59,6 @@
         super(new AudioFileFormat.Type[]{AudioFileFormat.Type.WAVE});
     }
 
-
-    // METHODS TO IMPLEMENT AudioFileWriter
-
-
     @Override
     public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
 
@@ -126,8 +101,7 @@
             throw new IOException("stream length not specified");
         }
 
-        int bytesWritten = writeWaveFile(stream, waveFileFormat, out);
-        return bytesWritten;
+        return writeWaveFile(stream, waveFileFormat, out);
     }
 
 
@@ -200,9 +174,9 @@
             encoding = streamEncoding;
             sampleSizeInBits = streamFormat.getSampleSizeInBits();
             if (streamEncoding.equals(AudioFormat.Encoding.ALAW)) {
-                waveType = WAVE_FORMAT_ALAW;
+                waveType = WaveFileFormat.WAVE_FORMAT_ALAW;
             } else {
-                waveType = WAVE_FORMAT_MULAW;
+                waveType = WaveFileFormat.WAVE_FORMAT_MULAW;
             }
         } else if ( streamFormat.getSampleSizeInBits()==8 ) {
             encoding = AudioFormat.Encoding.PCM_UNSIGNED;
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileReader.java	Thu Jan 07 23:50:00 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileReader.java	Thu Jan 14 23:14:01 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -67,8 +67,9 @@
                 fmt_found = true;
 
                 int format = chunk.readUnsignedShort();
-                if (format != 3) // WAVE_FORMAT_IEEE_FLOAT only
+                if (format != WaveFileFormat.WAVE_FORMAT_IEEE_FLOAT) {
                     throw new UnsupportedAudioFileException();
+                }
                 channels = chunk.readUnsignedShort();
                 samplerate = chunk.readUnsignedInt();
                 /* framerate = */chunk.readUnsignedInt();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/sound/sampled/spi/AudioFileReader/RecognizeWaveExtensible.java	Thu Jan 14 23:14:01 2016 +0300
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import javax.sound.sampled.AudioFileFormat;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+
+/**
+ * @test
+ * @bug 8147407
+ */
+public final class RecognizeWaveExtensible {
+
+    private static byte[] data = {
+            82, 73, 70, 70, 72, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 40,
+            0, 0, 0, -2, -1, 1, 0, 64, 31, 0, 0, 0, 125, 0, 0, 4, 0, 32, 0, 22,
+            0, 32, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 16, 0, -128, 0, 0, -86, 0,
+            56, -101, 113, 102, 97, 99, 116, 4, 0, 0, 0, 0, 0, 0, 0, 100, 97,
+            116, 97, 0, 0, 0, 0
+    };
+
+    public static void main(final String[] args) throws Exception {
+        final InputStream is = new ByteArrayInputStream(data);
+        final AudioFileFormat aff = AudioSystem.getAudioFileFormat(is);
+        System.out.println("AudioFileFormat: " + aff);
+        try (AudioInputStream ais = AudioSystem.getAudioInputStream(is)) {
+            System.out.println("AudioFormat: " + ais.getFormat());
+        }
+        System.out.println("new String(data) = " + new String(data));
+    }
+}