829N/A/*
6321N/A * Copyright (c) 2008, 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.File;
829N/Aimport java.io.IOException;
829N/Aimport java.io.OutputStream;
829N/A
829N/Aimport javax.sound.sampled.AudioFileFormat;
829N/Aimport javax.sound.sampled.AudioFormat;
2716N/Aimport javax.sound.sampled.AudioFormat.Encoding;
829N/Aimport javax.sound.sampled.AudioInputStream;
829N/Aimport javax.sound.sampled.AudioSystem;
829N/Aimport javax.sound.sampled.AudioFileFormat.Type;
829N/Aimport javax.sound.sampled.spi.AudioFileWriter;
829N/A
829N/A/**
829N/A * Floating-point encoded (format 3) WAVE file writer.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class WaveFloatFileWriter extends AudioFileWriter {
829N/A
829N/A public Type[] getAudioFileTypes() {
829N/A return new Type[] { Type.WAVE };
829N/A }
829N/A
829N/A public Type[] getAudioFileTypes(AudioInputStream stream) {
829N/A
2716N/A if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
829N/A return new Type[0];
829N/A return new Type[] { Type.WAVE };
829N/A }
829N/A
829N/A private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
829N/A if (!Type.WAVE.equals(type))
829N/A throw new IllegalArgumentException("File type " + type
829N/A + " not supported.");
2716N/A if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
829N/A throw new IllegalArgumentException("File format "
829N/A + stream.getFormat() + " not supported.");
829N/A }
829N/A
829N/A public void write(AudioInputStream stream, RIFFWriter writer)
829N/A throws IOException {
829N/A
829N/A RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
829N/A
829N/A AudioFormat format = stream.getFormat();
829N/A fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT
829N/A fmt_chunk.writeUnsignedShort(format.getChannels());
829N/A fmt_chunk.writeUnsignedInt((int) format.getSampleRate());
829N/A fmt_chunk.writeUnsignedInt(((int) format.getFrameRate())
829N/A * format.getFrameSize());
829N/A fmt_chunk.writeUnsignedShort(format.getFrameSize());
829N/A fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
829N/A fmt_chunk.close();
829N/A RIFFWriter data_chunk = writer.writeChunk("data");
829N/A byte[] buff = new byte[1024];
829N/A int len;
829N/A while ((len = stream.read(buff, 0, buff.length)) != -1)
829N/A data_chunk.write(buff, 0, len);
829N/A data_chunk.close();
829N/A }
829N/A
829N/A private static class NoCloseOutputStream extends OutputStream {
6321N/A final OutputStream out;
829N/A
6321N/A NoCloseOutputStream(OutputStream out) {
829N/A this.out = out;
829N/A }
829N/A
829N/A public void write(int b) throws IOException {
829N/A out.write(b);
829N/A }
829N/A
829N/A public void flush() throws IOException {
829N/A out.flush();
829N/A }
829N/A
829N/A public void write(byte[] b, int off, int len) throws IOException {
829N/A out.write(b, off, len);
829N/A }
829N/A
829N/A public void write(byte[] b) throws IOException {
829N/A out.write(b);
829N/A }
829N/A }
829N/A
829N/A private AudioInputStream toLittleEndian(AudioInputStream ais) {
829N/A AudioFormat format = ais.getFormat();
829N/A AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
829N/A .getSampleRate(), format.getSampleSizeInBits(), format
829N/A .getChannels(), format.getFrameSize(), format.getFrameRate(),
829N/A false);
829N/A return AudioSystem.getAudioInputStream(targetFormat, ais);
829N/A }
829N/A
829N/A public int write(AudioInputStream stream, Type fileType, OutputStream out)
829N/A throws IOException {
829N/A
829N/A checkFormat(fileType, stream);
829N/A if (stream.getFormat().isBigEndian())
829N/A stream = toLittleEndian(stream);
829N/A RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
829N/A write(stream, writer);
829N/A int fpointer = (int) writer.getFilePointer();
829N/A writer.close();
829N/A return fpointer;
829N/A }
829N/A
829N/A public int write(AudioInputStream stream, Type fileType, File out)
829N/A throws IOException {
829N/A checkFormat(fileType, stream);
829N/A if (stream.getFormat().isBigEndian())
829N/A stream = toLittleEndian(stream);
829N/A RIFFWriter writer = new RIFFWriter(out, "WAVE");
829N/A write(stream, writer);
829N/A int fpointer = (int) writer.getFilePointer();
829N/A writer.close();
829N/A return fpointer;
829N/A }
829N/A
829N/A}