Sunday, February 21, 2016

7. Drone bass

A C major scale has 8 notes. If all are quarter notes, one C major scale takes 2 measures, assuming no rests.


Here we have two C major scales over 4 measures, which is done by the function scale().


A drone consisting of note C3, two whole notes for first 2 measures, and four half notes for the next 2 measures, which is done by the function drone().


We can always include a documentation string as the first line of a function.


The first 2 and last 2 measures will sound different due to the increasing number of notes in the last 2 measures.


# mus7.py
# drone bass

from writeMIDI import writeMIDI

# beats per minute
bpm = 130

major = [2,2,1,2,2,2,1]

notes = []

def scale(n):
    "Add two C major scales (4 measures)"
    for j in range(2):
        midi = 60
        for i in range(8):
            n.append((midi,8*j+i,1,120))
            midi += major[i%7]

def drone(n):
    "Add drone C3 over 4 measures"
    n.append((48,0,4,115)) # whole note, measure 1
    n.append((48,4,4,115)) # whole note, measure 2
    n.append((48,8,2,115)) # half note, measure 3 - first half
    n.append((48,10,2,115)) # half note, measure 3 - second half
    n.append((48,12,2,115)) # half note, measure 4 - first half
    n.append((48,14,2,115)) # half note, measure 4 - second half
             
scale(notes)
drone(notes)
writeMIDI('C','piano',bpm,notes,'mus7')

This will generate this:


No comments:

Post a Comment