0

I have a problem turning a RandomAccessStream into a float array. The float array contains values that are NaN. I can't tell if they come from the stream, the byte array or the float array. Performance & quality are important in this so if there is a better way to do this let me know.

Last count I was getting 122 NaN's.

thanks

 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;
    }
Haydn
  • 293
  • 1
  • 2
  • 13
  • Try `private async Task ProcessStream(...)` and await it in the click handler `await ProcessStream(synthesisStream);` I'm just wondering of `CloneStream()` is an issue when sent to a fire-&-forget method. – Laith Apr 05 '17 at 02:54

1 Answers1

0

Found the answer At this SO post

Basically I did not know that the 32 bit wav format stored its data in a 16 bit format.

Community
  • 1
  • 1
Haydn
  • 293
  • 1
  • 2
  • 13