Skip to content

Commit 842ccbd

Browse files
author
Mike Solomon
committed
2 parents 34c72f2 + 42e8316 commit 842ccbd

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

examples/atari-speaks/AtariSpeaks.purs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Prelude
55
import Control.Alt (alt, (<|>))
66
import Control.Monad.ST.Class (liftST)
77
import Control.Monad.ST.Internal as RRef
8-
import Control.Plus (empty)
98
import Data.Array ((..))
109
import Data.ArrayBuffer.Typed (toArray)
1110
import Data.Foldable (for_, intercalate, fold)
@@ -27,8 +26,8 @@ import Effect.Class (liftEffect)
2726
import FRP.Behavior (sample_)
2827
import FRP.Event (Event, create, filterMap, hot, sampleOnRight, subscribe)
2928
import FRP.Event.Animate (animationFrameEvent)
30-
import Ocarina.Clock(WriteHead, fot, writeHead)
31-
import Ocarina.Control (analyser, gain, loopBuf, speaker2)
29+
import Ocarina.Clock (WriteHead, fot, writeHead)
30+
import Ocarina.Control (analyser_, gain_, loopBuf, speaker2)
3231
import Ocarina.Core (Audible, bangOn, opticN)
3332
import Ocarina.Example.Utils (RaiseCancellation)
3433
import Ocarina.Interpret (close, context, decodeAudioDataFromUri, effectfulAudioInterpret, getByteFrequencyData, makeFFIAudioSnapshot)
@@ -41,25 +40,25 @@ scene
4140
-> AnalyserNodeCb
4241
-> WriteHead Event
4342
-> Audible D2 payload
44-
scene atar cb wh =
43+
scene buffer cb wh =
4544
let
4645
tr = fot wh (mul pi)
4746
in
48-
analyser { cb } empty
49-
[ gain 1.0 empty
50-
[ gain 0.3 empty
51-
[ loopBuf { buffer: atar, playbackRate: 1.0 }
47+
analyser_ { cb }
48+
[ gain_ 1.0
49+
[ gain_ 0.3
50+
[ loopBuf { buffer, playbackRate: 1.0 }
5251
( bangOn <|>
5352
playbackRate <<<
54-
(over opticN (\rad -> 1.0 + 0.1 * sin rad)) <$> tr
53+
over opticN (\rad -> 1.0 + 0.1 * sin rad) <$> tr
5554
)
5655
]
57-
, gain 0.15 empty
58-
[ loopBuf { buffer: atar, playbackRate: 1.0 }
56+
, gain_ 0.15
57+
[ loopBuf { buffer, playbackRate: 1.0 }
5958
( bangOn
6059
<|>
6160
playbackRate <<<
62-
(over opticN (\rad -> 1.5 + 0.1 * sin (2.0 * rad)))
61+
over opticN (\rad -> 1.5 + 0.1 * sin (2.0 * rad))
6362
<$> tr
6463
<|>
6564
loopStart <<< (\rad -> 0.1 + 0.1 * sin rad)
@@ -70,8 +69,7 @@ scene atar cb wh =
7069
<<< view opticN <$> tr
7170
)
7271
]
73-
, gain 0.3 empty
74-
[ loopBuf { buffer: atar, playbackRate: 0.25 } bangOn ]
72+
, gain_ 0.3 [ loopBuf { buffer, playbackRate: 0.25 } bangOn ]
7573
]
7674
]
7775

@@ -83,16 +81,16 @@ data UIAction
8381
type Init = BrowserAudioBuffer
8482

8583
initializeAtariSpeaks :: Aff Init
86-
initializeAtariSpeaks = do
87-
atar <- liftEffect context >>= flip decodeAudioDataFromUri
88-
"https://freesound.org/data/previews/100/100981_1234256-lq.mp3"
89-
pure atar
84+
initializeAtariSpeaks =
85+
liftEffect context
86+
>>= flip decodeAudioDataFromUri
87+
"https://freesound.org/data/previews/100/100981_1234256-lq.mp3"
9088

9189
atariSpeaks
9290
:: BrowserAudioBuffer
9391
-> RaiseCancellation
9492
-> Nut
95-
atariSpeaks atar rc = bussed \push -> lcmap (alt (pure TurnOn)) \event ->
93+
atariSpeaks atari rc = bussed \push -> lcmap (alt (pure TurnOn)) \event ->
9694
DOM.div_
9795
[ DOM.h1_ [ text_ "Atari speaks" ]
9896
, DOM.button
@@ -109,7 +107,7 @@ atariSpeaks atar rc = bussed \push -> lcmap (alt (pure TurnOn)) \event ->
109107
let wh = writeHead 0.04 ctx
110108
let
111109
audioE = speaker2
112-
[ scene atar
110+
[ scene atari
113111
( AnalyserNodeCb
114112
( \a -> do
115113
analyserE.push (Just a)
@@ -128,22 +126,10 @@ atariSpeaks atar rc = bussed \push -> lcmap (alt (pure TurnOn)) \event ->
128126
( \(Tuple audio analyser) -> do
129127
audio ffi2
130128
for_ analyser \a -> do
131-
frequencyData <-
132-
getByteFrequencyData a
129+
frequencyData <- getByteFrequencyData a
133130
arr <- toArray frequencyData
134-
push $ AsciiMixer $
135-
intercalate "\n"
136-
( map
137-
( \ii ->
138-
fold
139-
( ( 0 ..
140-
(toInt ii)
141-
) $> ">"
142-
)
143-
144-
)
145-
arr
146-
)
131+
push $ AsciiMixer $ intercalate "\n" $
132+
map (\ii -> fold ((0 .. toInt ii) $> ">")) arr
147133
)
148134
let unsub = unsub' *> afe.unsubscribe
149135
rc $ Just { unsub, ctx }
@@ -163,14 +149,14 @@ atariSpeaks atar rc = bussed \push -> lcmap (alt (pure TurnOn)) \event ->
163149
)
164150
]
165151
[ text
166-
( event # map case _ of
152+
( event <#> case _ of
167153
TurnOn -> "Turn on"
168154
_ -> "Turn off"
169155
)
170156
]
171157
, DOM.p_
172158
[ text
173-
( event # map case _ of
159+
( event <#> case _ of
174160
AsciiMixer s -> s
175161
_ -> ""
176162
)
@@ -179,5 +165,5 @@ atariSpeaks atar rc = bussed \push -> lcmap (alt (pure TurnOn)) \event ->
179165

180166
main :: Effect Unit
181167
main = launchAff_ do
182-
atar <- initializeAtariSpeaks
183-
liftEffect $ runInBody (atariSpeaks atar (const $ pure unit))
168+
atari <- initializeAtariSpeaks
169+
liftEffect $ runInBody (atariSpeaks atari (const $ pure unit))

0 commit comments

Comments
 (0)