以编程方式合并两个音频片段

发布于 2024-10-19 02:51:26 字数 293 浏览 1 评论 0原文

我有两个不同音频剪辑的两个样本数组。如果我只是以编程方式将它们添加在一起,这是否相当于在音频编辑套件中将一个轨道叠加在另一个轨道上?就像如果我有一个贝斯的音频片段和另一个鼓的音频片段,我希望它们一起演奏。

我可能会做这样的事情:

 for (int i = 0; i < length_of_array; i++){
   final_array[i] = first_array[i] + second_array[i];
 }

如果不这样做,我可以获得一些关于正确方法的指示吗?

I have two arrays of samples of two different audio clips. If I just programmatically add them together will this be the equivalent of layering one track over another in an audio editing suite? Like if I have one audio clip of bass the other of a drum and I want them playing together.

I would probably do something like this:

 for (int i = 0; i < length_of_array; i++){
   final_array[i] = first_array[i] + second_array[i];
 }

If it is not done this way, could I get some indication of what would be the correct way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-10-26 02:51:26

这是一个正确的方法。合并在音频术语中称为“MIXING”。

但是:

如果您的样本很短(16 位签名) - 您将必须使用 int (32 位签名)进行添加,然后手动剪辑样本。如果不这样做,您的值将会换行,并且您会在听自己所做的事情时获得很多乐趣:)

这是代码:

short first_array[1024];
short second_array[1024];
short final_array[1024];
for (int i = 0; i < length_of_array; i++)
{
    int mixed=(int)first_array[i] + (int)second_array[i];
    if (mixed>32767) mixed=32767;
    if (mixed<-32768) mixed=-32768;
    final_array[i] = (short)mixed;
}

在大多数情况下,您不需要任何其他内容来处理正常的音频样本,因为会发生削波在极其罕见的条件下。我是从实践而不是从理论来谈论这个。

This IS a correct way. Merging is called MIXING in audio jargon.

BUT:

If your samples are short (16 bit signed) - you will have to use int (32 bit signed) for addition and then clip the samples manually. If you don't, your values will wrap and you'll have so much fun listening to what you did :)

Here comes the code:

short first_array[1024];
short second_array[1024];
short final_array[1024];
for (int i = 0; i < length_of_array; i++)
{
    int mixed=(int)first_array[i] + (int)second_array[i];
    if (mixed>32767) mixed=32767;
    if (mixed<-32768) mixed=-32768;
    final_array[i] = (short)mixed;
}

In MOST cases you don't need anything else for normal audio samples, as the clipping will occur in extremely rare conditions. I am talking this from practice, not from theory.

木有鱼丸 2024-10-26 02:51:26

如果两个音频剪辑的采样率和所需的混合级别相同,则上述合并方法将起作用。如果所需的混合级别不同,那么混合器的稍微更通用的形式将类似于:

mixed_result[i] = rescale_and_clip_fix( volume1 * input1[i] + volume2 * input2[i] );

其中 rescale_and_clip_fix() 可能是限制器或压缩器,然后确保乘法后的比例对于结果的数据类型是正确的。如果结果数组是整数数据类型,那么您可能还想在缩放时进行舍入或噪声过滤。

如果采样率不同,则您需要先对其中一个输入通道进行采样率转换,然后再对结果进行转换。

Your above merging method will work if the sample rates and desired mix level of the two audio clips are identical. If the desired mix levels are different, then a slightly more general form of your mixer would be something like:

mixed_result[i] = rescale_and_clip_fix( volume1 * input1[i] + volume2 * input2[i] );

Where rescale_and_clip_fix() might be a limiter or compressor, following making sure the scale after multiplication is correct for the result's data type. If the result array is an integer data type, then you may also want to do rounding or noise filtering while scaling.

If the sample rates are different, then you will need to do a sample rate conversion on one of the input channels first and/or the result afterwards.

ζ澈沫 2024-10-26 02:51:26

一般来说,这会得到你想要的 - 但是,请注意剪辑。也就是说,要小心不要以整数溢出告终;并且不要通过仅将值限制为相关类型的最大值/最小值来避免这种情况。添加值后,您可能需要应用压缩器将值恢复到范围内。

In general, this will get you what you want - however, watch for clipping. That is, be careful not to end up with integer overflow; and don't avoid this by just limiting the value to the max/minimum of the type in question. You may need to apply a compressor to bring the values back into range after adding them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文