# Problem Set 7 - Due Fri Dec 11 at 23:59

Back to the problem set 7 page

The task 2 rubric shows how this task will be graded, and can be used as a checklist to make sure you are done with the task.

Put all of your work for this task into the file ``.

• For part A of this task, you will create a non-fruitful recursive `risingNotes` function that adds a series of rising notes to the current track.
• For part B, you will create a non-fruitful recursive `branchingNotes` function which adds a pattern of notes to the current track which starts and ends on the same note, but which travels down and then up in pitch in between.
• For part C, you will create a fruitful recursive `chaosNotes` function which works just like `branchingNotes`, except that it randomly skips each note 50% of the time. This function will return the total duration of all notes that it adds to the track.

## Part A: `risingNotes`

For the `risingNotes` function, there are three arguments:

• `middlePitch` determines the pitch of the middle note of the sequence.
• `duration` determines the duration of each note.
• `complexity` determines how complex the sequence is.

When `complexity` is 0, the 'sequence' is just a single `keyboard` note (with the given pitch and duration). When the complexity is 1, it becomes a sequence of three notes, where the first note is one step below the middle pitch (using `stepDown`), the second note is at the middle pitch, and the third note is one step above the middle pitch. When the complexity is 2, the pattern becomes a series of 3 3-note patterns, each equivalent to the complexity-1 pattern, but with their middle pitches one step apart (these entire patterns have the same sequence of middle pitches that the individual notes did at complexity 1). If complexity were 3, this pattern of recursion would continue and there would be 27 notes, grouped into 3 patterns of 9 notes which are each a complexity-2 pattern.

Using the letters A through G to represent subsequent pitches that are 1 step apart, we would observe the following patterns if the middle pitch were set to D (using parentheses and curly braces to show the recursive structure):

• Complexity 0: `D`
• Complexity 1: `C D E`
• Complexity 2: `(B C D) (C D E) (D E F)`
• Complexity 3: `{(A B C) (B C D) (C D E)} {(B C D) (C D E) (D E F)} {(C D E) (D E F) (E F G)}`

Here is what the result of `risingNotes(D4, 0.2, 3)` sounds like:

Show the printed output for `risingNotes(D4, 0.2, 3)`
``````a 0.2s keyboard note at 220 Hz
and a 0.2s keyboard note at 247 Hz
and a 0.2s keyboard note at 262 Hz
and a 0.2s keyboard note at 247 Hz
and a 0.2s keyboard note at 262 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 262 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 330 Hz
and a 0.2s keyboard note at 247 Hz
and a 0.2s keyboard note at 262 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 262 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 330 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 330 Hz
and a 0.2s keyboard note at 349 Hz
and a 0.2s keyboard note at 262 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 330 Hz
and a 0.2s keyboard note at 294 Hz
and a 0.2s keyboard note at 330 Hz
and a 0.2s keyboard note at 349 Hz
and a 0.2s keyboard note at 330 Hz
and a 0.2s keyboard note at 349 Hz
and a 0.2s keyboard note at 392 Hz``````

And here is what the results of `risingNotes` should sound like when `middlePitch` is C4, `duration` is 0.2, and `complexity` is 0, 1, and 2 (this clip includes all three complexity results with short pauses in between to emphasize their similarities).

## Part B: `branchingNotes`

For this part of the task, you must write a function called `branchingNotes` which creates a more complex recursive pattern of notes.

`branchingNotes` accepts three arguments:

• `pitch` determines the pitch of the first and last notes of the pattern.
• `duration` determines the duration of the first and last notes of the pattern.
• `complexity` determines the complexity of the pattern, as described below.

When the complexity is 0, the pattern is just two `keyboard` notes with the given pitch and duration. When the complexity is 1, two complexity-zero patterns are added between these end notes, the first with its pitch shifted down using `leapDown`, and the second with its pitch shifted up using `leapUp`. The duration for both of these interior patterns is 75% of the original duration. If complexity is 2, this process repeats itself, so there are the two end notes, with two reduced-duration, pitch-shifted complexity-1 patterns in between them, and so on and so forth for higher complexities. Concretely, for a starting pitch of P10 (the P-based pitch variables are each spaced one 'leap' apart) and duration of 0.64, the pitch/duration of each note at different complexity levels would be as follows:

• Complexity 0: `P10/0.64 P10/0.64`
• Complexity 1 (with parentheses to indicate structure):
`P10/0.64 (P9/0.48 P9/0.48) (P11/0.48 P11/0.48) P10/0.64`
• Complexity 2 (with parentheses, curly braces, and line breaks to indicate structure):
``````P10/0.64
{P9/0.48 (P8/0.36 P8/0.36) (P10/0.36 P10/0.36)  P9/0.48}
{P11/0.48 (P10/0.36 P10/0.36) (P12/0.36 P12/0.36) P11/0.48}
P10/0.64``````

