【问题标题】:UWP stream to float array c#UWP流到浮点数组c#
【发布时间】:2017-04-04 21:36:05
【问题描述】:

我在将 RandomAccessStream 转换为浮点数组时遇到问题。浮点数组包含 NaN 值。我不知道它们是来自流、字节数组还是浮点数组。性能和质量在这方面很重要,如果有更好的方法,请告诉我。

最后数我得到了 122 个 NaN。

谢谢

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

【问题讨论】:

  • 尝试private async Task ProcessStream(...) 并在点击处理程序中等待它await ProcessStream(synthesisStream); 我只是想知道CloneStream() 在发送到fire-&-forget 方法时会出现问题。

标签: c# arrays uwp


【解决方案1】:

找到答案At this SO post

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多