Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: sintone.cpp
- Revision:
- 0:325b79275f25
- Child:
- 1:516ea1ca7abb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sintone.cpp Thu Apr 05 04:42:52 2018 +0000 @@ -0,0 +1,55 @@ +#include <mbed.h> +#define samplerate 16000 +#define MAXFREQS 10 + +Serial pc(USBTX, USBRX); +Ticker tick1; +PwmOut pwm1(p21); +AnalogOut da1(p18); + +const int scalefreqs[12*2]={440, 466,494,523,554,587,622,659,698,740,784,830, + 880,932,987,1046,1109,1175,1245,1319,1397,1480,1568,1661}; +signed char bytesintab[samplerate]; +int freqlist[MAXFREQS]; +int seqlist[MAXFREQS]={0}; +int maxfreqs=0; + +void make_sintab(int count) { + int i; + for (i=0; i<count; i++) { + bytesintab[i]=sin( 2.0 * 3.141592 *((float) i)/((float)samplerate)) *120; + } } + +void gentone() { + float outval=0; + int i; + for (i=0; i<maxfreqs; i++) { + outval += bytesintab[seqlist[i]]; + seqlist[i] += freqlist[i]; + seqlist[i] = seqlist[i] % samplerate; //clip at 160000 + } + da1.write((outval/(256.0*maxfreqs))+0.5); } + +void setfreq(const int tone, const int scale) { + freqlist[tone]=scalefreqs[scale]; + seqlist[tone]=0; } + +main() { + int ch, chord=0; + make_sintab(samplerate); setfreq(0,0); maxfreqs=1; + tick1.attach(&gentone, 1.0/(float)samplerate); + while (1) { + ch=pc.getc(); cdec + if ('a'<=ch && ch<='l') { + setfreq(0, ch-'a'); + if (chord<=1) maxfreqs=1; + else if (chord>1) { setfreq(1,ch-'a'+7); maxfreqs=2;} + if (chord>2) { setfreq(2,ch-'a'+4); maxfreqs=3;} + if (chord>3) { setfreq(2,ch-'a'+3); /*minor*/ } + if (chord==7) { setfreq(3, ch-'a'+10); maxfreqs=4; }/*seventh*/ + } + if ('1'<=ch && ch<='7') chord=ch-'0'; + pc.printf("%d %d\n\r", freqlist[0], chord); } + }; + + \ No newline at end of file