8146239: Support of PCM_FLOAT for the AU file format
authorserb
Wed, 16 Mar 2016 15:55:27 +0300
changeset 36888 0905e6c2d1af
parent 36887 b05841864f56
child 36889 e36687a00487
8146239: Support of PCM_FLOAT for the AU file format Reviewed-by: amenkov
jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileFormat.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileFormat.java	Wed Mar 16 15:36:06 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileFormat.java	Wed Mar 16 15:55:27 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2015, 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
@@ -45,11 +45,12 @@
     static final int AU_LINEAR_24    = 4;  /* 24-bit linear PCM */
     static final int AU_LINEAR_32    = 5;  /* 32-bit linear PCM */
     static final int AU_FLOAT        = 6;  /* 32-bit IEEE floating point */
-    static final int AU_DOUBLE       = 7;  /* 64-bit IEEE floating point */
-    static final int AU_ADPCM_G721   = 23; /* 4-bit CCITT g.721 ADPCM */
-    static final int AU_ADPCM_G722   = 24; /* CCITT g.722 ADPCM */
-    static final int AU_ADPCM_G723_3 = 25; /* CCITT g.723 3-bit ADPCM */
-    static final int AU_ADPCM_G723_5 = 26; /* CCITT g.723 5-bit ADPCM */
+//  we don't support these ...
+//  static final int AU_DOUBLE       = 7;  /* 64-bit IEEE floating point */
+//  static final int AU_ADPCM_G721   = 23; /* 4-bit CCITT g.721 ADPCM */
+//  static final int AU_ADPCM_G722   = 24; /* CCITT g.722 ADPCM */
+//  static final int AU_ADPCM_G723_3 = 25; /* CCITT g.723 3-bit ADPCM */
+//  static final int AU_ADPCM_G723_5 = 26; /* CCITT g.723 5-bit ADPCM */
     static final int AU_ALAW_8       = 27; /* 8-bit ISDN A-law */
 
     static final int AU_HEADERSIZE       = 24;
@@ -64,24 +65,28 @@
 
         auType = -1;
 
-        if( AudioFormat.Encoding.ALAW.equals(encoding) ) {
-            if( format.getSampleSizeInBits()==8 ) {
+        if (AudioFormat.Encoding.ALAW.equals(encoding)) {
+            if (format.getSampleSizeInBits() == 8) {
                 auType = AU_ALAW_8;
             }
-        } else if( AudioFormat.Encoding.ULAW.equals(encoding) ) {
-            if( format.getSampleSizeInBits()==8 ) {
+        } else if (AudioFormat.Encoding.ULAW.equals(encoding)) {
+            if (format.getSampleSizeInBits() == 8) {
                 auType = AU_ULAW_8;
             }
-        } else if( AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ) {
-            if( format.getSampleSizeInBits()==8 ) {
+        } else if (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
+            if (format.getSampleSizeInBits() == 8) {
                 auType = AU_LINEAR_8;
-            } else if( format.getSampleSizeInBits()==16 ) {
+            } else if (format.getSampleSizeInBits() == 16) {
                 auType = AU_LINEAR_16;
-            } else if( format.getSampleSizeInBits()==24 ) {
+            } else if (format.getSampleSizeInBits() == 24) {
                 auType = AU_LINEAR_24;
-            } else if( format.getSampleSizeInBits()==32 ) {
+            } else if (format.getSampleSizeInBits() == 32) {
                 auType = AU_LINEAR_32;
             }
+        } else if (AudioFormat.Encoding.PCM_FLOAT.equals(encoding)) {
+            if (format.getSampleSizeInBits() == 32) {
+                auType = AU_FLOAT;
+            }
         }
     }
 
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java	Wed Mar 16 15:36:06 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java	Wed Mar 16 15:55:27 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2015, 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
@@ -30,6 +30,7 @@
 import java.io.InputStream;
 
 import javax.sound.sampled.AudioFileFormat;
+import javax.sound.sampled.AudioFileFormat.Type;
 import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioSystem;
 import javax.sound.sampled.UnsupportedAudioFileException;
@@ -56,7 +57,7 @@
 
         final int headerSize = dis.readInt();
         final int dataSize = dis.readInt();
-        final int encoding_local = dis.readInt();
+        final int auType = dis.readInt();
         final int sampleRate = dis.readInt();
         final int channels = dis.readInt();
         if (channels <= 0) {
@@ -65,40 +66,38 @@
 
         final int sampleSizeInBits;
         final AudioFormat.Encoding encoding;
-        switch (encoding_local) {
-        case AuFileFormat.AU_ULAW_8:
-            encoding = AudioFormat.Encoding.ULAW;
-            sampleSizeInBits = 8;
-            break;
-        case AuFileFormat.AU_ALAW_8:
-            encoding = AudioFormat.Encoding.ALAW;
-            sampleSizeInBits = 8;
-            break;
-        case AuFileFormat.AU_LINEAR_8:
-            // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
-            encoding = AudioFormat.Encoding.PCM_SIGNED;
-            sampleSizeInBits = 8;
-            break;
-        case AuFileFormat.AU_LINEAR_16:
-            encoding = AudioFormat.Encoding.PCM_SIGNED;
-            sampleSizeInBits = 16;
-            break;
-        case AuFileFormat.AU_LINEAR_24:
-            encoding = AudioFormat.Encoding.PCM_SIGNED;
-
-            sampleSizeInBits = 24;
-            break;
-        case AuFileFormat.AU_LINEAR_32:
-            encoding = AudioFormat.Encoding.PCM_SIGNED;
-
-            sampleSizeInBits = 32;
-            break;
-            // $jb: 03.19.99: we don't support these ...
-            /*          case AuFileFormat.AU_FLOAT:
-                        encoding = new AudioFormat.FLOAT;
-                        sampleSizeInBits = 32;
-                        break;
-                        case AuFileFormat.AU_DOUBLE:
+        switch (auType) {
+            case AuFileFormat.AU_ULAW_8:
+                encoding = AudioFormat.Encoding.ULAW;
+                sampleSizeInBits = 8;
+                break;
+            case AuFileFormat.AU_ALAW_8:
+                encoding = AudioFormat.Encoding.ALAW;
+                sampleSizeInBits = 8;
+                break;
+            case AuFileFormat.AU_LINEAR_8:
+                // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
+                encoding = AudioFormat.Encoding.PCM_SIGNED;
+                sampleSizeInBits = 8;
+                break;
+            case AuFileFormat.AU_LINEAR_16:
+                encoding = AudioFormat.Encoding.PCM_SIGNED;
+                sampleSizeInBits = 16;
+                break;
+            case AuFileFormat.AU_LINEAR_24:
+                encoding = AudioFormat.Encoding.PCM_SIGNED;
+                sampleSizeInBits = 24;
+                break;
+            case AuFileFormat.AU_LINEAR_32:
+                encoding = AudioFormat.Encoding.PCM_SIGNED;
+                sampleSizeInBits = 32;
+                break;
+            case AuFileFormat.AU_FLOAT:
+                encoding = AudioFormat.Encoding.PCM_FLOAT;
+                sampleSizeInBits = 32;
+                break;
+            // we don't support these ...
+            /*          case AuFileFormat.AU_DOUBLE:
                         encoding = new AudioFormat.DOUBLE;
                         sampleSizeInBits = 8;
                         break;
@@ -117,9 +116,9 @@
                         SamplePerUnit = 8;
                         break;
             */
-        default:
-            // unsupported filetype, throw exception
-            throw new UnsupportedAudioFileException("not a valid AU file");
+            default:
+                // unsupported filetype, throw exception
+                throw new UnsupportedAudioFileException("not a valid AU file");
         }
 
         final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
@@ -136,7 +135,6 @@
         final AudioFormat format = new AudioFormat(encoding, sampleRate,
                                                    sampleSizeInBits, channels,
                                                    frameSize, sampleRate, true);
-        return new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize,
-                                format, length);
+        return new AuFileFormat(Type.AU, dataSize + headerSize, format, length);
     }
 }
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java	Wed Mar 16 15:36:06 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java	Wed Mar 16 15:55:27 2016 +0300
@@ -39,6 +39,7 @@
 import java.util.Objects;
 
 import javax.sound.sampled.AudioFileFormat;
+import javax.sound.sampled.AudioFileFormat.Type;
 import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioInputStream;
 import javax.sound.sampled.AudioSystem;
@@ -59,32 +60,32 @@
      * Constructs a new AuFileWriter object.
      */
     public AuFileWriter() {
-        super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AU});
+        super(new Type[]{Type.AU});
     }
 
     @Override
