# Lesson 12 # Arrays Lesson - Part 2. In this lesson, # you will learn about arrays of arrays # (multi-dimensional arrays) and rings. # General Instructions: # # This lesson contains two examples and # two exercises (A, B). # # To listen to an example, change "comment" # to "uncomment" on the "comment do" line, # then click the "Run" menu command at the # top of the Sonic Pi editor window. # When you are done with the example, turn # it off by changing "uncomment" back to # "comment," and move on to the next section # of the lesson. # # Arrays can hold any kind of object, # including other arrays. This can be very # useful for musical programming. # Example 1: Multi-dimensional Array. comment do triads_array = [[:C4, :E4, :G4], [:F4, :A4, :C5], [:G4, :B4, :D5]] count = triads_array.length index = 0 count.times do triad_array = triads_array[index] t_count = triad_array.length t_index = 0 t_count.times do note = triad_array[t_index] play note sleep 1 t_index += 1 end index += 1 end end # Exercise A # 1) Change the name and contents of the # triads_array in Example 1 # to the following: # a) seventh_chord_array (V7 chords in C, F, # and G Major) # b) interval_array (M2, M3, P4, P5, M6, M7, P8) # c) rhythms_array (Pie, Apple, Huckleberry, # Blackberry, Butterscotch) # 2) For a) and b), change the name of # triad_array in the inner loop to match # the changed triads_array. # For c), choose a note to play, and # use the sleep command to create a rhythm # based on each value in each rhythm array # retrieved from the rhythms_array. Change # the name of triad_array to rhythm_array. # Try moving the sleep 1 command for a) so # that each 7th chord plays harmonically # as a block rather than as a broken chord. # Sonic Pi has a type of array called a ring # that is circular. The index of a ring is not # limited to the number of values in the ring # as it is in a standard array. Indexes of a # ring "wrap around" so that numbers higher # (or lower) than the boundaries of the ring # access the next position. # Example 2: Ring comment do scale_ring = (ring :C4, :D4, :E4, :F4, :G4, :A4, :B4, :C5) index = 0 24.times do note = scale_ring[index] play note sleep 1 index += 1 end end # Exercise B # 1) Change the index increment in Example 2 # to index += 2. What happens? # 2) Change the index increment in Example 2 # to decrement: index -= 1. What happens?