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.
Photo

Speaker class and Netduino Keyboard


  • Please log in to reply
13 replies to this topic

#1 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 24 April 2011 - 02:29 PM

Hi :)

While toying with PWM I connected a speaker to my Netduino. I got some noise from it in no-time, but regulated sounds was a challenge. I got some help from a good friend of me and with his help I crafted the attached class. Driving it is very simple if you are familiar with ancient BASIC-dialects.

For my example I connected an old PC-speaker to pin 9 and ground.

First we need to initialize the speaker:
Speaker PCSpeaker = new Speaker(Pins.GPIO_PIN_D9);
Then we have three methods, as mentioned before I took the syntax from BASIC.

For a simple beep:
PCSpeaker.Beep();

To produce a 440 hertz tone for 5 ticks:
PCSpeaker.Sound(440, 5);

To play a simple melody:
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.

The syntax of the Play-method is the same as in BASIC and according to the Music Macro Language. Here's a summary:
// 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).
// 
// 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.

After this class was done I made a simple keyboard, see this video for more details:

http://www.youtube.com/watch?v=m0FUJ579bp0

:D

In QBasic there were a lot of people who liked to put up some (part of) songs, I found these quite nice:
- http://www.madhouseb...iew&view=qbasic
- http://www.network54...asic Play Music
- http://www.scribd.co...irit-Qbasic-txt

You can just use these strings for the Speaker.Play()-method to get the same result!

Downloads:
- Speaker.zip (Just the driver and a very simple sample project)
- NetduinoKeyboard.zip (The keyboard-project from the movie)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#2 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 24 April 2011 - 02:51 PM

You have cool friends :)
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 24 April 2011 - 02:54 PM

HA!...Amazing!...Really cool!
Stefan: at the moment I really can't add anything more than this comment, because I must go to my parent's home...(Ops!)...Happy Easter to you and all your family, assuming you are christian, otherwise happy week-end anyway!

Please take a look at this OLD chip from Texas Instruments: SN 76477.
I have one in my might box!...

Take a look what you can do:

Cheers!
Biggest fault of Netduino? It runs by electricity.

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 24 April 2011 - 03:39 PM

just added a video to the post :)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 25 April 2011 - 10:53 AM

To also support RTTTL-files, I wrote a conversion tool. You can use it at: https://stefan.co/st...tttl_to_mml.php I still only keep supporting MML at the moment since that format takes less resources to store and it's for a micro framework :) This makes it possible to find a lot of tunes to play on netduino; http://www.google.co...h?q=rtttl tones
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#6 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 03 November 2011 - 09:28 PM

It would be nice to have this speaker class in NuGet. aalmada

#7 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 03 November 2011 - 09:36 PM

It would be nice to have this speaker class in NuGet.

Hi aalmada,

NuGet is for regular .NET right? In that case there is no PWM but a sound card driver (DirectSound) to communicate with. That's a totally different kind of programming.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#8 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 28 November 2011 - 02:45 AM

Hey guys.

Is this a valid series of tones?
PCSpeaker.Play("T220 L16 O7 E P16 G P16 O8 E P16 C P16 D P16 G");

I am getting an "IndexOutOfRange" Exception when it tries to access a "note" (89) from the "tone bar" (which has an upper limit 84).

Thanks.

Mark

[Edit]

I was messing around with the .Sound method, and formulated a workaround. I did some googling to figure out the freq of each of the tones above and just fired off the raw sound instead of having to go through the tonebar with my notes.
PCSpeaker.Sound(1318.51F, 2.5F);
PCSpeaker.Sound(1567.982F, 2.5F);
PCSpeaker.Sound(2637.021F, 2.5F);
PCSpeaker.Sound(2093.005F, 2.5F);
PCSpeaker.Sound(2349.318F, 2.5F);
PCSpeaker.Sound(3135.964F, 2.5F);

Not the most elegant, but it works for my six notes. Any more notes and I probably would've created a fancy float array and a loop.

Give it a shot and see if you recognize the tune :)

Also, I'm still curious about why the notes I was trying to pass weren't in the tone bar. Is my note string malformed?

Mark

#9 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 28 November 2011 - 07:31 AM

Hi Mark,

From http://netmftoolbox....ardware.Speaker :

Ooctave Sets the current octave (0 - 6).

I see you're using O7 and O8. The code just isn't prepared for that.

I just added two more octaves to speaker.cs, downloadable from http://netmftoolbox....list/changesets :)

If you look at the difference, you'll see how to add even more octaves.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#10 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 28 November 2011 - 08:02 PM

I see you're using O7 and O8. The code just isn't prepared for that.


Totally missed the octave range limit in the documentation. Thanks for the info (and changeset) :)

Mark

#11 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 18 February 2012 - 01:48 AM

Truly great stuff! Listening to an Adele midi converted by your PHP page. Wish you had a .Net class we could convert on the fly. But I ain't complaining. Thanks for sharing!

#12 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 18 February 2012 - 10:42 PM

Wow, I Netduino piano has seen the light of day! The next logical step would be support for midi-songs be played from an SD card - when will we see that?

#13 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 18 February 2012 - 11:18 PM

Wow, I Netduino piano has seen the light of day! The next logical step would be support for midi-songs be played from an SD card - when will we see that?

perhaps, some day ;)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#14 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 18 February 2012 - 11:51 PM

perhaps, some day ;)

Well, the night is still youngPosted Image

Actually, this is also PWM but at a slightly different level.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.