ELEC2645 (2019/20) / Mbed 2 deprecated el18loc_final

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sound.cpp Source File

Sound.cpp

00001 #include "Sound.h"
00002 
00003 //Global Variables
00004 volatile extern uint16_t sin_wavtable[1024];
00005 volatile extern uint16_t tri_wavtable[1024];
00006 volatile extern uint16_t pulse_wavtable[1024];
00007 
00008 
00009 Sound::Sound()
00010 {
00011 }
00012 
00013 Sound::~Sound()
00014 {
00015 }
00016 
00017 //PUBLIC:-----------------------------------------------------------------------
00018 
00019 
00020 uint16_t Sound::sound_main(bool initial, int waveform, int frequency)
00021 {
00022     if (initial==true) { //initiialises
00023         i=0;
00024         return(i);
00025     }
00026     i=wavetable_itt(i,frequency);//itterates i value
00027     
00028     if (waveform==1) { //sin values
00029         #ifdef SLOW_TIME
00030         printf("SIN_wavtable[%u]\n",i);
00031         #endif
00032         return (sin_wavtable[i]);
00033     }
00034         if (waveform==2) { //tri
00035         #ifdef SLOW_TIME
00036         printf("TRI_wavtable[%u]\n",i);
00037         #endif
00038         return (tri_wavtable[i]);
00039     }
00040         if (waveform==3) { //pulse
00041         #ifdef SLOW_TIME
00042         printf("SQR_wavtable[%u]\n",i);
00043         #endif
00044         return (pulse_wavtable[i]);
00045     }
00046     return(0);
00047 }
00048 
00049 //PRIVATE:----------------------------------------------------------------------
00050 
00051 uint16_t Sound::wavetable_itt(uint16_t i, int frequency)
00052 {
00053     i_d = i + ((1024*frequency)/ 10000); //i+((samples*f)*Ts)
00054     i=ceil(i_d); //rounds to int
00055     
00056     if (i>=1024) { //Sorts Overlap
00057         i=i-1024;
00058     }
00059     return(i);
00060 }