【问题标题】:Scrolling through a waveform滚动波形
【发布时间】: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 videoThis is what I want the scroll to look like):

如您所见,它们略有不同。整体结构在那里,但波形最终不同。

我认为这将是一个简单的解决方法 - 将开始音频值移动到最接近的 packSize(即 audioStart += packSize % audioStartaudioStart != 0),但这不起作用。同样的问题仍然存在。

如果有人对我如何在滚动时保持波形一致有任何建议,将不胜感激。

【问题讨论】:

    标签: c# unity3d audio waveform


    【解决方案1】:

    尽管有多年的编程经验,但我似乎仍然无法正确地舍入一个数字。就这么简单。

    线

    if (audioStart != 0) {
        audioStart += packSize % audioStart;
    }
    

    应该是

    audioStart = (int) Mathf.Round(audioStart / packSize) * packSize;
    

    waveform 添加1 个额外字节也是必要的,因为舍入的一半时间会导致包含一个额外的样本。因此,waveform 应定义为:

    float[] waveform = new float[textWidth+1];
    

    这解决了问题,并且样本的选择保持一致。我不太确定像 audacity 这样的程序是如何设法获得不那么嘈杂的漂亮波形(下面是同一首歌的比较:我的在上面,audacity 在下面),但这是另一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多