我正在尝试创建声音混响效果。它工作得很好,但有一个问题:它以不同的偏移量和不同的音量复制一个巨大的数组数千次,并且需要 10 分钟来处理一个 4 分钟音频样本的小混响效果。有没有办法加快这个过程?如果需要,我可以轻松列出所有延迟。
编辑:这是我的代码。有没有更好的方法来达到相同或相似的结果?SoundSource 是一个包含声音和位置的类。WaveNode 包含一个位置以及 WavePath 实例的 ArrayList,这些实例定义了有关回波的各种参数。请告诉我是否还有其他不清楚的地方。
public static double[][] render(int sampleLength, int sampleRate, int depth) //depth is number of echoes, must be at least 0
{
init(sampleLength, sampleRate);
for (int iSource = 0; iSource < sourceList.size(); iSource++)
{
renderNode(sourceList.get(iSource).getSoundSource(), sourceList.get(iSource), 0, depth);
}
double[][] r = sampleData.clone();
sampleData = null;
return r;
}
private static void renderNode(SoundSource source, WaveNode node, int curDepth, int maxDepth)
{
SoundSource newSource;
WaveNode newNode;
WavePath path;
addSource(source.clone(), node.smoothing, node.decay);
if (curDepth < maxDepth)
{
for (int i = 0; i < node.getPathList().size(); i++)
{
path = node.getPathList().get(i);
newNode = findReflection(path.destId);
newSource = SoundSource.reflection(SoundSource.swapData(source, Filter.amplify(source.getSampleData(), path.decay)), newNode.getPos(), path.smoothing);
renderNode(newSource, newNode, curDepth + 1, maxDepth);
}
}
}
private static void addSource(SoundSource source, double smoothing, double decay)
{
Vector3d pos, pos2;
double dist, volume, soundPos, sample;
int delay;
double[] sourceData;
for (int channel = 0; channel < 2; channel++)
{
pos = source.getPos();
pos2 = new Vector3d(pos.x, pos.y, pos.z);
pos2.x += earDist / 2 - earDist * channel;
dist = Math.max(pos2.length() + source.getSourceDist(), 0.000001);
volume = decay / dist;
delay = (int)(dist / 343.2 * sampleRate);
sourceData = source.getSampleData();
sample = 0;
for (int j = 0; j < Math.min(sourceData.length, sampleLength - delay); j++)
{
sample = sourceData[j] / smoothing + sample * (1 - 1 / smoothing);
sampleData[channel][j + delay] += sample * volume;
}
}
}