Hi Omar,
Just had a look at your player for a little holiday project I'm working on and found that some notes couldn't be heard (I guess the same thing happens with the two notes you claim to have missing in the Mission Impossible
ringtone).
After some investigation, what I found was that your PlayNote method allows the duration to be longer than the period, which I understand is not allowed. If I understand it correctly PWM sets a period (the total time of one cycle in the square wave) and the duration is the total time in that period that it's ON).
This means that the duration should therefore be less than the period. However, if you make the duration the same as the period or slightly less, it still drops off (you can still hear it, but only faint). I found that a max duration of 95% of the period worked well for me. I changed your PlayNote method as follows:
public static void PlayNote(uint frequencyInHertz, uint durationMilliseconds)
{
if (frequencyInHertz != 0)
{
uint timbre = 50; // 50% = perfect square wave.
pwm.SetDutyCycle(timbre);
uint period = (uint)1000000 / frequencyInHertz; // 1000,000 instead of 1,000 because SetPulse duration is in microseconds, not in milliSeconds.
//Duration needs to be less than the period. To make sure we have at least some sound, make sure we stay below 95% of the period:
double maxDuration = 0.95*period;
uint duration = (uint)(durationMilliseconds <= maxDuration ? durationMilliseconds : maxDuration);
Debug.Print("Frequency -> " + frequencyInHertz + ", Period -> " + period + ", durationMilliseconds -> " + durationMilliseconds + ", Max Duration -> " + maxDuration + ", duration -> " + duration);
pwm.SetPulse(period, duration);
}
//-- Wait for note (or silence) to play out, then be quiet --
Thread.Sleep((int)durationMilliseconds);
pwm.SetDutyCycle(0);
}
Hope this helps. If anyone has some suggestions or want to tell me I'm completely wrong, please feel free. I'm an experienced .Net developer, but new to Netduino/electronics, so...
Robert :-)