Skip to content

Instantly share code, notes, and snippets.

@bion
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save bion/be58c2e9ba8283dc0206 to your computer and use it in GitHub Desktop.

Select an option

Save bion/be58c2e9ba8283dc0206 to your computer and use it in GitHub Desktop.
Sine Table
#include <math.h>
#define PI 3.14159265358979323846
int sinetable(double **table, double frequency, int sample_rate) {
int i = 0;
double phase = 0;
double cycles_per_frame = frequency / sample_rate;
int frames_per_cycle = (int) (1 / cycles_per_frame);
double phase_increment = cycles_per_frame * 2 * PI;
if ((*table = malloc(sizeof(double) * frames_per_cycle) == NULL)
return -1;
for (i = 0; i < frames_per_cycle; ++i) {
(*table)[i] = sin(phase);
phase += phase_increment;
}
return frames_per_cycle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment