/** * Outputs the data in wav format. * * @param fileName * file name * @param sampleRate * sample rate * @throws IOException * IOException */ private void doWrite(String fileName, int sampleRate) throws IOException { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName))); dos.writeBytes("RIFF"); // "RIFF" in ascii dos.writeInt(byteswap(36 + buf.length)); // Chunk size dos.writeBytes("WAVEfmt "); dos.writeInt(byteswap(16)); // chunk size, 16 for PCM dos.writeShort(byteswap((short) 1)); // PCM format dos.writeShort(byteswap((short) 1)); // Mono, one channel dos.writeInt(byteswap(sampleRate)); // Samplerate dos.writeInt(byteswap(sampleRate * nBytesPerSample)); // Byte-rate dos.writeShort(byteswap((short) (nBytesPerSample))); // Nbr of bytes per samples x nbr of channels dos.writeShort(byteswap((short) (nBytesPerSample * 8))); // nbr of bits per sample dos.writeBytes("data"); dos.writeInt(byteswap(buf.length)); dos.write(buf); // <= This buffer should already be byte-swapped at this stage dos.close(); }
/** * Outputs the data in wav format. * * @param fileName * file name * @param sampleRate * sample rate * @throws IOException * IOException */ private void doWrite(String fileName, int sampleRate) throws IOException { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName))); dos.writeBytes("RIFF"); // "RIFF" in ascii dos.writeInt(byteswap(36 + buf.length)); // Chunk size dos.writeBytes("WAVEfmt "); dos.writeInt(byteswap(16)); // chunk size, 16 for PCM dos.writeShort(byteswap((short) 1)); // PCM format dos.writeShort(byteswap((short) 1)); // Mono, one channel dos.writeInt(byteswap(sampleRate)); // Samplerate dos.writeInt(byteswap(sampleRate * nBytesPerSample)); // Byte-rate dos.writeShort(byteswap((short) (nBytesPerSample))); // Nbr of bytes per samples x nbr of channels dos.writeShort(byteswap((short) (nBytesPerSample * 8))); // nbr of bits per sample dos.writeBytes("data"); dos.writeInt(byteswap(buf.length)); dos.write(buf); // <= This buffer should already be byte-swapped at this stage dos.close(); }