Reading .pcm file to double array
Asked Answered
B

0

2

I'm developing an android application. What I need to do is reading a per recorded audio file. Audio file has been recorded and saved as .pcm file. I use this code to read the .pcm file.

public double[] readPCM() {
    double[] result = null;

    try {
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/recording.pcm");
        InputStream in = new FileInputStream(file);
        int bufferSize = (int) (file.length()/2);

        result = new double[bufferSize];

        DataInputStream is = new DataInputStream(in);

        for (int i = 0; i < bufferSize; i++) {
            result[i] = is.readShort() / 32768.0;
        }

    } catch (FileNotFoundException e) {
        Log.i("File not found", "" + e);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;

}

I want to know is this code correct because in this code bufferSize is fileLength/2. It is confusing me. Please explain it to me. By the way it does return a double array with values. this code works fine but I want to make sure this works exactly. Thank you in advance.

Bridgettebridgewater answered 2/8, 2014 at 17:29 Comment(2)
Your use of readShort() implies that the data in the file is 16-bit values stored as pairs of consecutive bytes. So the count of values will be half the count of bytes. Don't forget there may be header data, and that the bytes might not be in the same order Java expects (there are two standards). Also the file could be either mono, or stereo with interleaved samples.Paragraphia
Thank you for your quick response @ChrisStratton . But in this question says raw pcm file doesn't keep header information. I'm new to audio processing.Bridgettebridgewater

© 2022 - 2024 — McMap. All rights reserved.