Last active
August 29, 2015 14:20
-
-
Save bion/be58c2e9ba8283dc0206 to your computer and use it in GitHub Desktop.
Sine Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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