Question

        File pcmFile = new File(mediaPath, TEMP_PCM_FILE_NAME);

        if (pcmFile.exists())
            pcmFile.delete();

        int total = 0;
        mAudioRecordInstance.startRecording();
        try {

            DataOutputStream pcmDataOutputStream = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(pcmFile)));

            while (isRecording) {
                mAudioRecordInstance.read(mBuffer, 0, mBufferSize);
                for (int i = 0; i < mBuffer.length; i++) {
                    Log.d("Capture", "PCM Write:["+i+"]:" + mBuffer[i]);
                    pcmDataOutputStream.writeShort(mBuffer[i]);
                    total++;
                }
            }
            pcmDataOutputStream.close();
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.ERROR_CREATING_FILE;
                    showDialog(e.getValue());
                    actionButton.performClick();
                }
            });
            return;
        } catch (OutOfMemoryError om) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.OUT_OF_MEMORY;
                    showDialog(e.getValue());
                    System.gc();
                    actionButton.performClick();
                }
            });
        }

        Log.d("Capture", "Stopping recording!!!");
        mAudioRecordInstance.stop();
        Log.d("Capture", "Processing starts");
        short[] shortBuffer = new short[total];

        try {
            DataInputStream pcmDataInputStream = new DataInputStream(
                    new BufferedInputStream(new FileInputStream(pcmFile)));


            for (int j = 0; pcmDataInputStream.available() > 0; j++) {
                shortBuffer[j] = pcmDataInputStream.readShort();
                Log.d("Capture", "PCM Read:[" + j + "]:" + shortBuffer[j] );
            }
            outStream.write(Utilities.shortToBytes(shortBuffer));
            pcmDataInputStream.close();

        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.ERROR_CREATING_FILE;
                    showDialog(e.getValue());
                    outFile = null;
                    actionButton.performClick();
                }
            });
            return;
        } catch (OutOfMemoryError om) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.OUT_OF_MEMORY;
                    showDialog(e.getValue());
                    System.gc();
                    actionButton.performClick();
                }
            });
        }

I am trying to write PCM data to temp file, so that I can later process it without loosing anything recordable. Initially I tried processing in the same recording loop but the recorded duration didn't matched with the actual duration. Now what I want is to read short from PCM file and write it to WAV file (Want to process short data later if this issue is fixed) with header. If I open the file in Audacity it is coming out to be empty. If I write directly to WAV file instead of temp PCM file it works fine.

Other issue is I am using the handler to run a thread in which I update the duration of recording and update VU meter view. I use mBuffer data to display in VU Meter view and is invalidated every second. No synchronization is used on the data but still it effects the recorded duration. Sometimes it comes out to be thrice the original duration.

Questions are (1) Why reading and writing PCM data to temp file is causing WAV file to be empty? Why reading from the unsynchronized short buffer (member variable) in a thread managed by handler is adding duration to WAV data, this happens when I write recorded buffer to WAV file directly?

Was it helpful?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top