A Portion of Buff

Everybody else had one, so...

The subdued garbage collector

I want Noise to support any .Net language, not just C# (although why anyone would use another language...).  At the moment, the plugin interface exposes unsafe methods - Process and ProcessReplacing - which take float* parameters and are therefore unusable by a pussy language like VB.Net.  Pointers are used because it allows us to write directly to the host's memory instead of allocating our own buffers and copying them back to the host.

If we are prepared to take a performance hit, we can make this interface safe.  Not only is there the cost of copying unmanaged memory to a managed buffer on the way in, and then back again on the way out, we need to allocate some managed memory in the first place.  Allocations lead to collections, collections lead to hate, hate leads to suffering.

However, we can take advantage of the fact that the maximum number of samples being passed to the plugin rarely changes (usually not until the host's latency setting has been changed, which is hardly ever under normal use), although the host is free to send fewer samples if it wishes.  The host tells us the maximum block size by calling the SetBlockSize() method, which is a good place to allocate our buffers:


public override void SetBlockSize(int blockSize)
{
    leftBufferIn = new float[blockSize];
    rightBufferIn = new float[blockSize];
    leftBufferOut = new float[blockSize];
    rightBufferOut = new float[blockSize];
}


and in the Process or ProcessReplacing methods, we can just fill these buffers using the Marshal class:

public override void ProcessReplacing(float* inLeft, float* inRight, float* outLeft, float* outRight, int sampleFrames)
{
    Marshal.Copy(new IntPtr(inLeft), leftBufferIn, 0, sampleFrames);
    Marshal.Copy(new IntPtr(inRight), rightBufferIn, 0, sampleFrames);
    Marshal.Copy(new IntPtr(outLeft), leftBufferOut, 0, sampleFrames);
    Marshal.Copy(new IntPtr(outRight), rightBufferOut, 0, sampleFrames);
//...
}


It doesn't matter if sampleFrames is less than blockSize, we just copy that many samples to our buffers.  This enables us to call out to a safe method which deals with float arrays, and performance is constant because no new allocations occur.  Hurrah.

Comments

Big D said:

It's not really subdued. It's probably lonely.
# April 6, 2005 5:40 AM

Big D said:

And horny.
# April 6, 2005 9:47 AM

BRADSHAW21MEGHAN said:

Papers writing services compose hundreds essay every month. Only masters are able to do this. So, if you want to get high quality <a href="http://www.essaysbank.com">Shakespeare essays paper</a>, you will have to go to the research papers writing service.

# January 27, 2011 7:22 AM

paper services said:

Do you guess that to get help from term paper services is not good? This statement used to be created by tutors in order to prevent plagiarism! Do not rely on such stories!

# June 3, 2011 3:02 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)