Chris@2: /* Chris@2: Copyright (C) 2001, 2006 by Simon Dixon Chris@2: Chris@2: This program is free software; you can redistribute it and/or modify Chris@2: it under the terms of the GNU General Public License as published by Chris@2: the Free Software Foundation; either version 2 of the License, or Chris@2: (at your option) any later version. Chris@2: Chris@2: This program is distributed in the hope that it will be useful, Chris@2: but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@2: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@2: GNU General Public License for more details. Chris@2: Chris@2: You should have received a copy of the GNU General Public License along Chris@2: with this program (the file gpl.txt); if not, download it from Chris@2: http://www.gnu.org/licenses/gpl.txt or write to the Chris@2: Free Software Foundation, Inc., Chris@2: 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Chris@2: */ Chris@2: Chris@2: package at.ofai.music.audio; Chris@2: Chris@2: import java.io.File; Chris@2: import java.io.FileOutputStream; Chris@2: import java.io.FileNotFoundException; Chris@2: import java.io.IOException; Chris@2: import java.io.UnsupportedEncodingException; Chris@2: Chris@2: public class WavWrite { Chris@2: Chris@2: public static void toByte(byte[] out, String data, int offset) { Chris@2: try { Chris@2: byte[] b = data.getBytes("US-ASCII"); Chris@2: for (int i = 0; i < b.length; i++) Chris@2: out[offset++] = b[i]; Chris@2: } catch (UnsupportedEncodingException e) { Chris@2: System.err.println(e); Chris@2: } Chris@2: } // toByte() Chris@2: Chris@2: public static void toByte(byte[] out, long data,int offset,int len){ Chris@2: for (int stop = offset + len; offset < stop; offset++) { Chris@2: out[offset] = (byte)data; Chris@2: data >>= 8; Chris@2: } Chris@2: } // toByte() Chris@2: Chris@2: /** Opens a file output stream and writes a WAV file header to it */ Chris@2: public static FileOutputStream open(String fileName, int byteLength, Chris@2: int channels, int rate, int audioSize) { Chris@2: FileOutputStream out; Chris@2: try { Chris@2: out = new FileOutputStream(new File(fileName)); Chris@2: byte[] wavHeader = new byte[44]; Chris@2: toByte(wavHeader, "RIFF", 0); Chris@2: toByte(wavHeader, byteLength+36, 4, 4); Chris@2: toByte(wavHeader, "WAVEfmt ", 8); Chris@2: toByte(wavHeader, 16, 16, 4); // chunk length Chris@2: toByte(wavHeader, 1, 20, 2); // PCM encoding Chris@2: toByte(wavHeader, channels, 22, 2); // channels Chris@2: toByte(wavHeader, rate, 24, 4); // sampling rate Chris@2: toByte(wavHeader, audioSize * channels * rate, 28, 4);// bytes per s Chris@2: toByte(wavHeader, audioSize * channels, 32, 2); // block alignment Chris@2: toByte(wavHeader, 8 * audioSize, 34, 2); // bits per sample Chris@2: toByte(wavHeader, "data", 36); Chris@2: toByte(wavHeader, byteLength, 40, 4); Chris@2: out.write(wavHeader); Chris@2: } catch (FileNotFoundException e) { Chris@2: System.err.println("WavWrite: Error opening output file: "+ Chris@2: fileName + "\n" + e); Chris@2: return null; Chris@2: } catch (IOException e) { Chris@2: System.err.println("Error writing output file header\n"+e); Chris@2: return null; Chris@2: } Chris@2: return out; Chris@2: } // open() Chris@2: Chris@2: } // class WavWrite()