UMass Lowell Dept. of Computer Science
COMP 2120 / MUED 2120 — Sound Thinking
Spring 2016 Semester, Section 201
Prof. Jesse M. Heines and Prof. Gena Greher
Notes for Class No. 13
Working with Intervals (continued): Automatic Generation of 2nds and 5ths
Thursday, March 3, 2016
A video of this class is (or will be) posted at:
http://echo360.uml.edu/heines2016/comp-mued2120.html
Handouts and Materials
Scales and Intervals
- C Major Scale
- A Natural Minor Scale
- Chromatic Scale
- 2nds & 5ths
Openings / Announcements / Reminders
Reminder: Assignment No. 4 on Sequencing Sounds with Scratch is due tonight
Class Notes
Coding with Intervals
All of the following code is available at http://scratch.mit.edu/projects/19092595/#editor
The Major Fourth and Its Inversion
- remember that an octave is 12 semitones
- note that the fourth interval is 5 semitones
- note that the inversion of the fourth interval is therefore 12 - 5 = 7 semitones
- note that if both intervals start on the same note (MIDI 57 in this case, of A), the ending notes of the interval and its inversion (62 and 50 in this case) will be an octave apart (12 semitones)
Playing an Interval Using a Broadcast
- note the need for a broadcast / when I receive pair
- note the difference between broadcast vs. broadcast and wait
Creating a Generic Play Fourth Stack
- to a computer, a string is a series of characters (letters and numerical digits)
- this code uses a new variable named direction to hold one of two strings: “up” or “down”
- the value of the direction variable is set in the upper stack based on whether the value of the counter variable is even or odd
- the mod function essentially gives the remainder of an integer division
- thus counter mod 2 means to divide the value of the counter variable by 2 and tell us the remainder
- for example, 1 mod 2 = 1, while 2 mod 2 = 0, and 3 mod 2 = 1
- this little arithmetic trick differentiates odd and even numbers
- as the counter increases, this little calculation alternates between returning 1 and 0
- thus, if we increase the counter by 1 each time through the loop, it’ll alternate between setting the direction variable to “up” and setting it to “down”
- the lower stack plays the starting note and then looks to see if the value of the direction variable is “up” or “down”
- if it’s “up,” the lower stack plays the fourth interval (5 semitones above the starting note)
- otherwise (else), the lower stack plays the inversion of the fourth interval (7 semitones below the starting note)
- the final trick is not to increase (change) the counter by 1, but to use the pick random function to change it by either 0 (no change) or 1
- this is the last block in the repeat 10 loop in the upper stack
- the pick random function picks a number at random between the two numbers specified, which are 0 and 1 in this case
- since there are only two choices (0 and 1), 50% of the time we’ll get 0 and 50% of the time we’ll get 1
- in other words, 50% of the time the interval will stay the same (when 0 is selected) and 50% of the time the interval will change (when 1 is selected)
- this plays more interesting music because it is unpredictable
Run the code at http://scratch.mit.edu/projects/19092595/#editor to hear what each of these bits of code produces
Putting It All Together
All of the following code is available at http://scratch.mit.edu/projects/19070222/
Sprite Control
- the hide block hides the costume for this sprite
Sprite Init (Initialize)
- here we are assigning values to four variables that were created in the Data section
- note that the numbers assigned are differences in MIDI note values from the starting note to the interval note
Sprite Maj. 2nd (generic Major 2nd interval)
- the top stack is the one that plays a major 2nd interval
- the bottom two stacks are only for testing and debugging
- look carefully at the set Ending Note to Starting Note - 12 - Delta 2nd block
- it is difficult to see in the screen capture, but the Starting Note - 12 - Delta 2nd part is really ( Starting Note - ( 12 - Delta 2nd ) )
- this means that ( 12 - Delta 2nd ) is computed first, and then that result is subtracted from the value of Starting Note
- thus, if Starting Note is 60 and Delta 2nd is 2, we get ( 60 - ( 12 - 2 ) ) = ( 60 - 10 ) = 50
- this makes sense because ( Starting Note + Delta 2nd ) = ( 60 + 2 ) = 62
- now you can see that the difference between 50 and 62 is 12, which is an octave, as it should be
Sprite Per. 4th (generic Perfect 4th interval)
- note the similarity of the stack that plays a perfect 4th to the previous one that plays a major 2nd
Sprite Per. 5th (generic Perfect 5th interval)
- again note the structural similarity of the top stack to the previous ones
- the other stacks are, once again, only for testing and debugging
Sprite Min. 7th (generic Minor 7th interval)
Sprite Ver. 1, which puts these all together to play a melody
Version 2 — Selecting a Direction and an Interval at Random
Version 3 — Correcting Note Doubling and Adding Musicality
The next version is the same as the previous one with the addition of the Play Starting Note variable
- before entering the loop, set Play Starting Note to True so that the first execution of an interval script plays the starting note
- at the end of the loop, set Play Starting Note to False so that subsequent executions of interval scripts do *not* play the starting note
The interval scripts have all been modified to check the Play Starting Note variable before playing the starting note
- if Play Starting Note is True, these scripts play the starting note
- if Play Starting Note is False, they do not