Skip to content

Commit

Permalink
Adds state verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Solomon committed Oct 20, 2021
1 parent 1fca63a commit 4f03c7f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.4] - 2021-10-05
## [0.5.5] - 2021-10-20

### Added

- resume and state method for audio context.

## [0.5.4] - 2021-10-20

### Added

Expand Down
1 change: 1 addition & 0 deletions examples.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in conf
# [ "halogen"
, "arrays"
, "arraybuffer"
, "console"
, "halogen-subscriptions"
, "identity"
, "nonempty"
Expand Down
13 changes: 10 additions & 3 deletions examples/atari-speaks/AtariSpeaks.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Data.UInt (toInt)
import Effect (Effect)
import Effect.Aff.Class (class MonadAff)
import Effect.Class (class MonadEffect)
import Effect.Console as Log
import FRP.Event (subscribe)
import Halogen as H
import Halogen.Aff (awaitBody, runHalogenAff)
Expand All @@ -28,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, decodeAudioDataFromUri, getByteFrequencyData, makeUnitCache)
import WAGS.Interpret (close, context, contextState, contextResume, decodeAudioDataFromUri, getByteFrequencyData, makeUnitCache)
import WAGS.Run (Run, RunAudio, RunEngine, SceneI(..), run)
import WAGS.WebAPI (AnalyserNode, AnalyserNodeCb, AudioContext, BrowserAudioBuffer)

Expand Down Expand Up @@ -115,6 +116,7 @@ main =
type State
=
{ unsubscribe :: Effect Unit
, unsubscribeFromHalogen :: Maybe H.SubscriptionId
, audioCtx :: Maybe AudioContext
, freqz :: Array String
}
Expand All @@ -136,6 +138,7 @@ initialState :: forall input. input -> State
initialState _ =
{ unsubscribe: pure unit
, audioCtx: Nothing
, unsubscribeFromHalogen: Nothing
, freqz: []
}

Expand All @@ -160,6 +163,9 @@ handleAction = case _ of
{ emitter, listener } <- H.liftEffect HS.create
unsubscribeFromHalogen <- H.subscribe emitter
audioCtx <- H.liftEffect context
-- just for kicks
H.liftEffect $ contextState audioCtx >>= Log.info
H.liftAff $ toAffE $ contextResume audioCtx
unitCache <- H.liftEffect makeUnitCache
atar <-
H.liftAff $ toAffE
Expand All @@ -183,10 +189,11 @@ handleAction = case _ of
HS.notify listener (Freqz ((map (\i -> unsafeRepeat (toInt i + 1) ">") arr)))
pure unit
)
H.modify_ _ { unsubscribe = unsubscribe, audioCtx = Just audioCtx }
H.modify_ _ { unsubscribe = unsubscribe, unsubscribeFromHalogen = Just unsubscribeFromHalogen, audioCtx = Just audioCtx }
Freqz freqz -> H.modify_ _ { freqz = freqz }
StopAudio -> do
{ unsubscribe, audioCtx } <- H.get
{ unsubscribe, unsubscribeFromHalogen, audioCtx } <- H.get
H.liftEffect unsubscribe
for_ unsubscribeFromHalogen H.unsubscribe
for_ audioCtx (H.liftEffect <<< close)
H.modify_ _ { unsubscribe = pure unit, audioCtx = Nothing }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "purescript-wags",
"version": "0.5.4",
"version": "0.5.5",
"description": "Web Audio Graphs as a Stream",
"scripts": {
"build": "spago build",
Expand Down
10 changes: 10 additions & 0 deletions src/WAGS/Interpret.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
exports.context = function () {
return new (window.AudioContext || window.webkitAudioContext)();
};
exports.contextState = function(audioCtx) {
return function() {
return audioCtx.state;
}
}
exports.contextResume = function(audioCtx) {
return function() {
return audioCtx.resume();
}
}
exports.makeUnitCache = function () {
return {};
};
Expand Down
8 changes: 8 additions & 0 deletions src/WAGS/Interpret.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module WAGS.Interpret
, close
, connectXToY
, context
, contextState
, contextResume
, decodeAudioDataFromBase64EncodedString
, decodeAudioDataFromUri
, defaultFFIAudio
Expand Down Expand Up @@ -215,6 +217,12 @@ foreign import makeFloatArray :: Array Number -> Effect WebAPI.BrowserFloatArray
-- | Make a new audio context.
foreign import context :: Effect WebAPI.AudioContext

-- | Get the state of the context
foreign import contextState :: WebAPI.AudioContext -> Effect String

-- | Get the state of the context
foreign import contextResume :: WebAPI.AudioContext -> Effect (Promise Unit)

-- | Close an audio context.
foreign import close :: WebAPI.AudioContext -> Effect Unit

Expand Down

0 comments on commit 4f03c7f

Please sign in to comment.