Fall2006.CSCI199Homework4Template History

Hide minor edits - Show changes to output

Added lines 15-30:
!!!Approach

Describe your approach. What does the-System model? What is(are) its sound input(s). What is the output?

!!!Input

Attach sample inputs (accepted formats are .mid (max 60K) and .aif (max 3M).

!!!Output

Attach sample outputs (accepted formats are .mid (max 60K) and .aif (max 3M).

!!!Code

Attach your code below.
Changed line 4 from:
* Assignment: Homework 1 \\
to:
* Assignment: Homework 4 \\
Changed line 17 from:
# l-system.music.py Version 1.0 23-sep-06 (bzm)
to:
# l-system.music.py Version 1.0 23-nov-06 (bzm)
Changed line 17 from:
# l-system.tree.py Version 1.0 23-sep-06 (bzm)
to:
# l-system.music.py Version 1.0 23-sep-06 (bzm)
Changed line 19 from:
# Demonstrates how to draw a fractal tree using L-systems.
to:
# Demonstrates how to generate music using L-systems.
Added lines 22-23:
from jMusic import *
Deleted line 24:
from xturtle import * # for drawing
Changed lines 34-42 from:
model.depth = 8 # number of times to apply rules
model.length = 0.5 # length of first branch
model.angle = 90 # initial orientation (12 o'clock)
model.tilt = 180.0/7 # angle change between main branch and sub-branches
model.origin = (-135, -192) # starting position (root)
model.updateFrequency = 20 # how often to update screen (set to 'False' for fastest)

#print repr(model)
to:
model.depth = 2
Changed line 39 from:
turtle = Pen()
to:
phrase = Phrase()
Deleted lines 41-43:
# setup drawing model
initializeTurtle(turtle, model)
Changed line 43 from:
def F():
to:
def F(phrase):
Changed lines 45-46 from:
turtle.down()
turtle.forward(model.length)
to:
note = Note(C4, HN)
phrase.addNote(note)
Changed line 49 from:
def f():
to:
def f(phrase):
Changed lines 51-52 from:
turtle.up()
turtle.forward(model.length)
to:
note = Rest()
phrase.addNote(note)
Changed lines 55-57 from:
def push():
"Save current position, heading, length."
stack.push( (turtle.pos(), turtle.heading(), model.length) )
to:
def push(phrase):
"Save current state."
pass
Changed lines 60-65 from:
def pop():
"Restore saved position, heading, length."
position, model.angle, model.length = stack.pop()
turtle.up()
turtle.goto(position)
turtle.setheading(model.angle)
to:
def pop(phrase):
"Restore saved state."
pass
Changed line 65 from:
def left():
to:
def left(phrase):
Changed lines 67-68 from:
turtle.left(model.tilt)
to:
note = Note(E4, QN)
phrase.addNote(note)
Changed line 71 from:
def right():
to:
def right(phrase):
Changed lines 73-74 from:
turtle.right(model.tilt)
to:
note = Note(G4, QN)
phrase.addNote(note)
Changed line 77 from:
def A():
to:
def A(phrase):
Changed line 81 from:
def error():
to:
def error(phrase):
Changed lines 98-110 from:
dictionary.get(symbol, error)()

def initializeTurtle(turtle, model):
"Sets up turtle graphics for drawing."

turtle.speed(0)
turtle.hideturtle()
turtle.tracer(model.updateFrequency)
turtle.up()
turtle.goto(model.origin)
turtle.left(model.angle)
turtle.down()
to:
dictionary.get(symbol, error)(phrase)

# pack the phrase into a part
part = Part()
part.addPhrase(phrase)

# pack the part into a score titled 'Bing'
score = Score("L-System")
score.addPart(part)

# play the score as MIDI (1 means 'do not exit')
#Play.midi(score, 1)

# write the score as a MIDI file to disk
Write.midi(score, "l-system.mid")
Added lines 1-135:
/**
* Author: {Your Name} \\
* Course: CSCI 199, Section 03 \\
* Assignment: Homework 1 \\
* Due Date: {The assignment's due date} \\
* \\
* Certification of Authenticity {include one of the following}: \\
* \\
* I certify that this work is entirely my own. \\
* \\
* I certify that this work is my own, but I received \\
* some assistance from: {Name(s), References, etc.} \\
*/

(:source lang=Python tabwidth=3 -trim :)

# l-system.tree.py Version 1.0 23-sep-06 (bzm)
#
# Demonstrates how to draw a fractal tree using L-systems.
#

from lsystem import * # for recursive system modeling
from xturtle import * # for drawing
from stack import * # for storing things

# define the axiom and rules
model = LSystem('A',
[
('A', 'FF[-A][+A]FA'),
('F', 'FF')
])

model.depth = 8 # number of times to apply rules
model.length = 0.5 # length of first branch
model.angle = 90 # initial orientation (12 o'clock)
model.tilt = 180.0/7 # angle change between main branch and sub-branches
model.origin = (-135, -192) # starting position (root)
model.updateFrequency = 20 # how often to update screen (set to 'False' for fastest)

#print repr(model)

def interpet(model):
"Defines the meaning of each production symbol."

turtle = Pen()
stack = Stack()

# setup drawing model
initializeTurtle(turtle, model)

# define rules
def F():
"Draw one length in current orientation"
turtle.down()
turtle.forward(model.length)
#print "down, forward", model.length

def f():
"Move (no draw) one length in current orientation."
turtle.up()
turtle.forward(model.length)
#print "up, forward", model.length

def push():
"Save current position, heading, length."
stack.push( (turtle.pos(), turtle.heading(), model.length) )
#print "push, new length", model.length, "heading", turtle.heading()

def pop():
"Restore saved position, heading, length."
position, model.angle, model.length = stack.pop()
turtle.up()
turtle.goto(position)
turtle.setheading(model.angle)
#print "pop, new length", model.length, "heading", turtle.heading()

def left():
"Tilt to the left."
turtle.left(model.tilt)
#print "turn left, new orientation", model.angle

def right():
"Tilt to the right."
turtle.right(model.tilt)
#print "turn right, new orientation", turtle.heading()

def A():
"Do nothing."
#print "do nothing"

def error():
"Unrecognized symbol."
raise ValueError(symbol) # let them know

# create a dictionary of symbol meanings
dictionary = {}
dictionary['F'] = F
dictionary['f'] = f
dictionary['['] = push
dictionary[']'] = pop
dictionary['-'] = left
dictionary['+'] = right
dictionary['A'] = A

# interpet symbols
for symbol in model.string:
#print symbol
dictionary.get(symbol, error)()

def initializeTurtle(turtle, model):
"Sets up turtle graphics for drawing."

turtle.speed(0)
turtle.hideturtle()
turtle.tracer(model.updateFrequency)
turtle.up()
turtle.goto(model.origin)
turtle.left(model.angle)
turtle.down()

def main():

# generate productions
while not model.done and model.generation < model.depth:
#print model.generation, model.string
model.step()

# interpret latest production
#print "Current directions", model.string
interpet(model)

if __name__ == '__main__':
main()

(:sourcend:)