gpt4 book ai didi

android - 如何在android中将8位和16位pcm文件合并到单独的音频 channel 中

转载 作者:行者123 更新时间:2023-12-02 23:42:25 27 4
gpt4 key购买 nike

我知道有很多类似的问题,我阅读了很多,但我无法让它发挥作用。正如我在标题中所说,我有一个 8 位和一个 16 位 pcm 单声道文件,我想将它们合并到一个立体声波文件中,但在不同的 channel 中,一个在左 channel ,一个在右 channel 。

到目前为止,我已经完成了以下操作,但无论我做什么,都会出现两个问题:
1-我不能将它们写在输出文件的单独 channel 中。
2- 其中一个文件会写得像噪音(“RECORDER_BPP”是 8 还是 16)

我的代码:

new Thread(new Runnable() {
@Override
public void run() {
int channels = 2;
RECORDER_BPP = 16;
int RECORDER_SAMPLERATE = 44100;
int minBufferSize = AudioTrack.getMinBufferSize(maxCaptureRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

byte[] data = new byte[minBufferSize];
long longSampleRate = RECORDER_SAMPLERATE;
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;

try {
FileInputStream in1 = new FileInputStream(rawAudio1);
FileInputStream in2 = new FileInputStream(rawAudio2);
FileOutputStream out = new FileOutputStream(wavAudio);
long totalAudioLen = in1.getChannel().size();
long totalDataLen = totalAudioLen + 36;

WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);

while(in1.read(data) != -1){
out.write(data);

//2nd channel ?! // unsuccessful
in2.read(data);
out.write(data);
}

in1.close();
in2.close();
out.close();
} catch (Exception e) {
Log.d("EEE-2ch", e.getMessage());
}
}
}).start();

这是标题部分:
private void WriteWaveFileHeader(
FileOutputStream out, 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) (channels * RECORDER_BPP / 8); // block align
header[33] = 0;
header[34] = (byte) 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);

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

感谢您的帮助,谢谢

最佳答案

输出数据需要交错,这意味着您需要从 in1 读取单个样本,将其写入输出,然后从 in2 读取单个样本并将其写入输出。由于 in2 是 8 位音频,您需要读取 8 位样本,然后写出 16 位。

byte[] data = new byte[2];
while(in1.read(data) != -1) { // read 2 bytes
out.write(data); // write 2 bytes (L)
in2.read(data, 0, 1); // read 1 byte
data[1] = 0; // set the other byte to zero
// if this is the wrong byte order then do this
// data[1] = data[0];
// data[0] = 0;
out.write(data); // write 2 bytes (R)
}

此代码假定 in1 是具有 16 位音频的文件。如果那是错误的,那么您将需要对其进行一些修改。

关于android - 如何在android中将8位和16位pcm文件合并到单独的音频 channel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36695553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com