写PCM记录的数据为.wav文件(java的机器人)机器人、文件、数据、PCM

2023-09-12 04:03:57 作者:长大后娶你

我用AudioRecord记录在Android的16位PCM数据。记录的数据,并将其保存到一个文件后,我读它回到它保存为.wav文件。

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.

的问题是,WAV文件是由媒体播放器识别,但玩什么,但纯噪音。此刻我最好的猜测是,我的WAV文件头是不正确的,但我一直无法看到究竟是什么问题。 (我想这是因为我可以发挥我记录在Audacity的原始PCM数据)

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)

下面是我的code读取原始PCM文件并将其保存为一个.wav:

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)};
    }

我还没有包括getBytesFromFile(),因为它占用了太多的空间,它是一个久经考验的方法。 总之,这里的code,做实际的记录:

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.

推荐答案

我一直在摔跤几个小时这个确切同样的问题,现在,我的问题主要是,在16位录音时,你必须非常小心什么你写的输出。 WAV文件预计,小端格式的数据,但使用与writeShort写入为大端输出。我也得到了有趣的结果使用其他功能的时候,所以我回到了正确的顺序写入字节并且工作。

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.

我用一个十六进制编辑器,广泛调试时这一点。我可以推荐你做同样的。此外,在上述工作答案的头,我用它来检查对自己的code,这头是相当简单的。

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.

 
精彩推荐
图片推荐