Splicing musical notes
# You will need to do something like the following in the command area
canvasDuration = 4 # I.e. 4 seconds long
soundCanvas = makeEmptySound(canvasDuration)
# and then copy your note into soundCanvas
# Copy an nth note from a source sound into a longer destination sound starting at
# a given sample number location. n must be a number in the sequence 1, 2, 4....
# We assume that sourceSound is a musical tone, not an arbitrary sound
# and that sourceSound is a whole note in musical terms.
# Typically, this function is called several times in succession.
# We return the location that the copying finished at so that we don't have to calculate
# where to start copying the next note.
def copyNthNote(sourceSound, n, destSound, location):
# Validate inputs
if .... add a condition here that checks that n is valid ....:
print "An nth note must be a whole note, half note, quarter note, etc."
print "So n must be one of [1, 2, 4, 8, 16]"
print "I was not able to do what you wanted"
return
if .... you can add similar conditions to check that you're not copying off the end of the canvas ....:
# Calculate how many samples to copy
endSample = ...... what, exactly?
# It's going to be lower if n is larger because an eighth note is shorter than a quarter note.
# Do the actual copying
for fromSample in range(1, endSample):
sourceValue = getSampleValueAt(sourceSound, fromSample)
setSampleValueAt(destSound, location, sourceValue)
location = location + 1
return location
Link to this Page