wavetable.c (1169B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdint.h> 4 #include <math.h> 5 #include "wavetable.h" 6 7 void wt_triangle(int16_t*); 8 void wt_sin(int16_t*); 9 void wt_pulse(int16_t*); 10 void wt_saw(int16_t*); 11 void wt_silence(int16_t*); 12 13 int16_t **wavetable; 14 15 void 16 wt_init(void) 17 { 18 int i; 19 wavetable = malloc(sizeof(int16_t*) * WT_SIZE); 20 for (i=0;i<WT_SIZE;i++) 21 wavetable[i] = malloc(sizeof(int16_t) * WT_LENGTH); 22 wt_sin(wavetable[0]); 23 wt_triangle(wavetable[1]); 24 wt_pulse(wavetable[2]); 25 wt_silence(wavetable[3]); 26 } 27 28 void 29 wt_triangle(int16_t *w) 30 { 31 size_t i, pos; 32 for (i = 0; i < WT_LENGTH/2; i++){ 33 pos = (i-WT_LENGTH/4)%WT_LENGTH; 34 w[pos] = -0x7fff + i * 0xffff * 2 / WT_LENGTH; 35 } 36 for (i = 0; i < WT_LENGTH/2; i++){ 37 pos = (i+WT_LENGTH/4)%WT_LENGTH; 38 w[pos] = 0x7fff - i * 0xffff * 2 / WT_LENGTH; 39 } 40 } 41 42 void 43 wt_pulse(int16_t *w) 44 { 45 size_t i; 46 for (i=0; i<WT_LENGTH/2; i++) w[i] = -0x7fff; 47 for (; i<WT_LENGTH; i++) w[i] = 0x7fff; 48 } 49 50 void 51 wt_sin(int16_t *w) 52 { 53 size_t i; 54 for (i=0; i<WT_LENGTH; i++){ 55 w[i] = sin(2 * M_PI * (double)i/(double)WT_LENGTH) * 0x7fff; 56 } 57 } 58 59 void 60 wt_silence(int16_t *w) 61 { 62 size_t i; 63 for (i=0; i<WT_LENGTH; i++) w[i] = 0; 64 }