Title: Computing with Music: Creating Musical Patterns
1Computing with MusicCreating Musical Patterns
- John Peterson
- No Longer At Yale University
- http//haskell.org/edsl
2Play Some Music
- Copy haskore from my shared to C
- Click on output.mid and make sure you hear
something then close the player. - Use TextPad to open test1.hs
- Change the e3 to ef3
- Save this file and then double click it.
- If everything is OK, type play music1
- Open output.mid again to see if it worked.
- Change something, save it, and type r to
reload - Play it again!
3Notation
- The computer wants you to write down definitions
in a very particular way - name thing
- melody c4 d4 e4
- chord n n ! up 4 n ! up 7 n
These have to start in the first column!
4About Haskore
- A program is like a dictionary it is a set of
definitions, each starting in the leftmost
column, in any order you want. - There is a built-in vocabulary that you use to
build new definitions - You load in a program and can then perform any
piece of music defined in the program using play
5A Vocabulary of Music
- Instead of nouns, verbs, or adjectives our
language will have the following types of
things - Music - something you can listen to
- Music glue - something that combines music
together - Music changer - something that modifies music
- Numbers - things like 2 or 57 or 3.4
- Lists a sequence of things like c4, d4, e4
- In addition well also add our own functions
which can use these objects.
6Notation
- We write f x instead of f(x) and f x y
instead of f(x,y). We still use parens for
grouping f (g 1) (h 2). - Function application happens before operators
like f x g y means (f x) (g y) - You have to say 0.2 instead of .2
7Note Names
- The basic vocabulary gives names to the notes of
the scale. Notes are the letters a - g. We add
a number to them to give an octave number.
Notes can also be flat (f) or sharp (s). These
notes are all whole notes.
c3 e4 af4 gs7
There are lots of C notes so we need the number
too
8Rests
- There is a special note called a rest it
doesnt have a sound but its still useful. Use
r for a rest note.
9Naming Music
- Names allow you to use the same bit of music over
and over. Like naming a song and asking the DJ
to play it for you. - As you do exercises, you will give names to
pieces of music. Use any name you want but you
cant use the same name twice. As you go on to
other exercises you can use these named pieces of
music again.
10Music Glue
- We can glue pieces of music together in two
ways play one first and then the other, or play
both at the same time. We use for playing
sequentially and ! for playing at the same
time. You can glue any two pieces of music, not
just single notes.
Exercise 1 Write three phrases, each with three
notes. One sequential, one all together, and one
using both and !. Put a rest in one of
them Add these to your MyMusic file and play
them. Give each a different name.
11Changing Music
- There are lots of ways to change a piece of
music - Play it faster or slower
- Play it higher or lower
- Play on different instruments
- All of these things are in our music vocabulary.
12Faster / Slower (Tempo)
- Musicians use whole notes, half notes,
quarter notes, and so on to tell a performer
how long to play a note. A half note is 1/2 as
long as a whole note (duh), and so on. - We use music changers to make any piece of music
(not just notes!) go faster or slower.
13Simple Tempo Changers
- Here are the names of our tempo changers
- h - turns whole notes into half
- notes (go twice as fast)
- q - quarter notes (4x as fast)
- e - eighth notes (8x as fast)
- s - sixteenth notes (16x as fast)
- dh, dq, de, ds dotted notes
- (Adds an extra ½ beat)
14Any Speed You Want!
- You can also say faster or slower.
- For example, h is just faster 2.
- Exercise 2 write a 4 note phrase that uses a
half, quarter, and two eighth notes and play it.
Then write another definition that plays the
phrase twice at different speeds.
15Moving music up and down
- The up and down functions change the pitch in
a piece of music. You tell these functions how
many piano keys to move the music. - m1 c3 d3 e3
- m2 up 2 m1
- m3 m1 ! m2 ! up 4 m1
16Different Instruments
- There are a lot of different instruments you can
play music with. - piano, harpsichord, vibes, organ, guitar,
electric, bass, violin, viola, cello, trumpet,
trombone, horn, sax, oboe, bassoon, clarinet,
flute, panFlute, kalimba, woodblock - m oboe (c4 g4)
- Exercise 3 take a piece of music you have
already written and play it three times, each
with a - different instrument and starting with a
different note.
17Reversing
- You can reverse a piece of music with the rev
function. - m1 q (c4 e3 g3)
- m2 rev m1
18Activity 1 A Simple Pattern
- Write a piece of music from 2 different musical
patterns, each containing just 4 notes. - Use naming, reversal, tempo changes, and
transposition to create a short bit of music. A
small example to get you started is in MyMusic.hs - Guidelines
- not too long!
- think of a nice pattern for your piece
19Functions
- Definitions often are parameterized. That is,
they contain names that are filled in when the
definition is used. - Example a major chord built on a note n is n
played with the notes 4 and 7 steps higher than
n. - majchord n n ! up 4 n ! up 7 n
parameter
20Play it Again
- Heres a really simple pattern play something
twice. We express this by giving twice a
parameter the music to be played. - twice music music music
- Heres how its used
- m2 twice m1
21Exercise 4
- Many songs are in ABA form. Write a function
that has two parameters, the A and B parts of the
song, and puts them all together. - Test this with two phrases youve already written.
22What Just Happened?
- Our computer language allows us to give names to
musical patterns and use them to make it easier
to write music. - You cant do that using regular sheet music
(well, you can do simple patterns like twice or
ABA )
23General Repetition
- We can use a counter to repeat n times instead
of just twice - rpt 0 m empty
- rpt n m m rpt (n-1) m
- Use it like this
- m1 rpt 3 m2
24Exercise 5
- Use rpt to add a repeating bass to a piece of
music. Define two fragments a slow one with 4
whole notes another one with 4 quarter notes.
Play them at the same time, repeating the one
with quarter notes so they last the same amount
of time. - p1
- p2
- ex5 p1 ! (rpt 4 p2)
25One More Music Function
- The invert function turns music upside down.
You need to give it the - note that wont change notes above go below and
notes below go above. - invertPhrase q (c4 d4 e4 g4)
- invertDemo invert c4 invertPhrase
This is in MyMusic try it out
26Functions in Functions
- Function arguments need not be just notes they
can be functions that change notes. Heres a
function that repeats and changes music - rptChange 1 f m m
- rptChange n f m
- m rptChange (n-1) f (f m)
27Using rptChange
- What do these do?
- m1 rptChange 10 (up 1) (q c4)
- m2 rptChange 10 (faster (5/4)) (q c4)
- addNote m q c4 up 2 m
- m3 rptChange 5 addNote (q c4)
28Exercise 6
- Write a short phrase (1 - 4 notes) and then use
repeatChange to do something new with it. Some
possibilities - add a new note each time
- add an echo
- reverse the music each time
- invert the music each time
29Pattern Music
- Well conclude with two ways of creating simple
musical patterns - rseq n seed m1, m2, m3, mn
- This creates a random seqence of n selections
drawn from m1 .. mn. - The seed determines which specific sequence you
get use any integer you want here. If you
dont like the music you get maybe changing this
helps.
30Example
- p1 e (d4 e4 g4)
- p2 s (c5 a4 g4 e4 d4)
- p3 q (c4 ! e4 ! g4 ! a4)
- p4 q (e4 a4)
- rm rseq 10 113 p1, p2, p3, p4
31Random Mutation
- A more involved way of generating random patterns
is to start with a piece of music and then
randomly apply some sort of music transformation
repeatedly - You need
- number of mutations
- random seed
- a list of music changers
- a piece of music to start with
32Random Mutation Example
- ch1 up 4
- ch2 up 3
- ch3 m s c4 m
- ch4 faster (5/4)
- rm2 rchange 15 23
- ch1, ch2, ch3, ch4
- (s e4)
33Activity 2
Using a phrases of no more that three notes write
a musical pattern. You can use any of the
functions weve already seen like rpt or
rptChange or rseq or or rchange you can define
your own pattern function. Use both and ! Use
different instruments Write a new function if you
want
34Homework 1
- Create an interesting piece of music make it
abstract, not something you need to key a lot of
notes for. - Make a wiki page with your music on it upload
the audio file too. - Read about Extreme Programming for Wednesday.