Function that (nearly) creates a musical scale
# Need for os.sep. Will be explained in class
import os
def nearlyScale(dir):
length = 0
sounds = []
# Go through all the notes in the following list in this standard musical order:
for note in ["c", "d", "e", "f", "g", "a", "b"]:
fileName = dir + os.sep + "string-" + note + "4.wav"
sound = makeSound(fileName)
sounds += [sound] # += is a shorthand to be explained in class
length += getLength(sound)
# Create a blank sound that is long enough for the eight notes being spliced in.
# Assume that all sounds have same sampling rate, and use sounds[0] as example.
seconds = len(sounds) * int((length/getSamplingRate(sounds[0])) + 1)
canvas = makeEmptySound(seconds)
# Copy the notes/sounds, one by one. The starting point for each sound in the
# canvas is "start". This is initially 1 for the very first sound.
start = 1
for sound in sounds:
# Go through the sound, copying the sample values into the corresponding
# positions in the canvas.
for position in range(1, getLength(sound)):
value = getSampleValueAt(sound, position)
setSampleValueAt(canvas, position+start-1, value)
# The starting position for the next sound has to be advanced by the number of
# samples that we have just copied.
start += getLength(sound)
return canvas
Link to this Page