1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
<title> Haskore Tutorial: Basics </title>
<body bgcolor="#ffffff"><i>The Haskore Tutorial</i><br><a href="index.html">top</a> <a href="Haskore.html">back</a> <a href="Performance.html">next</a><hr>
<a name="basics"></a>
<a name="sect3"></a>
<h2>3<tt> </tt>The Basics</h2>
<p>
<tt> <br>
<br>
> module Basics where<br>
> infixr 5 :+:, :=:<br>
<br>
</tt>
Perhaps the most basic musical idea is that of a <I>pitch</I>, which
consists of a <I>pitch class</I> (i.e. one of 12 semi-tones) and an
<I>octave</I>:
<tt> <br>
<br>
> type Pitch = (PitchClass, Octave)<br>
> data PitchClass = Cf | C | Cs | Df | D | Ds | Ef | E | Es | Ff | F | Fs<br>
> | Gf | G | Gs | Af | A | As | Bf | B | Bs<br>
> deriving (Eq,Ord,Ix,Show)<br>
> type Octave = Int<br>
<br>
</tt>
So a <tt>Pitch</tt> is a pair consisting of a pitch class and an octave.
Octaves are just integers, but we define a datatype for pitch classes,
since distinguishing enharmonics (such as G#and Ab) may be important
(especially for notation!). By convention, A440 = <tt>(A,4)</tt>.<p>
Musical objects are captured by the <tt>Music</tt> datatype: (I
prefer to call these "musical objects" rather than "musical
values" because the latter may be confused with musical aesthetics.)
<tt> <br>
<br>
> data Music = Note Pitch Dur [NoteAttribute] -- a note \ atomic <br>
> | Rest Dur -- a rest / objects<br>
> | Music :+: Music -- sequential composition<br>
> | Music :=: Music -- parallel composition<br>
> | Tempo Int Int Music -- scale the tempo<br>
> | Trans Int Music -- transposition<br>
> | Instr IName Music -- instrument label<br>
> | Player PName Music -- player label<br>
> | Phrase [PhraseAttribute] Music -- phrase attributes<br>
> deriving Show<br>
><br>
> type Dur = Float -- in whole notes<br>
> type IName = String<br>
> type PName = String<br>
<br>
</tt>
Here a <tt>Note</tt> is its pitch paired with its duration (in number of
whole notes), along with a list of <tt>NoteAttributes</tt> (defined
later). A <tt>Rest</tt> also has a duration, but of course no pitch or
other attributes.<p>
From these two atomic constructors we can build more complex musical
objects using the other constructors, as follows:
<UL><LI><tt>m1 :+: m2</tt> is the sequential composition of <tt>m1</tt> and
<tt>m2</tt>; i.e. <tt>m1</tt> and <tt>m2</tt> are played in sequence.
<LI><tt>m1 :=: m2</tt> is the parallel composition of <tt>m1</tt> and
<tt>m2</tt>; i.e. <tt>m1</tt> and <tt>m2</tt> are played simultaneously.
<LI><tt>Tempo a b m</tt> scales the rate at which
<tt>m</tt> is played (i.e. its tempo) by a factor of <tt>a/b</tt>.
<LI><tt>Trans i m</tt> transposes <tt>m</tt> by interval <tt>i</tt> (in semitones).
<LI><tt>Instr iname m</tt> declares that <tt>m</tt> is to be performed using
instrument <tt>iname</tt>.
<LI><tt>Player pname m</tt> declares that <tt>m</tt> is to be performed by
player <tt>pname</tt>.
<LI><tt>Phrase pas m</tt> declares that <tt>m</tt> is to be played using
the phrase attributes (described later) in the list <tt>pas</tt>.
</UL><p>
It is convenient to represent these ideas in Haskell as a recursive
datatype because we wish to not only construct musical objects, but
also take them apart, analyze their structure, print them in a
structure-preserving way, interpret them for performance purposes,
etc.<a name="auxiliaries"></a><p>
<a name="sect3.1"></a>
<h3>3.1<tt> </tt>Convenient Auxiliary Functions</h3>
<p>
For convenience we first create a few names for familiar notes,
durations, and rests, as shown in Figure <a href="basics.html#note-names">2</a>. Treating
pitches as integers is also useful in many settings, so we define some
functions for converting between <tt>Pitch</tt> values and <tt>AbsPitch
</tt>values (integers). These also are shown in Figure <a href="basics.html#note-names">2</a>,
along with a definition of <tt>trans</tt>, which transposes pitches
(analogous to <tt>Trans</tt>, which transposes values of type
<tt>Music</tt>).<p>
<p>
<B>Exercise<br>
</B>Show that <tt>abspitch . pitch = id</tt>, and,
up to enharmonic equivalences,<br>
<tt>pitch . abspitch = id</tt>.
<p>
<p>
<p>
<B>Exercise<br>
</B>Show that <tt>trans i (trans j p) = trans (i+j) p</tt>.
<p>
<p>
<table border=2 cellpadding=3>
<tr><td>
<tt> <br>
<br>
> cf,c,cs,df,d,ds,ef,e,es,ff,f,fs,gf,g,gs,af,a,as,bf,b,bs :: <br>
> Octave -> Dur -> [NoteAttribute] -> Music<br>
><br>
> cf o = Note (Cf,o); c o = Note (C,o); cs o = Note (Cs,o)<br>
> df o = Note (Df,o); d o = Note (D,o); ds o = Note (Ds,o)<br>
> ef o = Note (Ef,o); e o = Note (E,o); es o = Note (Es,o)<br>
> ff o = Note (Ff,o); f o = Note (F,o); fs o = Note (Fs,o)<br>
> gf o = Note (Gf,o); g o = Note (G,o); gs o = Note (Gs,o)<br>
> af o = Note (Af,o); a o = Note (A,o); as o = Note (As,o)<br>
> bf o = Note (Bf,o); b o = Note (B,o); bs o = Note (Bs,o)<br>
><br>
> wn, hn, qn, en, sn, tn :: Dur<br>
> wnr, hnr, qnr, enr, snr, tnr :: Music<br>
><br>
> wn = 1 ; wnr = Rest wn -- whole note rest<br>
> hn = 1/2 ; hnr = Rest hn -- half note rest<br>
> qn = 1/4 ; qnr = Rest qn -- quarter note rest<br>
> en = 1/8 ; enr = Rest en -- eight note rest<br>
> sn = 1/16 ; snr = Rest sn -- sixteenth note rest<br>
> tn = 1/32 ; tnr = Rest tn -- thirty-second note rest<br>
><br>
> pitchClass :: PitchClass -> Int<br>
><br>
> pitchClass pc = case pc of<br>
> Cf -> -1; C -> 0; Cs -> 1 -- or should Cf be 11?<br>
> Df -> 1; D -> 2; Ds -> 3<br>
> Ef -> 3; E -> 4; Es -> 5<br>
> Ff -> 4; F -> 5; Fs -> 6<br>
> Gf -> 6; G -> 7; Gs -> 8<br>
> Af -> 8; A -> 9; As -> 10<br>
> Bf -> 10; B -> 11; Bs -> 12 -- or should Bs be 0?<br>
><br>
> type AbsPitch = Int<br>
><br>
> absPitch :: Pitch -> AbsPitch<br>
> absPitch (pc,oct) = 12*oct + pitchClass pc<br>
><br>
> pitch :: AbsPitch -> Pitch<br>
> pitch ap = ( [C,Cs,D,Ds,E,F,Fs,G,Gs,A,As,B] !! mod ap 12, <br>
> quot ap 12)<br>
><br>
> trans :: Int -> Pitch -> Pitch<br>
> trans i p = pitch (absPitch p + i)<br>
<br>
</tt><div align=center><B>Figure 2: Convenient note names and pitch conversion functions.</B></div>
</td></tr></table>
<a name="basic-examples"></a><p>
<a name="sect3.2"></a>
<h3>3.2<tt> </tt>Some Simple Examples</h3>
<p>
With this modest beginning, we can already express quite a few musical
relationships simply and effectively. For example, two common ideas
in music are the construction of notes in a horizontal fashion (a
<I>line</I> or <I>melody</I>), and in a vertical fashion (a <I>chord</I>):
<tt><br>
<br>
> line, chord :: [Music] -> Music<br>
> line = foldr (:+:) (Rest 0) <br>
> chord = foldr (:=:) (Rest 0) <br>
<br>
<p>
</tt>From the notes in the C major triad in register 4, I can now construct
a C major arpeggio and chord as well:
<tt> <br>
<br>
> cMaj = map (\f->f 4 qn []) [c, e, g] -- octave 4, quarter notes<br>
><br>
> cMajArp = line cMaj<br>
> cMajChd = chord cMaj<br>
<br>
</tt> <p>
Suppose now we wish to describe a melody <tt>m</tt> accompanied by an
identical voice a perfect 5th higher. In Haskore we simply write
"<tt>m :=: Trans 7 m</tt>." Similarly, a canon-like structure
involving <tt>m</tt> can be expressed as "<tt>m :=: delay d m</tt>,"
where:
<tt> <br>
<br>
> delay :: Dur -> Music -> Music<br>
> delay d m = Rest d :+: m<br>
<br>
</tt> <p>
Of course, Haskell's non-strict semantics also allows us to define
infinite musical objects. For example, a musical object may be
repeated <I>ad nauseum</I> using this simple function:
<tt> <br>
<br>
> repeatM :: Music -> Music<br>
> repeatM m = m :+: repeatM m<br>
<br>
</tt>
Thus an infinite ostinato can be expressed in this way, and then used
in different contexts that extract only the portion that's actually
needed.<p>
The notions of inversion, retrograde, retrograde inversion, etc. used
in 12-tone theory are also easily captured in Haskore. First let's
define a transformation from a line created by <tt>line</tt> to a list:
<tt> <br>
<br>
> lineToList :: Music -> [Music]<br>
> lineToList n@(Rest 0) = []<br>
> lineToList (n :+: ns) = n : lineToList ns<br>
><br>
> retro, invert, retroInvert, invertRetro :: Music -> Music<br>
> retro = line . reverse . lineToList<br>
> invert m = line (map inv l)<br>
> where l@(Note r _ _: _) = lineToList m<br>
> inv (Note p d nas) = Note (pitch (2*(absPitch r) - absPitch p)) d nas<br>
> inv (Rest d) = Rest d<br>
> retroInvert = retro . invert<br>
> invertRetro = invert . retro<br>
<br>
</tt> <p>
<p>
<B>Exercise<br>
</B>Show that "<tt>retro . retro</tt>,"
"<tt>invert . invert</tt>," and "<tt>retroInvert . invertRetro</tt>"
are the identity on values created by <tt>line</tt>.
<p>
<p>
<table border=2 cellpadding=3>
<tr><td><div align=center><img src="poly.gif" alt="Polyrhythms">
<div align=center><B>Figure 3: Nested Polyrhythms</B></div>
</td></tr></table>
<p>
For some rhythmical ideas, consider first a simple <I>triplet</I> of
eighth notes; it can be expressed as "<tt>Tempo 3 2 m</tt>," where
<tt>m</tt> is a line of 3 eighth notes. So in fact <tt>Tempo</tt> can be used to
create quite complex rhythmical patterns. For example, consider the
"nested polyrhythms" shown in Figure <a href="basics.html#polyrhythms">3</a>. They can be
expressed quite naturally in Haskore as follows (note the use of the
<I>where</I> clause in <tt>pr2</tt> to capture recurring phrases):
<tt> <br>
<br>
> pr1, pr2 :: Pitch -> Music<br>
> pr1 p = Tempo 5 6 (Tempo 4 3 (mkLn 1 p qn :+:<br>
> Tempo 3 2 (mkLn 3 p en :+:<br>
> mkLn 2 p sn :+:<br>
> mkLn 1 p qn ) :+:<br>
> mkLn 1 p qn) :+:<br>
> Tempo 3 2 (mkLn 6 p en))<br>
><br>
> pr2 p = Tempo 7 6 (m1 :+:<br>
> Tempo 5 4 (mkLn 5 p en) :+:<br>
> m1 :+:<br>
> mkLn 2 p en)<br>
> where m1 = Tempo 5 4 (Tempo 3 2 m2 :+: m2)<br>
> m2 = mkLn 3 p en<br>
><br>
> mkLn n p d = line (take n (repeat (Note p d [])))<br>
<br>
</tt>To play polyrhythms <tt>pr1</tt> and <tt>pr2</tt> in parallel using middle C
and middle G, respectively, we would do the following (middle C is in
the 5th octave):
<tt> <br>
<br>
> pr12 :: Music<br>
> pr12 = pr1 (C,5) :=: pr2 (G,5)<br>
<br>
</tt> <p>
As a final example in this section, we can can compute the duration in
beats of a musical object, a notion we will need in Section
<a href="performance.html#performance">4</a>, as follows:
<tt><br>
<br>
> dur :: Music -> Dur<br>
><br>
> dur (Note _ d _) = d<br>
> dur (Rest d) = d<br>
> dur (m1 :+: m2) = dur m1 + dur m2<br>
> dur (m1 :=: m2) = dur m1 `max` dur m2<br>
> dur (Tempo a b m) = dur m * float b / float a<br>
> dur (Trans _ m) = dur m<br>
> dur (Instr _ m) = dur m<br>
> dur (Player _ m) = dur m<br>
> dur (Phrase _ m) = dur m<br>
><br>
> float = fromInteger . toInteger :: Int -> Float<br>
<br>
</tt>
Using <tt>dur</tt> we can define a function <tt>revM</tt> that reverses any
<tt>Music</tt> value (and is thus considerably more useful than
<tt>retro</tt> defined earlier). Note the tricky treatment of <tt>(:=:)</tt>.
<tt><br>
<br>
> revM :: Music -> Music<br>
> revM n@(Note _ _ _) = n<br>
> revM r@(Rest _) = r<br>
> revM (Tempo i1 i2 m) = Tempo i1 i2 (revM m)<br>
> revM (Trans i m) = Trans i (revM m)<br>
> revM (Instr i m) = Instr i (revM m)<br>
> revM (Phrase pas m) = Phrase pas (revM m)<br>
> revM (m1 :+: m2) = revM m2 :+: revM m1<br>
> revM (m1 :=: m2) = let d1 = dur m1<br>
> d2 = dur m2<br>
> in if d1>d2 then revM m1 :=:<br>
> (Rest (d1-d2) :+: revM m2)<br>
> else (Rest (d2-d1) :+: revM m1) :=:<br>
> revM m2<br>
<br>
</tt> <p>
<p>
<B>Exercise<br>
</B>Find a simple piece of music written by your favorite composer, and
transcribe it into Haskore. In doing so, look for repeating patterns,
transposed phrases, etc. and reflect this in your code, thus revealing
deeper structural aspects of the music than that found in common
practice notation.
<p>
<p>
Appendix <a href="childsong6.html#chick">C</a> shows the first 28 bars of Chick Corea's
"Children's Song No. 6" encoded in Haskore.<a name="phrasing"></a><p>
<a name="sect3.3"></a>
<h3>3.3<tt> </tt>Phrasing and Articulation</h3>
<p>
Recall that the <tt>Note</tt> constructor contained a field of
<tt>NoteAttributes</tt>. These are values that are attached to notes for the
purpose of notation or musical interpretation. Likewise, the
<tt>Phrase</tt> constructor permits one to annotate an entire musical object
with <tt>PhraseAttributes</tt>. These two attribute datatypes cover a
wide range of attributions found in common practice notation, and are
shown in Figure <a href="basics.html#attributes">4</a>. Beware that use of them requires
the use of a player that knows how to interpret them! Players will be
described in more detail in Section <a href="performance.html#players">5</a>.<p>
<table border=2 cellpadding=3>
<tr><td><tt> <br>
<br>
> data NoteAttribute = Volume Float -- by convention: 0=min, 100=max<br>
> | Fingering Int<br>
> | Dynamics String<br>
> deriving Show<br>
><br>
> data PhraseAttribute = Dyn Dynamic<br>
> | Art Articulation<br>
> | Orn Ornament<br>
> deriving Show<br>
><br>
> data Dynamic = Accent Float | Crescendo Float | Diminuendo Float<br>
> | PPP | PP | P | MP | SF | MF | NF | FF | FFF | Loudness Float<br>
> | Ritardando Float | Accelerando Float<br>
> deriving Show<br>
><br>
> data Articulation = Staccato Float | Legato Float | Slurred Float<br>
> | Tenuto | Marcato | Pedal | Fermata | FermataDown | Breath<br>
> | DownBow | UpBow | Harmonic | Pizzicato | LeftPizz <br>
> | BartokPizz | Swell | Wedge | Thumb | Stopped<br>
> deriving Show<br>
><br>
> data Ornament = Trill | Mordent | InvMordent | DoubleMordent<br>
> | Turn | TrilledTurn | ShortTrill<br>
> | Arpeggio | ArpeggioUp | ArpeggioDown<br>
> | Instruction String | Head NoteHead<br>
> deriving Show<br>
><br>
> data NoteHead = DiamondHead | SquareHead | XHead | TriangleHead<br>
> | TremoloHead | SlashHead | ArtHarmonic | NoHead<br>
> deriving Show<br>
<br>
</tt><div align=center><B>Figure 4: Note and Phrase Attributes.</B></div>
</td></tr></table>
<p>
Note that some of the attributes are parameterized with a numeric
value. This is used by a player to control the degree to which
an articulation is to be applied. For example, we would expect
<tt>Legato 1.2</tt> to create more of a legato feel than <tt>Legato 1.1</tt>.
The following constants represent default values for some of the
parameterized attributes:
<tt><br>
<br>
> legato, staccato :: Articulation<br>
> accent, bigAccent :: Dynamic<br>
><br>
> legato = Legato 1.1<br>
> staccato = Staccato 0.5<br>
> accent = Accent 1.2<br>
> bigAccent = Accent 1.5<br>
<br>
</tt> <p>
To understand exactly how a player interprets an attribute requires
knowing how players are defined. Haskore defines only a few simple
players, so in fact many of the attributes in Figure <a href="basics.html#attributes">4</a>
are to allow the user to give appropriate interpretations of them by
her particular player. But before looking at the structure of players
we will need to look at the notion of a <I>performance</I> (these two
ideas are tightly linked, which is why the <tt>Players</tt> and
<tt>Performance</tt> modules are mutually recursive).<p>
<hr><body bgcolor="#ffffff"><i>The Haskore Tutorial</i><br><a href="index.html">top</a> <a href="Haskore.html">back</a> <a href="Performance.html">next</a>
|