我生成不同频率的声音样本(sin /锯/三角形发生器)作为双值[-1 ... 1](1-最大幅度)的数组。我想将所有信号合并为一个。
1)如果我添加( combineWithNormalize )并最终归一化为[-1 ... 1] - 声音质量很好,但信号太沉默。
2)如果我添加使用线性( combineWithLinearDynaRangeCompression )或log( combineWithLnDynaRangeCompression )压缩 - 信号更响亮,但质量很差(金属探测)。 我究竟做错了什么?我想我错过了处理步骤。 什么是通常可接受的算法,用于添加来自几个源wav文件的音频信号并创建最终文件(哪些方法像Yamaha等合成器用于此目的)?
Additionals :
- 我生成的音频(梳理两个样本:top - combineWithNormalize,bottom - combineWithLnDynaRangeCompression)。顶部信号安静,但正确。底部 - 更响亮,但可怕。 audio samples
-Java代码(草稿,未优化):
// add samples and linear normalize to [-1,1]
public static double[] combineWithNormalize( double[]... audio) {
if (audio.length == 0) return null;
if (audio.length == 1) return audio[0];
int maxIdx = 0;
// look for the longest sample
for(double[] arr: audio)
if (arr.length > maxIdx) maxIdx = arr.length;
// add 0 to the end of short samples
for(int i=0; i < audio.length; i++)
if (audio[i].length < maxIdx) audio[i] = Arrays.copyOf(audio[i], maxIdx);
// add all samples to result (+ find absolute max value)
double[] result = new double[maxIdx];
double normalizer = 1.0;
for (int i = 0; i < maxIdx; i++) {
for (int j = 0; j < audio.length; j++)
result[i] += audio[j][i];
double res = Math.abs(result[i]);
if (res > normalizer)
normalizer = res;
}
//normalize rezult
double coeff = 1.0/ normalizer;
if (normalizer !=1.0)
for (int i = 0; i < maxIdx; i++)
result[i] *= coeff;
return result;
}
// add samples and liners compression (all samples must be [-1..1])
public static double[] combineWithLinearDynaRangeCompression(double threshold, double[]... audio) {
if (audio.length == 0 || threshold >= 1 || threshold < 0) return null;
if (audio.length == 1) return audio[1];
int maxIdx = 0;
// look for the longest sample
for(double[] arr: audio)
if (arr.length > maxIdx) maxIdx = arr.length;
// add 0 to the end of short samples
for(int i=0; i < audio.length; i++)
if (audio[i].length < maxIdx) audio[i] = Arrays.copyOf(audio[i], maxIdx);
double[] result = Arrays.copyOf(audio[0], maxIdx); // Copy first sample
double linearCoeff = (1-threshold)/(2-threshold);
// Add all samples to first + compression
for (int j = 0; j < maxIdx; j++) {
double res = 0;
for (int i = 1; i < audio.length; i++)
res = result[j] + audio[i][j];
double absRes = Math.abs(res);
result[j] = (absRes <= threshold) ? res : Math.signum(res) * (threshold + linearCoeff * (absRes - threshold));
}
return result;
}
// add samples and log compression (all samples must be [-1..1])
public static double[] combineWithLnDynaRangeCompression(double threshold, double[]... audio) {
if (audio.length == 0 || threshold >= 1 || threshold < 0) return null;
if (audio.length == 1) return audio[0];
int maxIdx = 0;
// look for the longest sample
for(double[] arr: audio)
if (arr.length > maxIdx) maxIdx = arr.length;
// add 0 to the end of short samples
for(int i=0; i < audio.length; i++)
if (audio[i].length < maxIdx) audio[i] = Arrays.copyOf(audio[i], maxIdx);
double[] result = Arrays.copyOf(audio[0], maxIdx); // Copy first sample
double expCoeff = alphaT[(int) threshold*100];
// Add all samples to first + compression
for (int j = 0; j < maxIdx; j++) {
double res = 0;
for (int i = 1; i < audio.length; i++)
res = result[j] + audio[i][j];
double absRes = Math.abs(res);
result[j] = (absRes <= threshold) ? res :
Math.signum(res) * (threshold + ( 1 - threshold) *
Math.log(1.0 + expCoeff * (absRes-threshold) /(2-threshold)) / Math.log(1.0 + expCoeff ));
}
return result;
}
// Solutions of equations pow(1+x,1/x)=exp((1-t)/(2-t)) for t=0, 0.01, 0.02 ... 0.99
final private static double[] alphaT =
{
2.51286, 2.54236, 2.57254, 2.60340, 2.63499, 2.66731, 2.70040, 2.73428, 2.76899, 2.80454,
2.84098, 2.87833, 2.91663, 2.95592, 2.99622, 3.03758, 3.08005, 3.12366, 3.16845, 3.21449,
3.26181, 3.31048, 3.36054, 3.41206, 3.46509, 3.51971, 3.57599, 3.63399, 3.69380, 3.75550,
3.81918, 3.88493, 3.95285, 4.02305, 4.09563, 4.17073, 4.24846, 4.32896, 4.41238, 4.49888,
4.58862, 4.68178, 4.77856, 4.87916, 4.98380, 5.09272, 5.20619, 5.32448, 5.44790, 5.57676,
5.71144, 5.85231, 5.99980, 6.15437, 6.31651, 6.48678, 6.66578, 6.85417, 7.05269, 7.26213,
7.48338, 7.71744, 7.96541, 8.22851, 8.50810, 8.80573, 9.12312, 9.46223, 9.82527, 10.21474,
10.63353, 11.08492, 11.57270, 12.10126, 12.67570, 13.30200, 13.98717, 14.73956, 15.56907, 16.48767,
17.50980, 18.65318, 19.93968, 21.39661, 23.05856, 24.96984, 27.18822, 29.79026, 32.87958, 36.59968,
41.15485, 46.84550, 54.13115, 63.74946, 76.95930, 96.08797, 125.93570, 178.12403, 289.19889, 655.12084
};
提前致谢。
答案 0 :(得分:0)
我还没有对您的代码进行过测试,但我会分享一些常规提示:
&#34;我以不同的频率生成声音样本(sin / saw / triangle generator)&#34;
所以你在一些字节数组中有PCM样本。假设16位,每个短 @ [i]
保持样本 @ [i]
的幅度。 [i]
是您在总样本数量中的位置。
...&#34;作为
Double
值的数组&#34; ...
对于您的数字声音(PCM),您应该使用Floats
。您的输入是否以16位格式发声?您可以稍后转换为16位值整数(或短裤)。
另请查看另一个答案:https://stackoverflow.com/a/10325317/2057709
问题......
&#34;将音频样本添加到没有剪辑的#34;
的正确方法是什么?
使用+
添加 ing有什么问题?
final_sample[i] = ( sourceA[i] /2 ) + ( sourceB[i] /2 ); //divide by 2 to halve amplitudes
我们将2除以每个源的幅度减半。这样,即使每个Source
的样本值(幅度)为1.0
,在混合期间,它们也会将0.5
作为其最大值。
混合final_sample
现在总计为1.0
。希望没有剪辑。
&#34; 1)如果我添加(
combineWithNormalize
)并最终规范化为[-1...1]
:
结果:声音质量很好,但信号太沉默。&#34;
通过乘以样本值来尝试提升信号。例如:signal * 2.0 //double volume
。
PS:查看此文章+其他 Stack Exchange 任何想法的答案:
(1) blog: Mix Audio Samples on iOS(尝试相同的逻辑,代码很容易理解)。
(2) SO: Modify volume gain on audio sample buffer。
(3) SO: Mixing PCM audio samples。
(4) SO: Algorithm To Mix Sound。
(5) DSP: Algorithm(s) to mix audio signals without clipping。