/
Functions.purs
289 lines (263 loc) · 8.53 KB
/
Functions.purs
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
module WAGS.Control.Functions
( start
, istart
, modifyRes
, imodifyRes
, makeScene
, makeSceneFlipped
, makeSceneR
, makeSceneRFlipped
, makeSceneR'
, makeSceneR'Flipped
, loop
, iloop
, branch
, ibranch
, icont
, freeze
, (@>)
, (@!>)
, (@|>)
, (@||>)
, (<@)
, (<|@)
, (<||@)
) where
import Prelude
import Control.Comonad (extract)
import Data.Either (Either(..))
import WAGS.Control.Indexed (IxWAG(..), IxFrame)
import WAGS.Control.Types (AudioState', EFrame, Frame, Frame0, InitialWAG, Scene(..), Scene', WAG, oneFrame, unsafeUnWAG, unsafeWAG)
import WAGS.Interpret (class AudioInterpret)
-- | The initial `Frame` that is needed to begin any `Scene`.
-- |
-- | ```purescript
-- | piece :: Scene (SceneI Unit Unit) FFIAudio (Effect Unit) Frame0
-- | piece =
-- | WAGS.do
-- | start -- initial frame
-- | { time } <- env
-- | create (scene time) $> Right unit
-- | @> loop
-- | ( const
-- | $ WAGS.do
-- | { time } <- env
-- | ivoid $ change (scene time)
-- | )
-- | ```
start ::
forall audio engine res.
Monoid res =>
AudioInterpret audio engine =>
InitialWAG audio engine res Unit
start = unsafeWAG { context: initialAudioState, value: unit }
initialAudioState :: forall audio engine res. Monoid res => AudioState' audio engine res
initialAudioState =
{ res: mempty
, instructions: []
}
-- | Make a scene. The infix operator for this operation is `@>`.
-- |
-- | It accepts as arguments:
-- | - a frame to render
-- | - a function that accepts a frame from the next moment in time (`proofB`) and returns a scene.
-- |
-- | From these arguments, it produces a `Scene`.
-- | ```
makeScene ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
EFrame env audio engine proofA res { | graph } control ->
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
Scene env audio engine proofA res
makeScene m trans = Scene go
where
go :: forall proofB. env -> Scene' env audio engine proofB res
go env = case m env of
Left s -> oneFrame s env
Right r ->
let
{ context, value } = unsafeUnWAG r
in
{ instructions: context.instructions
, res: context.res
, next:
trans
$ unsafeWAG { context: context { instructions = [], res = mempty }, value }
}
infixr 6 makeScene as @>
makeSceneFlipped ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
EFrame env audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
makeSceneFlipped trans m = makeScene m trans
infixr 6 makeSceneFlipped as <@
istart ::
forall env audio engine res graph control.
Monoid res =>
AudioInterpret audio engine =>
IxFrame env audio engine Frame0 res {} { | graph } control ->
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
Scene env audio engine Frame0 res
istart m = makeSceneR (\e -> let IxWAG f = m e in f start)
infixr 6 istart as @!>
-- | Loops audio.
-- |
-- | The first argument is the loop and the second argument is the incoming graph that gets rendered before the loop.
-- | This means that all changes applied in the loop must be separately applied to the incoming frame if they are relevant.
-- | ```
loop ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Frame env audio engine proofB res { | graph } control
) ->
WAG audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
loop fa ma = makeSceneR (fa ma) (loop fa)
iloop ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
env -> control -> IxWAG audio engine proofB res { | graph } { | graph } control
) ->
WAG audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
iloop fa = loop (\wa e -> let IxWAG f = fa e (extract wa) in f wa)
-- | Accepts a "branch" frame for making a scene, where `Left` is a new scene and `Right` is the current scene looped.
-- |
branch ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
WAG audio engine proofB res { | graph } control ->
EFrame env audio engine proofB res { | graph } control
) ->
WAG audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
branch fa w = makeScene (fa w) (branch fa)
icont ::
forall env audio engine proof res graphi grapho a b.
Monoid res =>
AudioInterpret audio engine =>
( WAG audio engine proof res { | grapho } b ->
Scene env audio engine proof res
) ->
IxWAG audio engine proof res { | graphi } { | grapho } b ->
WAG audio engine proof res { | graphi } a ->
Scene env audio engine proof res
icont c (IxWAG x) = c <<< x
ibranch ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
env ->
control ->
Either
(WAG audio engine proofB res { | graph } control -> Scene env audio engine proofB res)
(IxWAG audio engine proofB res { | graph } { | graph } control)
) ->
WAG audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
ibranch fa =
branch
( \wa e -> case fa e (extract wa) of
Left l -> Left $ l wa
Right (IxWAG r) -> Right $ r wa
)
-- | Freezes the current audio frame.
-- |
freeze ::
forall env audio engine proof res graph x.
Monoid res =>
AudioInterpret audio engine =>
WAG audio engine proof res { | graph } x ->
Scene env audio engine proof res
freeze s = makeScene (pure $ Right s) freeze
-- | Similar to `makeScene`, but without the possibility to branch to a new scene. Aliased as `@|>`.
makeSceneR ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
Frame env audio engine proofA res { | graph } control ->
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
Scene env audio engine proofA res
makeSceneR a b = makeScene (map Right a) b
infixr 6 makeSceneR as @|>
makeSceneRFlipped ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
Frame env audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
makeSceneRFlipped a b = makeSceneR b a
infixr 6 makeSceneRFlipped as <|@
-- | Similar to `makeSceneR'`, but without the possibility to consult an env. Aliased as `@||>`.
makeSceneR' ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
WAG audio engine proofA res { | graph } control ->
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
Scene env audio engine proofA res
makeSceneR' a b = makeSceneR (pure a) b
infixr 6 makeSceneR' as @||>
makeSceneR'Flipped ::
forall env audio engine proofA res graph control.
Monoid res =>
AudioInterpret audio engine =>
( forall proofB.
WAG audio engine proofB res { | graph } control ->
Scene env audio engine proofB res
) ->
WAG audio engine proofA res { | graph } control ->
Scene env audio engine proofA res
makeSceneR'Flipped a b = makeSceneR' b a
infixr 6 makeSceneR'Flipped as <||@
-- | Modifies the residual for a frame and returns the result.
-- | If a frame never modifies its residual, the value of `mempty`
-- | for `res` is returned to the scene.
modifyRes ::
forall audio engine proof res i a.
AudioInterpret audio engine =>
(res -> res) -> WAG audio engine proof res i a -> WAG audio engine proof res i res
modifyRes f w = unsafeWAG { context: (i { res = res' }), value: res' }
where
{ context: i, value } = unsafeUnWAG w
res' = f i.res
-- | Modifies the residual for a frame and returns the result.
-- | If a frame never modifies its residual, the value of `mempty`
-- | for `res` is returned to the scene.
imodifyRes ::
forall audio engine proof res i.
AudioInterpret audio engine =>
(res -> res) -> IxWAG audio engine proof res i i res
imodifyRes f = IxWAG (modifyRes f)