Piano sounds frequencies and tone function questions


I'm new to Arduino and I've been playing around with sound.
I just found this site http://jarv.org/files/midi-avr/arduino_all_piano_tones.html
It has a table for all the piano note frequencies. Now I see that those frequencies have a decimal.
example : C8 (Eighth octave) = 4186.01
But the tone() function only accepts an unsigned int as the frequency parameter.
Could I just drop the decimals and use the rounded values? Will this still sound good?

I've read ton's more about generation audio with an Arduino, including wavetable synthesis combined with envelopes, but for now I've realised that that way of generating sound is too complicated for me. So I'll settle with the tone() function and the Tone library that allows playing 2 tones at the same time...

Also if one presses a piano key, the sound it produces will fade out over time instead of abruptly stopping.
Is this possible to simulate this with the tone() function? Or does that require wavetable synthesis too?
Or could I maybe achieve some effect like this with a circuit at the output useing 555 timers/capacitators/whatever... ?

Thanks for reading & kind regards,

Yes, drop the decimal point, doubt you will hear any difference.

Envelope - use same key press that start the tone generation to have a 555 create an envelope.
Use the envelope to control a Voltage Controlled Amplifier

I don't know if this one is still purchasable.

This one seems to be in production still

You can search for same from other sources as well - linear.com, maxim-ic.com, microchip.com for example

The envelope ideally will have an Attack period where the volume ramps up, a Decay period where it holds the level, a Release period where it drops off, a Sustain period where it fades out, and then a Release period where it finally stops. You will see this referred to as ADSR.
This circuit might do it for you. Scroll around, interesting stuff

When I was in college in early 80's, we could buy chips where the 4 periods could be set up with potentiometers, and used to feed voltage controlled amplifiers to control the envelope of a sound waveform. Maybe static hiss for a snare drum, lower frequency for a bass drum, etc..

This one seems recent, a microcontroller programmed for ADSR:


Chips for sale:

It has a table for all the piano note frequencies. Now I see that those frequencies have a decimal.
example : C8 (Eighth octave) = 4186.01

You only need the highest octave in the table

C8 = 4186 => C7 = C8/2 C6 = C8/4 C5 = C*/8 etc

so if you know the tone and octave you can calculate the freq very fast // assume tone = 0..11 mapping C..B#
The table must be of the type int.

freq = table[tone] >> (8-octave); // 1 bit shifted is division by 2 .

@crossroads: there is an inexpensive substitute for the ssm2164: V2164 (coolaudio)
i think, this is the only affordable vca chip at the moment...
otherwise: build it yourself :slight_smile: http://hem.bredband.net/bersyn/VCA/vca_shootout.htm

@memorex: //555 timer
i´ve played around with this two (basic!) schematics:
555 timer as vco: 19.FM - VCO - 555Timer I Ham lesson o' de day
getting cv-voltage with pwm: http://i50.tinypic.com/29ffjoo.png
please note, that using other opamps (like tl072,...) than the lm358 in the cv results in strange behaviors
I try to realize just a VCA using digital potentiometers.

Some fundamental resources for adsr envelopes and coding them in C(++): http://hackmeopen.com/2011/12/synth-diy-software-for-generating-adsr-envelopes/

"there is an inexpensive substitute for the ssm2164: V2164 (coolaudio)

Is there a datasheet available? Pinout? I'm not seeing one on the page.

Ah - here we go

At this moment the toneAC library is being developed.
You need version 1.2 (will be ready soon).

The toneAC library is very small and fast and can control the volume.
So some kind of fade out is possible. But with digital pulsed sound, also the sound changes.
But if you make a fast fade out, it's not a big problem.
It is ment to drive a loudspeaker with two pins of the Arduino, it is not a signal for an amplifier.

Another option is the Mozzi library.
The Mozzi library is very big, and can create awesome sounds.