Project - PWM Audio Utility - Music Macro Language

PWM Audio Utility - Music Macro Languate

The contained PWMAudio class provides a very simple interface based on Music Macro Language to generate simplistic beeps using a piezo electric speaker. GW-BASIC or BASIC-A anyone :slight_smile:

See more about MML on wikipedia : Music Macro Language - Wikipedia

I have purposely avoided any of the Gadgeteer specific classes etc. and opted to use the GHIElectronics.NETMF.Hardware.PWM class to provide the PWM functionality.

Basic usage:

// Get a PWM object
PWM pwm = new PWM(PWM.Pin.PWM3);

// Create an instance of the PWMAudio class 
// passing the PWM object used for generating the audio
PWMAudio audio = new PWMAudio(pwm);

// Play a tune blocking until complete

Available methods include:
  Sound(int frequency, int duration) -
    Generates a tone for the specified duration and blocks the caller until complete

  SoundAsync(int frequency, int duration) -
    Generates a tone for the specified duration, does not block the caller

  Play(string commands) -
    Executes the MML commands and blocks the caller until all commands have completed

  PlayAsync(string commands) -
    Executes the MML commands, does not blocks the caller

    Stops the current audio. This call blocks until the execution stops.

Note: Starting a new sound while another sound is already running causes the previous sound to stop.

Supported MML commands: (From Q-Basic Documentation)

Ln        Sets the duration (length) of the notes. The variable n does not indicate an actual duration
          amount but rather a note type; L1 - whole note, L2 - half note, L4 - quarter note, etc.
          (L8, L16, L32, L64, ...). By default, n = 4.

          The shorthand notation of length is also provided for a note. For example, "L4 CDE L8 FG L4 AB"
          can be shortened to "L4 CDE F8G8 AB". F and G play as eighth notes while others play as quarter

On        Sets the current octave. Valid values for n are 0 through 6. An octave begins with C and ends with B.
          Remember that C- is equivalent to B. 

<  >      Changes the current octave respectively down or up one level.

Nn        Plays a specified note in the seven-octave range. Valid values are from 0 to 84. (0 is a pause.)
          Cannot use with sharp and flat. Cannot use with the shorthand notation neither.

MN        Stand for Music Normal. Note duration is 7/8ths of the length indicated by Ln. It is the default mode.
ML        Stand for Music Legato. Note duration is full length of that indicated by Ln.

MS        Stand for Music Staccato. Note duration is 3/4ths of the length indicated by Ln.

Pn        Causes a silence (pause) for the length of note indicated (same as Ln). 

Tn        Sets the number of "L4"s per minute (tempo). Valid values are from 32 to 255. The default value is T120. 

.        When placed after a note, it causes the duration of the note to be 3/2 of the set duration.
         This is how to get "dotted" notes. "L4 C#." would play C sharp as a dotted quarter note.
         It can be used for a pause as well.

Looks good! Can you also add some tags, makes it easier to read. Above post only. Thanks!

@ Architect - Thanks and Fixed. I did not realize that posting the code would also create the Forum post.

Yeah, it is a nice feature. Code share is not as visible as the forum and automatic post bring the attention and a good place to discuss the submission.

I wrote this piece of code mostly because I wanted to start playing with more of the hardware type interfaces and PWM with Audio was the only thing I could think of. Now I need to get some kit so that I can start learning/playing with things like I2C, right now it is all theory and no practice…

What kit are you thinking about? Your Fez device should have everything you need.

I do not have any device (that I know of) that require I2C for example. So I need to get some modules that I can toy with, I currently have the Spider Starter Kit + the E-Block pack.

@ taylorza - Last summer I had a couple of interns at work and one was learning .NET MF. There was no Gadgeteer yet so he was learning on FEZ Cobra and using Glide as UI. He made a nice app that you might find interesting (regarding audio):

1 Like

I recognize the melody :D. Pretty cool project!

@ Gralin - That is pretty sweet! Did the intern ever take that beyond the piezo?

@ taylorza - the intern put a lot of effort into the project because he had to overcome his lacks in programming skills and electronics knowledge. His advantage was the music knowledge. Since this project was his initiative our company had nothing against releasing the code on codeplex. Unfortunately i don’t see the project evolving so i guess he abandoned it after the internship.