| 13 | /// @author shannah |
| 14 | /// |
| 15 | public class WAVWriter implements AutoCloseable { |
| 16 | private final File outputFile; |
| 17 | private final int samplingRate; |
| 18 | private final int channels; |
| 19 | private final int numBits; |
| 20 | private OutputStream out; |
| 21 | private long dataLength; |
| 22 | |
| 23 | /// Creates a new writer for writing a WAV file. |
| 24 | /// |
| 25 | /// #### Parameters |
| 26 | /// |
| 27 | /// - `outputFile`: The output file. |
| 28 | /// |
| 29 | /// - `samplingRate`: The sampling rate. E.g. 44100 |
| 30 | /// |
| 31 | /// - `channels`: The number of channels. E.g. 1 or 2 |
| 32 | /// |
| 33 | /// - `numBits`: 8 or 16 |
| 34 | /// |
| 35 | /// #### Throws |
| 36 | /// |
| 37 | /// - `IOException` |
| 38 | public WAVWriter(final File outputFile, final int samplingRate, final int channels, final int numBits) throws IOException { |
| 39 | this.outputFile = outputFile; |
| 40 | this.out = FileSystemStorage.getInstance().openOutputStream(outputFile.getAbsolutePath()); |
| 41 | this.samplingRate = samplingRate; |
| 42 | this.channels = channels; |
| 43 | this.numBits = numBits; |
| 44 | } |
| 45 | |
| 46 | private void writeHeader() throws IOException { |
| 47 | final byte[] header = new byte[44]; |
| 48 | long totalDataLen = dataLength + 36; |
| 49 | final long bitrate = (long) this.samplingRate * this.channels * this.numBits; |
| 50 | header[0] = 82; |
| 51 | header[1] = 73; |
| 52 | header[3] = (header[2] = 70); |
| 53 | header[4] = (byte) (totalDataLen & 0xFFL); |
| 54 | header[5] = (byte) (totalDataLen >> 8 & 0xFFL); |
| 55 | header[6] = (byte) (totalDataLen >> 16 & 0xFFL); |
| 56 | header[7] = (byte) (totalDataLen >> 24 & 0xFFL); |
| 57 | header[8] = 87; |
| 58 | header[9] = 65; |
| 59 | header[10] = 86; |
| 60 | header[11] = 69; |
| 61 | header[12] = 102; |
| 62 | header[13] = 109; |
| 63 | header[14] = 116; |
| 64 | header[15] = 32; |
| 65 | header[16] = (byte) this.numBits; |
| 66 | header[17] = 0; |
| 67 | header[19] = (header[18] = 0); |
| 68 | header[20] = 1; |
| 69 | header[21] = 0; |
| 70 | header[22] = (byte) this.channels; |
| 71 | header[23] = 0; |
| 72 | header[24] = (byte) (this.samplingRate & 0xFF); |
nothing calls this directly
no outgoing calls
no test coverage detected