4
这里就是我与现在的工作:如何混合PCM音频源(Java)?
for (int i = 0, numSamples = soundBytes.length/2; i < numSamples; i += 2)
{
// Get the samples.
int sample1 = ((soundBytes[i] & 0xFF) << 8) | (soundBytes[i + 1] & 0xFF); // Automatically converts to unsigned int 0...65535
int sample2 = ((outputBytes[i] & 0xFF) << 8) | (outputBytes[i + 1] & 0xFF); // Automatically converts to unsigned int 0...65535
// Normalize for simplicity.
float normalizedSample1 = sample1/65535.0f;
float normalizedSample2 = sample2/65535.0f;
float normalizedMixedSample = 0.0f;
// Apply the algorithm.
if (normalizedSample1 < 0.5f && normalizedSample2 < 0.5f)
normalizedMixedSample = 2.0f * normalizedSample1 * normalizedSample2;
else
normalizedMixedSample = 2.0f * (normalizedSample1 + normalizedSample2) - (2.0f * normalizedSample1 * normalizedSample2) - 1.0f;
int mixedSample = (int)(normalizedMixedSample * 65535);
// Replace the sample in soundBytes array with this mixed sample.
soundBytes[i] = (byte)((mixedSample >> 8) & 0xFF);
soundBytes[i + 1] = (byte)(mixedSample & 0xFF);
}
从据我所知,这是该算法的精确表示此页面上的定义:http://www.vttoth.com/CMS/index.php/technical-notes/68
然而,仅仅混合声音沉默(全0)会产生很明显听起来不正确的声音,也许最好将其描述为更高调更响亮。
希望能帮助您确定我是否正确实施算法,或者如果我只是需要以不同的方式(不同的算法/方法)去解决它?
试过这段代码。经过少量编译和丢失括号错误后,当两个音频源混合时背景中仍然存在白噪声。是否还有其他遗漏? –
经过了很长时间的处理这个问题,我决定不使用这种转换方式,而是'ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer()。get(shorts);',在短裤上。然后返回字节ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer()。put(shorts);'..这完美地工作。 –