All pastes #249556 Raw Edit

Something

public text v1 · immutable
#249556 ·published 2006-11-15 22:20 UTC
rendered paste body
#define blep_size 64
static float* pdc_buffer;
static float* future_buffer;
static float* blep_buffer;
static float* buffer;
static float* buffer2;
static float* buffer3;
static int futureindex;
static int pdcindex;
static int index;
static int dirtyat; // used for tracking how much of the buffer needs to be redrawn

float sinc(float n)
{
	float r = 1;
	if(n != 0) {
		r = (sin(n * M_PI + DENORMAL) / (n + DENORMAL)) / M_PI;
	}
	return(r);
}

#define cutoff_freq 50

float lowpass1(float x, float cutoff, float* state)
{
	//integrator += (osc_freq * 2.0f / samplerate) * (n - integrator); 
	// y = y*(1-g) + x*g 
	//g = 1-exp(-2*pi*fc/Fs)
	float g;
	g = 1.0 - exp(-2.0 * M_PI * cutoff / samplerate);
	//y = y * (1.0 - g) + x * g;
	*state += g * (x - *state);
	return *state; 
}				   

float integrate(float x, float* state)
{
	*state += x;
	return *state;
}

static void cmd_process_refresh(int argc, char** argv)
{
	samplerate = cvar_get_float("samplerate");
	osc_freq = cvar_get_float("osc_freq");
	buffer_size = cvar_get_int("buffer_size");
	//
	if(buffer_size) {
		if(buffer)
			free(buffer);
		if(buffer2)
			free(buffer2);
		if(buffer3)
			free(buffer3);
		buffer = malloc(sizeof(float) * buffer_size);
		buffer2 = malloc(sizeof(float) * buffer_size);
		buffer3 = malloc(sizeof(float) * buffer_size);
	}
	//index = 0;
	dirtyat = 0;
}

void process_init()
{
	// build blep buffer
	int i;
	blep_buffer = malloc(blep_size * sizeof(float));
	for(i = 0; i < blep_size; i++) {
		blep_buffer[i] = sinc(((((float) i) - 32.0) / 32.0) * M_PI);
	}
	pdc_buffer = malloc(blep_size * sizeof(float) / 2);
	for(i = 0; i < blep_size / 2; i++) {
		pdc_buffer[i] = 0.0;
	}
	future_buffer = malloc(blep_size * sizeof(float) / 2);
	for(i = 0; i < blep_size / 2; i++) {
		future_buffer[i] = 0.0;
	}
	cmd_process_refresh(0, NULL);
	cvar_set_cmd("process_refresh", cmd_process_refresh);
	index = 0;
	pdcindex = 0;
	futureindex = 0;
}

float saw = 0.0;
float phase=0.0;
float state1=0.0;
void process()
{
	float p = phase*8.0;
	buffer[index] = sinc(p) * cos(p / (M_PI*M_PI));
	buffer2[index] = integrate(buffer[index] / 20, &state1) / 2;
	//buffer3[index] = buffer[index] * buffer2[index];
	phase += 0.01;
	if(phase > M_PI) phase -= 2*M_PI;
	// maintenance
	index = (index + 1) % buffer_size;

}

void process_draw(SDL_Surface* s)
{
	SDL_Rect r;
	int x;
	r.x = r.y = 0;
	r.h = s->h; 
	r.w = s->w;
	SDL_FillRect(s, &r, 0);
	draw_line(s, 0, s->h / 2 - 1, s->w, s->h / 2 - 1, -1);
	draw_buffer_exclude(s, r, buffer, buffer_size, 1.0, 0xff0000ff, index);
	draw_buffer_exclude(s, r, buffer2, buffer_size, 1.0, 0xff00ff00, index);
	draw_buffer_exclude(s, r, buffer3, buffer_size, 1.0, 0xffff0000, index);
	x = SCALE(index, buffer_size, s->w);
	draw_line(s, x, 0, x, s->h - 1, -1);
}