Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Tue May 19 15:04:33 2020 +0000
Revision:
14:9cfe0041cc4e
Parent:
11:6ae098535da9
Child:
15:1c67f064278e
Actually make proper sound atlast. Checked Concert A against piano. included more preprocessor debug (SLOW TIME) for Debug if output check required w. printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lukeocarwright 7:33cb5f2db1ee 1 #include "mbed.h"
lukeocarwright 7:33cb5f2db1ee 2 #include "LUTs.h"
lukeocarwright 7:33cb5f2db1ee 3
lukeocarwright 7:33cb5f2db1ee 4 LUTs::LUTs()
lukeocarwright 7:33cb5f2db1ee 5 {
lukeocarwright 7:33cb5f2db1ee 6 }
lukeocarwright 7:33cb5f2db1ee 7 LUTs::~LUTs()
lukeocarwright 7:33cb5f2db1ee 8 {
lukeocarwright 7:33cb5f2db1ee 9 }
lukeocarwright 10:258a1eca02cc 10 volatile uint16_t sin_wavtable[1024];
lukeocarwright 10:258a1eca02cc 11 volatile uint16_t tri_wavtable[1024];
lukeocarwright 11:6ae098535da9 12 volatile uint16_t pulse_wavtable[1024];
lukeocarwright 10:258a1eca02cc 13
lukeocarwright 10:258a1eca02cc 14 void LUTs::initial_wavetables()
lukeocarwright 10:258a1eca02cc 15 {
lukeocarwright 10:258a1eca02cc 16 sin_wavetable();
lukeocarwright 10:258a1eca02cc 17 tri_wavetable(50);
lukeocarwright 11:6ae098535da9 18 pulse_wavetable(50);
lukeocarwright 10:258a1eca02cc 19 }
lukeocarwright 7:33cb5f2db1ee 20
lukeocarwright 7:33cb5f2db1ee 21 void LUTs::sin_wavetable()
lukeocarwright 7:33cb5f2db1ee 22 {
lukeocarwright 10:258a1eca02cc 23 printf("Generating sin Wavetable \n");
lukeocarwright 9:f6ba53e355a0 24 for (int i=0; i<1024; i++) {
lukeocarwright 9:f6ba53e355a0 25 sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5);
lukeocarwright 9:f6ba53e355a0 26 //calculates remainder for rounding
lukeocarwright 9:f6ba53e355a0 27 rem= fmod(sin_d,1);
lukeocarwright 9:f6ba53e355a0 28 //printf("preround= %g -", sin_d); //DEBUG
lukeocarwright 9:f6ba53e355a0 29 if (rem>=0.5) {
lukeocarwright 9:f6ba53e355a0 30 sin_d=ceil(sin_d); //round UP
lukeocarwright 9:f6ba53e355a0 31 } else {
lukeocarwright 9:f6ba53e355a0 32 sin_d= floor(sin_d-rem); //round DOWN
lukeocarwright 9:f6ba53e355a0 33 }
lukeocarwright 10:258a1eca02cc 34 // printf("Postround= %g -",sin_d); //DEBUG
lukeocarwright 9:f6ba53e355a0 35 sin_u=((uint16_t)sin_d);
lukeocarwright 9:f6ba53e355a0 36 //printf("sin_u= %u \n", sin_u);
lukeocarwright 9:f6ba53e355a0 37 sin_wavtable[i]=sin_u;
lukeocarwright 9:f6ba53e355a0 38 //sin_wavtable[i]=get_sin(i);
lukeocarwright 9:f6ba53e355a0 39 }
lukeocarwright 10:258a1eca02cc 40 /*//DEBUG
lukeocarwright 9:f6ba53e355a0 41 for (int i=0; i<1024; i=i+128) {
lukeocarwright 9:f6ba53e355a0 42 printf("sin_wav[%d]= %u \n", i, sin_wavtable);
lukeocarwright 7:33cb5f2db1ee 43 }
lukeocarwright 9:f6ba53e355a0 44 */
lukeocarwright 9:f6ba53e355a0 45 }
lukeocarwright 9:f6ba53e355a0 46
lukeocarwright 10:258a1eca02cc 47 void LUTs::tri_wavetable(int pulsewidth)
lukeocarwright 10:258a1eca02cc 48 {
lukeocarwright 10:258a1eca02cc 49 printf("Generating Tri-wavetable\n");
lukeocarwright 10:258a1eca02cc 50 tri_wavtable[0]=0;
lukeocarwright 10:258a1eca02cc 51 rise_t=(pulsewidth*1024/100);
lukeocarwright 11:6ae098535da9 52 rise_tu=(uint16_t)rise_t;
lukeocarwright 11:6ae098535da9 53 fall_tu=1-rise_tu;
lukeocarwright 10:258a1eca02cc 54 dif=65536/rise_t;
lukeocarwright 10:258a1eca02cc 55 dif_u=(uint16_t)dif;
lukeocarwright 11:6ae098535da9 56 for (int i=1; i<=rise_tu; i++) {
lukeocarwright 10:258a1eca02cc 57 tri_wavtable[i]=tri_wavtable[i-1]+dif_u;
lukeocarwright 10:258a1eca02cc 58 }
lukeocarwright 11:6ae098535da9 59 dif=65536/fall_tu;
lukeocarwright 10:258a1eca02cc 60 dif_u=(uint16_t)dif;
lukeocarwright 11:6ae098535da9 61 for (int i=rise_t+1; i<1024; i++) {
lukeocarwright 10:258a1eca02cc 62 tri_wavtable[i]=tri_wavtable[i-1]-dif_u;
lukeocarwright 10:258a1eca02cc 63 }
lukeocarwright 10:258a1eca02cc 64 }
lukeocarwright 9:f6ba53e355a0 65
lukeocarwright 9:f6ba53e355a0 66
lukeocarwright 11:6ae098535da9 67 void LUTs::pulse_wavetable(int pulsewidth)
lukeocarwright 11:6ae098535da9 68 {
lukeocarwright 11:6ae098535da9 69 printf("Generating Pulse-wavetable\n");
lukeocarwright 11:6ae098535da9 70 up_t=(pulsewidth*1024/100);
lukeocarwright 11:6ae098535da9 71 up_tu=(uint16_t)up_t;
lukeocarwright 11:6ae098535da9 72 for (int i=0; i<=up_tu; i++) {
lukeocarwright 11:6ae098535da9 73 pulse_wavtable[i]=65535;
lukeocarwright 11:6ae098535da9 74 //printf("up itt = %d\n",i);
lukeocarwright 11:6ae098535da9 75 }
lukeocarwright 14:9cfe0041cc4e 76
lukeocarwright 11:6ae098535da9 77 for (int i=up_tu+1; i<1024; i++) {
lukeocarwright 11:6ae098535da9 78 pulse_wavtable[i]=0;
lukeocarwright 11:6ae098535da9 79 //printf("down itt = %d\n",i);
lukeocarwright 11:6ae098535da9 80 }
lukeocarwright 11:6ae098535da9 81 }
lukeocarwright 11:6ae098535da9 82
lukeocarwright 11:6ae098535da9 83
lukeocarwright 9:f6ba53e355a0 84 /*
lukeocarwright 9:f6ba53e355a0 85 uint16_t get_sin(int i)
lukeocarwright 9:f6ba53e355a0 86 {
lukeocarwright 9:f6ba53e355a0 87 //calculates sin val as double (0->65535)
lukeocarwright 9:f6ba53e355a0 88
lukeocarwright 9:f6ba53e355a0 89 sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5);
lukeocarwright 9:f6ba53e355a0 90 //calculates remainder for rounding
lukeocarwright 9:f6ba53e355a0 91 rem= fmod(sin_d,1);
lukeocarwright 9:f6ba53e355a0 92 //printf("preround= %g -", sin_d[i]); //DEBUG
lukeocarwright 9:f6ba53e355a0 93 if (rem>=0.5) {
lukeocarwright 9:f6ba53e355a0 94 sin_d=ceil(sin_d); //round UP
lukeocarwright 9:f6ba53e355a0 95 } else {
lukeocarwright 9:f6ba53e355a0 96 sin_d= floor(sin_d-rem); //round DOWN
lukeocarwright 9:f6ba53e355a0 97 }
lukeocarwright 9:f6ba53e355a0 98 //printf("Postround= %g -",sin_d[i]); //DEBUG
lukeocarwright 9:f6ba53e355a0 99 return((uint16_t)sin_d);
lukeocarwright 9:f6ba53e355a0 100
lukeocarwright 9:f6ba53e355a0 101 //summed into one function
lukeocarwright 9:f6ba53e355a0 102 return((uint16_t)floor(65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5)));
lukeocarwright 9:f6ba53e355a0 103 }
lukeocarwright 9:f6ba53e355a0 104 */