Skip to content

Commit

Permalink
audio analysis pathways probably all restored to the point where all …
Browse files Browse the repository at this point in the history
…0.4 functionality in Estuary works the same way with 0.5
  • Loading branch information
dktr0 committed Nov 6, 2024
1 parent c27d40f commit 313adf5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 68 deletions.
126 changes: 63 additions & 63 deletions punctual.js

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions src/AudioAnalyser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import WebAudio

type AudioAnalyser = {
webAudioContext :: WebAudioContext,
sourceNode :: WebAudioNode,
sourceNode :: Ref WebAudioNode,
mAnalyserNode :: Ref (Maybe WebAudioNode),
analyserArray :: AnalyserArray,
lo :: Ref Number,
Expand All @@ -21,7 +21,8 @@ type AudioAnalyser = {
}

newAudioAnalyser :: WebAudioContext -> WebAudioNode -> Effect AudioAnalyser
newAudioAnalyser webAudioContext sourceNode = do
newAudioAnalyser webAudioContext n = do
sourceNode <- new n
mAnalyserNode <- new Nothing
analyserArray <- _analyserArray 512
lo <- new 0.0
Expand All @@ -43,7 +44,8 @@ _disactivateAnalysis a = do
case mAnalyserNode of
Nothing -> pure unit -- analysis is not currently active, so nothing more to do
Just analyserNode -> do -- disactivate
disconnect a.sourceNode analyserNode
sourceNode <- read a.sourceNode
disconnect sourceNode analyserNode
write Nothing a.mAnalyserNode
log "punctual: disactivating an audio analyser..."

Expand All @@ -54,7 +56,8 @@ _activateAnalysis a = do
Just analyserNode -> pure analyserNode -- analysis is already active, so nothing more to do
Nothing -> do -- analysis is not active, so need to make new source and analyser nodes
analyserNode <- _analyserNode a.webAudioContext 1024 0.5
connect a.sourceNode analyserNode
sourceNode <- read a.sourceNode
connect sourceNode analyserNode
write (Just analyserNode) a.mAnalyserNode
log "punctual: activating an audio analyser..."
pure analyserNode
Expand All @@ -76,6 +79,17 @@ updateAnalyser a needs = do
when (unwrap needs.hi) $ do
x <- _getHi a.analyserArray
write x a.hi

setSourceNode :: AudioAnalyser -> WebAudioNode -> Effect Unit
setSourceNode a newSourceNode = do
mAnalyserNode <- read a.mAnalyserNode
case mAnalyserNode of
Just analyserNode -> do
oldSourceNode <- read a.sourceNode
disconnect oldSourceNode analyserNode
connect newSourceNode analyserNode
Nothing -> pure unit
write newSourceNode a.sourceNode

foreign import data AnalyserArray :: Type

Expand Down
4 changes: 3 additions & 1 deletion src/SharedResources.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Data.Newtype (unwrap)

import WebGLCanvas (WebGLCanvas, WebGLContext, WebGLTexture)
import WebAudio
import AudioAnalyser (AudioAnalyser,newAudioAnalyser,updateAnalyser)
import AudioAnalyser (AudioAnalyser,newAudioAnalyser,updateAnalyser,setSourceNode)
import Value (LibraryCache)

type URL = String
Expand Down Expand Up @@ -217,12 +217,14 @@ disactivateAudioInput sr = do
log "punctual audio input disactivated"

-- called externally to use audio output other than the audio context destination
-- note: cannot be set to destination of audio context (since that can't be a source for audio analysis)
setAudioOutput :: SharedResources -> WebAudioNode -> Effect Unit
setAudioOutput sr newExternalAudioOutputNode = do
externalAudioOutputNode <- read sr.externalAudioOutputNode
disconnect sr.internalAudioOutputNode externalAudioOutputNode
connect sr.internalAudioOutputNode newExternalAudioOutputNode
write newExternalAudioOutputNode sr.externalAudioOutputNode
setSourceNode sr.outputAnalyser newExternalAudioOutputNode


-- Brightness
Expand Down

0 comments on commit 313adf5

Please sign in to comment.