/src/com/sun/media/sound/WaveFloatFileWriter.java

https://gitlab.com/borneywpf/openjdk-7-src · Java · 146 lines · 96 code · 21 blank · 29 comment · 7 complexity · 675609b85d72c2a539a49812c538c8d8 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, 2013, 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. package com.sun.media.sound;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.io.OutputStream;
  29. import javax.sound.sampled.AudioFileFormat;
  30. import javax.sound.sampled.AudioFormat;
  31. import javax.sound.sampled.AudioFormat.Encoding;
  32. import javax.sound.sampled.AudioInputStream;
  33. import javax.sound.sampled.AudioSystem;
  34. import javax.sound.sampled.AudioFileFormat.Type;
  35. import javax.sound.sampled.spi.AudioFileWriter;
  36. /**
  37. * Floating-point encoded (format 3) WAVE file writer.
  38. *
  39. * @author Karl Helgason
  40. */
  41. public final class WaveFloatFileWriter extends AudioFileWriter {
  42. public Type[] getAudioFileTypes() {
  43. return new Type[] { Type.WAVE };
  44. }
  45. public Type[] getAudioFileTypes(AudioInputStream stream) {
  46. if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
  47. return new Type[0];
  48. return new Type[] { Type.WAVE };
  49. }
  50. private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
  51. if (!Type.WAVE.equals(type))
  52. throw new IllegalArgumentException("File type " + type
  53. + " not supported.");
  54. if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
  55. throw new IllegalArgumentException("File format "
  56. + stream.getFormat() + " not supported.");
  57. }
  58. public void write(AudioInputStream stream, RIFFWriter writer)
  59. throws IOException {
  60. RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
  61. AudioFormat format = stream.getFormat();
  62. fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT
  63. fmt_chunk.writeUnsignedShort(format.getChannels());
  64. fmt_chunk.writeUnsignedInt((int) format.getSampleRate());
  65. fmt_chunk.writeUnsignedInt(((int) format.getFrameRate())
  66. * format.getFrameSize());
  67. fmt_chunk.writeUnsignedShort(format.getFrameSize());
  68. fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
  69. fmt_chunk.close();
  70. RIFFWriter data_chunk = writer.writeChunk("data");
  71. byte[] buff = new byte[1024];
  72. int len;
  73. while ((len = stream.read(buff, 0, buff.length)) != -1)
  74. data_chunk.write(buff, 0, len);
  75. data_chunk.close();
  76. }
  77. private static class NoCloseOutputStream extends OutputStream {
  78. final OutputStream out;
  79. NoCloseOutputStream(OutputStream out) {
  80. this.out = out;
  81. }
  82. public void write(int b) throws IOException {
  83. out.write(b);
  84. }
  85. public void flush() throws IOException {
  86. out.flush();
  87. }
  88. public void write(byte[] b, int off, int len) throws IOException {
  89. out.write(b, off, len);
  90. }
  91. public void write(byte[] b) throws IOException {
  92. out.write(b);
  93. }
  94. }
  95. private AudioInputStream toLittleEndian(AudioInputStream ais) {
  96. AudioFormat format = ais.getFormat();
  97. AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
  98. .getSampleRate(), format.getSampleSizeInBits(), format
  99. .getChannels(), format.getFrameSize(), format.getFrameRate(),
  100. false);
  101. return AudioSystem.getAudioInputStream(targetFormat, ais);
  102. }
  103. public int write(AudioInputStream stream, Type fileType, OutputStream out)
  104. throws IOException {
  105. checkFormat(fileType, stream);
  106. if (stream.getFormat().isBigEndian())
  107. stream = toLittleEndian(stream);
  108. RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
  109. write(stream, writer);
  110. int fpointer = (int) writer.getFilePointer();
  111. writer.close();
  112. return fpointer;
  113. }
  114. public int write(AudioInputStream stream, Type fileType, File out)
  115. throws IOException {
  116. checkFormat(fileType, stream);
  117. if (stream.getFormat().isBigEndian())
  118. stream = toLittleEndian(stream);
  119. RIFFWriter writer = new RIFFWriter(out, "WAVE");
  120. write(stream, writer);
  121. int fpointer = (int) writer.getFilePointer();
  122. writer.close();
  123. return fpointer;
  124. }
  125. }