0

In my libsndfile wrapper currently im using this. To read my stream (Must be a stream im loading from resource) But is this the best way i can fill the pointer with the requested data?

public static void ReadStream(Stream input,IntPtr output,int size)
{
    byte[] data = new byte[BufferSize];
    int i = BufferSize;
    int ptr_offset = 0;

    while (i == BufferSize || ptr_offset==size)
    {
        i = input.Read(data, 0, BufferSize);

        if (i + ptr_offset > size)
            i = size - ptr_offset;

        MarshalExt.Copy_MU(data, 0, output, ptr_offset, (uint)i);
        ptr_offset += i;
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • While this question is about Java, I believe its answer applies here and will be helpful: https://stackoverflow.com/questions/236861/how-do-you-determine-the-ideal-buffer-size-when-using-fileinputstream –  Jul 25 '18 at 17:11
  • @Amy i see. would this still apply in memory to memory transfers? Mainly doing this so i dont have 3 buffers the size of 44100*4 floating around. – Courtney The coder Jul 25 '18 at 17:15
  • It is a bit clumsy, but that is unavoidable when you only have a Stream to read from. It just doesn't matter, latency is very low and this is many orders of magnitude faster than you'd ever need to keep libsndfile happy. – Hans Passant Jul 25 '18 at 17:31
  • @HansPassant So it should be fast and will keep my need of not using two massive buffers. – Courtney The coder Jul 25 '18 at 18:12

0 Answers0