This project is read-only.

Toolbox.NETMF.Hardware.Speaker


Toolbox.NETMF.Hardware.Speaker(SpeakerPin)
  • SpeakerPin The pin the speaker is connected to (must be a PWM-pin!)

Events

  • OnPlay Triggered after every played tone. data2 contains the remaining bytes to play

Supported methods

  • Beep() produces a short beep
  • Sound(Frequency, Duration) produces a tone at a specific frequency for a given duration (duration is in ticks with 18.2 ticks per second)
  • Play(CommandString) is the most advanced method and plays a song according to a variant of the Music Macro Language. Full supported syntax below.

Supported Music Macro Language commands

Octave and tone commands:
Ooctave Sets the current octave (0 - 6).
< or > Moves up or down one octave.
A - G Plays the specified note in the current octave.
Nnote Plays a specified note (0 - 84) in the seven octave range (0 is a rest).

Duration and tempo commands:
Llength Sets the length of each note (1 - 64). L1 is whole note, L2 is half note, etc.
ML Sets music legato.
MN Sets music normal.
MS Sets music staccato.
Ppause Specifies a pause (1 - 64). P1 is a whole-note pause, P2 is a half-note pause, etc.
Ttempo Sets the tempo in quarter notes per minute (32 - 255).

Mode commands:
MF Plays music in foreground.
MB Plays music in background.

Suffix commands:
# or + Turns preceding note into a sharp.
- Turns preceding note into a flat.
. Plays the preceding note 3/2 as long as specified.

RTTTL Support

Besides MML, there's another standard for storing monophonic sounds, called RTTTL. Used quite a lot by Nokia for their ringtones, but RTTTL strings are mostly bigger as MML strings. Therefor we only support MML.
To convert RTTTL to MML, use this converter: http://www.netmftoolbox.com/tools/rtttl_to_mml.php

MIDI Support

There are a lot of music formats. Midi is one of them. Simple midi files can be converted so it can be used with MML. Keep in mind though, Midi is way more advanced, supports multiple channels at the same time, different instruments, etcetera.
To convert MIDI to MML, use this converter: http://www.netmftoolbox.com/tools/midi_to_mml.php

How to connect

Toolbox.NETMF.Hardware.Speaker.png

Sample code in C#

// Initializes the speaker
Speaker PCSpeaker = new Speaker(new Netduino.PWM(Pins.GPIO_PIN_D9));

// Produces a simple beep
PCSpeaker.Beep();

// Wait for 2 seconds
Thread.Sleep(2000);

// Produces a 440 hertz tone for 5 ticks
PCSpeaker.Sound(440, 5);

// Wait for 2 seconds
Thread.Sleep(2000);

// Plays a little song
PCSpeaker.Play("cdec cdec");     // Are you sleeping? Are you sleeping?
PCSpeaker.Play("efg efg");       // Brother John, Brother John,
PCSpeaker.Play("gagfec gagfec"); // Morning bells are ringing! Morning bells are ringing!
PCSpeaker.Play("c<g>c c<g>c");   // Ding, dang, dong. Ding, dang, dong.

Sample code in VB

' Initializes the speaker
Dim PCSpeaker As Speaker = New Speaker(new Netduino.PWM(Pins.GPIO_PIN_D9))

' Produces a simple beep
PCSpeaker.Beep()

' Wait for 2 seconds
Thread.Sleep(2000)

' Produces a 440 hertz tone for 5 ticks
PCSpeaker.Sound(440, 5)

' Wait for 2 seconds
Thread.Sleep(2000)

' Plays a little song
PCSpeaker.Play("cdec cdec")     ' Are you sleeping? Are you sleeping?
PCSpeaker.Play("efg efg")       ' Brother John, Brother John,
PCSpeaker.Play("gagfec gagfec") ' Morning bells are ringing! Morning bells are ringing!
PCSpeaker.Play("c<g>c c<g>c")   ' Ding, dang, dong. Ding, dang, dong.

Last edited May 14, 2012 at 10:50 PM by Garrcomm, version 6