Writing PCM recorded data into a .wav file (java android)
Asked Answered
D

5

37

I'm using AudioRecord to record 16 bit PCM data in android. After recording the data and saving it to a file, I read it back to save it as .wav file.

The problem is that the WAV files are recognized by media players but play nothing but pure noise. My best guess at the moment is that my wav file headers are incorrect but I have been unable to see what exactly the problem is. (I think this because I can play the raw PCM data that I recorded in Audacity)

Here's my code for reading the raw PCM file and saving it as a .wav:

private void properWAV(File fileToConvert, float newRecordingID){
    try {
        long mySubChunk1Size = 16;
        int myBitsPerSample= 16;
        int myFormat = 1;
        long myChannels = 1;
        long mySampleRate = 22100;
        long myByteRate = mySampleRate * myChannels * myBitsPerSample/8;
        int myBlockAlign = (int) (myChannels * myBitsPerSample/8);

        byte[] clipData = getBytesFromFile(fileToConvert);

        long myDataSize = clipData.length;
        long myChunk2Size =  myDataSize * myChannels * myBitsPerSample/8;
        long myChunkSize = 36 + myChunk2Size;

        OutputStream os;        
        os = new FileOutputStream(new File("/sdcard/onefile/assessor/OneFile_Audio_"+ newRecordingID+".wav"));
        BufferedOutputStream bos = new BufferedOutputStream(os);
        DataOutputStream outFile = new DataOutputStream(bos);

        outFile.writeBytes("RIFF");                                 // 00 - RIFF
        outFile.write(intToByteArray((int)myChunkSize), 0, 4);      // 04 - how big is the rest of this file?
        outFile.writeBytes("WAVE");                                 // 08 - WAVE
        outFile.writeBytes("fmt ");                                 // 12 - fmt 
        outFile.write(intToByteArray((int)mySubChunk1Size), 0, 4);  // 16 - size of this chunk
        outFile.write(shortToByteArray((short)myFormat), 0, 2);     // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
        outFile.write(shortToByteArray((short)myChannels), 0, 2);   // 22 - mono or stereo? 1 or 2?  (or 5 or ???)
        outFile.write(intToByteArray((int)mySampleRate), 0, 4);     // 24 - samples per second (numbers per second)
        outFile.write(intToByteArray((int)myByteRate), 0, 4);       // 28 - bytes per second
        outFile.write(shortToByteArray((short)myBlockAlign), 0, 2); // 32 - # of bytes in one sample, for all channels
        outFile.write(shortToByteArray((short)myBitsPerSample), 0, 2);  // 34 - how many bits in a sample(number)?  usually 16 or 24
        outFile.writeBytes("data");                                 // 36 - data
        outFile.write(intToByteArray((int)myDataSize), 0, 4);       // 40 - how big is this data chunk
        outFile.write(clipData);                                    // 44 - the actual data itself - just a long string of numbers

        outFile.flush();
        outFile.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}


private static byte[] intToByteArray(int i)
    {
        byte[] b = new byte[4];
        b[0] = (byte) (i & 0x00FF);
        b[1] = (byte) ((i >> 8) & 0x000000FF);
        b[2] = (byte) ((i >> 16) & 0x000000FF);
        b[3] = (byte) ((i >> 24) & 0x000000FF);
        return b;
    }

    // convert a short to a byte array
    public static byte[] shortToByteArray(short data)
    {
        /*
         * NB have also tried:
         * return new byte[]{(byte)(data & 0xff),(byte)((data >> 8) & 0xff)};
         * 
         */

        return new byte[]{(byte)(data & 0xff),(byte)((data >>> 8) & 0xff)};
    }

I haven't included getBytesFromFile() since it takes up too much space and its a tried and tested method. Anyway, here's the code that does the actual recording:

public void run() { 
    Log.i("ONEFILE", "Starting main audio capture loop...");

    int frequency = 22100;
    int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 

    final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding); 

    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);

    audioRecord.startRecording();
    ByteArrayOutputStream recData = new ByteArrayOutputStream(); 
    DataOutputStream dos = new DataOutputStream(recData);

    short[] buffer = new short[bufferSize];  
    audioRecord.startRecording();

    while (!stopped) {  
        int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);

        for(int i = 0; i < bufferReadResult;i++) {
            try {
                dos.writeShort(buffer[i]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }  
    audioRecord.stop();
    try {
        dos.flush();
        dos.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    audioRecord.stop();

    byte[] clipData = recData.toByteArray();

    File file = new File(audioOutputPath);
    if(file.exists())
        file.delete();
    file = new File(audioOutputPath);
    OutputStream os;
    try {
        os = new FileOutputStream(file);

        BufferedOutputStream bos = new BufferedOutputStream(os);
        DataOutputStream outFile = new DataOutputStream(bos);

        outFile.write(clipData);  

        outFile.flush();
        outFile.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Please suggest what could be going wrong.

Dolabriform answered 7/2, 2012 at 16:2 Comment(1)
Could you please display what your header looks like. E.G first 48 bytes (3 rows, 16 bytes in each) displayed as HEX values: e.g. 52 49 46 46 E1 C3 ...Potentiate
L
17

I've been wrestling with this exact same question for hours now, and my issue was mostly that when recording in 16 bits you have to be very careful about what you write to the output. The WAV file expects the data in Little Endian format, but using writeShort writes it to the output as Big Endian. I also got interesting results when using the other functions so I returned to writing bytes in the correct order and that works.

I used a Hex editor extensively while debugging this. I can recommend you do the same. Also, the header in the answer above works, I used it to check versus my own code and this header is rather foolproof.

Lecky answered 6/10, 2012 at 16:45 Comment(0)
W
11

As per the header is concern, I had followed this code (if it's helps you some way).

byte[] header = new byte[44];

        header[0] = 'R';  // RIFF/WAVE header
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';
        header[4] = (byte) (totalDataLen & 0xff);
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = 'W';
        header[9] = 'A';
        header[10] = 'V';
        header[11] = 'E';
        header[12] = 'f';  // 'fmt ' chunk
        header[13] = 'm';
        header[14] = 't';
        header[15] = ' ';
        header[16] = 16;  // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        header[20] = 1;  // format = 1
        header[21] = 0;
        header[22] = (byte) channels;
        header[23] = 0;
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        header[32] = (byte) (2 * 16 / 8);  // block align
        header[33] = 0;
        header[34] = RECORDER_BPP;  // bits per sample
        header[35] = 0;
        header[36] = 'd';
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

        out.write(header, 0, 44);
Wares answered 11/9, 2012 at 13:0 Comment(3)
Can you provide more details (e.g. possible values) on the variables (e.g. totalDataLen, byteRate) and how they relate to each other? Also, the value of header[32] does not depend on any variables?Underpart
This will not work exactly as stands without knowing how to fill those variables. You can find that information here. Also, the number of channels isn't correct if the value is > 255. As unlikely as it may seem, I am working on projects that use arbitrary input channels and store as WAV, so your writer wouldn't work. The fields should be: header[22] = (byte) (channels & 0xFF); header[23] = (byte) ((channels >> 8) & 0xFF); Also, another improvement is: header[32] = (byte) ((channels * RECORDER_BPP) / 8); // block alignApplique
Thank you! helped highlight that a space is required for "fmt "Bahena
S
4

Are you certain of the byte order? "RIFF", "WAV", "fmt", and "data" look fine but the numbers in the header may need to be a different order (little endian vs. big endian). You also don't need to convert to bytes manually using your intToByteArray method. You could use the writeInt and writeShort methods of DataOutputStream. For the first one, this would look something like:

outFile.writeInt(Integer.reverseBytes((int)myChunkSize));

For the shorts it'd be like:

outFile.writeShort(Short.reverseBytes((short)myFormat))

This way you also don't need to provide the offset and length (0, 4) numbers. It's nice.

Semidome answered 17/2, 2012 at 15:41 Comment(0)
T
3

As Ronald Kunenborg correctly states the problem is the Litte Endian / Big Endian conversion.

The simplest way is to write a short helper like this:

public static void writeShortLE(DataOutputStream out, short value) {
  out.writeByte(value & 0xFF);
  out.writeByte((value >> 8) & 0xFF);
}

This is very helpful if you record audio to a wave file with Android and you're in need of the short array, too.

(Credits: https://mcmap.net/q/426837/-java-writing-in-file-little-endian)

Tie answered 30/11, 2014 at 16:58 Comment(0)
P
1

The following is an example that creates a .wav file in Little Endian format with a proper header. It then appends empty audio data for a specified duration in seconds. In you case you want to append you recorded audio.

public static void createEmptyWaveFile(int sampleRate, short sampleSize, short channels, int duration, File file)
            throws IOException {

        // calculate some
        short blockAlign = (short) (sampleSize * channels / 8);
        int byteRate = sampleRate * sampleSize * channels / 8;
        int audioSize = byteRate * duration;
        int fileSize = audioSize + 44;

        // create an Array that contains empty audio data for the given duration
        byte[] audioData = new byte[audioSize];
        for (int i = 0; i < audioData.length; i++)
            audioData[i] = (byte) 0;

        // The stream that writes the audio file to the disk
        DataOutputStream out = new DataOutputStream(new FileOutputStream(file));

        // Write Header
        out.writeBytes("RIFF");// 0-4 ChunkId always RIFF
        out.writeInt(Integer.reverseBytes(fileSize));// 5-8 ChunkSize always audio-length +header-length(44)
        out.writeBytes("WAVE");// 9-12 Format always WAVE
        out.writeBytes("fmt ");// 13-16 Subchunk1 ID always "fmt " with trailing whitespace
        out.writeInt(Integer.reverseBytes(16)); // 17-20 Subchunk1 Size always 16
        out.writeShort(Short.reverseBytes((short) 1));// 21-22 Audio-Format 1 for PCM PulseAudio
        out.writeShort(Short.reverseBytes(channels));// 23-24 Num-Channels 1 for mono, 2 for stereo
        out.writeInt(Integer.reverseBytes(sampleRate));// 25-28 Sample-Rate
        out.writeInt(Integer.reverseBytes(byteRate));// 29-32 Byte Rate
        out.writeShort(Short.reverseBytes(blockAlign));// 33-34 Block Align
        out.writeShort(Short.reverseBytes(sampleSize));// 35-36 Bits-Per-Sample
        out.writeBytes("data");// 37-40 Subchunk2 ID always data
        out.writeInt(Integer.reverseBytes(audioSize));// 41-44 Subchunk 2 Size audio-length

        // Append the silent audio data or what you recorded from the mic
        out.write(audioData);
        out.close();// close the stream properly
    }

    public static void main(String[] args) throws IOException {
        createEmptyWaveFile(44100, (short) 16, (short) 2, 10, new File("out.wav"));
    }
Picnic answered 30/12, 2021 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.