Better tone

Using the note/pitches/tone "library" I noticed that it did not go down to C0 then I noticed that it also was not very accurate for some of the notes so I decided to make it a wee bit better.

#define c0  16.35
#define cs0 17.32
#define d0  18.35
#define ds0 19.45
#define e0  20.6
#define f0  21.83
#define fs0 23.12
#define g0  24.5
#define gs0 25.96
#define a0  27.5
#define as0 29.14
#define b0  30.87
#define c1  32.7
#define cs1 34.65
#define d1  36.71
#define ds1 38.89
#define e1  41.2
#define f1  43.65
#define fs1 46.25
#define g1  49
#define gs1 51.91
#define a1  55
#define as1 58.27
#define b1  61.74
#define c2  65.41
#define cs2 69.3
#define d2  73.42
#define ds2 77.78
#define e2  82.41
#define f2  87.31
#define fs2 92.5
#define g2  98
#define gs2 103.83
#define a2  110
#define as2 116.54
#define b2  123.47
#define c3  130.81
#define cs3 138.59
#define d3  146.83
#define ds3 155.56
#define e3  164.81
#define f3  174.61
#define fs3 185
#define g3  196
#define gs3 207.65
#define a3  220
#define as3 233.08
#define b3  246.94
#define c4  261.63
#define cs4 277.18
#define d4  293.66
#define ds4 311.13
#define e4  329.63
#define f4  349.23
#define fs4 369.99
#define g4  392
#define gs4 415.3
#define a4  440
#define as4 466.16
#define b4  493.88
#define c5  523.25
#define cs5 554.37
#define d5  587.33
#define ds5 622.25
#define e5  659.26
#define f5  698.46
#define fs5 739.99
#define g5  783.99
#define gs5 830.61
#define a5  880
#define as5 932.33
#define b5  987.77
#define c6  1046.5
#define cs6 1108.73
#define d6  1174.66
#define ds6 1244.51
#define e6  1318.51
#define f6  1396.91
#define fs6 1479.98
#define g6  1567.98
#define gs6 1661.22
#define a6  1760
#define as6 1864.66
#define b6  1975.53
#define c7  2093
#define cs7 2217.46
#define d7  2349.32
#define ds7 2489.02
#define e7  2637.02
#define f7  2793.83
#define fs7 2959.96
#define g7  3135.96
#define gs7 3322.44
#define a7  3520
#define as7 3729.31
#define b7  3951.07
#define c8  4186.01
#define cs8 4434.92
#define d8  4698.64
#define ds8 4978.03

And yes I know not all of these go directly in to 16 or 8 Mhz but it does make it slightly more accurate. Information found here http://www.phy.mtu.edu/~suits/notefreqs.html And this is not meant to impress, just help with simple tones. Also helps if you want to build a guitar/bass/stringed instrument tuner.

Hmm guess I need to get my audio equipment checked then. odd

oh ok I see now. well still helps if someone wants to make a guitar tuner.

Although this isn't useful for the actual tone library, thanks for amking this.

I was planning on doing this anyway, I have a project where I wrote all the timer management code for playing tones myself, so I can modify it for more precise freq's.

That would be a nice use for this.

KE7GKP: The tone() function takes only an integer argument for frequency. So, by definition/design it will never be capable of really accurate musical notes. Except for those notes that fall on integer values. Like most "A"s and a few "G"s. (Assuming modern tuning and temperament.)

Suggestion: You can set the tones ten times the freq you need, then use an external divide by ten chip to get tenth of a Hz resolution. I used this in a CTSS tone generator. Don Lancaster's CMOS Cookbook describes how to use a five stage walking ring counter as a divide by ten and implements a simple A/D resistor ladder to get a more sine looking waveform. The circuit needs a 4018 CMOS chip and four resistors.

See some waveforms in this Flickr set: http://www.flickr.com/photos/wb8nbs/sets/72157625989821423/

good idea wb8bs!

in fact any divider would do - I would prefer multiple divide by 2 dividers - as it would make it very easy to transpose a song ;) The table would just shift 12 places..

That said, I sometimes wonder why using a whole table with 8*12 entries as 12 highest notes would do. Another octave is just dividing by a power of 2

freq = NOTE >> (7-octave);

OK it consumes a bit more CPU time, but not that much. would free up 7 * 12 ints (floats) = 168 or 336 bytes. (8-16% of the free RAM) OK put the table in flash ..

This circuit http://electro-music.com/forum/phpbb-files/sine_wave_generator_156.gif is the same as the CMOS Cookbooks 5 stage converter except Lancaster specs 22k for R1 and R3, and 36k for R2 and R4. The Cookbook also shows a version with a divide by 8 configuration and 3 resistors but the output is a little rougher. Either is a lot better than a square wave.

Wow, I was not being entirely smart.

I had been using a table of the 12 LOWEST frequencies, and multiplying by powers of 2, which meant that the imprecision in the integer note frequencies was only multiplied in higher octaves. So I thought I would use an array of all the note frequencies, every octave, to solve the tuning issue. Never occurred to me to use the highest freq's and divide. :roll_eyes: