Music Project

In this project, you will use the Snap! play note block to create music while you review abstract data types and higher-order functions.

Reviewing Higher-Order Functions with Sound

  1. Build and compare these play scripts. Run each script a few times.

    1. The inputs values given to the play note block are musical pitches. Higher values create higher notes. You can input any integer from 0 and 127. The number 60 represents middle C.

      play note (item (any) of (list (60) (64) (67) (72) (60))) for (1) beats

    2. for each (note) of (list (60) (64) (67) (72) (60)) (play note (note) for (1) beats)

    3. for each (note) of (map (_ + 5) over (list (60) (64) (67) (72) (60))) (play note (note) for (1) beats)

    4. for each (note) of (keep items such that (() < 65) from (list (60) (64) (67) (72) (60))) (play note (note) for (1) beats)

Need to Review?

Creating an Abstract Data Type to Organize Musical Data

  1. Pitch is the amount of highness or lowness of a musical note; the pitch value goes in the first input slot of the play note block.

    The length of a note is the amount of time that the note plays (the number of beats); the length value goes in the second slot of play note.

    Create a note ADT to manage the pitch and length of each note in a song.

    1. Create the constructor:
      note, pitch: () length: ()

    2. Create two selectors:
      pitch from note: () length from note: ()

Creating Blocks to Play Music

  1. Use for each together with your selectors to build a play song {} block that takes a list of notes as input and plays each pitch for the specified number length of time.
    Play Song data type definition

Chart for note pitch numbers.

picture of keyboard with keys labeled with letters and numbers; 60 is middle C and the numbers change by 1 with each half step

  1. Create a reporter to reports the notes for a song of your choosing. Here’s an example:
    row row row your boat reporter block definition

  1. Test your song with your play song block, and debug any problems.
    play song (row row row your boat)

If There Is Time…

BPM stands for “beats per minute.”

  1. Use the set tempo to () bpm block to change the pace at which the notes are played. A higher number will make your song play faster; a lower number will make it play slower.

Transposing Music

  1. Talk with Your Partner Compare the output of these two scripts that you created above. Discuss what map + 5 does to the sounds you hear:
    for each (note) of (list (60) (64) (67) (72) (60)) (play note (note) for (1) beats)
    for each (note) of (map (_ + 5) over (list (60) (64) (67) (72) (60))) (play note (note) for (1) beats)

  1. Use map together with your ADT blocks to create a reporter that transposes (shifts) a list of notes. It should take a list of notes and a number indicating how much to transpose (shift) the song as input, and it should report the adjusted song. For example:
    play song (transpose (row row row your boat) by (19))

    Reveal a hint.

    transpose block definition hint showing 'transpose (notes) by (shift): report (map (note, pitch: ((pitch from note ([empty list input slot])) + ([something unreadable])) length: ([something unreadable])) over ([something unreadable]))'

  1. Try playing your song using your transpose block with several different shift numbers so that you can hear the impact of map.