/** * Export an array of shorts to a wav file. * * @param fileName * The name of the wav file. * @param sampleRate * The sample rate. * @param samples * The array of short samples. * @throws IOException * IOException */ public void export(String fileName, int sampleRate, short[] samples) throws IOException { nBytesPerSample = 2; buf = new byte[samples.length * 2]; // Cast the samples, and byte-swap them in the same loop for (int i = 0; i < samples.length; i++) { buf[2 * i] = (byte) ((samples[i] & 0xff00) >>> 8); buf[2 * i + 1] = (byte) ((samples[i] & 0x00ff)); } // Do the write doWrite(fileName, sampleRate); }
/** * 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(); }
/** * Export an array of shorts to a wav file. * * @param fileName * The name of the wav file. * @param sampleRate * The sample rate. * @param samples * The array of short samples. * @throws IOException * IOException */ public void export(String fileName, int sampleRate, short[] samples) throws IOException { nBytesPerSample = 2; buf = new byte[samples.length * 2]; // Cast the samples, and byte-swap them in the same loop for (int i = 0; i < samples.length; i++) { buf[2 * i] = (byte) ((samples[i] & 0xff00) >>> 8); buf[2 * i + 1] = (byte) ((samples[i] & 0x00ff)); } // Do the write doWrite(fileName, sampleRate); }
/** * Export an array of bytes to a wav file. * * @param fileName * The name of the wav file. * @param sampleRate * The sample rate. * @param samples * The array of short samples, given as a byte array (with low and hi bytes separated). * @throws IOException * IOException */ public void export(String fileName, int sampleRate, byte[] samples) throws IOException { nBytesPerSample = 2; buf = new byte[samples.length]; System.arraycopy(samples, 0, buf, 0, samples.length); // Byte-swap the samples byte b = 0; for (int j = 0; j < buf.length; j += 2) { b = buf[j]; buf[j] = buf[j + 1]; buf[j + 1] = b; } // Do the write doWrite(fileName, sampleRate); }
/** * Export an array of bytes to a wav file. * * @param fileName * The name of the wav file. * @param sampleRate * The sample rate. * @param samples * The array of short samples, given as a byte array (with low and hi bytes separated). * @throws IOException * IOException */ public void export(String fileName, int sampleRate, byte[] samples) throws IOException { nBytesPerSample = 2; buf = new byte[samples.length]; System.arraycopy(samples, 0, buf, 0, samples.length); // Byte-swap the samples byte b = 0; for (int j = 0; j < buf.length; j += 2) { b = buf[j]; buf[j] = buf[j + 1]; buf[j + 1] = b; } // Do the write doWrite(fileName, sampleRate); }