读取麦克风分贝和 pitch/frequency
Read microphone decibels and pitch/frequency
我正在尝试制作一款游戏,当麦克风发出足够大的声音时,我的角色就会射击(在 Unity 中)。但是我不知道如何开始。
感谢您的帮助!
您可以通过 AudioSource.GetOutputData
函数检索当前播放的麦克风输出数据块来获取麦克风的分贝数。要从此数据中获取 dB,您需要对数据样本求和,然后计算 RMS。这是 RMS 值,您可以使用 20 * Mathf.Log10 (RMS / refVal)
.
计算 dB
Unity's post 上有关于此的完整示例。您可以阅读它以获取更多信息,下面的代码基于此:
public float rmsVal;
public float dbVal;
public float pitchVal;
private const int QSamples = 1024;
private const float RefValue = 0.1f;
private const float Threshold = 0.02f;
float[] _samples;
private float[] _spectrum;
private float _fSample;
void Start()
{
_samples = new float[QSamples];
_spectrum = new float[QSamples];
_fSample = AudioSettings.outputSampleRate;
}
void Update()
{
AnalyzeSound();
Debug.Log("RMS: " + rmsVal.ToString("F2"));
Debug.Log(dbVal.ToString("F1") + " dB");
Debug.Log(pitchVal.ToString("F0") + " Hz");
}
void AnalyzeSound()
{
GetComponent<AudioSource>().GetOutputData(_samples, 0); // fill array with samples
int i;
float sum = 0;
for (i = 0; i < QSamples; i++)
{
sum += _samples[i] * _samples[i]; // sum squared samples
}
rmsVal = Mathf.Sqrt(sum / QSamples); // rms = square root of average
dbVal = 20 * Mathf.Log10(rmsVal / RefValue); // calculate dB
if (dbVal < -160) dbVal = -160; // clamp it to -160dB min
// get sound spectrum
GetComponent<AudioSource>().GetSpectrumData(_spectrum, 0, FFTWindow.BlackmanHarris);
float maxV = 0;
var maxN = 0;
for (i = 0; i < QSamples; i++)
{ // find max
if (!(_spectrum[i] > maxV) || !(_spectrum[i] > Threshold))
continue;
maxV = _spectrum[i];
maxN = i; // maxN is the index of max
}
float freqN = maxN; // pass the index to a float variable
if (maxN > 0 && maxN < QSamples - 1)
{ // interpolate index using neighbours
var dL = _spectrum[maxN - 1] / _spectrum[maxN];
var dR = _spectrum[maxN + 1] / _spectrum[maxN];
freqN += 0.5f * (dR * dR - dL * dL);
}
pitchVal = freqN * (_fSample / 2) / QSamples; // convert index to frequency
}
我正在尝试制作一款游戏,当麦克风发出足够大的声音时,我的角色就会射击(在 Unity 中)。但是我不知道如何开始。
感谢您的帮助!
您可以通过 AudioSource.GetOutputData
函数检索当前播放的麦克风输出数据块来获取麦克风的分贝数。要从此数据中获取 dB,您需要对数据样本求和,然后计算 RMS。这是 RMS 值,您可以使用 20 * Mathf.Log10 (RMS / refVal)
.
Unity's post 上有关于此的完整示例。您可以阅读它以获取更多信息,下面的代码基于此:
public float rmsVal;
public float dbVal;
public float pitchVal;
private const int QSamples = 1024;
private const float RefValue = 0.1f;
private const float Threshold = 0.02f;
float[] _samples;
private float[] _spectrum;
private float _fSample;
void Start()
{
_samples = new float[QSamples];
_spectrum = new float[QSamples];
_fSample = AudioSettings.outputSampleRate;
}
void Update()
{
AnalyzeSound();
Debug.Log("RMS: " + rmsVal.ToString("F2"));
Debug.Log(dbVal.ToString("F1") + " dB");
Debug.Log(pitchVal.ToString("F0") + " Hz");
}
void AnalyzeSound()
{
GetComponent<AudioSource>().GetOutputData(_samples, 0); // fill array with samples
int i;
float sum = 0;
for (i = 0; i < QSamples; i++)
{
sum += _samples[i] * _samples[i]; // sum squared samples
}
rmsVal = Mathf.Sqrt(sum / QSamples); // rms = square root of average
dbVal = 20 * Mathf.Log10(rmsVal / RefValue); // calculate dB
if (dbVal < -160) dbVal = -160; // clamp it to -160dB min
// get sound spectrum
GetComponent<AudioSource>().GetSpectrumData(_spectrum, 0, FFTWindow.BlackmanHarris);
float maxV = 0;
var maxN = 0;
for (i = 0; i < QSamples; i++)
{ // find max
if (!(_spectrum[i] > maxV) || !(_spectrum[i] > Threshold))
continue;
maxV = _spectrum[i];
maxN = i; // maxN is the index of max
}
float freqN = maxN; // pass the index to a float variable
if (maxN > 0 && maxN < QSamples - 1)
{ // interpolate index using neighbours
var dL = _spectrum[maxN - 1] / _spectrum[maxN];
var dR = _spectrum[maxN + 1] / _spectrum[maxN];
freqN += 0.5f * (dR * dR - dL * dL);
}
pitchVal = freqN * (_fSample / 2) / QSamples; // convert index to frequency
}