-    public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
+    public Type[] getAudioFileTypes(AudioInputStream stream) {
 
-        AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
+        Type[] filetypes = new Type[types.length];
         System.arraycopy(types, 0, filetypes, 0, types.length);
 
         // make sure we can write this stream
         AudioFormat format = stream.getFormat();
         AudioFormat.Encoding encoding = format.getEncoding();
 
-        if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
-            (AudioFormat.Encoding.ULAW.equals(encoding)) ||
-            (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
-            (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
-
+        if (AudioFormat.Encoding.ALAW.equals(encoding)
+                || AudioFormat.Encoding.ULAW.equals(encoding)
+                || AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
+                || AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)
+                || AudioFormat.Encoding.PCM_FLOAT.equals(encoding)) {
             return filetypes;
         }
 
-        return new AudioFileFormat.Type[0];
+        return new Type[0];
     }
 
     @Override
-    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
+    public int write(AudioInputStream stream, Type fileType, OutputStream out) throws IOException {
         Objects.requireNonNull(stream);
         Objects.requireNonNull(fileType);
         Objects.requireNonNull(out);
@@ -101,7 +102,7 @@
     }
 
     @Override
-    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
+    public int write(AudioInputStream stream, Type fileType, File out) throws IOException {
         Objects.requireNonNull(stream);
         Objects.requireNonNull(fileType);
         Objects.requireNonNull(out);
@@ -141,61 +142,35 @@
      * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
      * Throws IllegalArgumentException if not supported.
      */
-    private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
+    private AudioFileFormat getAudioFileFormat(Type type, AudioInputStream stream) {
         if (!isFileTypeSupported(type, stream)) {
             throw new IllegalArgumentException("File type " + type + " not supported.");
         }
 
-        AudioFormat format = null;
-        AuFileFormat fileFormat = null;
-        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
-
         AudioFormat streamFormat = stream.getFormat();
-        AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
-
-
-        int sampleSizeInBits;
-        int fileSize;
+        AudioFormat.Encoding encoding = streamFormat.getEncoding();
 
-        if( (AudioFormat.Encoding.ALAW.equals(streamEncoding)) ||
-            (AudioFormat.Encoding.ULAW.equals(streamEncoding)) ) {
-
-            encoding = streamEncoding;
-            sampleSizeInBits = streamFormat.getSampleSizeInBits();
-
-        } else if ( streamFormat.getSampleSizeInBits()==8 ) {
-
+        if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {
             encoding = AudioFormat.Encoding.PCM_SIGNED;
-            sampleSizeInBits=8;
-
-        } else {
-
-            encoding = AudioFormat.Encoding.PCM_SIGNED;
-            sampleSizeInBits=streamFormat.getSampleSizeInBits();
         }
 
+        // We always write big endian au files, this is by far the standard
+        AudioFormat format = new AudioFormat(encoding,
+                                             streamFormat.getSampleRate(),
+                                             streamFormat.getSampleSizeInBits(),
+                                             streamFormat.getChannels(),
+                                             streamFormat.getFrameSize(),
+                                             streamFormat.getFrameRate(), true);
 
-        format = new AudioFormat( encoding,
-                                  streamFormat.getSampleRate(),
-                                  sampleSizeInBits,
-                                  streamFormat.getChannels(),
-                                  streamFormat.getFrameSize(),
-                                  streamFormat.getFrameRate(),
-                                  true);        // AU is always big endian
-
-
-        if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
+        int fileSize;
+        if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
             fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
         } else {
             fileSize = AudioSystem.NOT_SPECIFIED;
         }
 
-        fileFormat = new AuFileFormat( AudioFileFormat.Type.AU,
-                                       fileSize,
-                                       format,
-                                       (int)stream.getFrameLength() );
-
-        return fileFormat;
+        return new AuFileFormat(Type.AU, fileSize, format,
+                                (int) stream.getFrameLength());
     }
 
     private InputStream getFileStream(AuFileFormat auFileFormat, AudioInputStream audioStream) throws IOException {
@@ -212,7 +187,7 @@
         if (dataSizeInBytes>0x7FFFFFFFl) {
             dataSizeInBytes=UNKNOWN_SIZE;
         }
-        int encoding_local = auFileFormat.getAuType();
+        int auType = auFileFormat.getAuType();
         int sampleRate     = (int)format.getSampleRate();
         int channels       = format.getChannels();
 
@@ -222,43 +197,17 @@
         DataOutputStream dos = null;
         SequenceInputStream auStream = null;
 
-        AudioFormat audioStreamFormat = null;
-        AudioFormat.Encoding encoding = null;
-        InputStream codedAudioStream = audioStream;
-
-        // if we need to do any format conversion, do it here.
-
-        codedAudioStream = audioStream;
-
-        audioStreamFormat = audioStream.getFormat();
-        encoding = audioStreamFormat.getEncoding();
-
+        // if we need to do any format conversion, we do it here.
         //$$ fb 2001-07-13: Bug 4391108
-        if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
-            (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
-             && !audioStreamFormat.isBigEndian()) ) {
-            // We always write big endian au files, this is by far the standard
-            codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
-                                                                                 AudioFormat.Encoding.PCM_SIGNED,
-                                                                                 audioStreamFormat.getSampleRate(),
-                                                                                 audioStreamFormat.getSampleSizeInBits(),
-                                                                                 audioStreamFormat.getChannels(),
-                                                                                 audioStreamFormat.getFrameSize(),
-                                                                                 audioStreamFormat.getFrameRate(),
-                                                                                 true),
-                                                                                 audioStream );
-
-
-        }
+        audioStream = AudioSystem.getAudioInputStream(format, audioStream);
 
         baos = new ByteArrayOutputStream();
         dos = new DataOutputStream(baos);
 
-
         dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
         dos.writeInt(headerSize);
         dos.writeInt((int)dataSizeInBytes);
-        dos.writeInt(encoding_local);
+        dos.writeInt(auType);
         dos.writeInt(sampleRate);
         dos.writeInt(channels);
 
@@ -269,7 +218,7 @@
         header = baos.toByteArray();
         headerStream = new ByteArrayInputStream( header );
         auStream = new SequenceInputStream(headerStream,
-                        new NoCloseInputStream(codedAudioStream));
+                        new NoCloseInputStream(audioStream));
 
         return auStream;
     }