Here are the results of `branchingNotes(P10, 0.64, 3)`:

Show the printed output for `branchingNotes(P10, 0.64, 3)`
``````a 0.64s keyboard note at 523 Hz
and a 0.48s keyboard note at 440 Hz
and a 0.36s keyboard note at 392 Hz
and a 0.27s keyboard note at 330 Hz
and a 0.27s keyboard note at 330 Hz
and a 0.27s keyboard note at 440 Hz
and a 0.27s keyboard note at 440 Hz
and a 0.36s keyboard note at 392 Hz
and a 0.36s keyboard note at 523 Hz
and a 0.27s keyboard note at 440 Hz
and a 0.27s keyboard note at 440 Hz
and a 0.27s keyboard note at 587 Hz
and a 0.27s keyboard note at 587 Hz
and a 0.36s keyboard note at 523 Hz
and a 0.48s keyboard note at 440 Hz
and a 0.48s keyboard note at 587 Hz
and a 0.36s keyboard note at 523 Hz
and a 0.27s keyboard note at 440 Hz
and a 0.27s keyboard note at 440 Hz
and a 0.27s keyboard note at 587 Hz
and a 0.27s keyboard note at 587 Hz
and a 0.36s keyboard note at 523 Hz
and a 0.36s keyboard note at 659 Hz
and a 0.27s keyboard note at 587 Hz
and a 0.27s keyboard note at 587 Hz
and a 0.27s keyboard note at 784 Hz
and a 0.27s keyboard note at 784 Hz
and a 0.36s keyboard note at 659 Hz
and a 0.48s keyboard note at 587 Hz
and a 0.64s keyboard note at 523 Hz``````

This next clip includes the results of four `branchingNotes` calls with increasing complexity, to demonstrate their relationships:

• `branchingNotes(P7, 0.27, 0)`
• `branchingNotes(P8, 0.36, 1)`
• `branchingNotes(P9, 0.48, 2)`
• `branchingNotes(P10, 0.64, 3)`

## Part C: `chaosNotes`

For this part, you should start by copying and pasting your working `branchingNotes` function. The `chaosNotes` function takes the same parameters as `branchingNotes` and generates the exact same pattern of notes, except that it has a 50% chance to skip each note that it should normally generate. You must use the provided `shouldAddNote` function (which returns `True` half the time and `False` the other half) to make these decisions, and you may not use `random` in any other way.

`chaosNotes` must also be fruitful: it must return the total duration of all notes that it adds to the track, accounting for its own randomness, and including the duration of any notes added by recursive calls that it makes. This means that you will need to keep track of each decision whether or not to add a note, and if a note is added, you should add the duration of that note to an accumulator variable that you can return at the end of the function (you'll also need to accumulate results from recursive calls).

Here's an example of what `chaosNotes(P10, 0.4, 3)` might sound like (but you'll get a different melody every time because it's random):

Show the printed output for the specific melody generated above.
``````a 0.4s keyboard note at 523 Hz
and a 0.3s keyboard note at 440 Hz
and a 0.225s keyboard note at 392 Hz
and a 0.169s keyboard note at 330 Hz
and a 0.169s keyboard note at 330 Hz
and a 0.169s keyboard note at 440 Hz
and a 0.169s keyboard note at 440 Hz
and a 0.225s keyboard note at 392 Hz
and a 0.225s keyboard note at 523 Hz
and a 0.225s keyboard note at 523 Hz
and a 0.3s keyboard note at 587 Hz
and a 0.225s keyboard note at 523 Hz
and a 0.169s keyboard note at 440 Hz
and a 0.169s keyboard note at 587 Hz
and a 0.225s keyboard note at 523 Hz
and a 0.169s keyboard note at 587 Hz
and a 0.169s keyboard note at 587 Hz
and a 0.169s keyboard note at 784 Hz
and a 0.225s keyboard note at 659 Hz
and a 0.3s keyboard note at 587 Hz
and a 0.4s keyboard note at 523 Hz

Total duration of track is: 4.79s``````

For testing purposes, using complexity values of 0 and 1 will help you make sure that your returned duration values are correct, and 0.64 is a duration that can be multiplied by 0.75 several times while producing round results each time.

Note that in the output above, the message "Total duration of track is" is produced by the testing code, not printed by the `chaosNotes` function. `chaosNotes` needs to return a number, but should not print anything.

When you are done with this task, you should submit it via the Ocean server. Remember to follow the submission instructions when you are done with the whole problem set.