gpt4 book ai didi

java - 混合波形文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:09 24 4
gpt4 key购买 nike

我无法混合两个音频扩展文件 wav。我的工作:

byte[] bufData1 = null;
byte[] bufData2 = null;
ArrayList<Byte> bufData3 = new ArrayList<Byte>();

使用原始音频数据创建两个数组

 public void bootloadInputData(String p1, String p2) throws IOException {
bufData1 = bootloadReadFileByte(p1);
bufData2 = bootloadReadFileByte(p2);
System.arraycopy(bufData1, 44, bufData1, 0, (bufData1.length - 44));
System.arraycopy(bufData2, 44, bufData2, 0, (bufData2.length - 44));
}

public byte[] bootloadReadFileByte(String path) throws IOException{
ByteArrayOutputStream out = null;
InputStream input = null;
try{
out = new ByteArrayOutputStream();
input = new BufferedInputStream(new FileInputStream(path));
int data = 0;
while((data = input.read()) != -1){
out.write(data);
}
}
finally{
if(null != input){
input.close();
}
if(null != out){
out.close();
}
}
return out.toByteArray();
}

混合原始音频数据的字节

public void bootloadOutputData() throws IOException {
for(int i = 0; i < ((bufData1.length + bufData2.length) / 4); i += 4) {
if(i < bufData1.length){
bufData3.add(bufData1[i]);
bufData3.add(bufData1[i+1]);
bufData3.add(bufData1[i+2]);
bufData3.add(bufData1[i+3]);
}
if(i < bufData2.length){
bufData3.add(bufData2[i]);
bufData3.add(bufData2[i+1]);
bufData3.add(bufData2[i+2]);
bufData3.add(bufData2[i+3]);
}
}
}

创建一个新文件,填写标题和原始音频数据。

private void bootloadCreateWaveMix(String p1, String p2, String p3) throws IOException {
int size1 = 0;
int size2 = 0;

FileInputStream fis1 = null;
FileInputStream fis2 = null;
try {
fis1 = new FileInputStream(p1);
fis2 = new FileInputStream(p2);
size1 = fis1.available();
size2 = fis2.available();
} finally {
if(fis1 != null){
fis1.close();
}
if(fis2 != null){
fis2.close();
}
}

int mNumBytes = (size1 + size2);

DataOutputStream out = null;
try {
out = new DataOutputStream(new FileOutputStream(p3));
writeId(out, "RIFF");
writeInt(out, 36 + mNumBytes);
writeId(out, "WAVE");

writeId(out, "fmt ");
writeInt(out, 16);
writeShort(out, (short) 1);
writeShort(out, (short) 4);
writeInt(out, (int) 44100);
writeInt(out, 2 * 44100 * 16 / 8);
writeShort(out, (short)(2 * 16 / 8));
writeShort(out, (short) 16);

writeId(out, "data");
writeInt(out, mNumBytes);

out.write(toByteArray(bufData3));
} finally {
if(out != null){
out.close();
}
}
}

private static void writeId(OutputStream out, String id) throws IOException {
for (int i = 0; i < id.length(); i++) out.write(id.charAt(i));
}

private static void writeInt(OutputStream out, int val) throws IOException {
out.write(val >> 0);
out.write(val >> 8);
out.write(val >> 16);
out.write(val >> 24);
}

private static void writeShort(OutputStream out, short val) throws IOException {
out.write(val >> 0);
out.write(val >> 8);
}

public static byte[] toByteArray(ArrayList<Byte> in) {
byte[] data = new byte[in.size()];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) in.get(i);
}
return data;
}

问题:

This code does not correctly create a file that the computer can not play, but the device can. Reproduction is bad, there is some kind of interference at the end of the merged files. Also, playback ends when the first file ends, even if the second file is larger than the first one. Another problem with the channels on the idea is two stereo files, and in the title I indicate 4 life even though 2. The files will always be 44100/16 bit / stereo

最佳答案

如果我理解正确,您需要执行以下操作:

  1. 给定 2 个输入 WAV 文件,将它们混合在一起形成一个 WAV 文件。
  2. 输出的内容将是同时播放的输入文件,而不是一个接一个地播放。
  3. 新文件的长度将是最长输入文件的长度。
  4. 所有文件(输入和输出)均为 16 位、立体声 44100Hz。

如果是这样的话,以下是您的(一些)错误:

  1. 您需要解析传入的文件,以免将其 header 作为音频数据读取(不要仅仅因为您已经知道音频的格式而跳过此步骤。您需要读取 header 以确认数据格式并准确确定输入中的样本数量。另请注意,2/16/44100 WAV 文件可以具有不同大小的 header ,因为它们可以包含各种 block ,因此您不能只跳过 X 字节,然后读取文件 - 您必须解析标题!)。
  2. 如果 WAV 文件都是 16 位,则需要将传入数据从字节转换为短字节(注意,这不是简单的类型转换 - 您必须将 2 个字节打包到每个短字节中。我相信您可以为此使用 DataInputStream,但一定要考虑字节序 - WAV 文件是小字节序,而 Java 是大字节序)。获得代表样本的短裤后,对各个文件中的短裤进行平均以进行混合。然后,必须将平均值转换回字节 (DataOutputStream) 以保存结果文件。当一个文件中的数据用完时,请替换零。
  3. 您对 numBytes 的计算不正确 - 它不是两个文件中原始字节的总和,而是更复杂的计算。在你的情况下,你希望它等于这样的东西:
 n1 = number of samples in file 1
n2 = number of samples in file 2
n = MAX( n1 + n2 )
numBytes = n * (number of channels) * (number of bytes per channel) = n * 2 * 2

我强烈建议您考虑使用像 JMF 这样的库来解决 1 和 2。

关于java - 混合波形文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44458548/

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