0

I want to get voice records in a .wav file in pcm format in Android eclipse. I see data in sdcard0, but I can not listen. So I wonder about I saved or not. I read that I should add chunks i.e: RIFF header, FMT and DATA chunks.How can I do add? If anybody knows how can I do that ı will appreciate. Please give me some more hints. Thank you.`

 public static final int FREQUENCY = 44100;
 public static final int CHANNEL_CONFIGURATION = AudioFormat.CHANNEL_CONFIGURATION_MONO;
 public static final int AUDIO_ENCODING =  AudioFormat.ENCODING_PCM_16BIT;
 public String folder_main = "Rize.wav";

and its codes are like these;

enter coprivate void recordSound(){
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ folder_main);

        // Delete any previous recording.
        if (file.exists())
                file.delete();

        try {
                        file.createNewFile();

                        // Create a DataOuputStream to write the audio data into the saved file.
                        OutputStream os = new FileOutputStream(file);
                        BufferedOutputStream bos = new BufferedOutputStream(os);
                        DataOutputStream dos = new DataOutputStream(bos);



                        // Create a new AudioRecord object to record the audio.
                        int bufferSize = AudioRecord.getMinBufferSize(FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING);
                        AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING, bufferSize);
                          short[] buffer = new short[bufferSize]; 
                        audioRecord.startRecording();
                         isRecording = true;

                        while (isRecording) {
                                int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                                for (int i = 0; i < bufferReadResult; i++)
                                        dos.writeShort(buffer[i]);
                        }


                        audioRecord.stop();
                        audioRecord.release();
                        dos.close();
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

    }de here
Gianna
  • 11
  • 6

1 Answers1

1

I had a similar need, found this, and it worked:

The method below takes your pcmdata as a short array, adds the proper header for wav (based on your sample rate, number of channels, and bit format), and writes to a dataoutputstream (file).

ex// PCMtoFile(myDataOutStream, myPCMShortArray, 44100, 2, 16);

public static void PCMtoFile(OutputStream os, short[] pcmdata, int srate, int channel, int format) throws IOException {
    byte[] header = new byte[44];
    byte[] data = get16BitPcm(pcmdata);

    long totalDataLen = data.length + 36;
    long bitrate = srate * channel * format;

    //Marks the file as a riff file. Characters are each 1 byte long. 
    header[0] = 'R'; 
    header[1] = 'I';
    header[2] = 'F';
    header[3] = 'F';
    //Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation. 
    header[4] = (byte) (totalDataLen & 0xff);
    header[5] = (byte) ((totalDataLen >> 8) & 0xff);
    header[6] = (byte) ((totalDataLen >> 16) & 0xff);
    header[7] = (byte) ((totalDataLen >> 24) & 0xff);
   //File Type Header. For our purposes, it always equals "WAVE". 
    header[8] = 'W';
    header[9] = 'A';
    header[10] = 'V';
    header[11] = 'E';
    //Format chunk marker. Includes trailing null 
    header[12] = 'f'; 
    header[13] = 'm';
    header[14] = 't';
    header[15] = ' ';
    //Length of format data as listed above 
    header[16] = (byte) format; 
    header[17] = 0;
    header[18] = 0;
    header[19] = 0;
    //Type of format (1 is PCM) - 2 byte integer        
    header[20] = 1; 
    header[21] = 0;
  //Number of Channels - 2 byte integer 
    header[22] = (byte) channel; 
    header[23] = 0;
    //  Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT). 
    //Sample Rate = Number of Samples per second, or Hertz. 
    header[24] = (byte) (srate & 0xff);
    header[25] = (byte) ((srate >> 8) & 0xff);
    header[26] = (byte) ((srate >> 16) & 0xff);
    header[27] = (byte) ((srate >> 24) & 0xff);
    // (Sample Rate * BitsPerSample * Channels) / 8. 
    header[28] = (byte) ((bitrate / 8) & 0xff);
    header[29] = (byte) (((bitrate / 8) >> 8) & 0xff);
    header[30] = (byte) (((bitrate / 8) >> 16) & 0xff);
    header[31] = (byte) (((bitrate / 8) >> 24) & 0xff);
    //(BitsPerSample * Channels) / 8.1 - 8 bit mono2 - 
    //8 bit stereo/16 bit mono4 - 16 bit stereo 
    header[32] = (byte) ((channel * format) / 8); 
    header[33] = 0;
    //Bits per sample 
    header[34] = 16; 
    header[35] = 0;
    //"data" chunk header. Marks the beginning of the data section. 
    header[36] = 'd';
    header[37] = 'a';
    header[38] = 't';
    header[39] = 'a';
    //  Size of the data section. 
    header[40] = (byte) (data.length  & 0xff);
    header[41] = (byte) ((data.length >> 8) & 0xff);
    header[42] = (byte) ((data.length >> 16) & 0xff);
    header[43] = (byte) ((data.length >> 24) & 0xff);
    //write the byte form header
    os.write(header, 0, 44);
    //write the byte form data
    os.write(data);
    //close the output stream
    os.close();
}
DanTheMan
  • 11
  • 1