Skip to content

Commit

Permalink
audio worklet code in status/info display
Browse files Browse the repository at this point in the history
  • Loading branch information
dktr0 committed Nov 26, 2024
1 parent c862f2f commit f4b0610
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 222 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
gate 0.1 (maxp a (fb * 0.98)) >> add <> 5
</textarea>
</div>
<pre class="info" id="info" hidden="false"></pre>
<pre class="info" id="info" hidden="true"></pre>
<div class="status" id="status">
<div class="errors" id="errors"></div>
<div class="fps" id="fps">-- FPS</div>
Expand Down
388 changes: 196 additions & 192 deletions punctual.js

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions src/AudioPanning.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
module AudioPanning where

import Prelude ((>),($),pure,otherwise,(*),(<>),(/),(>>=),bind,(-),(+),(<=),map,(==),(<$>))
import Data.Maybe (Maybe(..))
import Data.Int (toNumber)
import Data.Number (pi,cos)
import Data.Ord (abs)
import Data.Either (Either(..))
import Data.List (take)
import Data.List.NonEmpty (head, length, zipWith, NonEmptyList, fromList, toList)
import Data.Unfoldable1 (iterateN)
import Data.Unfoldable (replicate)
import Data.List.NonEmpty (NonEmptyList, head, length, zipWith)
import Data.Unfoldable1 (iterateN,replicate1)
import Data.Traversable (traverse,sequence)
import Data.Tuple (Tuple(..))

Expand All @@ -29,14 +26,14 @@ splay nOutputChnls xs
xss <- sequence $ zipWith (pan nOutputChnls) inputPositions xs' -- :: NonEmptyList Frame -- one Frame per input, each Frame has nOutputChnls Samples
flatten <$> sumChannels xss

aout :: Int -> Int -> Int -> Frame -> W (NonEmptyList Sample)
aout totalOutputChnls nOutputChnls channelOffset xs = do
let a = replicate channelOffset zero -- :: List Sample
b <- toList <$> splay nOutputChnls xs -- :: List Sample
let c = replicate (totalOutputChnls - channelOffset - nOutputChnls) zero -- :: List Sample
case fromList (take totalOutputChnls $ a <> b <> c) of
Just x -> pure x
Nothing -> pure $ pure zero -- only possible with meaningless input (totalOutputChnls or nOutputChnls < 1)
aout :: Int -> Int -> Frame -> W (NonEmptyList Sample)
aout nOutputChnls channelOffset xs =
case channelOffset <= 0 of
true -> splay nOutputChnls xs
false -> do
let a = replicate1 channelOffset zero
b <- splay nOutputChnls xs
pure $ a <> b

pan :: Int -> Sample -> Sample -> W Frame
pan nOutputChnls pos i
Expand Down
8 changes: 4 additions & 4 deletions src/AudioWorklet.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module AudioWorklet where

