UWP流到浮点数组c#

时间:2017-04-04 21:36:06

标签: c# arrays uwp

将RandomAccessStream转换为float数组时遇到问题。 float数组包含NaN值。我无法判断它们是来自流,字节数组还是浮点数组。表演&质量在这方面很重要,所以如果有更好的方法,请告诉我。

最后一次计算我得到122 NaN&#39。

感谢

 private async void button_Click(object sender, RoutedEventArgs e)
    {
        string text = "this is text";

        SpeechSynthesizer synthesizer = new SpeechSynthesizer();
        SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(text);

        Stopwatch watch = new Stopwatch();
        watch.Start();
        ProcessStream(synthesisStream.CloneStream());
        watch.Stop();

        // Performance is important
        Debug.WriteLine(watch.Elapsed);
    }

    private async void ProcessStream(IRandomAccessStream stream)
    {
        // Create a buffer (somewhere to put the stream)
        byte[] bytes = new byte[stream.Size];

        // Add stream data to buffer (Following or After that) same result
        // IBuffer x = await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, InputStreamOptions.None);
        using (DataReader reader = new DataReader(stream))
        {
            await reader.LoadAsync((uint)stream.Size);
            reader.ReadBytes(bytes);
        }

        // Change buffer(in bytes) to a float array
        float[] floatArray = MainPage.ConvertByteToFloat(bytes.ToArray());

        int nanCount = 0;
        for (var index = 0; index < floatArray.Length; index++)
        {
            float value = floatArray[index];
            if (float.IsNaN(value))
            {
                nanCount++;
            }
        }

        Debug.WriteLine("Nan count: " + nanCount);
    }

    public static float[] ConvertByteToFloat(byte[] array)
    {
        float[] floatArr = new float[array.Length / 4];
        for (int i = 0; i < floatArr.Length; i++)
        {
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(array, i * 4, 4);
            }
            floatArr[i] = BitConverter.ToSingle(array, i * 4);
        }
        return floatArr;
    }

1 个答案:

答案 0 :(得分:0)

找到答案At this SO post

基本上我不知道32位wav格式以16位格式存储其数据。