[sdiy] help understanding circular buffers for delay line

Andrew Simper andy at cytomic.com
Fri Mar 2 07:42:50 CET 2012


On 2 March 2012 13:52, Matthew Smith <matt at smiffytech.com> wrote:
> Quoth Andrew Simper at 02/03/12 14:37...
>>
>> If you are using a power of 2 buffer length then just use an "&" of
>> the length - 1 for starters as this will be much easier to code, worry
>> about optimisation later:
>>
>> buffer [index&  (length - 1)]
>>
>>
>> so for a fixed integer sample delay you would have:
>>
>> inpos = (inpos + 1)&  (length - 1);
>> buffer [inpos] = input;
>> output = buffer [(inpos - delay)&  ( length - 1)];
>>
>>
>> and make sure delay is less than length - 1
>
>
> Cool, thanks for that little optimisation.
>
> Logical operation should mean that the process will take a more predictable
> time to execute, unlike using conditionals.
>
>
> --
> Matthew Smith

Before any statements can be made about one thing being quicker than
another you really need to run tests on the target architecture and
measure cpu usage. Using '&' is just easier to code to get up and
running if you have a power of 2 buffer that you want to wrap around.

Andy



More information about the Synth-diy mailing list