-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWaveFileWriter.java
More file actions
136 lines (119 loc) · 3.66 KB
/
WaveFileWriter.java
File metadata and controls
136 lines (119 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WaveFileWriter {
private String filename = null;
private FileOutputStream fos = null;
private BufferedOutputStream bos = null;
private long chunksize = 0;
private long subchunk1size = 0;
private int audioformat = 0;
private int numchannels = 0;
private long samplerate = 0;
private long byterate = 0;
private int blockalign = 0;
private int bitspersample = 0;
private long subchunk2size = 0;
public WaveFileWriter(String filename, int[][] data, long samplerate) {
this.initWriter(filename, data, 0, data[0].length, samplerate);
}
public WaveFileWriter(String filename, int[][] data, int offset, int len, long samplerate) {
this.initWriter(filename, data, offset, len, samplerate);
}
public void initWriter(String filename, int[][] data, int offset, int len, long samplerate) {
this.filename = filename;
try {
fos = new FileOutputStream(this.filename);
bos = new BufferedOutputStream(fos);
// int datalen = data[0].length;
int datalen = len;
this.samplerate = samplerate;
// this.bitspersample = bitspersample;
this.bitspersample = 16;
this.numchannels = data.length;
this.subchunk2size = this.numchannels * (this.bitspersample / 8) * datalen;
this.subchunk1size = 16;
this.audioformat = 1; // PCM
this.byterate = this.samplerate * this.bitspersample * this.numchannels / 8;
this.blockalign = this.numchannels * this.bitspersample / 8;
this.chunksize = this.subchunk2size + 8 + this.subchunk1size + 8 + 4;
writeString(WaveConstants.CHUNKDESCRIPTOR, WaveConstants.LENCHUNKDESCRIPTOR);
writeLong(this.chunksize);
writeString(WaveConstants.WAVEFLAG, WaveConstants.LENWAVEFLAG);
writeString(WaveConstants.FMTSUBCHUNK, WaveConstants.LENFMTSUBCHUNK);
writeLong(this.subchunk1size);
writeInt(this.audioformat);
writeInt(this.numchannels);
writeLong(this.samplerate);
writeLong(this.byterate);
writeInt(this.blockalign);
writeInt(this.bitspersample);
writeString(WaveConstants.DATASUBCHUNK, WaveConstants.LENDATASUBCHUNK);
writeLong(this.subchunk2size);
for (int i = 0; i < datalen; ++i) {
for (int n = 0; n < this.numchannels; ++n) {
if (this.bitspersample == 16) {
writeInt(data[n][i + offset]);
} else {
writeByte((byte) data[n][i + offset]);
}
}
}
bos.flush();
fos.flush();
bos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
}
private void writeString(String str, int len) {
if (str.length() != len) {
throw new IllegalArgumentException("length not match!!!");
}
byte[] bt = str.getBytes();
try {
bos.write(bt);
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeByte(byte data) {
try {
bos.write(new byte[] { data }, 0, 1);
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeInt(int data) {
byte[] buf = new byte[2];
buf[1] = (byte) (data >>> 8);
buf[0] = (byte) (data & 0xFF);
try {
bos.write(buf);
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeLong(long data) {
byte[] buf = new byte[4];
buf[0] = (byte) (data & 0x00ff);
buf[1] = (byte) ((data >> 8) & 0x00ff);
buf[2] = (byte) ((data >> 16) & 0x00ff);
buf[3] = (byte) ((data >> 24) & 0x00ff);
try {
bos.write(buf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean saveSingleChannel(String filename, int[] data, long samplerate) {
int[][] datar = new int[1][];
datar[0] = data;
WaveFileWriter writer = new WaveFileWriter(filename, datar, samplerate);
writer.close();
return true;
}
}