]> Git — Sourcephile - tmp/julm/arpeggigon.git/blob - RCMA/Semantics.hs
Translation from high to low level progressing. Should be finished soon.
[tmp/julm/arpeggigon.git] / RCMA / Semantics.hs
1 -- Basic Semantics V2 for a Reactive Music Cellular Automaton.
2 -- Inspired by the reacTogon.
3 -- Written by Henrik Nilsson, 2016-05-27
4 -- Based on an earlier version.
5 --
6 -- This gives the semantics of a single RCMA layer. The output is
7 -- a high-level representation of notes for each beat. This is to be
8 -- translated to low-level MIDI message by a subsequent translator
9 -- responsible for merging notes from different layers, ensuring that
10 -- a note off message corresponding to each note on message is always
11 -- emitted after the appropriate time, rendering any embellismnets
12 -- such as slides (while not generating too much MIDI data), etc.
13
14 -- ToDo:
15 -- * Add boolean flag to change direction to indicate start tile
16 -- DONE!
17 -- * Change main routine to generate start play heads from board
18 -- DONE!
19 -- * Add an optional restart facility: Maybe Int, restart every n
20 -- bars.
21 -- DONE!
22 -- * Interpret a negative repeat as repeat indefinitely.
23 -- DONE!
24 -- * Interpret a non-positve duration as mute: don't emit any note.
25 -- DONE!
26 -- * Eliminate Ignore as now almost the same as Absorb with duration 0?
27 -- The only difference is that Absorb mostly overrides the repeat count.
28 -- Absorb = Stop {duration 0, repeat 1}
29 -- And as absorb might be a common case, it might be useful to have
30 -- a distinct graphical representation?
31 -- DECIDED AGAINST FOR NOW
32
33 module RCMA.Semantics where
34
35 import Data.Array
36 import Data.List (intersperse, nub)
37 import Data.Maybe (catMaybes)
38 import Data.Ratio
39
40
41 ------------------------------------------------------------------------------
42 -- Basic Type Synonyms
43 ------------------------------------------------------------------------------
44
45 -- Unipolar control value; [0, 1]
46 type UCtrl = Double
47
48 -- Bipolar control value; [-1, 1]
49 type BCtrl = Double
50
51 -- Unipolar control values are usually between 0 and 127.
52 toUCtrl :: Int -> UCtrl
53 toUCtrl x = fromIntegral x / 127
54
55 fromUCtrl :: UCtrl -> Int
56 fromUCtrl x = floor $ x * 127
57
58 -- Bipolar control values are usually between -127 and 127.
59 toBCtrl :: Int -> BCtrl
60 toBCtrl = toUCtrl
61
62 fromBCtrl :: BCtrl -> Int
63 fromBCtrl = fromUCtrl
64
65 ------------------------------------------------------------------------------
66 -- Tempo
67 ------------------------------------------------------------------------------
68
69 type Tempo = Int
70
71 ------------------------------------------------------------------------------
72 -- Time and Beats
73 ------------------------------------------------------------------------------
74
75 -- The assumption is that the automaton is clocked by a beat clock and
76 -- thus advances one step per beat. For an automaton working in real time,
77 -- the beat clock would be defined externally, synchronized with other
78 -- layers and possibly external MIDI, and account for tempo, any swing, etc.
79
80 -- Beats and Bars
81
82 -- A beat as such is nothing.
83 type Beat = ()
84
85 -- Beats per Bar: number of beats per bar in the time signature of a layer.
86 -- Non-negative.
87 type BeatsPerBar = Int
88
89 -- The beat number in the time signature of the layer. The first beat is 1.
90 type BeatNo = Int
91
92 nextBeatNo :: BeatsPerBar -> BeatNo -> BeatNo
93 nextBeatNo bpb bn = bn `mod` bpb + 1
94
95
96 {-
97 -- Not needed for individual layers (at present)
98
99 -- Time; [0,+inf)
100 type Time = Double
101 -}
102
103
104 ------------------------------------------------------------------------------
105 -- MIDI
106 ------------------------------------------------------------------------------
107
108 -- This semantics mainly works with a high-level represemntation of notes.
109 -- But it is convenient to express some of the high-level aspects directly
110 -- in the corresponding MIDI terms to facilitate the translation.
111
112 -- MIDI note number; [0,127]
113 type MIDINN = Int
114
115
116 -- Assume MIDI convetion: 60 = "Middle C" = C4
117 middleC = 60
118 middleCOct = 4
119
120
121 -- MIDI velocity; [0,127]
122 type MIDIVel = Int
123
124
125 -- MIDI Program Change: Program Number; [0,127]
126 type MIDIPN = Int
127
128
129 -- MIDI Control Change: Control Number and Control Value; [0,127]
130 type MIDICN = Int
131 type MIDICV = Int
132
133 -- MIDICVRnd gives the option to pick a control value at random.
134 -- (Handled through subsequent translation to low-level MIDI events.)
135 data MIDICVRnd = MIDICV MIDICV | MIDICVRnd deriving (Eq, Show)
136
137 -- TEMPORARY
138 data Controller = Lol
139 --
140 ------------------------------------------------------------------------------
141 -- Notes
142 ------------------------------------------------------------------------------
143
144 -- Pitch
145
146 -- We chose to represent pitch by MIDI note number
147 newtype Pitch = Pitch MIDINN deriving Eq
148
149 pitchToMNN :: Pitch -> MIDINN
150 pitchToMNN (Pitch nn) = nn
151
152 instance Show Pitch where
153 show (Pitch nn) = names !! note ++ show oct
154 where
155 nn' = nn - middleC
156 note = nn' `mod` 12
157 oct = nn' `div` 12 + middleCOct
158 names = ["C", "C#", "D", "D#", "E", "F",
159 "F#", "G", "G#", "A", "A#", "B"]
160
161 -- Relative pitch in semi tones. Used for e.g. transposition.
162 type RelPitch = Int
163
164
165 -- Articulation
166
167 -- Each layer has a setting that indicate how strongly the notes
168 -- should normally be played as a percentage of full strength.
169 -- (In the real application, this settig can be set to a fixed value
170 -- or set to be derived from teh last input note, "as played").
171 -- Individual notes can tehn be accented (played more strongly),
172 -- either unconditionally or as a function of the beat count.
173
174 type Strength = UCtrl
175
176 -- This could of course be generalised, e.g. a list of beat numbers to
177 -- accentuate. But this is simple and accounts for the most common patterns.
178 data Articulation = NoAccent
179 | Accent
180 | Accent1
181 | Accent13
182 | Accent14
183 | Accent24
184 deriving (Eq, Show)
185
186 accentStrength = 1.2
187
188 -- Articulated strength
189 articStrength :: Strength -> BeatNo -> Articulation -> Strength
190 articStrength st bn art
191 | accentedBeat = st * accentStrength
192 | otherwise = st
193 where
194 accentedBeat =
195 case (bn, art) of
196 (_, NoAccent) -> False
197 (_, Accent) -> True
198 (1, Accent1) -> True
199 (1, Accent13) -> True
200 (3, Accent13) -> True
201 (1, Accent14) -> True
202 (4, Accent14) -> True
203 (1, Accent24) -> True
204 (4, Accent24) -> True
205 _ -> False
206
207
208 -- Duration
209
210 -- Duration in terms of a whole note at the *system* tempo. (Each layer
211 -- is clocked at a layer beat that is a fraction/multiple of the system
212 -- tempo). Note that notes are played a little shorter than their nominal
213 -- duration. This is taken care of by the translation into low-level
214 -- MIDI events. (One might consider adding indications of staccato or
215 -- tenuto.)
216 --
217 -- A non-positive duration is interpreted as mute: no note emitted.
218 type Duration = Rational
219
220
221 -- Ornamentation
222
223 -- Notes can be ornamented. Traditionnally, ornamenting refers to modifications
224 -- of the pitch, such as a trill or a grace note. Here we use the term in
225 -- a generalised sense.
226 -- * A MIDI program change (to be sent before the note).
227 -- * A MIDI Continuous Controler Change (to be sent before the note).
228 -- * A Slide
229 -- One might also consider adding trills, grace notes, MIDI after touch ...
230
231 data Ornaments = Ornaments {
232 ornPC :: Maybe MIDIPN,
233 ornCC :: [(MIDICN, MIDICVRnd)],
234 ornSlide :: SlideType
235 } deriving Show
236
237 data SlideType = NoSlide | SlideUp | SlideDn deriving (Eq, Show)
238
239 noOrn :: Ornaments
240 noOrn = Ornaments { ornPC = Nothing
241 , ornCC = []
242 , ornSlide = NoSlide
243 }
244
245 -- Notes
246
247 -- Attributes needed to generate a note.
248 -- * The pitch of a note is given by the position on the board
249 -- * The strength is given by the layer strength, beat no., and articulation
250 -- * Duratio and Ornamentatio are stored
251 data NoteAttr = NoteAttr {
252 naArt :: Articulation,
253 naDur :: Duration,
254 naOrn :: Ornaments
255 } deriving Show
256
257
258 -- High level note representation emitted form a layer
259 data Note = Note {
260 notePch :: Pitch,
261 noteStr :: Strength,
262 noteDur :: Duration,
263 noteOrn :: Ornaments
264 } deriving Show
265
266
267 ------------------------------------------------------------------------------
268 -- Board
269 ------------------------------------------------------------------------------
270
271 -- Numbering; row number inside tile, column number below:
272 -- _ _
273 -- _/2\_/2\_
274 -- / \_/1\_/1\
275 -- \_/1\_/1\_/
276 -- / \_/0\_/0\
277 -- \_/0\_/0\_/
278 -- \_/ \_/
279 -- -1 0 1 2
280
281
282 -- Angle measured in multiples of 60 degrees.
283 type Angle = Int
284
285 data Dir = N | NE | SE | S | SW | NW deriving (Enum, Eq, Show)
286
287
288 turn :: Dir -> Angle -> Dir
289 turn d a = toEnum ((fromEnum d + a) `mod` 6)
290
291
292 type Pos = (Int, Int)
293
294 -- Position of neighbour in given direction
295 neighbor :: Dir -> Pos -> Pos
296 neighbor N (x,y) = (x, y + 1)
297 neighbor NE (x,y) = (x + 1, y + 1 - x `mod` 2)
298 neighbor SE (x,y) = (x + 1, y - x `mod` 2)
299 neighbor S (x,y) = (x, y - 1)
300 neighbor SW (x,y) = (x - 1, y - x `mod` 2)
301 neighbor NW (x,y) = (x - 1, y + 1 - x `mod` 2)
302
303
304 -- Position and transposition to pitch:
305 -- * Harmonic Table" layout: N = +7; NE = +4; SE = -3
306 -- * (0,0) assumed to be "Middle C"
307 posToPitch :: Pos -> RelPitch -> Pitch
308 posToPitch (x,y) tr =
309 Pitch (y * 7 + x `div` 2 - 3 * (x `mod` 2) + middleC + tr)
310
311
312 -- Actions
313 -- A ChDir counter is optionally a start counter if the Boolean flag is
314 -- set to true.
315 -- Any counter can be made silent by setting the note duration to a
316 -- non-positive number.
317
318 data Action = Inert -- No action, play heads move through.
319 | Absorb -- Remove play head silently.
320 | Stop NoteAttr -- Play note then remove play head.
321 | ChDir Bool NoteAttr Dir -- Play note then change direction.
322 | Split NoteAttr -- Play note then split head into five.
323 deriving Show
324
325
326 -- Cells
327 -- A cell stores an action and a repetition number.
328 -- 0: the cell is completely bypassed, as if it wasn't there.
329 -- 1: the action is carried out once (default)
330 -- n > 1: any note output of the action is repeated (n-1) times before the
331 -- action is carried out.
332 -- n < 0: any note output of the action is repeated indefinitely (oo).
333
334 type Cell = (Action, Int)
335
336
337 -- Make a cell with a default repeat count of 1.
338 mkCell :: Action -> Cell
339 mkCell a = mkCellRpt a 1
340
341
342 -- Make a cell with a non-default repeition number.
343 mkCellRpt :: Action -> Int -> Cell
344 mkCellRpt a n = (a, n)
345
346
347 -- Board extent: south-west corner and north-east corner.
348 -- This covers most of the MIDI range: A#-1 (10) to G7 (103).
349 swc, nec :: Pos
350 swc = (-9, -6)
351 nec = (9, 6)
352
353
354 -- Test if a position is on the board as defined by swc and nec.
355 -- The assumption is that odd columns contain one more cell, as per the
356 -- picture above. Of course, one could opt for a "zig-zag" layout
357 -- with each column having the same number of cells which would be slightly
358 -- simpler.
359 onBoard :: Pos -> Bool
360 onBoard (x,y) = xMin <= x && x <= xMax
361 && yMin <= y
362 && (if even x then
363 y < yMax
364 else
365 y <= yMax)
366 where
367 (xMin, yMin) = swc
368 (xMax, yMax) = case nec of
369 (x, y) | even x -> (x, y + 1)
370 | otherwise -> (x, y)
371
372
373 type Board = Array Pos Cell
374
375
376 -- Build a board from a list specifying the non-empty cells.
377 makeBoard :: [(Pos, Cell)] -> Board
378 makeBoard pcs =
379 array (swc,nec')
380 ([(p, if onBoard p then mkCell Inert else mkCell Absorb)
381 | p <- range (swc, nec')]
382 ++ [(p,c) | (p, c) <- pcs, onBoard p])
383 where
384 -- This is to ensure (neighbor NW nec) is included on the board,
385 -- regardless of whether the column of nec is even or odd.
386 -- Otherwise, due to the "jagged" upper edge, the top row would
387 -- be missing, but every other cell of that *is* on the board.
388 -- The "superfluous" cells are set to Absorb above.
389 nec' = neighbor N nec
390
391
392 -- Look up a cell
393 lookupCell :: Board -> Pos -> Cell
394 lookupCell b p = if onBoard p then (b ! p) else (Absorb, 1)
395
396
397 ------------------------------------------------------------------------------
398 -- Play Head
399 ------------------------------------------------------------------------------
400
401 -- A play head is characterised by:
402 -- * Current position
403 -- * Number of beats before moving
404 -- * Direction of travel
405 -- If an action involves playing a note, this is repeated once for
406 -- each beat the play head is staying, with the rest of the action
407 -- carried out at the last beat.
408
409 data PlayHead =
410 PlayHead {
411 phPos :: Pos,
412 phBTM :: Int,
413 phDir :: Dir
414 }
415 deriving (Eq, Show)
416
417
418 ------------------------------------------------------------------------------
419 -- Start Heads
420 ------------------------------------------------------------------------------
421
422 startHeads :: Board -> [PlayHead]
423 startHeads bd =
424 [ PlayHead {
425 phPos = p,
426 phBTM = n,
427 phDir = d
428 }
429 | (p, (ChDir True _ d, n)) <- assocs bd ]
430
431
432 ------------------------------------------------------------------------------
433 -- State transition
434 ------------------------------------------------------------------------------
435
436 -- Advance the state of a single play head.
437 --
438 -- The result is a list of heads to be actioned at the *next* beat
439 -- later) and possibly a note to be played at *this* beat.
440
441 advanceHead :: Board -> BeatNo -> RelPitch -> Strength -> PlayHead
442 -> ([PlayHead], Maybe Note)
443 advanceHead bd bn tr st ph = ahAux (moveHead bd ph)
444 where
445 ahAux ph@PlayHead {phPos = p, phBTM = btm, phDir = d} =
446 case fst (lookupCell bd p) of
447 Inert -> ([ph], Nothing)
448 Absorb -> ([], Nothing) -- No point waiting until BTM=0
449 Stop na -> (newPHs [], mkNote p bn tr st na)
450 ChDir _ na d' -> (newPHs [ph {phDir = d'}],
451 mkNote p bn tr st na)
452 Split na -> (newPHs [ PlayHead {
453 phPos = p,
454 phBTM = 0,
455 phDir = d'
456 }
457 | a <- [-2 .. 2],
458 let d' = turn d a
459 ],
460 mkNote p bn tr st na)
461 where
462 newPHs phs = if btm == 0 then phs else [ph]
463
464
465 -- Moves a play head if the BTM counter has reached 0, otherwise decrement BTM.
466 -- Any encountered cells where the repeat count is < 1 are skipped.
467 moveHead :: Board -> PlayHead -> PlayHead
468 moveHead bd (ph@PlayHead {phPos = p, phBTM = btm, phDir = d})
469 | btm == 0 = let
470 p' = neighbor d p
471 btm' = snd (lookupCell bd p')
472 in
473 moveHead bd (ph {phPos = p', phBTM = btm'})
474 | btm > 0 = ph {phBTM = btm - 1}
475 | otherwise = ph -- Repeat indefinitely
476
477 mkNote :: Pos -> BeatNo -> RelPitch -> Strength -> NoteAttr -> Maybe Note
478 mkNote p bn tr st na@(NoteAttr {naDur = d})
479 | d <= 0 = Nothing -- Notes of non-positive length are silent.
480 | otherwise = Just $
481 Note {
482 notePch = posToPitch p tr,
483 noteStr = articStrength st bn (naArt na),
484 noteDur = naDur na,
485 noteOrn = naOrn na
486 }
487
488
489 -- Advance a list of heads, collecting all resulting heads and notes.
490 -- Any duplicate play heads are eliminated (or their number may uselessly
491 -- grow very quickly), and a cap (50, arbitrary, but should be plenty,
492 -- expecially given the board size) on the number of simultaneous playheads
493 -- per layer is imposed.
494 advanceHeads :: Board -> BeatNo -> RelPitch -> Strength -> [PlayHead]
495 -> ([PlayHead], [Note])
496 advanceHeads bd bn tr st phs =
497 let
498 (phss, mns) = unzip (map (advanceHead bd bn tr st) phs)
499 in
500 (take 50 (nub (concat phss)), catMaybes mns)
501
502
503 -- Given a board with start counters, run a board indefinitely, optionally
504 -- restarting every ri bars.
505 --
506 -- Arguments:
507 -- (1) Board (bd)
508 -- (2) Beats Per Bar (bpb); > 0
509 -- (3) Optioal repeat Interval (mri); In bars.
510 -- (4) Transposition (tr)
511 -- (5) Strength (st)
512 --
513 -- Returns:
514 -- Stream of notes played at each beat.
515 --
516 -- In the real implementation:
517 -- * A layer beat clock would be derived from the system beat (as a
518 -- fraction/multiple, adding any swing) and each clock event be tagged
519 -- with the beat number.
520 -- * The board (bd) would not necessarily be a constant input. (One might
521 -- consider allowing editing a layer while the machine is running)
522 -- * The time signature, and thus the beats per par (bpb), along with
523 -- repeat interval (ri) would likely be static (only changeable while
524 -- automaton is stopped).
525 -- * The transposition (tr) would be dynamic, the sum of a per layer
526 -- transposition that can be set through the user interface and the
527 -- difference between the MIDI note number of the last external
528 -- note received for the layer and middle C (say).
529 -- * The strength (st) would be dynamic, configurable as either the strength
530 -- set through the user interface or the strength of the last external
531 -- note received for the layer (derived from its MIDI velocity).
532
533 runRMCA :: Board -> BeatsPerBar -> Maybe Int -> RelPitch -> Strength
534 -> [[Note]]
535 runRMCA bd bpb mri tr st
536 | bpb > 0 =
537 case mri of
538 Nothing -> nss
539 Just ri
540 | ri > 0 -> cycle (take (ri * bpb) nss)
541 | otherwise -> error "The repeat interval must be at \
542 \least 1 bar."
543 | otherwise = error "The number of beats per bar must be at least 1."
544 where
545 nss = runAux 1 (startHeads bd)
546
547 runAux bn phs = ns : runAux (nextBeatNo bpb bn) phs'
548 where
549 (phs', ns) = advanceHeads bd bn tr st phs
550
551
552 -- Print played notes in a time-stamped (bar, beat), easy-to-read format.
553
554 ppNotes :: BeatsPerBar -> [[Note]] -> IO ()
555 ppNotes bpb nss = ppnAux (zip [(br,bn) | br <- [1..], bn <- [1..bpb]] nss)
556 where
557 ppnAux [] = return ()
558 ppnAux ((_, []) : tnss) = ppnAux tnss
559 ppnAux ((t, ns) : tnss) = do
560 putStrLn ((leftJustify 10 (show t)) ++ ": "
561 ++ concat (intersperse ", " (map show ns)))
562 ppnAux tnss
563
564
565 leftJustify :: Int -> String -> String
566 leftJustify w s = take (w - length s) (repeat ' ') ++ s
567
568
569 ------------------------------------------------------------------------------
570 -- Simple tests
571 ------------------------------------------------------------------------------
572
573 testBoard1 =
574 makeBoard [((0,0), mkCell (ChDir True na1 N)),
575 ((0,1), mkCell (ChDir False na1 SE)),
576 ((1,1), mkCell (Split na1)),
577 ((1,-1), mkCell (Split na1)),
578 ((-1,0), mkCell (ChDir False na2 NE))]
579
580 testBoard1a =
581 makeBoard [((0,0), mkCell (ChDir False na1 N)),
582 ((0,1), mkCell (ChDir False na1 SE)),
583 ((1,1), mkCell (Split na1)),
584 ((1,-1), mkCell (Split na1)),
585 ((-1,0), mkCell (ChDir False na2 NE))]
586
587 testBoard2 =
588 makeBoard [((0,0), mkCell (ChDir True na1 N)),
589 ((0,2), mkCellRpt (ChDir False na2 SE) 3),
590 ((2,1), mkCell (ChDir False na1 SW)),
591 ((1,1), mkCellRpt (ChDir False na1 N) 0) {- Skipped! -},
592 ((0,4), mkCellRpt (ChDir True na1 N) (-1)) {- Rpt indef. -},
593 ((0, -6), mkCell (ChDir True na1 N)),
594 ((0, -2), mkCell (ChDir False na3 S) {- Silent -})]
595
596 testBoard3 =
597 makeBoard [((0,0), mkCell (ChDir True na1 N))]
598
599 na1 = NoteAttr {
600 naArt = Accent13,
601 naDur = 1 % 4,
602 naOrn = Ornaments Nothing [] NoSlide
603 }
604
605 na2 = NoteAttr {
606 naArt = NoAccent,
607 naDur = 1 % 16,
608 naOrn = Ornaments Nothing [(10, MIDICVRnd)] SlideUp
609 }
610
611 na3 = NoteAttr {
612 naArt = Accent13,
613 naDur = 0,
614 naOrn = Ornaments Nothing [] NoSlide
615 }
616
617
618 bpb :: Int
619 bpb = 4
620
621 main = ppNotes bpb (take 50 (runRMCA testBoard3 bpb (Just 2) 0 0.8))