Shop OBEX P1 Docs P2 Docs Learn Events
Piezo music... Happy Birthday to you — Parallax Forums

Piezo music... Happy Birthday to you

Hello All,

Has anyone ever posted PBASIC Code for the song "Happy Birthday to you"? If so were can I get it. I saw the Parallax video about it, but no way can I ever put it together.


Thanks for any help

Comments

  • WhitWhit Posts: 4,191
    edited 2018-04-04 21:35
    hmlittle59,

    Do you know of this resource? https://forums.parallax.com/discussion/115409/playing-sheet-music-with-the-piezospeaker

    With the next post, you should be in business! Good luck.
  • WhitWhit Posts: 4,191
    This is from Wikipedia and the words and tune are now in the public domain.

    Happy%20Birthday%20from%20Wiki.JPG
    802 x 214 - 38K
  • WhitWhit Posts: 4,191
    Just so everything is in one place!

  • Hello Whit,

    Its now time to enter the "Happy Birthday" code to my programs and I must admit. I'm 100 times more confused with this new knowledge then I was before. I've found 3 or 4 songs and on the BS2p 48pin they sound horrible. I know the FREQ. is off but don't know if to take it up or down,,,left or right :zombie:
    I've seen the video (some links now broken) and links no longer available under new Parallax Site layout. (that I can find). Don't know any NOTE's.

    I will read it over and over and over again. Maybe the light will come on. :confused::D >:(

    Will re read the What's a Microcontroller about again also.

    Thanks
  • hmlittle59,

    Page 253 of WAM 3.0 lists the frequencies of several piano keys.
    Note that the center of the spiral of the Treble or G-Clef is actually note G4 but It's bumped up to G6 for the piezo so it sounds better.

    Also the BS2p runs faster than the BS2 so the FREQOUT frequency and duration values will need to be adjusted.
    FREQOUT is on page 199 of the BASIC Stamp Manual [Page 203 of the PDF].
    You need to multiply the frequency by 3.77 and the duration by 0.265 to use BS2 code on the BS2p.
  • Hello,

    Thanks for all the help. I'm giving it another go at this time. :o:o:o
    Back to the WAM file also. Don't know how to relate the Piano key freq. yet

    Here we go.

    Thanks again.
  • update:
    Gave up for now. Have read and down loaded some PBASIC short songs. I guess I am learning, just don't feel like it. Thanks to Whit for the Sheet music....but, don't know how to match anything up.

    Tomorrow.

    Thanks
  • hmlittle59,

    I find it easiest to convert a song to RTTTL and then use the RTTTL player in WAM.
    Since the program is large it's available as a download if you haven't already entered it in.
  • Genetix,

    I did look at that in WAM, I will look at that again and see if I can make some headway.

    Thanks
  • update:
    Some progress. Got the Twinkle Twinkle to play from Parallax and then converted it over to Happy Birthday. First I had to get a Google the song that included the Notes(letters, a,b,c...etc.). Sounds horrible but you can tell it's the song. Lots more work to do.

    For some reason it will not stop looping over a count of 15....

    Thanks again...will update.
  • I use this code in SPIN but you should be able to re-work it for your language of choice.

    First store the notes, their frequencies, and the note lengths. In this case I have stored three octaves of each note.
    notec         long      10465,20930,41860,0
    notecsharp    long      11087,22175,44349,0
    noted         long      11747,23493,46986,0
    notedsharp    long      12445,24890,49780,0
    notee         long      13185,26370,52740,0
    notef         long      13970,27938,55877,0
    notefsharp    long      14800,29600,59199,0
    noteg         long      15680,31360,62719,0
    notegsharp    long      16612,33224,66449,0
    notea         long      17600,35200,70400,0
    noteasharp    long      18647,37293,74586,0
    noteb         long      19755,39511,79021,0
    
    sixteenth long 62
    eighth  long  125
    eighthdot long 187
    quarter long  250
    quarterdot long 375
    half       long 500
    halfdot    long 750
    whole       long 1000
    

    Then for happy birthday, you just call each note like this:
            playnote(notec[1],eighthdot)
            playnote(notec[1],sixteenth)
            playnote(noted[1],quarter)
            playnote(notec[1],quarter)
            playnote(notef[1],quarter)
            playnote(notee[1],half)
            
            playnote(notec[1],eighthdot)
            playnote(notec[1],sixteenth)
            playnote(noted[1],quarter)
            playnote(notec[1],quarter)
            playnote(noteg[1],quarter)
            playnote(notef[1],half)
            
            playnote(notec[1],eighthdot)
            playnote(notec[1],sixteenth)
            playnote(notec[2],quarter)
            playnote(notea[1],quarter)
            playnote(notef[1],quarter)
            playnote(notee[1],quarter)
            playnote(noted[1],quarter)
            
            playnote(noteasharp[1],eighthdot)
            playnote(noteasharp[1],sixteenth)
            playnote(notea[1],quarter)
            playnote(notef[1],quarter)
            playnote(noteg[1],quarter)
            playnote(notef[1],halfdot)
    
    In spin the playnote methods look like this... For my use case the freqStart method actually just runs forever in a separate cog and responds to changes in the value of "notex".
    
    PRI freqStart(spk,dur)
      dira[buzzer]:=%1
      ctra[30..26] := %00100               ' Set ctra for "NCO single-ended"
      ctra[5..0] := %00001                   ' Set APIN to P11
      repeat
        frqa := notex                      ' Set frqa for 2093 Hz (C7 note)        
        Play(spk,dur)
    
    pri playnote(nt,len)
        notex:=nt
        timing.pause(len)
        nonote(50)
    
    PRI Play (spk,dur)  
      dira[spk]~~
      waitcnt(clkfreq / dur + cnt)
    
    
    I hope this helps, the frequencies may have to be adjusted for pbasic...
  • rogersyd,

    He's using a BASIC Stamp, not a Propeller.

    hmlittle59,

    Double check your code for an error or there might be a DATA value for the number of notes.
    It's best if you post your code.
  • Yes,
    That code made my head start hurting all over again. :zombie: :o :clown:

    It seems to be "ALMOST" working, but the "FREQ." just not working together from the sheet that I downloaded (D6,D7,D8).

    Will keep plugging away.
  • hmlittle59hmlittle59 Posts: 404
    edited 2018-09-01 00:22
    UpDate: Edited Aug 31, 2018...OOP's

    I think that I've got it working.....kinda of sort of.....

    I've tuned it, but it will need some fine tuning. The pitch (freq.) is a little off, but I can hear it. I go thru it twice (14 notes) of "Happy Birthday to you" pause 175 then once again.

    Thanks to all for helping to really really reduce my learning curve. Could not have done it on my own and that's a fact...

    PS: Now, maybe I can start to use my 25 yr old keyboard that's sitting here.....NOT BEING USED.

    Now I have to patch it into my code.
    Thanks again.

    The Math can be done to reduce the code from BS2 to BS2p.
    '
    ' Play the first 14 notes from Happy Birthday.
    
    '{$STAMP BS2p}
    '{$PBASIC 2.5}
    
    index               VAR   Nib
    noteLetter          VAR   Byte
    noteFreq            VAR   Word
    noteDuration        VAR   Word
    TmAdj               CON   $3c5    ' x 3.770     TmAdj */
    fradj               CON   $044    ' x 0.265     FrAdj */
    
    Notes            DATA  "D","D","E","D","G","F"
                     DATA  "D","D","E","D","G","F"
    
    Durations        DATA  Word 325, Word 325, Word 300, Word 300,
                           Word 300, Word 200,
                           Word 300, Word 300, Word 300, Word 300,
                           Word 300, Word 200
    
    Frequencies      DATA  Word 2349, Word 2349, Word 2673, Word 2349,
                           Word 3136, Word 2794,
                           Word 2349, Word 2349, Word 2673, Word 2349,
                           Word 3136, Word 2794
    
    
    DEBUG    "Note  Duration  Frequency", CR,
             "----  --------  ---------", CR
    
    FOR index = 0 TO 11
                             IF index = 6 THEN PAUSE 175
      READ Notes + index, noteLetter
      DEBUG "   ", noteLetter 
    
      READ Durations + (index * 2), Word noteDuration
      DEBUG "      ", DEC4 TmAdj */noteDuration
    
      READ Frequencies + (index * 2), Word noteFreq
      DEBUG "       ", DEC4 FrAdj */noteFreq, CR
    
      FREQOUT 12,TmAdj */ noteDuration, FrAdj */noteFreq
    
    NEXT
    
    STOP
    

    Edited Aug 31, 2018...OOP's
    Corrected "PAUSE" # 5 from 360 to 300.... and Corrected "NOTE" #12 to 2794.
    my bad.
    thanks
Sign In or Register to comment.