Vra

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.

Was dit nuttig?

Oplossing

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:

Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top