Skip to content

Commit

Permalink
Improves 0 timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Solomon committed Jan 1, 2022
1 parent 890a910 commit bd107c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.6.4] - 2021-12-15

### Fixed

- Makes `run` start at 0.0 seconds all the time, allowing for smoother starts of works. Additional optimizations can be done, like graph pre-rendering for the 0 time frame (not sure if this is in fact an optimization, could even be slower, but theoretically it would optimize things), but this sounds good enough on first blush to leave it as-is for the time being.

## [0.6.3] - 2021-12-15

### Fixed
Expand Down
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.6.3",
"version": "0.6.4",
"description": "Web Audio Graphs as a Stream",
"scripts": {
"build": "spago build",
Expand Down
23 changes: 13 additions & 10 deletions src/WAGS/Run.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ module WAGS.Run

import Prelude

import Control.Alt (alt)
import Control.Comonad.Cofree (Cofree, head, tail)
import Data.DateTime.Instant (Instant, unInstant)
import Data.Foldable (for_)
import Data.Int (floor, toNumber)
import Data.List (List(..))
import Data.Maybe (Maybe(..), isNothing)
import Data.Maybe (Maybe(..), fromMaybe, isNothing)
import Data.Newtype (class Newtype)
import Data.Symbol (class IsSymbol)
import Data.Time.Duration (Milliseconds)
Expand Down Expand Up @@ -166,7 +167,7 @@ run' :: forall analysersRL analysers analyserCallbacks analyserRefs trigger worl
run' loop trigger inWorld engineInfo audioInfo scene =
makeEvent \k -> do
refsForAnalysers <- makeAnalyserRefs (Proxy :: _ analysersRL)
audioClockStart <- getAudioClockTime (contextFromSnapshot audioInfo)
audioClockStartRef <- Ref.new (Nothing :: Maybe Number)
currentTimeoutCanceler <- Ref.new (pure unit :: Effect Unit)
currentScene <- Ref.new scene
currentEasingAlg <- Ref.new engineInfo.easingAlgorithm
Expand Down Expand Up @@ -195,7 +196,7 @@ run' loop trigger inWorld engineInfo audioInfo scene =
cancelTimeout <- Ref.read currentTimeoutCanceler
cancelTimeout
runInternal
audioClockStart
audioClockStartRef
ee
newWorld
currentTimeoutCanceler
Expand All @@ -211,7 +212,7 @@ run' loop trigger inWorld engineInfo audioInfo scene =
unsubscribe
where
runInternal
:: Number
:: Ref.Ref (Maybe Number)
-> { world :: world
, trigger :: Maybe trigger
, sysTime :: Milliseconds
Expand All @@ -228,12 +229,11 @@ run' loop trigger inWorld engineInfo audioInfo scene =
-> { | analyserRefs }
-> (Run res analysers -> Effect Unit)
-> Effect Unit
runInternal audioClockStart fromEvents world' currentTimeoutCanceler currentEasingAlg currentScene ffiSnapshot analyserCallbacks analyserRefs reporter = do
runInternal audioClockStartRef fromEvents world' currentTimeoutCanceler currentEasingAlg currentScene ffiSnapshot analyserCallbacks analyserRefs reporter = do
easingAlgNow <- Ref.read currentEasingAlg
sceneNow <- Ref.read currentScene
let ctx = contextFromSnapshot ffiSnapshot
audioClockPriorToComputation <- getAudioClockTime ctx
let
ctx = contextFromSnapshot ffiSnapshot
-- this is how far in the future we are telling the
-- algorithm to calculate with respect to the audio clock
-- it is a bet: we bet that the algorithm will finish
Expand All @@ -242,7 +242,11 @@ run' loop trigger inWorld engineInfo audioInfo scene =

headroomInSeconds = (toNumber headroom) / 1000.0

time = (audioClockPriorToComputation - audioClockStart) + headroomInSeconds
audioClockPriorToComputation <- getAudioClockTime ctx
audioClockStart <- fromMaybe audioClockPriorToComputation
<$> Ref.modify (flip alt (Just audioClockPriorToComputation)) audioClockStartRef
let
time = audioClockPriorToComputation - audioClockStart

fromScene =
oneFrame sceneNow
Expand All @@ -264,7 +268,6 @@ run' loop trigger inWorld engineInfo audioInfo scene =
renderAudio (map snd applied)
let
remainingTimeInSeconds = audioClockPriorToComputation + headroomInSeconds - audioClockAfterComputation

remainingTime = floor $ 1000.0 * remainingTimeInSeconds
Ref.write (tail easingAlgNow remainingTime) currentEasingAlg
Ref.write
Expand All @@ -289,7 +292,7 @@ run' loop trigger inWorld engineInfo audioInfo scene =
\{ world
, sysTime
} ->
runInternal audioClockStart
runInternal audioClockStartRef
{ world
, sysTime
, trigger: Nothing
Expand Down

0 comments on commit bd107c6

Please sign in to comment.