-
Notifications
You must be signed in to change notification settings - Fork 4
/
Operators.hs
159 lines (142 loc) · 3.28 KB
/
Operators.hs
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
{-|
Module : Bang.Music.Operators
Description : The DSL part of the Bang library
Copyright : (c) Benjamin Kovach, 2014
License : MIT
Maintainer : bkovach13@gmail.com
Stability : experimental
Portability : Mac OSX
Defines a number of operators to effectively piece together Bang compositions.
-}
module Bang.Music.Operators where
import Bang.Interface.Base
import Bang.Music.Class
import Bang.Music.Transform
import Data.Foldable (foldMap)
-- |Infix operator for `cappend`
infixr 6 ><
(><) :: Music a -> Music a -> Music a
(><) = cappend
infixr 0 !>
-- |Set the `Tempo` of a composition (default 1)
--
-- Example (play 4 bass drum hits at double speed):
--
-- > 2 !> (4 #> bd)
(!>) :: Rational -> Music a -> Music a
(!>) = tempo
infixr 1 #>
-- |Infix operator for 'repl'
--
-- Example (play a bass drum twice):
--
-- > 2 #> bd
(#>) :: Int -> Music a -> Music a
(#>) = repl
infixl 1 >>~
-- |Map a function over a list of compositions and sequentially compose them.
-- Note: This is just 'foldMap' specialized to the list Monoid.
--
-- Example (play 'sn', 't1' and 't2' all twice):
--
-- > (2 #>) >>~ [sn, t1, t2]
(>>~) :: Monoid b => (a -> b) -> [a] -> b
(>>~) = foldMap
-- |Infix operator for 'poly'
--
-- Example (A 3\/4 polyrhythm):
--
-- > (3, 3 #> bd) ~=~ (4, 4 #> sn)
(~=~) :: (Rational, Music b) -> (Rational, Music b) -> Music b
(~=~) = poly
infixl 2 ~=
-- |Infix operator for 'fitL'
--
-- Example (a 3\/4 polyrhythm with duration 3\/4):
--
-- > (3 #> bd) ~= (4 #> sn)
(~=) :: Music b -> Music b -> Music b
(~=) = fitL
infixr 2 =~
-- |Infix operator for 'fitR'
--
-- Example (a 3\/4 polyrhythm with duration 1:
--
-- > (3 #> bd) =~ (4 #> sn)
(=~) :: Music b -> Music b -> Music b
(=~) = fitR
infixr 2 ~~
-- |Infix operator for 'withDuration'
--
-- Example:
--
-- @
-- 2 ~~ mconcat [
-- 16 #> bd
-- , 4 #> sn
-- , wr
-- ]
-- @
(~~) :: Rational -> Music b -> Music b
(~~) = withDuration
infixr 2 <<~
-- |Infix operator for 'takeDur'
--
-- Example (Only play 2 bass drum hits):
--
-- > (1/2) <<~ (4 #> bd)
(<<~) :: Rational -> Music b -> Music b
(<<~) = takeDur
infixr 2 ~>>
-- |Infix operator for 'dropDur'
--
-- Example (play 2 closed hi-hats):
--
-- > (1/2) ~>> ( (2 #> bd) <> (2 #> hc) )
(~>>) :: Rational -> Music b -> Music b
(~>>) = dropDur
infixr 2 <@~
-- |Infix operator for 'hushFor'
--
-- Example (half rest, then two closed hi-hats):
--
-- > (1/2) ~@> ( (2 #> bd) <> (2 #> hc) )
(~@>) :: Rational -> Music b -> Music b
(~@>) = hushFor
infixr 2 ~@>
-- |Infix operator for 'hushFrom'
--
-- Example (two bass drum hits, then a half rest):
--
-- > (1/2) <@~ ( (2 #> bd) <> (2 #> hc) )
(<@~) :: Rational -> Music b -> Music b
(<@~) = hushFrom
infixr 0 <!>
-- |Infix operator for 'normalize'
--
-- Example (Play 12 bass drum hits, then 4 closed hi-hats, then 3 snares, each within a single measure's time):
--
-- @
-- 1 \<!\> [
-- 12 #> bd
-- , 4 #> hc
-- , 3 #> sn
-- ]
-- @
(<!>) :: Rational -> [Music b] -> Music b
(<!>) = normalize
infixr 0 >!<
-- |Infix operator for 'normalizeC'
--
-- Example: (Play 12 bass drum hits, then 4 closed hi-hats, then 3 snares,
-- all concurrently within a single measure's time):
--
-- @
-- 1 >!< [
-- 12 #> bd
-- , 4 #> hc
-- , 3 #> sn
-- ]
-- @
(>!<) :: Rational -> [Music b] -> Music b
(>!<) = normalizeC