我应该传递什么参数来调用该方法C#

时间:2012-12-10 17:27:34

标签: c# methods arguments

我想在BufferReady方法结束时调用Spectrum方法,但我不知道为什么我会收到错误,告诉我我向它传递了错误的参数。 Rawint

  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;
                }

1 个答案:

答案 0 :(得分:3)

ref关键字添加到Spectrum方法调用的第一个参数

double[] spectrum = FourierTransform.Spectrum(ref sampleBuffer, Raw);

更新 ref关键字状态,该数组应该通过引用Spectrum方法传递,如果您要在Spectrum方法中为x分配新值,那么这将是在microphone_BufferReady方法中为sampleBuffer变量分配新值。但正如Jon在评论中所述,在这种特殊情况下,ref可以从Spectrum方法定义中删除(但您也必须修改该方法的所有其他调用)。