import Prelude ((<>),Unit,pure,unit,discard,($),show,(>>=),(-),bind)
import Prelude ((<>),Unit,pure,unit,discard,($),show,(>>=),(-),bind,(+))
import Effect (Effect)
import Data.Nullable (Nullable,toMaybe)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
import Data.List.NonEmpty (length,zipWith)
import Data.List.NonEmpty (zipWith)
import Data.Unfoldable1 (range)
import Data.Foldable (fold)
-- import Effect.Class.Console (log)
Expand Down Expand Up @@ -49,7 +49,7 @@ foreign import setWorkletParamValue :: WebAudioNode -> String -> Number -> Effec
generateWorkletCode :: Signal -> Int -> Int -> String -> Number -> Number -> String
generateWorkletCode s nOutputChnls channelOffset name fInStart fInDur = prefix <> classHeader <> getParameterDescriptors <> constructor <> innerLoopPrefix <> fadeCalculations <> wState.code <> outputs <> restOfClass <> registerProcessor
where
Tuple frame wState = runW $ signalToFrame s >>= aout nOutputChnls nOutputChnls channelOffset
Tuple frame wState = runW $ signalToFrame s >>= aout nOutputChnls channelOffset
prefix = """'use strict';
function clamp(min,max,x) { return Math.max(Math.min(max,x),min); }
Expand Down Expand Up @@ -128,7 +128,7 @@ const sqr = this.sqr;
const tri = this.tri;
"""
fadeCalculations = "const fIn = clamp(0,1,(t-" <> show fInStart <> ")/" <> show fInDur <> ");\nconst fade = Math.min(fIn,fOut);\n"
outputIndices = range 0 (length frame - 1)
outputIndices = range 0 (nOutputChnls + channelOffset - 1)
outputF i x = "if(output[" <> show i <> "]!=null){output[" <> show i <> "][n] = " <> showSample x <> "*fade};\n"
outputs = fold $ zipWith outputF outputIndices frame
restOfClass = """}
Expand Down
14 changes: 10 additions & 4 deletions src/AudioZone.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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)
import Data.List (List(..),zipWith,length,catMaybes)
import Effect (Effect)
import Effect.Ref (Ref, new, read, write)
import Data.Foldable (fold)
import Data.Traversable (sequence,traverse,traverse_)
import Data.Unfoldable1 (replicate1)
import Data.DateTime (DateTime)
Expand Down Expand Up @@ -81,6 +82,11 @@ addWorklet sharedResources action t1 t2 = do
let channelOffset = audioOutputOffset action.output
runWorklet sharedResources.webAudioContext nAin sharedResources.internalAudioOutputNode ("W" <> show i) action.signal nOutputChnls channelOffset t1 (t2-t1)

calculateAudioZoneInfo :: AudioZone -> Effect String
calculateAudioZoneInfo z = do
ws <- catMaybes <$> read z.worklets
pure $ fold $ map (_.code >>> (_ <> "\n")) ws

-- to convert audio to POSIX, add clockdiff; to convert POSIX to audio, subtract clockdiff
calculateClockDiff :: SharedResources -> Effect Number
calculateClockDiff sharedResources = do
Expand All @@ -104,8 +110,8 @@ redefineAudioZone audioZone p = do
let actions' = extendByPadding Nothing n p.actions
clockDiff <- read audioZone.clockDiff
worklets'' <- sequence $ zipWith (addOrRemoveWorklet audioZone.sharedResources p.evalTime clockDiff) worklets' (map justAudioActions actions')
write worklets'' audioZone.worklets
write worklets'' audioZone.worklets

extendByPadding :: forall a. a -> Int -> List a -> List a
extendByPadding a n xs
| length xs >= n = xs
Expand Down
25 changes: 17 additions & 8 deletions src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import DateTime (numberToDateTime)
import SharedResources (SharedResources)
import SharedResources as SharedResources
import WebGL (WebGL, newWebGL, updateWebGL, deleteWebGL, drawWebGL)
import AudioZone (AudioZone,newAudioZone,redefineAudioZone,deleteAudioZone)
import AudioZone (AudioZone,newAudioZone,redefineAudioZone,deleteAudioZone,calculateAudioZoneInfo)
import WebAudio (WebAudioNode,WebAudioContext)

type Punctual = {
Expand Down Expand Up @@ -107,10 +107,14 @@ _newProgramInZone punctual zone newProgram = do
deleteWebGLForZone punctual zone
pure ""
-- update audio rendering system
case programHasAudioOutput newProgram of
true -> updateAudioForZone punctual zone newProgram
false -> deleteAudioForZone punctual zone
let debugInfo = newProgramDebug <> previousProgramDebug <> fragShaderDebug
audioDebugInfo <- case programHasAudioOutput newProgram of
true -> do
audioZoneInfo <- updateAudioForZone punctual zone newProgram
pure $ "audio worklets:\n" <> audioZoneInfo <> "\n\n"
false -> do
deleteAudioForZone punctual zone
pure ""
let debugInfo = newProgramDebug <> previousProgramDebug <> fragShaderDebug <> audioDebugInfo
-- log debugInfo
pure debugInfo

Expand Down Expand Up @@ -208,14 +212,19 @@ deleteWebGLForZone punctual z = do
write (delete z webGLs) punctual.webGLs
Nothing -> pure unit

updateAudioForZone :: Punctual -> Int -> Program -> Effect Unit
updateAudioForZone :: Punctual -> Int -> Program -> Effect String
updateAudioForZone punctual z prog = do
audioZones <- read punctual.audioZones
case lookup z audioZones of
Just x -> redefineAudioZone x prog
audioZone <- case lookup z audioZones of
Just x -> do
redefineAudioZone x prog
pure x
Nothing -> do
x <- newAudioZone punctual.sharedResources prog
write (insert z x audioZones) punctual.audioZones
pure x
calculateAudioZoneInfo audioZone


deleteAudioForZone :: Punctual -> Int -> Effect Unit
deleteAudioForZone punctual z = do
Expand Down
1 change: 1 addition & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ body {
box-shadow: none;
outline: none;
font-size: 1em;
overflow: scroll;
}

.editorArea {
Expand Down

0 comments on commit f4b0610

Please sign in to comment.