Shop OBEX P1 Docs P2 Docs Learn Events
Musical Notes, Octaves, Durations & Dots from Sheet Music — Parallax Forums

Musical Notes, Octaves, Durations & Dots from Sheet Music

DanNale7DanNale7 Posts: 9
edited 2014-03-12 03:06 in Propeller 1
Can some one please lead me to the source code in SPIN that enables songs to be played on the Propeller. I have source code for the different frequencies but I am not sure how to get the Octave and Durations & Dots. I have the code for the BASIC STAMP but I am not sure how to convert it to the SPIN language.
Thanks in advance!
Dan

Comments

  • RaymanRayman Posts: 13,903
    edited 2014-03-08 16:27
    If you can convert your music to MIDI format, there are a couple MIDI players around here or in OBEX...
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-08 16:59
    Thanks for the quick feedback. I am not sure I fully understand how to convert a MIDI file to SPIN for use on the Propeller. More specifically I was looking for the SPIN version of the BASIC Stamp code to play the musical notes on the Propeller with a piezospeaker.
  • kuronekokuroneko Posts: 3,623
    edited 2014-03-08 18:30
    DanNale7 wrote: »
    More specifically I was looking for the SPIN version of the BASIC Stamp code to play the musical notes on the Propeller with a piezospeaker.
    Did you have a look at OBEX:BS2_Functions? At least it should make the transition easier.
  • Bob Lawrence (VE1RLL)Bob Lawrence (VE1RLL) Posts: 1,720
    edited 2014-03-08 19:22
    This may help:


    Audio Signal Processing
    on the Parallax Propeller


    Multiprocessor Microcontroller

    2.4 Musical Score Notation ........


    http://petehemery.github.io/Final-Year-Project/Dissertation.pdf

    Have a look at the attached code in this post:(HemPeter )
    http://forums.parallax.com/showthread.php/140777-Audio-Signal-Processing-on-the-Propeller-Demo-Board?p=1105502#post1105502
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-08 20:02
    Here's another option.

    I pulled this off from someone's forum post. It looks like there's a few methods from BS2_Functions in it.

    I know I added to it but I don't remember what's mine and what was originally in it.

    I'm not sure, but I think I added the "note" array in the DAT section (or I might have just rearranged it).
    note    long 262, 294, 330, 349, 392, 440, 494 ' c' d' e' f' g' a' b'        long 523, 587, 659, 698, 784, 880, 988 ' c" d" e" f" g" a" b"
            long 1047, 1175, 1319, 1397, 1568, 1760, 1976 ' c"' d"' e"' f"' g"' a"' b"'
            long 2093, 2349, 2637, 2794, 3136, 3520, 3951 ' c"" d"" e"" f"" g"" a"" b""
            long 4186 ' c""'
    

    A great resource for sound generation is the S2 GUI. I often just use the GUI to produce cool sounding effects and then cut and paste the generated Spin code into my program.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-03-08 22:31
    Well, music remains music... regardless of the digital age.

    The most interesting item is that the Propeller has Log Base 2 tables and these can be useful in creating a musical scale with precise frequencies. If you ponder the scale, it is very closely related to Log Base 2.

    Midi is a code convention. The Propeller can handle it quite well, but you really have to learn to think and use Midi. The biggest advantage is there are keyboards and software that conform to it. So you have more resources available to create, replicate, and input music.

    Timing really remains independent of the music scale. Getting the right note is just half the battle. The other half is to provide the right duration with it all based on the correct underlying tempo.

    And so, it seems that one either joins the Midi crowd.. which includes a lot of musicians OR one has to provide three items from scratch [a] tempo, scale, [c] timing of each note.

    Much depends on your appetite for low level programming versus your desire to create music.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2014-03-08 23:43
    When I have converted simple melodies before I have done it in Forth and defined the C C# D D# E F F# G G# A A# B words as well as octave and duration. That way you can transpose music as a series of notes etc in a text file and either compile these into a definition or play them immediately etc.

    For instance after creating a music vocabulary each note and control is a word in the dictionary and this might be the way you would write the first part of Ode to Joy for instance.

    & OdeToJoy E E F G G F E D C C D E . E ^ D o D |

    The & is really just a : word while the | is equivalent to a ;

    I can probably write a quick demo and post a video if anyone fancies that.
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-09 08:34
    Duane Degn wrote: »
    Here's another option.

    I pulled this off from someone's forum post. It looks like there's a few methods from BS2_Functions in it.

    I know I added to it but I don't remember what's mine and what was originally in it.

    I'm not sure, but I think I added the "note" array in the DAT section (or I might have just rearranged it).
    note    long 262, 294, 330, 349, 392, 440, 494 ' c' d' e' f' g' a' b'        long 523, 587, 659, 698, 784, 880, 988 ' c" d" e" f" g" a" b"
            long 1047, 1175, 1319, 1397, 1568, 1760, 1976 ' c"' d"' e"' f"' g"' a"' b"'
            long 2093, 2349, 2637, 2794, 3136, 3520, 3951 ' c"" d"" e"" f"" g"" a"" b""
            long 4186 ' c""'
    

    A great resource for sound generation is the S2 GUI. I often just use the GUI to produce cool sounding effects and then cut and paste the generated Spin code into my program.
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-09 08:44
    Duane,
    Thank you for the response and the spin code. I have attempted to use it and I am not exactly sure why I can not get it to work. Where do you set the piezospeaker PIN in the code?
    Dan
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-09 10:39
    Dan,

    Sorry, I didn't know your experience level.

    The program I posted needs to be called from a parent object.

    I'll post a short demo soon (it should be today).
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-09 11:40
    When I have converted simple melodies before I have done it in Forth and defined the C C# D D# E F F# G G# A A# B words as well as octave and duration. That way you can transpose music as a series of notes etc in a text file and either compile these into a definition or play them immediately etc.

    For instance after creating a music vocabulary each note and control is a word in the dictionary and this might be the way you would write the first part of Ode to Joy for instance.

    & OdeToJoy E E F G G F E D C C D E . E ^ D o D |

    The & is really just a : word while the | is equivalent to a ;

    I can probably write a quick demo and post a video if anyone fancies that.
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-09 11:41
    Peter,
    This is more in line with what I am attempting to accomplish. Please post some of your progress in this area.
    Thanks!
    Dan
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-09 15:07
    I cleaned up the Piezo object and added a demo.

    One of the methods I added to the Piezo object is "PlaySong".

    I translated a few of my sound effect songs generated by the S2 GUI into a format used by "PlaySong".
    DAT
    
    ' Generated by S2 GUI and converted to note index used here
    stopSong      long 100, 22, 50, 18{1500}, 90, 21, 120, 19{1670}, 90, 21, 120, 15
                  long 68, 8{560}, 100, 25, Piezo#END_OF_SONG_MARKER
    
    
    angleSong     long 90, 23{2500}, 150, 25, 70, 14, 100, 17{1460}, 50, 22, 90, 21
                  long 120, 15{1200}, 70, 24, Piezo#END_OF_SONG_MARKER
    
    
    freedomSong   long 152, 11, 35, Piezo#REST, 187, 11, 187, 9, 187, 12, 375, 11, 375, 9
                  long Piezo#END_OF_SONG_MARKER
    
    
    unstuckSong   long 187, 4, 187, 7, 187, 9, 187, 11, 187, Piezo#REST, 187, 9, 1125, 11
                  long Piezo#END_OF_SONG_MARKER 
    
    
    

    The first number is the duration followed by the index number of the note array which contains the frequency.

    You could easily define the index numbers with A1, C3 etc.

    The note array doesn't include any sharps or flats. I don't think it would be very hard to add sharps or flats to the array. You'd need to adjust the index numbers used in the songs if you did so.

    The "songs" listed above are played with the code:
    Pst.Str(string(13, "Playing some short songs."))    Pst.Str(string(13, "Stop Song"))
        Piezo.PlaySong(@stopSong)
        Piezo.Pause(500)
        Pst.Str(string(13, "Found Best Angle Song"))
        Piezo.PlaySong(@angleSong)
        Piezo.Pause(500)
        Pst.Str(string(13, "Freedom Song"))
        Piezo.PlaySong(@freedomSong)
        Piezo.Pause(500)
        Pst.Str(string(13, "Unstuck Song"))
        Piezo.PlaySong(@unstuckSong)
        Piezo.Pause(500)
    

    As others have mentioned, there are a lot of really good objects for playing sounds on the Propeller. I wouldn't include the attached program in the list of "good" sound programs for the Propeller the attached program will probably be easier to understand than the more advanced programs. The only PASM is in the serial driver.

    Make sure and change the constant "PIEZO_PIN" to match your setup.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-09 15:17
    This may help:


    Audio Signal Processing
    on the Parallax Propeller


    Multiprocessor Microcontroller

    2.4 Musical Score Notation ........


    http://petehemery.github.io/Final-Year-Project/Dissertation.pdf

    Have a look at the attached code in this post:(HemPeter )
    http://forums.parallax.com/showthread.php/140777-Audio-Signal-Processing-on-the-Propeller-Demo-Board?p=1105502#post1105502

    Wow!

    That's a lot of great information.

    Thank you for the links.
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-09 16:10
    Duane Degn wrote: »
    I cleaned up the Piezo object and added a demo.

    One of the methods I added to the Piezo object is "PlaySong".

    I translated a few of my sound effect songs generated by the S2 GUI into a format used by "PlaySong".
    DAT
    
    ' Generated by S2 GUI and converted to note index used here
    stopSong      long 100, 22, 50, 18{1500}, 90, 21, 120, 19{1670}, 90, 21, 120, 15
                  long 68, 8{560}, 100, 25, Piezo#END_OF_SONG_MARKER
    
    
    angleSong     long 90, 23{2500}, 150, 25, 70, 14, 100, 17{1460}, 50, 22, 90, 21
                  long 120, 15{1200}, 70, 24, Piezo#END_OF_SONG_MARKER
    
    
    freedomSong   long 152, 11, 35, Piezo#REST, 187, 11, 187, 9, 187, 12, 375, 11, 375, 9
                  long Piezo#END_OF_SONG_MARKER
    
    
    unstuckSong   long 187, 4, 187, 7, 187, 9, 187, 11, 187, Piezo#REST, 187, 9, 1125, 11
                  long Piezo#END_OF_SONG_MARKER 
    
    
    

    The first number is the duration followed by the index number of the note array which contains the frequency.

    You could easily define the index numbers with A1, C3 etc.

    The note array doesn't include any sharps or flats. I don't think it would be very hard to add sharps or flats to the array. You'd need to adjust the index numbers used in the songs if you did so.

    The "songs" listed above are played with the code:
    Pst.Str(string(13, "Playing some short songs."))    Pst.Str(string(13, "Stop Song"))
        Piezo.PlaySong(@stopSong)
        Piezo.Pause(500)
        Pst.Str(string(13, "Found Best Angle Song"))
        Piezo.PlaySong(@angleSong)
        Piezo.Pause(500)
        Pst.Str(string(13, "Freedom Song"))
        Piezo.PlaySong(@freedomSong)
        Piezo.Pause(500)
        Pst.Str(string(13, "Unstuck Song"))
        Piezo.PlaySong(@unstuckSong)
        Piezo.Pause(500)
    

    As others have mentioned, there are a lot of really good objects for playing sounds on the Propeller. I wouldn't include the attached program in the list of "good" sound programs for the Propeller the attached program will probably be easier to understand than the more advanced programs. The only PASM is in the serial driver.

    Make sure and change the constant "PIEZO_PIN" to match your setup.
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-09 16:12
    Duane,
    That is truly amazing! I wanted to program some Christmas Songs and Lights for a project and I need to get through the SPIN source code programing first. Let me know if you decide to put in the sharps and the flatsand the durations of the notes.
    Thank you again! Very impressive!
    Dan
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-11 19:59
    I added more notes to the note array.

    The code I posted earlier had a total of 25 notes and didn't include any sharps or flats.

    The code attached to this post has 108 notes and includes all the notes listed on this webpage. Since the page listed the frequencies to a tenth of a Hertz, I multiplied the given frequency by ten so this extra precision could hopefully be included. I really doubt the extra precision matters when playing the note through a piezo speaker.

    There's the new note array:
    DAT                      '' Notes from chart @ http://www.phy.mtu.edu/~suits/notefreqs.html
            '' These values are ten times the actual frequencies.
               ''  c      c#     d      d#     e      f      f#     g      g#     a      a#     b     
    note    long   164,   173,   184,   195,   206,   218,   231,   245,   260,   275,   291,   309 ' C0 - B0
            long   327,   347,   367,   389,   412,   437,   463,   490,   519,   550,   583,   617 ' C1 - B1
            long   654,   693,   734,   778,   824,   873,   925,   980,  1038,  1100,  1165,  1235 ' C2 - B2
            long  1308,  1386,  1468,  1556,  1648,  1746,  1850,  1960,  2077,  2200,  2331,  2469 ' C3 - B3
            long  2616,  2772,  2937,  3111,  3296,  3492,  3700,  3920,  4153,  4400,  4662,  4939 ' C4 - B4
            long  5233,  5544,  5873,  6223,  6593,  6985,  7400,  7840,  8306,  8800,  9323,  9878 ' C5 - B5
            long 10465, 11087, 11747, 12445, 13185, 13970, 14800, 15680, 16612, 17600, 18647, 19755 ' C6 - B6
            long 20930, 22175, 23493, 24890, 26370, 27938, 29600, 31360, 33224, 35200, 37293, 39511 ' C7 - B7
            long 41860, 44349, 46986, 49780, 52740, 55877, 59199, 62719, 66449, 70400, 74586, 79021 ' C8 - B8
             
    
    
    majorScale    long 0, 2, 4, 5, 7, 9, 11
    
    
    

    The array "majorScale" is used to skip the sharps and flats when playing a scale.

    To make it easier to enter music in the DAT section, I used constant names for each of the notes. I also included constant names for the note duration.
    CON ' Timing Names
    
      MAX_ALLOWED_DURATION = 30_000
      TIMING_CONSTANT_OFFSET = MAX_ALLOWED_DURATION + 1
      #TIMING_CONSTANT_OFFSET, T1_, T1, T2_, T2, T4_, T4, T8_, T8, T16_, T16, T32_, T32, T64_, T64
      ' dotted whole note, whole note, dotted half note, half note,
      ' dotted quarter note, quarter note, dotted eighth note, eighth note, etc.
      '' An underscore indicates the time is half again as long as the time
      '' without the underscore.
    
    
    

    The duration can still be listed in milliseconds but now there's also the option of using one of the timing names.

    Here's how one of the songs looks using constants to set duration and notes.
    unstuckSong             long T4, G4, T4, C5, T4, E5, T4, G5, T4, Piezo#REST, T4, E5, T1_, G5
                            long Piezo#END_OF_SONG_MARKER
    
    
    

    The constant "T4" is used to indicate a quarter note. The constant "T1_" is a dotted whole note. I used the underscore to indicate a duration is to be half again as long as the duration of the denoted by the constant without an underscore.

    The duration of a quarter note defaults to 250ms but this duration can be changed with the "SetQuarterNoteTime" method. The duration times are also changed when the method is called.

    I've included a way to enter notes using the keyboard as an input device.

    When the keyboard mode is entered (either by pressing "k" at the beginning of the program or by pressing "x" and then "k" once the demo loop has started), the following text will be displayed in the terminal window.
        Piezo Speaker Demonstration
    
    
    Press "x" to eXit to main loop.
    Use the keys on the keyboard to play notes.
    The keys "a, s, d, f, g, h, j, k" to play notes in
    active key and octave.
    The keys "w, e" and "t, y, u"
    act are the flats and sharps (black keys).
    
    
    Use "*" to raise the octave one position.
    Use "/" to lower the octave one position.
    Use "+" to shift the active key up one semitone.
    Use "-" to shift the active key down one semitone.
    
    
    
    
    
    
    Active Octave = 5, Active Key = C
    
    
       Db5 Eb5     Gb5 Ab5 Bb5
       [W] [E]     [T] [Y] [U]
     [A] [S] [D] [F] [G] [H] [J] [K]
     C5  D5  E5  F5  G5  A5  B5  C6
    
    

    The note corresponding to the above listed keys will continue to play until a different note is selected or is any other key on the keyboard is pressed.

    As I previously mentioned, there are 108 notes in the array. Not all of these 108 notes are appropriate for use with a piezo speaker. Some of the notes sound more like clicking than a tone on my piezo speaker.

    This is one of those projects I do when I'm avoiding some other project. There are lots of really great sound programs for the Propeller, I think this particular program fills a need that never existed. If you want to make music by pressing keys on your computer's keyboard, there are a lot of better ways to do it than by using the attached program.

    The program does make it pretty easy to enter music into the DAT section for later playback.

    Let me know if anyone has any questions about this monstrosity.

    Edit: I just noticed a bug in the way the notes are displayed with the keyboard chart. I'll fix and and upload the fixed version in a bit. If you download this version (e) make sure and come back the fixed version.
    Edit again: The bug has been fixed. I changed the output display above to show what the corrected code displays.

    Edit(3/11/15): Warning, the code attached is an old version. There are likely better options available.
    I plan to upload this program or an improved version to my GitHub account
    If there isn't code similar to what is attached here on my on GitHub, send me a message and I'll make and check for any improved versions of the code.
  • DanNale7DanNale7 Posts: 9
    edited 2014-03-12 03:06
    Duane Degn wrote: »
    I added more notes to the note array.

    The code I posted earlier had a total of 25 notes and didn't include any sharps or flats.

    The code attached to this post has 108 notes and includes all the notes listed on this webpage. Since the page listed the frequencies to a tenth of a Hertz, I multiplied the given frequency by ten so this extra precision could hopefully be included. I really doubt the extra precision matters when playing the note through a piezo speaker.

    There's the new note array:
    DAT                      '' Notes from chart @ http://www.phy.mtu.edu/~suits/notefreqs.html
            '' These values are ten times the actual frequencies.
               ''  c      c#     d      d#     e      f      f#     g      g#     a      a#     b     
    note    long   164,   173,   184,   195,   206,   218,   231,   245,   260,   275,   291,   309 ' C0 - B0
            long   327,   347,   367,   389,   412,   437,   463,   490,   519,   550,   583,   617 ' C1 - B1
            long   654,   693,   734,   778,   824,   873,   925,   980,  1038,  1100,  1165,  1235 ' C2 - B2
            long  1308,  1386,  1468,  1556,  1648,  1746,  1850,  1960,  2077,  2200,  2331,  2469 ' C3 - B3
            long  2616,  2772,  2937,  3111,  3296,  3492,  3700,  3920,  4153,  4400,  4662,  4939 ' C4 - B4
            long  5233,  5544,  5873,  6223,  6593,  6985,  7400,  7840,  8306,  8800,  9323,  9878 ' C5 - B5
            long 10465, 11087, 11747, 12445, 13185, 13970, 14800, 15680, 16612, 17600, 18647, 19755 ' C6 - B6
            long 20930, 22175, 23493, 24890, 26370, 27938, 29600, 31360, 33224, 35200, 37293, 39511 ' C7 - B7
            long 41860, 44349, 46986, 49780, 52740, 55877, 59199, 62719, 66449, 70400, 74586, 79021 ' C8 - B8
             
    
    
    majorScale    long 0, 2, 4, 5, 7, 9, 11
    
    
    

    The array "majorScale" is used to skip the sharps and flats when playing a scale.

    To make it easier to enter music in the DAT section, I used constant names for each of the notes. I also included constant names for the note duration.
    CON ' Timing Names
    
      MAX_ALLOWED_DURATION = 30_000
      TIMING_CONSTANT_OFFSET = MAX_ALLOWED_DURATION + 1
      #TIMING_CONSTANT_OFFSET, T1_, T1, T2_, T2, T4_, T4, T8_, T8, T16_, T16, T32_, T32, T64_, T64
      ' dotted whole note, whole note, dotted half note, half note,
      ' dotted quarter note, quarter note, dotted eighth note, eighth note, etc.
      '' An underscore indicates the time is half again as long as the time
      '' without the underscore.
    
    
    

    The duration can still be listed in milliseconds but now there's also the option of using one of the timing names.

    Here's how one of the songs looks using constants to set duration and notes.
    unstuckSong             long T4, G4, T4, C5, T4, E5, T4, G5, T4, Piezo#REST, T4, E5, T1_, G5
                            long Piezo#END_OF_SONG_MARKER
    
    
    

    The constant "T4" is used to indicate a quarter note. The constant "T1_" is a dotted whole note. I used the underscore to indicate a duration is to be half again as long as the duration of the denoted by the constant without an underscore.

    The duration of a quarter note defaults to 250ms but this duration can be changed with the "SetQuarterNoteTime" method. The duration times are also changed when the method is called.

    I've included a way to enter notes using the keyboard as an input device.

    When the keyboard mode is entered (either by pressing "k" at the beginning of the program or by pressing "x" and then "k" once the demo loop has started), the following text will be displayed in the terminal window.
        Piezo Speaker Demonstration
    
    
    Press "x" to eXit to main loop.
    Use the keys on the keyboard to play notes.
    The keys "a, s, d, f, g, h, j, k" to play notes in
    active key and octave.
    The keys "w, e" and "t, y, u"
    act are the flats and sharps (black keys).
    
    
    Use "*" to raise the octave one position.
    Use "/" to lower the octave one position.
    Use "+" to shift the active key up one semitone.
    Use "-" to shift the active key down one semitone.
    
    
    
    
    
    
    Active Octave = 5, Active Key = C
    
    
       Db5 Eb5     Gb5 Ab5 Bb5
       [W] [E]     [T] [Y] [U]
     [A] [S] [D] [F] [G] [H] [J] [K]
     C5  D5  E5  F5  G5  A5  B5  C6
    
    

    The note corresponding to the above listed keys will continue to play until a different note is selected or is any other key on the keyboard is pressed.

    As I previously mentioned, there are 108 notes in the array. Not all of these 108 notes are appropriate for use with a piezo speaker. Some of the notes sound more like clicking than a tone on my piezo speaker.

    This is one of those projects I do when I'm avoiding some other project. There are lots of really great sound programs for the Propeller, I think this particular program fills a need that never existed. If you want to make music by pressing keys on your computer's keyboard, there are a lot of better ways to do it than by using the attached program.

    The program does make it pretty easy to enter music into the DAT section for later playback.

    Let me know if anyone has any questions about this monstrosity.

    Edit: I just noticed a bug in the way the notes are displayed with the keyboard chart. I'll fix and and upload the fixed version in a bit. If you download this version (e) make sure and come back the fixed version.
    Edit again: The bug has been fixed. I changed the output display above to show what the corrected code displays.
Sign In or Register to comment.