Android将PCM字节数组写入wav文件。
创始人
2024-10-08 12:31:12
0

以下是一个示例代码,演示如何将PCM字节数组写入WAV文件:

import java.io.FileOutputStream;
import java.io.IOException;

public class PCMToWavConverter {

    private static final int RECORDER_BPP = 16;
    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = 1;

    public static void convertPCMToWAV(byte[] pcmData, String filePath) throws IOException {
        long totalAudioLen = pcmData.length;
        long totalDataLen = totalAudioLen + 36;
        long longSampleRate = RECORDER_SAMPLERATE;
        int channels = RECORDER_CHANNELS;
        long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;

        FileOutputStream wavFile = new FileOutputStream(filePath);
        writeWaveFileHeader(wavFile, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate);
        wavFile.write(pcmData);
        wavFile.close();
    }

    private static void writeWaveFileHeader(FileOutputStream wavFile, long totalAudioLen, long totalDataLen,
            long longSampleRate, int channels, long byteRate) throws IOException {
        byte[] header = new byte[44];

        header[0] = 'R'; // RIFF/WAVE header
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';
        header[4] = (byte) (totalDataLen & 0xff);
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = 'W';
        header[9] = 'A';
        header[10] = 'V';
        header[11] = 'E';
        header[12] = 'f'; // 'fmt ' chunk
        header[13] = 'm';
        header[14] = 't';
        header[15] = ' ';
        header[16] = 16; // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        header[20] = 1; // format = 1
        header[21] = 0;
        header[22] = (byte) channels;
        header[23] = 0;
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        header[32] = (byte) (2 * 16 / 8); // block align
        header[33] = 0;
        header[34] = RECORDER_BPP; // bits per sample
        header[35] = 0;
        header[36] = 'd';
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

        wavFile.write(header, 0, 44);
    }
}

使用方法:

byte[] pcmData = // your PCM byte array
String filePath = // your output WAV file path

try {
    PCMToWavConverter.convertPCMToWAV(pcmData, filePath);
    System.out.println("PCM to WAV conversion completed.");
} catch (IOException e) {
    e.printStackTrace();
}

以上代码将接收一个PCM字

相关内容

热门资讯

透视软件!wepoker挂,德... 透视软件!wepoker挂,德普之星透视辅助,总结教程(切实真的有挂)-哔哩哔哩1、下载好德普之星透...
透视ai代打!aapoker发... 透视ai代打!aapoker发牌逻辑(透视)开挂辅助神器(竟然有挂)-哔哩哔哩aapoker发牌逻辑...
透视软件!poker mast... 透视软件!poker master辅助(透视)开挂辅助方法(确实是真的挂)-哔哩哔哩poker ma...
透视ai!德州局怎么透视,hh... 透视ai!德州局怎么透视,hhpoker有辅助吗,举措教程(原来真的有挂)-哔哩哔哩1、德州局怎么透...
透视规律!hhpoker一直输... 透视规律!hhpoker一直输有挂吗,hhpoker万能辅助器,攻略教程(总是有挂)-哔哩哔哩所有人...
透视苹果版!德州私人局脚本(透... 透视苹果版!德州私人局脚本(透视)开挂辅助软件(切实有挂)-哔哩哔哩小薇(透视辅助)致您一封信;亲爱...
透视ai!hhpoker有没有... 透视ai!hhpoker有没有辅助辅助,智星德州插件,指引教程(一直有挂)-哔哩哔哩1、hhpoke...
透视新版!wepoker怎么提... 透视新版!wepoker怎么提高运气(透视)开挂辅助挂(确实存在有挂)-哔哩哔哩1、透视新版!wep...
透视玄学!epoker透视底牌... 您好,哈糖大菠萝开挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
透视开挂!we-poker辅助... 透视开挂!we-poker辅助软件教程(透视)开挂辅助app(总是是有挂)-哔哩哔哩1)we-pok...