1 /*
2  * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.media.sound;
27 
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.util.Objects;
32 
33 import javax.sound.sampled.AudioFileFormat;
34 import javax.sound.sampled.AudioFileFormat.Type;
35 import javax.sound.sampled.AudioFormat;
36 import javax.sound.sampled.AudioFormat.Encoding;
37 import javax.sound.sampled.AudioInputStream;
38 import javax.sound.sampled.AudioSystem;
39 import javax.sound.sampled.spi.AudioFileWriter;
40 
41 /**
42  * Floating-point encoded (format 3) WAVE file writer.
43  *
44  * @author Karl Helgason
45  */
46 public final class WaveFloatFileWriter extends AudioFileWriter {
47 
48     @Override
getAudioFileTypes()49     public Type[] getAudioFileTypes() {
50         return new Type[]{Type.WAVE};
51     }
52 
53     @Override
getAudioFileTypes(AudioInputStream stream)54     public Type[] getAudioFileTypes(AudioInputStream stream) {
55 
56         if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
57             return new Type[0];
58         return new Type[] { Type.WAVE };
59     }
60 
checkFormat(AudioFileFormat.Type type, AudioInputStream stream)61     private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
62         if (!Type.WAVE.equals(type))
63             throw new IllegalArgumentException("File type " + type
64                     + " not supported.");
65         if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
66             throw new IllegalArgumentException("File format "
67                     + stream.getFormat() + " not supported.");
68     }
69 
write(AudioInputStream stream, RIFFWriter writer)70     public void write(AudioInputStream stream, RIFFWriter writer)
71             throws IOException {
72         try (final RIFFWriter fmt_chunk = writer.writeChunk("fmt ")) {
73             AudioFormat format = stream.getFormat();
74             fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT
75             fmt_chunk.writeUnsignedShort(format.getChannels());
76             fmt_chunk.writeUnsignedInt((int) format.getSampleRate());
77             fmt_chunk.writeUnsignedInt(((int) format.getFrameRate())
78                                                * format.getFrameSize());
79             fmt_chunk.writeUnsignedShort(format.getFrameSize());
80             fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
81         }
82         try (RIFFWriter data_chunk = writer.writeChunk("data")) {
83             byte[] buff = new byte[1024];
84             int len;
85             while ((len = stream.read(buff, 0, buff.length)) != -1) {
86                 data_chunk.write(buff, 0, len);
87             }
88         }
89     }
90 
91     private static final class NoCloseOutputStream extends OutputStream {
92         final OutputStream out;
93 
NoCloseOutputStream(OutputStream out)94         NoCloseOutputStream(OutputStream out) {
95             this.out = out;
96         }
97 
98         @Override
write(int b)99         public void write(int b) throws IOException {
100             out.write(b);
101         }
102 
103         @Override
flush()104         public void flush() throws IOException {
105             out.flush();
106         }
107 
108         @Override
write(byte[] b, int off, int len)109         public void write(byte[] b, int off, int len) throws IOException {
110             out.write(b, off, len);
111         }
112 
113         @Override
write(byte[] b)114         public void write(byte[] b) throws IOException {
115             out.write(b);
116         }
117     }
118 
toLittleEndian(AudioInputStream ais)119     private AudioInputStream toLittleEndian(AudioInputStream ais) {
120         AudioFormat format = ais.getFormat();
121         AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
122                 .getSampleRate(), format.getSampleSizeInBits(), format
123                 .getChannels(), format.getFrameSize(), format.getFrameRate(),
124                 false);
125         return AudioSystem.getAudioInputStream(targetFormat, ais);
126     }
127 
128     @Override
write(AudioInputStream stream, Type fileType, OutputStream out)129     public int write(AudioInputStream stream, Type fileType, OutputStream out)
130             throws IOException {
131         Objects.requireNonNull(stream);
132         Objects.requireNonNull(fileType);
133         Objects.requireNonNull(out);
134 
135         checkFormat(fileType, stream);
136         if (stream.getFormat().isBigEndian())
137             stream = toLittleEndian(stream);
138         try (final RIFFWriter writer = new RIFFWriter(
139                 new NoCloseOutputStream(out), "WAVE")) {
140             write(stream, writer);
141             return (int) writer.getFilePointer();
142         }
143     }
144 
145     @Override
write(AudioInputStream stream, Type fileType, File out)146     public int write(AudioInputStream stream, Type fileType, File out)
147             throws IOException {
148         Objects.requireNonNull(stream);
149         Objects.requireNonNull(fileType);
150         Objects.requireNonNull(out);
151 
152         checkFormat(fileType, stream);
153         if (stream.getFormat().isBigEndian())
154             stream = toLittleEndian(stream);
155         try (final RIFFWriter writer = new RIFFWriter(out, "WAVE")) {
156             write(stream, writer);
157             return (int) writer.getFilePointer();
158         }
159     }
160 }
161