简体   繁体   English

是否可以在 getAudioSpectum() 中消除音量和频谱之间的相关性?

[英]is it possible to remove correlation between volume and spectrum in getAudioSpectum()?

I've been making a music game in which I use myAudioSource.getSpectrumData() to get spectrum.我一直在制作一个音乐游戏,在其中我使用myAudioSource.getSpectrumData()来获取频谱。 But I noticed that the spectrum directly depends on the volume of the sound, in direct proportion: the bigger volume the bigger spectrum.但我注意到频谱直接取决于声音的音量,成正比:音量越大,频谱越大。 And that brings problems like: zero volume makes game to stop, big volume makes it too sensitive and to work incorrectly.这带来了一些问题:零音量使游戏停止,大音量使其过于敏感并无法正常工作。 I've tried to normalize the spectrum but it's correlated anyway, just in [0,1].我试图对频谱进行归一化,但无论如何它都是相关的,就在 [0,1] 中。 Make some transformations with volume?用音量做一些转换? It will correlate anyway.无论如何它都会相关。

Is here a way to get spectrum data or transforms it in some way so that it will be independent from volume?这是一种获取光谱数据或以某种方式对其进行转换以使其独立于体积的方法吗?

For example, if a song is too loud so the biggest part of values from spectrum will be in [0.9, 1] for normalized spectrum data.例如,如果一首歌曲太大声,那么对于归一化的频谱数据,频谱值的最大部分将在[0.9, 1]中。 If the same song make quieter, it will be [0.1, 0.3] interval.如果同一首歌变得更安静,它将是[0.1, 0.3]间隔。

Code:代码:


private AudioSource src;

private void Start()
{
     _samplesArray =  new float[64];
}

private void Update() 
{
      // Every frame get spectrum into _samplesArray with size 64
      src.GetSpectrumData(_samplesArray, 0, FFTWindow.Rectangular);
}

That makes totally sense:这完全有道理:

  • If the volume is low then on the entire spectrum you get lower values / ranges / peaks如果音量很低,那么在整个频谱上你会得到更低的值/范围/峰值
  • If the volume is high then you can expect the entire spectrum to move to a higher range如果音量很高,那么您可以预期整个频谱会移动到更高的范围

You can / have to simply normalize the spectrum using eg您可以/必须使用例如简单地标准化频谱

using System.Linq;

...

_samplesArray = _samplesArray.Select(s => s / src.volume).ToArray();

or without Linq maybe better to understand或没有 Linq 可能更好理解

for(var i = 0; i < _samplesArray.Length; i++)
{
    _samplesArray[i] /= src.volume;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM