Skip to content

Commit

Permalink
Makes wags safer (#34)
Browse files Browse the repository at this point in the history
* Uses variants

* Updates examples

* Fixes referrential transaprency problems

Co-authored-by: Mike Solomon <mike@wavr.so>
  • Loading branch information
Mike Solomon and Mike Solomon authored Nov 22, 2021
1 parent 8039e1d commit 52746bf
Show file tree
Hide file tree
Showing 53 changed files with 4,108 additions and 3,209 deletions.
10 changes: 2 additions & 8 deletions examples/atari-speaks/AtariSpeaks.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import WAGS.Control.Types (Frame0, Scene)
import WAGS.Create (icreate)
import WAGS.Create.Optionals (CGain, CLoopBuf, CSpeaker, CAnalyser, analyser, gain, loopBuf, speaker)
import WAGS.Graph.AudioUnit (TAnalyser, TGain, TLoopBuf, TSpeaker)
import WAGS.Interpret (close, context, contextState, contextResume, decodeAudioDataFromUri, getByteFrequencyData, makeUnitCache)
import WAGS.Interpret (close, context, contextResume, contextState, decodeAudioDataFromUri, getByteFrequencyData, makeFFIAudioSnapshot)
import WAGS.Run (Run, RunAudio, RunEngine, SceneI(..), run)
import WAGS.WebAPI (AnalyserNode, AnalyserNodeCb, AudioContext, BrowserAudioBuffer)

Expand Down Expand Up @@ -166,17 +166,11 @@ handleAction = case _ of
-- just for kicks
H.liftEffect $ contextState audioCtx >>= Log.info
H.liftAff $ toAffE $ contextResume audioCtx
unitCache <- H.liftEffect makeUnitCache
ffiAudio <- H.liftEffect $ makeFFIAudioSnapshot audioCtx
atar <-
H.liftAff $ decodeAudioDataFromUri
audioCtx
"https://freesound.org/data/previews/100/100981_1234256-lq.mp3"
let
ffiAudio =
{ context: audioCtx
, writeHead: 0.0
, units: unitCache
}
unsubscribe <-
H.liftEffect
$ subscribe
Expand Down
16 changes: 5 additions & 11 deletions examples/drum-machine/DrumMachine.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import WAGS.Control.Functions.Graph (iloop, (@!>))
import WAGS.Control.Types (Frame0, Scene)
import WAGS.Create (icreate)
import WAGS.Create.Optionals (CGain, CSpeaker, CPlayBuf, gain, speaker, playBuf)
import WAGS.Graph.AudioUnit (OnOff(..), TGain, TLoopBuf, TSpeaker)
import WAGS.Graph.AudioUnit (TGain, TLoopBuf, TSpeaker, _offOn, _on)
import WAGS.Graph.Parameter (ff)
import WAGS.Interpret (close, context, decodeAudioDataFromUri, makeUnitCache)
import WAGS.Interpret (close, context, decodeAudioDataFromUri, makeFFIAudioSnapshot)
import WAGS.Run (RunAudio, RunEngine, SceneI(..), Run, run)
import WAGS.WebAPI (AudioContext, BrowserAudioBuffer)

Expand Down Expand Up @@ -61,9 +61,9 @@ scene shouldReset (SceneI { time, world: { snare } }) =
playBuf
{ onOff:
if (not shouldReset) then
pure On
pure _on
else
ff ((toNumber (tgFloor + 1) * gap) - time) (pure OffOn)
ff ((toNumber (tgFloor + 1) * gap) - time) (pure _offOn)
}
snare
}
Expand Down Expand Up @@ -160,7 +160,7 @@ handleAction :: forall output m. MonadEffect m => MonadAff m => Action -> H.Halo
handleAction = case _ of
StartAudio -> do
audioCtx <- H.liftEffect context
unitCache <- H.liftEffect makeUnitCache
ffiAudio <- H.liftEffect $ makeFFIAudioSnapshot audioCtx
ibuf <-
H.liftAff $ decodeAudioDataFromUri
audioCtx
Expand All @@ -175,12 +175,6 @@ handleAction = case _ of
launchAff_ do
buf <- decodeAudioDataFromUri audioCtx (head cf)
H.liftEffect $ Ref.write buf bf
let
ffiAudio =
{ context: audioCtx
, writeHead: 0.0
, units: unitCache
}
unsubscribe <-
H.liftEffect
$ subscribe
Expand Down
6 changes: 2 additions & 4 deletions examples/hello-world/HelloWorld.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import WAGS.Control.Types (Frame0, Scene)
import WAGS.Create (icreate)
import WAGS.Create.Optionals (CGain, CSpeaker, CSinOsc, gain, sinOsc, speaker)
import WAGS.Graph.AudioUnit (TGain, TSinOsc, TSpeaker)
import WAGS.Interpret (close, context, defaultFFIAudio, makeUnitCache)
import WAGS.Interpret (close, context, makeFFIAudioSnapshot)
import WAGS.Run (RunAudio, RunEngine, SceneI(..), Run, run)
import WAGS.WebAPI (AudioContext)

Expand Down Expand Up @@ -118,9 +118,7 @@ handleAction :: forall output m. MonadEffect m => MonadAff m => Action -> H.Halo
handleAction = case _ of
StartAudio -> do
audioCtx <- H.liftEffect context
unitCache <- H.liftEffect makeUnitCache
let
ffiAudio = defaultFFIAudio audioCtx unitCache
ffiAudio <- H.liftEffect $ makeFFIAudioSnapshot audioCtx
unsubscribe <-
H.liftEffect
$ subscribe
Expand Down
10 changes: 2 additions & 8 deletions examples/kitchen-sink/KitchenSink.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Halogen.Subscription as HS
import Halogen.VDom.Driver (runUI)
import Math (abs, pi)
import WAGS.Example.KitchenSink.Piece (piece)
import WAGS.Interpret (close, context, decodeAudioDataFromUri, getMicrophoneAndCamera, makeFloatArray, makePeriodicWave, makeUnitCache, mediaRecorderToUrl)
import WAGS.Interpret (close, context, decodeAudioDataFromUri, getMicrophoneAndCamera, makeFFIAudioSnapshot, makeFloatArray, makePeriodicWave, mediaRecorderToUrl)
import WAGS.Run (Run, run)
import WAGS.WebAPI (AudioContext, BrowserAudioBuffer, MediaRecorderCb(..))

Expand Down Expand Up @@ -116,7 +116,7 @@ handleAction = case _ of
{ emitter, listener } <- H.liftEffect HS.create
unsubscribeFromHalogen <- H.subscribe emitter
audioCtx <- H.liftEffect context
unitCache <- H.liftEffect makeUnitCache
ffiAudio <- H.liftEffect $ makeFFIAudioSnapshot audioCtx
myWave <-
H.liftEffect
$ makePeriodicWave audioCtx (0.0 +> -0.1 +> empty) (0.0 +> 0.05 +> empty)
Expand All @@ -131,12 +131,6 @@ handleAction = case _ of
chimes <- fetchBuffer audioCtx "https://freesound.org/data/previews/353/353194_5121236-hq.mp3"
shruti <- fetchBuffer audioCtx "https://freesound.org/data/previews/513/513742_153257-hq.mp3"
reverb <- fetchBuffer audioCtx "https://freesound.org/data/previews/555/555786_10147844-hq.mp3"
let
ffiAudio =
{ context: audioCtx
, writeHead: 0.0
, units: unitCache
}
unsubscribeFromWAGS <-
H.liftEffect
$ subscribe
Expand Down
4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/TLP/SawtoothOsc.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import WAGS.Example.KitchenSink.TLP.Convolver (doConvolver)
import WAGS.Example.KitchenSink.TLP.LoopSig (StepSig)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.SawtoothOsc (SawtoothOscGraph)
import WAGS.Graph.AudioUnit (OnOff(..))
import WAGS.Graph.AudioUnit (_on)
import WAGS.Graph.Parameter (AudioParameter_)
import WAGS.Patch (ipatch)
import WAGS.Run (SceneI(..))
Expand All @@ -30,4 +30,4 @@ doSawtoothOsc =
ipure lsig
else
Left
$ icont doConvolver (ipatch { microphone: Nothing } :*> ichange { buf: { buffer: myBuffer, onOff: On }, convolver: reverb } $> lsig)
$ icont doConvolver (ipatch { microphone: Nothing } :*> ichange { buf: { buffer: myBuffer, onOff: _on }, convolver: reverb } $> lsig)
5 changes: 2 additions & 3 deletions examples/kitchen-sink/KitchenSink/Types/Allpass.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CAllpass, CPlayBuf, allpass, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TAllpass, TPlayBuf)
import WAGS.Graph.AudioUnit (TAllpass, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type AllpassGraph
Expand All @@ -30,12 +30,11 @@ deltaKsAllpass { buffers: { "my-buffer": myBuffer, shruti } } =
let
switchOO = time % 2.0 < 1.0
switchW = time % 4.0 < 2.0
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
changes =
{ mix: if time > (timing.ksAllpass.dur - 1.0) then 0.0 else 1.0
, allpass: calcSlope 0.0 300.0 timing.ksAllpass.dur 2000.0 time
, buf: { onOff, buffer: if switchW then myBuffer else shruti }
}
in
ichange changes

4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/Bandpass.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CBandpass, CPlayBuf, bandpass, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TBandpass, TPlayBuf)
import WAGS.Graph.AudioUnit (TBandpass, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type BandpassGraph
Expand All @@ -30,7 +30,7 @@ deltaKsBandpass { buffers: { "my-buffer": myBuffer, shruti } } =
let
switchOO = time % 2.0 < 1.0
switchW = time % 4.0 < 2.0
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
changes =
{ mix: if time > (timing.ksBandpass.dur - 1.0) then 0.0 else 1.0
Expand Down
4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/Delay.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CDelay, CGain, CPlayBuf, Ref, delay, gain, playBuf
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TDelay, TGain, TPlayBuf)
import WAGS.Graph.AudioUnit (TDelay, TGain, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type DelayGraph
Expand Down Expand Up @@ -40,7 +40,7 @@ deltaKsDelay { buffers: { "my-buffer": myBuffer, shruti } } =
changes =
{ mix: if time > (timing.ksDelay.dur - 1.0) then 0.0 else 1.0
, delay: calcSlope 0.0 0.3 timing.ksDelay.dur 0.6 time
, buf: { onOff: if switchOO then On else Off, buffer: if switchW then myBuffer else shruti }
, buf: { onOff: if switchOO then _on else _off, buffer: if switchW then myBuffer else shruti }
}
in
ichange changes
5 changes: 2 additions & 3 deletions examples/kitchen-sink/KitchenSink/Types/Feedback.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CDelay, CGain, CPlayBuf, Ref, CHighpass, delay, ga
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TDelay, TGain, THighpass, TPlayBuf)
import WAGS.Graph.AudioUnit (TDelay, TGain, THighpass, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type FeedbackGraph
Expand Down Expand Up @@ -47,8 +47,7 @@ deltaKsFeedback { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > (timing.ksFeedback.dur - 1.0) then 0.0 else 1.0
delay = calcSlope 0.0 0.3 timing.ksFeedback.dur 0.6 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, delay, buf: { onOff, buffer } }

5 changes: 2 additions & 3 deletions examples/kitchen-sink/KitchenSink/Types/Highpass.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CHighpass, CPlayBuf, highpass, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), THighpass, TPlayBuf)
import WAGS.Graph.AudioUnit (THighpass, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type HighpassGraph
Expand All @@ -32,8 +32,7 @@ deltaKsHighpass { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > (timing.ksHighpass.dur - 1.0) then 0.0 else 1.0
highpass = calcSlope 0.0 300.0 timing.ksHighpass.dur 2000.0 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, highpass, buf: { onOff, buffer: buffer } }

4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/Highshelf.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CHighshelf, CPlayBuf, highshelf, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), THighshelf, TPlayBuf)
import WAGS.Graph.AudioUnit (THighshelf, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type HighshelfGraph
Expand All @@ -32,7 +32,7 @@ deltaKsHighshelf { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > timing.ksHighshelf.dur - 1.0 then 0.0 else 1.0
highshelf = calcSlope 0.0 300.0 timing.ksHighshelf.dur 2000.0 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, highshelf, buf: { onOff, buffer } }
4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/LoopBuf.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CLoopBuf, loopBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (pieceTime, timing)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TLoopBuf)
import WAGS.Graph.AudioUnit (TLoopBuf, _off, _on)

