-1

I am trying to play a PCM file which was created while recording audio from the microphone.

I am using AudioRecord because I want to analyse the frequency of the sound which is recorded by the microphone.

The snippet of code which plays the PCM file is as below. But when I tried to play the file, there is a heavy noise which is being played.

FileInputStream fis=null;
    File l=null;
    l=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Notate/f1.pcm");

    byte[] buffer=new byte[(int)l.length()];

    try {
        fis = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/f1.pcm");
        fis.read(buffer);
    }
    catch(Exception e)
    {

    }
    int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);
    if (at!=null) {
        at.play();
        at.write(buffer, 0, buffer.length);
        at.stop();
        at.release();
    }

The code which is used to store the PCM file is below.

private class RecordAudio extends AsyncTask<Void, Double, Void> {
    FileOutputStream os = null;
    BufferedOutputStream bos =null;
    DataOutputStream dos = null;
    @Override
    protected Void doInBackground(Void... params) {
        int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding);                // Gets the minimum buffer needed
        AudioRecord audioRecord = new AudioRecord(audioSource, sampleRate, channelConfig, audioEncoding, bufferSize);   // The RAW PCM sample recording
        File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Notate/");
        if(!f.isDirectory()) {
            File newDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/");
            newDirectory.mkdirs();
        }
        String filepath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Notate/f1.pcm";
        short[] buffer = new short[blockSize];          // Save the raw PCM samples as short bytes
        try{
            os=new FileOutputStream(filepath);
        }
        catch(FileNotFoundException e)
        {

        }
        bos = new BufferedOutputStream(os);
        dos = new DataOutputStream(bos);
        try {
            audioRecord.startRecording();  //Start
        } catch (Throwable t) {
        }
        while (started) {
            int length=audioRecord.read(buffer, 0, blockSize);

            Yin alpha = new Yin(44100, 1024, 0.2);
            float[] floaters = new float[buffer.length];
            for (int i = 0; i < buffer.length; i++) {
                floaters[i] = buffer[i];
            }
            for(int k=0;k<length;k++) {
                try {
                    dos.writeShort(buffer[k]);


                } catch (IOException e) {

                }
            }
            float result = alpha.getPitch(floaters);
            publishProgress((double) result);

        }
        try{
            dos.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }

        return null;
    }
shivram
  • 469
  • 2
  • 10
  • 26

1 Answers1

0

writeShort writes the values in a big-endian layout, i.e. the most significant byte first, while AudioTrack probably expects the samples to be little-endian (least significant byte first).

You'd make things simpler for yourself if you used a byte[] instead of a short[] when recording. Then you can simply write to your DataOutputStream with dos.write(buffer, 0, length);. Your conversion to floating-point values would have to be modified a bit in that case:

for (int i = 0; i < length; i += 2) {
    floaters[i] = (short)(buffer[i] | (buffer[i+1] << 8));
}
Michael
  • 57,169
  • 9
  • 80
  • 125