avoiding loops, allocation using arrays and ranges

Danni Coy via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 28 00:07:47 PDT 2016


Something just clicked in my head in the last week in terms of
component based programming.

I am currently porting a C++ realtime audio app to D.
The app uses jackd.
I have a series of float[] buffers I read and write audio data from.

lets say I want to crossfade to audio buffers I would do the following

float[1024] a; //actually these allocated by jackd in C but for
purposes of illustration
float[1024] b;

length = min(length,a.length,b.length);

foreach(i; 0..nframes)
{
   /* calculations */
    a[i] = a[i]*ratio + b[i] * (1-ratio);
}

I can do :

auto crossfade = sequence(/*calculations*/)(a[],b[],length);
foreach(i; 0..length) a[i] = crossfade[i];

but it would be nice if I could do :

auto crossfade = sequence(/*calculations*/)(a[],b[],length);
a[0..length] = crossfade[0..length];

or even better
auto ramp = sequence(/*calculations*/)(length); // create once and
store for later
a[] = a[]*ramp[] + b[]*(1.0-ramp[]);

I know I can use array but that allocates memory and I don't want to
do that in my realtime thread.
I could store the ramp as a static array but I want to option to do
this lazily, particularly if I end up with a lot of different
permutations.


More information about the Digitalmars-d mailing list