#import "Delay.h"
#import <stdlib.h>


@implementation Delay : Object
{
	float sr,*delayLine;
	int phs,len;

}

+ create {
	id newInstance;
	newInstance = [ self new ];
	return newInstance;
}

-size:(float)aSize sRate:(int)aSR {
//	char *malloc();
	sr = aSR;
	len = aSize * aSR;
	if((delayLine = (float *)malloc((int)(len *  4))) == 0) {
		printf("can't malloc memory for delayline\n");
		exit(-1);
	}
	for(phs = 0; phs< (aSize * aSR); phs++) delayLine[phs] = 0;
	phs = 0;
}
	
- put: (float) sample {
	delayLine[phs] = sample;
	phs++;
	if(phs == len) phs = 0;
	return self;
}

- (float)get: (float)wait{
	int i;
	i = phs - (int)(wait * sr);
	if(i < 0) i += len;
	return(delayLine[i]);
}
- release {
	free(delayLine);
	return self;
}
@end
