Chris@2
|
1 /*
|
Chris@2
|
2 Copyright (C) 2001, 2006 by Simon Dixon
|
Chris@2
|
3
|
Chris@2
|
4 This program is free software; you can redistribute it and/or modify
|
Chris@2
|
5 it under the terms of the GNU General Public License as published by
|
Chris@2
|
6 the Free Software Foundation; either version 2 of the License, or
|
Chris@2
|
7 (at your option) any later version.
|
Chris@2
|
8
|
Chris@2
|
9 This program is distributed in the hope that it will be useful,
|
Chris@2
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@2
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@2
|
12 GNU General Public License for more details.
|
Chris@2
|
13
|
Chris@2
|
14 You should have received a copy of the GNU General Public License along
|
Chris@2
|
15 with this program (the file gpl.txt); if not, download it from
|
Chris@2
|
16 http://www.gnu.org/licenses/gpl.txt or write to the
|
Chris@2
|
17 Free Software Foundation, Inc.,
|
Chris@2
|
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
Chris@2
|
19 */
|
Chris@2
|
20
|
Chris@2
|
21 package at.ofai.music.audio;
|
Chris@2
|
22
|
Chris@2
|
23 import java.io.File;
|
Chris@2
|
24 import java.io.FileOutputStream;
|
Chris@2
|
25 import java.io.FileNotFoundException;
|
Chris@2
|
26 import java.io.IOException;
|
Chris@2
|
27 import java.io.UnsupportedEncodingException;
|
Chris@2
|
28
|
Chris@2
|
29 public class WavWrite {
|
Chris@2
|
30
|
Chris@2
|
31 public static void toByte(byte[] out, String data, int offset) {
|
Chris@2
|
32 try {
|
Chris@2
|
33 byte[] b = data.getBytes("US-ASCII");
|
Chris@2
|
34 for (int i = 0; i < b.length; i++)
|
Chris@2
|
35 out[offset++] = b[i];
|
Chris@2
|
36 } catch (UnsupportedEncodingException e) {
|
Chris@2
|
37 System.err.println(e);
|
Chris@2
|
38 }
|
Chris@2
|
39 } // toByte()
|
Chris@2
|
40
|
Chris@2
|
41 public static void toByte(byte[] out, long data,int offset,int len){
|
Chris@2
|
42 for (int stop = offset + len; offset < stop; offset++) {
|
Chris@2
|
43 out[offset] = (byte)data;
|
Chris@2
|
44 data >>= 8;
|
Chris@2
|
45 }
|
Chris@2
|
46 } // toByte()
|
Chris@2
|
47
|
Chris@2
|
48 /** Opens a file output stream and writes a WAV file header to it */
|
Chris@2
|
49 public static FileOutputStream open(String fileName, int byteLength,
|
Chris@2
|
50 int channels, int rate, int audioSize) {
|
Chris@2
|
51 FileOutputStream out;
|
Chris@2
|
52 try {
|
Chris@2
|
53 out = new FileOutputStream(new File(fileName));
|
Chris@2
|
54 byte[] wavHeader = new byte[44];
|
Chris@2
|
55 toByte(wavHeader, "RIFF", 0);
|
Chris@2
|
56 toByte(wavHeader, byteLength+36, 4, 4);
|
Chris@2
|
57 toByte(wavHeader, "WAVEfmt ", 8);
|
Chris@2
|
58 toByte(wavHeader, 16, 16, 4); // chunk length
|
Chris@2
|
59 toByte(wavHeader, 1, 20, 2); // PCM encoding
|
Chris@2
|
60 toByte(wavHeader, channels, 22, 2); // channels
|
Chris@2
|
61 toByte(wavHeader, rate, 24, 4); // sampling rate
|
Chris@2
|
62 toByte(wavHeader, audioSize * channels * rate, 28, 4);// bytes per s
|
Chris@2
|
63 toByte(wavHeader, audioSize * channels, 32, 2); // block alignment
|
Chris@2
|
64 toByte(wavHeader, 8 * audioSize, 34, 2); // bits per sample
|
Chris@2
|
65 toByte(wavHeader, "data", 36);
|
Chris@2
|
66 toByte(wavHeader, byteLength, 40, 4);
|
Chris@2
|
67 out.write(wavHeader);
|
Chris@2
|
68 } catch (FileNotFoundException e) {
|
Chris@2
|
69 System.err.println("WavWrite: Error opening output file: "+
|
Chris@2
|
70 fileName + "\n" + e);
|
Chris@2
|
71 return null;
|
Chris@2
|
72 } catch (IOException e) {
|
Chris@2
|
73 System.err.println("Error writing output file header\n"+e);
|
Chris@2
|
74 return null;
|
Chris@2
|
75 }
|
Chris@2
|
76 return out;
|
Chris@2
|
77 } // open()
|
Chris@2
|
78
|
Chris@2
|
79 } // class WavWrite()
|