Java File Write via ByteBuffer writeWaveFile44100_16Bit_Mono(short[] data, File file)

Here you can find the source of writeWaveFile44100_16Bit_Mono(short[] data, File file)

Description

write Wave Fil Bi Mono

License

Apache License

Declaration

public static void writeWaveFile44100_16Bit_Mono(short[] data, File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 See AUTHORS file.// w w  w  . j  a  v  a  2  s.c  om
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static void writeWaveFile44100_16Bit_Mono(short[] data, File file) {
        final ByteBuffer buffer = ByteBuffer.allocate(data.length * 2).order(ByteOrder.LITTLE_ENDIAN);

        buffer.asShortBuffer().put(data).position(0);

        try {
            final FileOutputStream os = new FileOutputStream(file);
            final ByteBuffer converter = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);

            // The header
            os.write((byte) 'R');
            os.write((byte) 'I');
            os.write((byte) 'F');
            os.write((byte) 'F');

            // The chunk size
            final int chunkSize = 44 + (data.length * 2) - 8;
            converter.limit(4);
            converter.putInt(chunkSize).position(0);
            os.write(converter.array());

            // The format
            os.write((byte) 'W');
            os.write((byte) 'A');
            os.write((byte) 'V');
            os.write((byte) 'E');

            // The "fmt" subchunk for the wave format.
            os.write((byte) 'f');
            os.write((byte) 'm');
            os.write((byte) 't');
            os.write((byte) ' ');

            // 16" is the size of the rest of the subchunk.
            converter.limit(4);
            converter.putInt(16).position(0);
            os.write(converter.array());

            // "1" indicates PCM.
            converter.limit(2);
            converter.putShort((short) 1).position(0);
            os.write(converter.array(), 0, 2);

            // # of channels.
            converter.limit(2);
            converter.putShort((short) 1).position(0);
            os.write(converter.array(), 0, 2);

            // Sample rate.
            converter.limit(4);
            converter.putInt(44100).position(0);
            os.write(converter.array());

            // Byte rate
            converter.limit(4);
            converter.putInt(88200).position(0);
            os.write(converter.array());

            // Block align
            converter.limit(2);
            converter.putShort((short) 2).position(0);
            os.write(converter.array(), 0, 2);

            // Bits per sample
            converter.limit(2);
            converter.putShort((short) 16).position(0);
            os.write(converter.array(), 0, 2);

            // Data subchunk
            os.write((byte) 'd');
            os.write((byte) 'a');
            os.write((byte) 't');
            os.write((byte) 'a');

            // Data subchunk size.
            converter.limit(4);
            converter.putInt(data.length * 2).position(0);
            os.write(converter.array());

            // Data
            try {
                os.write(buffer.array());
            } finally {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. writeStringData(DataOutputStream mDataOutputStream, String token)
  2. writeStringToSocketChannel(String text, SocketChannel sc)
  3. writeToFile(String path, ByteOrder byteOrder)
  4. writeUtf8(DataOutput out, String string)
  5. writeVector(DataOutputStream output, float[] vector)