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:
- 2:82a0131e3c0e
- Parent:
- 1:516ea1ca7abb
- Child:
- 3:6c77b5c7510f
diff -r 516ea1ca7abb -r 82a0131e3c0e sintone.cpp
--- a/sintone.cpp Wed May 23 01:22:50 2018 +0000
+++ b/sintone.cpp Sun Apr 21 02:54:33 2019 +0000
@@ -1,3 +1,8 @@
+/* sinetone --multiple sin wave generator */
+/* copyright Toshihiro Matsui, IISEC, 2018 */
+/* Any use/modification/copy of this program is granted to anyone
+ as long as the above copyright is maintained*/
+
#include <mbed.h>
#define samplerate 16000
#define MAXFREQS 10
@@ -7,6 +12,7 @@
PwmOut pwm1(p21);
AnalogOut da1(p18);
+// Frequencies of A, A#, B, C, C#, D, D#, E, F, F#, G, G#, totally 12
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];
@@ -26,7 +32,7 @@
for (i=0; i<maxfreqs; i++) {
outval += bytesintab[seqlist[i]];
seqlist[i] += freqlist[i];
- seqlist[i] = seqlist[i] % samplerate; //clip at 160000
+ seqlist[i] = seqlist[i] % samplerate; //wrap around at 160000
}
da1.write((outval/(256.0*maxfreqs))+0.5); }
@@ -40,13 +46,15 @@
tick1.attach(&gentone, 1.0/(float)samplerate);
while (1) {
ch=pc.getc();
+ /* 1..7 changes chord (major, minor, 7th)*/
+ /* a..l changes the base */
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*/
+ else if (chord>1) { setfreq(1,ch-'a'+7); maxfreqs=2;} /*add 5th*/
+ if (chord>2) { setfreq(2,ch-'a'+4); maxfreqs=3;} /*add major 3rd*/
+ if (chord>3) { setfreq(2,ch-'a'+3); /*minor*/ } /*add minor 3rd*/
+ if (chord==7) { setfreq(3, ch-'a'+10); maxfreqs=4; }/*7th*/
}
if ('1'<=ch && ch<='7') chord=ch-'0';
pc.printf("%d %d\n\r", freqlist[0], chord); }