【发布时间】:2021-08-10 09:12:17
【问题描述】:
我有一个波形可视化器,我正在尝试进行一些音频编辑,并且需要能够滚动查看波形。我目前使用的代码来自this question,在我进行了一些修改以允许指定开始音频时间和结束音频时间后工作:
public Texture2D PaintWaveformSpectrum(AudioClip audio, int textWidth, int textHeight, int audioStart, int audioEnd, Color col) {
Texture2D tex = new Texture2D(textWidth, textHeight, TextureFormat.RGBA32, false);
float[] samples = new float[audioLength];
float[] waveform = new float[textWidth];
audio.GetData(samples, 0);
int packSize = ((audioEnd - audioStart) / textWidth) + 1;
if (audioStart != 0) {
audioStart += packSize % audioStart;
}
int s = 0;
for (int i = audioStart; i < audioEnd; i += packSize) {
waveform[s] = Mathf.Abs(samples[i]);
s++;
}
for (int x = 0; x < textWidth; x++) {
for (int y = 0; y < textHeight; y++) {
tex.SetPixel(x, y, Color.gray);
}
}
for (int x = 0; x < waveform.Length; x++) {
for (int y = 0; y <= waveform[x] * ((float)textHeight * .75f); y++) {
tex.SetPixel(x, (textHeight / 2) + y, col);
tex.SetPixel(x, (textHeight / 2) - y, col);
}
}
tex.Apply();
return tex;
}
然而,这里的问题是,当我滚动音频时,波形会发生变化。它确实滚动,但问题是它现在在波形中显示不同的值。这是因为样本明显多于像素,因此需要下采样。目前,每第 n 个样本被选中,但问题是起点不同,将选择不同的样本。下面的图片用于比较(另外,here's a video。This is what I want the scroll to look like):
如您所见,它们略有不同。整体结构在那里,但波形最终不同。
我认为这将是一个简单的解决方法 - 将开始音频值移动到最接近的 packSize(即 audioStart += packSize % audioStart 时 audioStart != 0),但这不起作用。同样的问题仍然存在。
如果有人对我如何在滚动时保持波形一致有任何建议,将不胜感激。
【问题讨论】: