I wanted to invoke Spectrum method at the end of the BufferReady method but I don’t know why I get error which tells me that I pass wrong arguments to it. Raw is an int.
void microphone_BufferReady(object sender, EventArgs e) {
if (buffer.Length <= 0) return;
// Retrieve audio data
microphone.GetData(buffer);
double[] sampleBuffer = new double[(Utilities.NextPowerOfTwo((uint)buffer.Length))];
int index = 0;
for (int i = 0; i < 2048; i += 2) {
sampleBuffer[index] = Convert.ToDouble(BitConverter.ToInt16((byte[])buffer, i)); index++;
}
//ERROR UNDER
double[] spectrum = FourierTransform.Spectrum(sampleBuffer, Raw);// I GOT ERROR HERE
}
-----------------------
public static double[] Spectrum(ref double[] x, int method = Raw)
{
//uint pow2Samples = FFT.NextPowerOfTwo((uint)x.Length);
double[] xre = new double[x.Length];
double[] xim = new double[x.Length];
Compute((uint)x.Length, x, null, xre, xim, false);
double[] decibel = new double[xre.Length / 2];
for (int i = 0; i < decibel.Length; i++)
decibel[i] = (method == Decibel) ? 10.0 * Math.Log10((float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i])))) : (float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i])));
return decibel;
}
Add
refkeyword to first parameter ofSpectrummethod callUPDATE
refkeyword states, that array should be passed by reference to Spectrum method, and if you will assign new value toxin Spectrum method, then this will assign new value tosampleBuffervariable in microphone_BufferReady method. But as Jon stated in comments, in this particular caserefcould be removed from your Spectrum method definition (but also you will have to modify all other calls of that method).