Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

octoberclub's Content

There have been 1 items by octoberclub (Search limited from 29-March 23)


By content type

See this member's

Sort by                Order  

#37763 Music with a Piezo Speaker

Posted by octoberclub on 22 October 2012 - 01:08 PM in Project Showcase

Its been a while and I forgot to update this post with the change I'd made to calculate the frequency.

Finally written it up here: http://octoberclub.w...-making-sounds/

The fully working project is on github:

https://github.com/o...uino.PiezoTest2

and here's the modified code to calculate frequencies:


using System;
using Microsoft.SPOT;

public static class NoteFrequencies
{
    /// <summary>
    /// Static method to calculate the frequency of a note from its name and octave.
    /// </summary>
    /// <param name="octave">Octave number (0-7)</param>
    /// <param name="note">Note to be found: eg. F#, A, Bb</param>
    /// <returns></returns>
    public static float CalculateFrequency(int octave, string note)
    {
        string noteLC = note.ToLower();
        string[] notes = "c,c#,d,d#,e,f,f#,g,g#,a,a#,b".Split(',');

        // loop through each note until we find the index of the one we want        
        for (int n = 0; n < notes.Length; n++)
        {
            if (notes[n] == noteLC // frequency found for major and sharp notes
                || (note.Length > 1 && noteLC[1] == 'b' && notes[n + 1][0] == noteLC[0])) // or flat of next note
            {
                // Multiply initial note by 2 to the power (n / 12) to get correct frequency, 
                //  (where n is the number of notes above the first note). 
                //  Then mutiply that value by 2 to go up each octave
                return (16.35f * (float)System.Math.Pow(2, (n / 12)))
                    * (float)System.Math.Pow(2, octave);
            }
        }
        throw new ArgumentException("No frequency found for note : " + note, note);
    }
}





home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.