Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否将memorystream转换为双数组?_C#_Fft_Memorystream_Naudio - Fatal编程技术网

C# 是否将memorystream转换为双数组?

C# 是否将memorystream转换为双数组?,c#,fft,memorystream,naudio,C#,Fft,Memorystream,Naudio,我从一个wave文件中得到了一个原始音乐数据的pcm流,我想将它转换成一个双数组(以便随后应用fft) 我现在得到的结果包含非常高或很低的双倍数(1.0E-200和1.0E+300),我不确定这些是否正确 这是我现在正在使用的代码: WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3); double[] real = new double[pcm.Length]; byte[] buffer = new byte[8]

我从一个wave文件中得到了一个原始音乐数据的pcm流,我想将它转换成一个双数组(以便随后应用fft)

我现在得到的结果包含非常高或很低的双倍数(1.0E-200和1.0E+300),我不确定这些是否正确

这是我现在正在使用的代码:

WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3);
double[] real = new double[pcm.Length];
byte[] buffer = new byte[8];
int count = 0;

while ((read = pcm.Read(buffer, 0, buffer.Length)) > 0)
{
   real[count] = BitConverter.ToDouble(buffer, 0);
   count++;
}

您的PCM流几乎肯定是16位的。因此,不要使用
BitConverter.ToDouble
而是使用
ToInt16
。然后除以32768.0,进入+/-1.0的范围,您的PCM流几乎肯定是16位的。因此,不要使用
BitConverter.ToDouble
而是使用
ToInt16
。然后除以32768.0,进入+/-1.0的范围,我意识到这个问题很老了;但是,我想我可以提供这种调用BitConverter.ToDouble的替代方法

    public static double[] ToDoubleArray(this byte[] bytes)
    {
        Debug.Assert(bytes.Length % sizeof(double) == 0, "byte array must be aligned on the size of a double.");

        double[] doubles = new double[bytes.Length / sizeof(double)];
        GCHandle pinnedDoubles = GCHandle.Alloc(doubles, GCHandleType.Pinned);
        Marshal.Copy(bytes, 0, pinnedDoubles.AddrOfPinnedObject(), bytes.Length);
        pinnedDoubles.Free();
        return doubles;
    }

    public static double[] ToDoubleArray(this MemoryStream stream)
    {
        return stream.ToArray().ToDoubleArray();
    }

我意识到这个问题由来已久;但是,我想我可以提供这种调用BitConverter.ToDouble的替代方法

    public static double[] ToDoubleArray(this byte[] bytes)
    {
        Debug.Assert(bytes.Length % sizeof(double) == 0, "byte array must be aligned on the size of a double.");

        double[] doubles = new double[bytes.Length / sizeof(double)];
        GCHandle pinnedDoubles = GCHandle.Alloc(doubles, GCHandleType.Pinned);
        Marshal.Copy(bytes, 0, pinnedDoubles.AddrOfPinnedObject(), bytes.Length);
        pinnedDoubles.Free();
        return doubles;
    }

    public static double[] ToDoubleArray(this MemoryStream stream)
    {
        return stream.ToArray().ToDoubleArray();
    }

我不知道音频流,但也许你真的想把音频数据的每个字节转换成一个双字节,或者这可能是一个big-endian和little-endian的问题?如果是这样的话,在转换为double之前,您需要反转每组8字节。我不知道音频流,但可能您真的想将音频数据的每个字节转换为double,或者可能是big-endian与little-endian的问题?如果是这样,在转换为双字节之前,需要反转每组8字节。