< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java

Print this page

        

@@ -37,10 +37,11 @@
 import java.io.RandomAccessFile;
 import java.io.SequenceInputStream;
 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;
 
 /**

@@ -57,36 +58,36 @@
 
     /**
      * 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);
 
         // we must know the total data length to calculate the file length

@@ -99,11 +100,11 @@
         AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
         return writeAuFile(stream, auFileFormat, out);
     }
 
     @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);
 
         // throws IllegalArgumentException if not supported

@@ -139,65 +140,39 @@
 
     /**
      * 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;
-
-        if( (AudioFormat.Encoding.ALAW.equals(streamEncoding)) ||
-            (AudioFormat.Encoding.ULAW.equals(streamEncoding)) ) {
-
-            encoding = streamEncoding;
-            sampleSizeInBits = streamFormat.getSampleSizeInBits();
-
-        } else if ( streamFormat.getSampleSizeInBits()==8 ) {
-
-            encoding = AudioFormat.Encoding.PCM_SIGNED;
-            sampleSizeInBits=8;
-
-        } else {
+        AudioFormat.Encoding encoding = streamFormat.getEncoding();
 
+        if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {
             encoding = AudioFormat.Encoding.PCM_SIGNED;
-            sampleSizeInBits=streamFormat.getSampleSizeInBits();
         }
 
-
-        format = new AudioFormat( encoding,
+        // We always write big endian au files, this is by far the standard
+        AudioFormat format = new AudioFormat(encoding,
                                   streamFormat.getSampleRate(),
-                                  sampleSizeInBits,
+                                             streamFormat.getSampleSizeInBits(),
                                   streamFormat.getChannels(),
                                   streamFormat.getFrameSize(),
-                                  streamFormat.getFrameRate(),
-                                  true);        // AU is always big endian
-
+                                             streamFormat.getFrameRate(), true);
 
-        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 {
 
         // private method ... assumes auFileFormat is a supported file type

@@ -210,68 +185,42 @@
         //int dataSizeInBytes = dataSize * format.getFrameSize();
         long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();
         if (dataSizeInBytes>0x7FFFFFFFl) {
             dataSizeInBytes=UNKNOWN_SIZE;
         }
-        int encoding_local = auFileFormat.getAuType();
+        int auType = auFileFormat.getAuType();
         int sampleRate     = (int)format.getSampleRate();
         int channels       = format.getChannels();
 
         byte header[] = null;
         ByteArrayInputStream headerStream = null;
         ByteArrayOutputStream baos = null;
         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);
 
         // Now create a new InputStream from headerStream and the InputStream
         // in audioStream
 
         dos.close();
         header = baos.toByteArray();
         headerStream = new ByteArrayInputStream( header );
         auStream = new SequenceInputStream(headerStream,
-                        new NoCloseInputStream(codedAudioStream));
+                        new NoCloseInputStream(audioStream));
 
         return auStream;
     }
 
     private int writeAuFile(AudioInputStream in, AuFileFormat auFileFormat, OutputStream out) throws IOException {
< prev index next >