Old Pages‎ > ‎Old Music Tech‎ > ‎Notes and Assignments‎ > ‎Nov. 4-8‎ > ‎

Sonification Demo

## Copy and paste this demo, or just read it.


from earsketch import *
init()
setTempo(100)

# This function takes an image array, and goes through the rows and columns.
# If it finds a darker square, it hits but a lighter square continues whatever started
def createDrumbeats(anyImage):   #function takes an array as a parameter
    
    drumBeats = []  # creates an empty array to hold a string
    for size in range(len(anyImage)):   # start the for loop, loops based on the length of the array
        drumBeats.append("")            # adds/appends a blank space for the length of the array given
    
    for outerCounter in range(len(anyImage)):     #  for loop, for length of the array's number of rows, it's primary length
        for innerCounter in range(len(anyImage[0])):  # for loop, for the length of the first row, which is the length of all rows
            print("row: ")                           # print the word row: "
            print(outerCounter)                     # print the row we are in, represented by outercounter
            print(", column: ")                       # print the word column:
            println(innerCounter)                     # print the  column we are in, represented by the word innercounter
            if (anyImage[outerCounter][innerCounter] < 127):  # if the color is darker
                drumBeats[outerCounter] = drumBeats[outerCounter] + "0" # then hit
            else:
                drumBeats[outerCounter] = drumBeats[outerCounter] + "+" # otherwise, continue sound
                
    return drumBeats   # this returns a beatstring

# now, use the createDrumbeats function to create a list of drumbeats representing each row of the image
myImage_2d = [[5,247],  [248,248]]   # this is my simple matrix
snare = Y01_SNARE_1                  # just a snare drum, could be any sound

drumBeats = createDrumbeats(myImage_2d)  # this calls the function above, gives it my image array as a parameters

for counter in range(len(drumBeats)): #use the first row to demonstrate making a beat using the first list in the myImage variable
    makeBeat(snare, 1, counter + 1, drumBeats[counter]) # build a beat with the contents of the current row

setEffect(1, VOLUME, GAIN, 12)  # this is just a volume increase effect

finish()

Comments