type LoopBufGraph
= TopWith { loopBuf :: Unit }
Expand All @@ -30,7 +30,7 @@ deltaKsLoopBuf { buffers: { "my-buffer": myBuffer, shruti } } =
switchOO = time % 2.0 < 1.0
switchW = time % 4.0 < 2.0
changeRec =
{ onOff: if switchOO then On else Off
{ onOff: if switchOO then _on else _off
, playbackRate: 1.0 + (0.1 * sin rad)
, loopStart: 1.0
, loopEnd: 1.4 + 0.2 * (sin rad)
Expand Down
4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/Lowpass.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CLowpass, CPlayBuf, lowpass, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TLowpass, TPlayBuf)
import WAGS.Graph.AudioUnit (TLowpass, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type LowpassGraph
Expand All @@ -32,7 +32,7 @@ deltaKsLowpass { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > (timing.ksLowpass.dur - 1.0) then 0.0 else 1.0
lowpass = calcSlope 0.0 300.0 timing.ksLowpass.dur 2000.0 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, lowpass, buf: { onOff, buffer } }
4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/Lowshelf.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CLowshelf, CPlayBuf, lowshelf, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TLowshelf, TPlayBuf)
import WAGS.Graph.AudioUnit (TLowshelf, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type LowshelfGraph
Expand All @@ -32,7 +32,7 @@ deltaKsLowshelf { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > (timing.ksLowshelf.dur - 1.0) then 0.0 else 1.0
lowshelf = calcSlope 0.0 300.0 timing.ksLowshelf.dur 2000.0 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, lowshelf, buf: { onOff, buffer } }
4 changes: 2 additions & 2 deletions examples/kitchen-sink/KitchenSink/Types/Notch.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CNotch, CPlayBuf, notch, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TNotch, TPlayBuf)
import WAGS.Graph.AudioUnit (TNotch, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type NotchGraph
Expand All @@ -32,7 +32,7 @@ deltaKsNotch { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > (timing.ksNotch.dur - 1.0) then 0.0 else 1.0
notch = calcSlope 0.0 300.0 timing.ksNotch.dur 2000.0 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, notch, buf: { onOff, buffer } }
5 changes: 2 additions & 3 deletions examples/kitchen-sink/KitchenSink/Types/Peaking.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WAGS.Create.Optionals (CPeaking, CPlayBuf, peaking, playBuf)
import WAGS.Example.KitchenSink.TLP.LoopSig (IxWAGSig', World)
import WAGS.Example.KitchenSink.Timing (timing, pieceTime)
import WAGS.Example.KitchenSink.Types.Empty (TopWith)
import WAGS.Graph.AudioUnit (OnOff(..), TPeaking, TPlayBuf)
import WAGS.Graph.AudioUnit (TPeaking, TPlayBuf, _off, _on)
import WAGS.Math (calcSlope)

type PeakingGraph
Expand All @@ -32,8 +32,7 @@ deltaKsPeaking { buffers: { "my-buffer": myBuffer, shruti } } =
switchW = time % 4.0 < 2.0
mix = if time > (timing.ksPeaking.dur - 1.0) then 0.0 else 1.0
peaking = calcSlope 0.0 300.0 timing.ksPeaking.dur 2000.0 time
onOff = if switchOO then On else Off
onOff = if switchOO then _on else _off
buffer = if switchW then myBuffer else shruti
in
ichange { mix, peaking, buf: { onOff, buffer } }

Loading

0 comments on commit 52746bf

Please sign in to comment.