本文介绍了如何合并/合并两个wav文件到一个wav文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以合并使用Java 2 wav文件?

How can I merge two wav files using java?

我试过但它没有正常工作,是他们任何其他方式做到这一点?

I tried this but it didn't work correctly, is their any other way to do it?

推荐答案

如果您有wav文件的字节工作直接可以使用同样的策略在任何编程语言。在这个例子中,我会假设两个源文件具有相同的比特率/ numchannels和长度相同/大小。
(如果没有,你可能可以在启动合并之前对其进行修改)。

If you work with the bytes of a wav file directly you can use the same strategy in any programming language. For this example I'll assume the two source files have the same bitrate/numchannels and are the same length/size.(if not you can probably edit them before starting the merge).

在WAV specificaiton首先来看看,我发现在的课程网站一个很好的:

First look over the wav specificaiton, I found a good one at a stanford course website:

常见的头部长度是44或46字节。

Common header lengths are 44 or 46 bytes.

如果你想连接两个文件(即打一WAV那么其他在一个单一的文件):

If you want to concatenate two files (ie play one wav then the other in a single file):


  1. 找出格式化的WAV文件

  2. 砍掉第44/46个字节是头,该文件的剩余部分是数据

  3. 创建一个新的文件,并坚持在头之一。

  1. find out what format your wav files are
  2. chop off the first 44/46 bytes which are the headers, the remainder of the file is the data
  3. create a new file and stick one of the headers in that.

新的wav文件= {标题} = {} 44/46字节长

new wav file = {header} = {44/46} bytes long

从原来的文件添加两个数据部分

add the two data parts from the original files

新的wav文件= {头+数据1 +数据2} = {44/46 +尺寸(数据1)+尺寸(数据2)}字节长

new wav file = {header + data1 + data2 } = {44/46 + size(data1) + size(data2)} bytes long

修改你的头在两个地方,以反映新的文件的长度。

modify your header in two places to reflect the new file's length.

一个。修改字节4 + 4(即从4个字节偏移4)。
新的值应该是一个十六进制数再presenting新的wav文件的字节{44/46 +尺寸(数据1)+尺寸(数据2)}大小 - 8个字节

a. modify bytes 4+4 (ie. 4 bytes starting at offset 4).The new value should be a hexadecimal number representing the size of the new wav file in bytes {44/46 + size(data1) + size(data2)} - 8bytes.

乙。修改字节40 + 4或42 + 4(起始于4字节偏移40或42,取决于如果您有一个44byte的头或46字节的头上)。
新的值应该是一个十六进制数再presenting新wav文件的总大小。即{44/46 +尺寸(数据1)+尺寸(数据2)}

b. modify bytes 40+4 or 42+4 (the 4 bytes starting at offset 40 or 42, depending on if you have a 44byte header or 46 byte header).The new value should be a hexadecimal number representing the total size of the new wav file. ie {44/46 + size(data1) + size(data2)}

如果你想,而不是合并或两个文件混合(让他们都在同一时间玩的话):

If you want to instead merge or mix the two files (so that they both play at the same time then):


  1. 您不会有如果这两个文件是相同的长度,以编辑页眉。

  2. 开始字节44/46你将不得不修改每个样本是在数据1 +值在数据2的值。
    因此,例如,如果你的采样率是8位应该修改1字节,如果你的采样率为16位,你会修改2个字节。
    该文件的其余部分仅仅是1/2字节存储再$ P $当时psenting声音的波形一个int值的样品。

  1. you won't have to edit the header if both files are the same length.
  2. starting at byte 44/46 you will have to edit each sample to be the value in data1 + the value in data2.so for example if your SampleRate was 8 bits you would modify 1 byte, if your sample rate was 16bits you would modify 2 bytes.the rest of the file is just Samples of 1/2bytes storing an int value representing the waveform of the sound at that time.

一个。对于每一个文件中的剩余样品的抢1/2字节十六进制字符串,并从两个文件data1和data2的int值。

a. For each of the remaining samples in the file grab the 1/2 byte hex string and get the int value from both files data1 and data2.

乙。添加1/2字节整数在一起
结果转换回十六进制和输出文件使用该值。

b. add the 1/2 byte integers togetherconvert the result back to hexadecimal and use that value in your output file.

℃。通常必须除以2该号码以获得适合回到原来的1/2字节样本块的平均值。我是越来越扭曲,当我在objc尝试过(可能与符号或无符号整数),只是跳过了分裂的部分,因为它只会很可能是一个问题,如果你正在合并非常响亮的声音在一起。
即当数据1 +数据2大于1/2个字节的声音剪辑。有一个关于裁剪问题的讨论here你可能想尝试那些剪裁技术之一。

c. You normally have to divide that number by 2 to get an average value that fits back in the original 1/2byte sample block. I was getting distortion when i tried it in objc(probably related to signed or unsigned ints) and just skipped the division part since it will only likely be a problem if you are merging very loud sounds together.ie when data1 + data2 is larger than 1/2 bytes the sound will clip. There was a discussion about the clipping issue here and you may want to try one of those clipping techniques.

这篇关于如何合并/合并两个wav文件到一个wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:07