How can the value referenced by a variable of type float array have type byte array?

StackOverflow https://stackoverflow.com/questions/23331340

  •  10-07-2023
  •  | 
  •  

Domanda

The NAudio library provides the following interface.

interface ISampleProvider
{
    WaveFormat WaveFormat { get; }
    int Read(float[] buffer, int offset, int count);
}

I made a class that implements this interface. In my implementation of Read I am getting very strange behavior. The type of the buffer argument is showing up as byte[] even though that variable is declared to be a float[].

    int Read(float[] buffer, int offset, int count)
    {
        //The type of buffer is System.Byte[]!
        var type = buffer.GetType();
        ...
    }

How can this be?

I don't know how the NAudio library is calling my Read implementation, but I'm guessing it may be via some unsafe interop.

È stato utile?

Soluzione

You're seeing the WaveBuffer class in action, which is a way that NAudio tricks .NET into letting us "cast" a byte[] into a float[] without having to copy data or pin buffers.

Read more about it:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top