Skip to content

Commit

Permalink
changing audio output causes new audio worklet even when signal remai…
Browse files Browse the repository at this point in the history
…ns the same
  • Loading branch information
dktr0 committed Nov 27, 2024
1 parent 44116fb commit 56805dd
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 123 deletions.
226 changes: 113 additions & 113 deletions punctual.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/AudioWorklet.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import Signal (Signal)
import WebAudio (WebAudioContext,WebAudioNode)
import W
import AudioPanning (aout)
import Output (Output,audioOutputChannels,audioOutputOffset)

type AudioWorklet = {
name :: String,
signal :: Signal,
output :: Output,
code :: String,
audioWorklet' :: AudioWorklet'
}
Expand All @@ -27,11 +29,13 @@ type AudioWorklet' = {
audioWorkletNode :: Nullable WebAudioNode
}

runWorklet :: WebAudioContext -> Nullable WebAudioNode -> WebAudioNode -> String -> Signal -> Int -> Int -> Number -> Number -> Effect AudioWorklet
runWorklet ctx ain aout name signal nOutputChnls channelOffset fInStart fInDur = do
runWorklet :: WebAudioContext -> Nullable WebAudioNode -> WebAudioNode -> String -> Signal -> Int -> Output -> Number -> Number -> Effect AudioWorklet
runWorklet ctx ain aout name signal maxChnls output fInStart fInDur = do
let nOutputChnls = audioOutputChannels maxChnls output
let channelOffset = audioOutputOffset output
let code = generateWorkletCode signal nOutputChnls channelOffset name fInStart fInDur
audioWorklet' <- _runWorklet ctx ain aout name code (nOutputChnls+channelOffset)
pure { name, signal, code, audioWorklet' }
pure { name, signal, output, code, audioWorklet' }

foreign import _runWorklet :: WebAudioContext -> Nullable WebAudioNode -> WebAudioNode -> String -> String -> Int -> Effect AudioWorklet'

Expand Down
12 changes: 5 additions & 7 deletions src/AudioZone.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AudioZone where

import Prelude (Unit,map,bind,pure,($),discard,otherwise,(+),(<$>),(<>),show,(==),unit,max,(>=),(-),(<<<),(/),(>>>))
import Prelude (Unit,map,bind,pure,($),discard,otherwise,(+),(<$>),(<>),show,(==),unit,max,(>=),(-),(<<<),(/),(>>>),(&&))
import Data.Maybe (Maybe(..))
import Data.List (List(..),zipWith,length,catMaybes)
import Effect (Effect)
Expand All @@ -20,7 +20,7 @@ import SharedResources (SharedResources, activateAudioInput, getOutputChannelCou
import Program (Program)
import AudioWorklet (AudioWorklet,runWorklet,stopWorklet)
import Action (Action,actionTimesAsAudioTime,actionHasAudioInput)
import Output (isAudioOutput,audioOutputChannels,audioOutputOffset)
import Output (isAudioOutput)
import WebAudio (resumeWebAudioContext,currentTime)

type AudioZone = {
Expand Down Expand Up @@ -56,8 +56,8 @@ addOrRemoveWorklet sharedResources _ _ (Just prevWorklet) Nothing = do
stopWorklet prevWorklet (t+0.25) 0.1
pure Nothing
addOrRemoveWorklet sharedResources evalTime clockDiff (Just prevWorklet) (Just action) = do
case prevWorklet.signal == action.signal of
true -> pure (Just prevWorklet) -- no change in signal, maintain existing worklet
case (prevWorklet.signal == action.signal && prevWorklet.output == action.output) of
true -> pure (Just prevWorklet) -- no change in signal/output, maintain existing worklet
false -> do
tempo <- read sharedResources.tempo
let Tuple t1 t2 = actionTimesAsAudioTime tempo evalTime clockDiff action
Expand All @@ -78,9 +78,7 @@ addWorklet sharedResources action t1 t2 = do
log "worklet does not have audio input"
pure null
maxChnls <- getOutputChannelCount sharedResources
let nOutputChnls = audioOutputChannels maxChnls action.output
let channelOffset = audioOutputOffset action.output
runWorklet sharedResources.webAudioContext nAin sharedResources.internalAudioOutputNode ("W" <> show i) action.signal nOutputChnls channelOffset t1 (t2-t1)
runWorklet sharedResources.webAudioContext nAin sharedResources.internalAudioOutputNode ("W" <> show i) action.signal maxChnls action.output t1 (t2-t1)

calculateAudioZoneInfo :: AudioZone -> Effect String
calculateAudioZoneInfo z = do
Expand Down

0 comments on commit 56805dd

Please sign in to comment.