Repository navigation
Expand file tree
/
Copy pathWavFileWriter.java
More file actions
58 lines (49 loc) · 1.87 KB
/
Copy pathWavFileWriter.java
File metadata and controls
58 lines (49 loc) · 1.87 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
package com.blindgyi.tts;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WavFileWriter {
private File file;
public WavFileWriter(File file) {
this.file = file;
}
public void writeWavFile(byte[] pcmData, int sampleRate) throws IOException {
int channels = 1;
int bitDepth = 16;
int byteRate = sampleRate * channels * (bitDepth / 8);
int dataSize = pcmData.length;
int totalDataLen = dataSize + 36;
FileOutputStream out = new FileOutputStream(file);
// WAV Header ရေးသားခြင်း
out.write("RIFF".getBytes());
out.write(intToByteArray(totalDataLen), 0, 4);
out.write("WAVE".getBytes());
out.write("fmt ".getBytes());
out.write(intToByteArray(16), 0, 4); // SubChunk1Size (PCM = 16)
out.write(shortToByteArray((short) 1), 0, 2); // AudioFormat (PCM = 1)
out.write(shortToByteArray((short) channels), 0, 2);
out.write(intToByteArray(sampleRate), 0, 4);
out.write(intToByteArray(byteRate), 0, 4);
out.write(shortToByteArray((short) (channels * (bitDepth / 8))), 0, 2); // BlockAlign
out.write(shortToByteArray((short) bitDepth), 0, 2); // BitsPerSample
out.write("data".getBytes());
out.write(intToByteArray(dataSize), 0, 4);
// Raw PCM Data ထည့်သွင်းခြင်း
out.write(pcmData);
out.close();
}
private byte[] intToByteArray(int i) {
return new byte[] {
(byte) (i & 0xFF),
(byte) ((i >> 8) & 0xFF),
(byte) ((i >> 16) & 0xFF),
(byte) ((i >> 24) & 0xFF)
};
}
private byte[] shortToByteArray(short s) {
return new byte[] {
(byte) (s & 0xFF),
(byte) ((s >> 8) & 0xFF)
};
}
}