Song Mapper

MARY TODO: add a reminder about how the blank in the functional input to MAP works. --MF, 2/27/22
In this activity, you will use the map block to transpose (shift) all the pitches in your song.

Pamela, I debated whether to teach the ADT first so they can traverse a 2D list or to show them how to use MAP to transpose first. I went with MAP first and letting 2D lists/ADTs be a later optional page (perhaps not even to be written now or perhaps just a "Take It Further" on the last page) since the goal of the course is functional, and MAP would align with that. There is no real need to teach ADTs although the context does naturally inspire it. --MF, 2/12/22

When transposing a song, it's especially important not to call MIDI note numbers "pitch." The actual pitches are in a geometric progression, not a linear one, and when you transpose by n steps you're actually multiplying the pitches by (\sqrt[12]{2})^n. --bh 3/3/22

I agree to rename the variable for next year. --MF, 3/6/22

Another advantage of using a list to store your notes is that you can use higher order functions to process your list. You can use map to play a song higher or lower than the original pitches.
map (( ) + (3)) over (list ((64) (62) (60) (62) (64) (64) (64))) reporting the list 67, 65, 63, 65, 67, 67, 67

If you store the list of pitches in a variable, you can use play song together with map to play the same song several times in different ways.
set ('Mary Had a Little Lamb') to list ((64) (62) (60) (62) (64) (64) (64))
play song (Mary Had a Little Lamb)
play song (map (( ) + (3)) over (Mary Had a Little Lamb))

  1. Set Up Your Headphones or Speakers Open this Mary Had a Little Lamb map example. Click each block to: set the variable, play the song, and then play the transposed (shifted) song.
  2. Try changing the shift (the second input to the + function in the map block) from 3 to some other integer. (You can make it any integer—positive or negative—but if it's too high or low, you won't be able to hear the song).
Do they know the word "apply" in this sense? -bh

See 1.4.4. --MF, 3/6/22

The map block takes two inputs: a function (in this case ( ) + (3)) and a list, applies the function to each item in the list, and reports a new list of the results.

Recall, a function that takes a function as input (like map) is called a higher order function.

  1. Open this Twinkle Twinkle Little Star map example, and play the song.
  2. Remember, you can turn on Visible Stepping (Visible Stepping button with two footprints) to see which block is running as it runs.
  3. Modify this song by adding additional play song blocks, changing the shift values in the map blocks, or changing the pitches in the song.
  4. Now Is a Good Time to Save
  1. Now Is a Good Time to Save
    "Your latest project" should be more specific. -bh 3/3/22
    Open your latest project in Snap!, and use map and play song together with one of the variables that you used to store your pitches to make your song more complex.
  2. Add additional variables to store different verses, and create more interesting music.
  3. I'm not sure what this bullet ("additional variables") is suggesting I do. Add more verses? -pf 3/6/22

    Better? --MF, 3/7/22

  4. In this activity, you changed the pitches by adding or subtracting, but you could also multiply or divide. Try it and see what happens!
    You can pull the map block out of the play song with pitches and beats block temporarily and then click it to see what values it reports.
In this activity, you used the higher order function map to transpose lists of pitches stored in variables and create more complex music.