packages feed

musicw 0.3.6 → 0.3.7

raw patch · 4 files changed

+64/−10 lines, 4 files

Files

musicw.cabal view
@@ -1,5 +1,5 @@ name:                musicw-version:             0.3.6+version:             0.3.7 synopsis:            Sound synthesis library, to be used with GHCJS and Web Audio API description:         A library for sound synthesis, currently targeting GHCJS and the Web Audio API. Used by Inner Ear, Estuary, Punctual and possibly other projects. homepage:            https://github.com/dktr0/musicw/blob/master/README.md
src/Sound/MusicW/Node.hs view
@@ -530,3 +530,11 @@ foreign import javascript unsafe   "$1.startable"   js_startable :: Node -> Bool++foreign import javascript unsafe+  "$1.numberOfInputs"+  numberOfInputs :: Node -> Int++foreign import javascript unsafe+  "$1.numberOfOutputs"+  numberOfOutputs :: Node -> Int
src/Sound/MusicW/Synth.hs view
@@ -60,14 +60,41 @@   disconnectOnStop ns   return $ Synth { cachedDestination = dest, spec = x, nodes = ns } + makeConnections :: AudioIO m => Node -> [Node] -> NodeRef -> NodeRef -> m ()-makeConnections dest ns (NodeRef from (_,_)) DestinationRef = connectNodes (ns!!from) dest-makeConnections _ ns (NodeRef from (_,_)) (NodeRef to (_,_)) = connectNodes (ns!!from) (ns!!to)-makeConnections _ ns (NodeRef from (_,_)) (ParamRef to pType) = createParameter (ns!!to) pType >>= connectNodes (ns!!from)-makeConnections _ ns (NodeOutputRef fromNode fromChannel) (NodeRef to (_,_)) = connectNodes'' (ns!!fromNode) fromChannel (ns!!to)++-- connections to (deferred) destination+makeConnections dest ns (NodeRef from _) DestinationRef = connectNodes (ns!!from) dest+makeConnections dest ns (NodeOutputRef from fromIndex) DestinationRef = connectNodes'' (ns!!from) fromIndex dest+makeConnections dest ns (ExternalNodeRef from _) DestinationRef = connectNodes from dest+makeConnections dest ns (ExternalNodeOutputRef from fromIndex) DestinationRef = connectNodes'' from fromIndex dest++-- connections to NodeRef+makeConnections _ ns (NodeRef from _) (NodeRef to _) = connectNodes (ns!!from) (ns!!to)+makeConnections _ ns (NodeOutputRef from fromIndex) (NodeRef to _) = connectNodes'' (ns!!from) fromIndex (ns!!to)+makeConnections _ ns (ExternalNodeRef from _) (NodeRef to _) = connectNodes from (ns!!to)+makeConnections _ ns (ExternalNodeOutputRef from fromIndex) (NodeRef to _) = connectNodes'' from fromIndex (ns!!to)++-- connections to ExternalNodeRef+makeConnections _ ns (NodeRef from _) (ExternalNodeRef to _) = connectNodes (ns!!from) to+makeConnections _ ns (NodeOutputRef from fromIndex) (ExternalNodeRef to _) = connectNodes'' (ns!!from) fromIndex to+makeConnections _ ns (ExternalNodeRef from _) (ExternalNodeRef to _) = connectNodes from to+makeConnections _ ns (ExternalNodeOutputRef from fromIndex) (ExternalNodeRef to _) = connectNodes'' from fromIndex to++-- connections to ParamRef+makeConnections _ ns (NodeRef from _) (ParamRef to pType) = createParameter (ns!!to) pType >>= connectNodes (ns!!from) makeConnections _ ns (NodeOutputRef fromNode fromChannel) (ParamRef to pType) = createParameter (ns!!to) pType >>= connectNodes'' (ns!!fromNode) fromChannel-makeConnections _ ns (NodeOutputRef fromNode fromChannel) (NodeInputRef toNode toChannel) = connectNodes' (ns!!fromNode) fromChannel (ns!!toNode) toChannel-makeConnections _ _ _ _ = error "Malformed graph structure."+makeConnections _ ns (ExternalNodeRef from _) (ParamRef to pType) = createParameter (ns!!to) pType >>= connectNodes from+makeConnections _ ns (ExternalNodeOutputRef from fromIndex) (ParamRef to pType) = createParameter (ns!!to) pType >>= connectNodes'' from fromIndex++-- connections to NodeInputRef (note that connections from NodeRef/ExternalNodeRef not supported)+makeConnections _ ns (NodeOutputRef from fromIndex) (NodeInputRef to toIndex) = connectNodes' (ns!!from) fromIndex (ns!!to) toIndex+makeConnections _ ns (ExternalNodeOutputRef from fromIndex) (NodeInputRef to toIndex) = connectNodes' from fromIndex (ns!!to) toIndex++-- connections to ExternalNodeInputRef (note that connections from NodeRef/ExternalNodeRef not supported)+makeConnections _ ns (NodeOutputRef from fromIndex) (ExternalNodeInputRef to toIndex) = connectNodes' (ns!!from) fromIndex to toIndex+makeConnections _ ns (ExternalNodeOutputRef from fromIndex) (ExternalNodeInputRef to toIndex) = connectNodes' from fromIndex to toIndex+  -- *** Note: there is probably a bug connected to the definition of disconnectOnStop below: -- it attaches an onended callback to a single source node from a list of nodes, which doesn't
src/Sound/MusicW/SynthDef.hs view
@@ -11,7 +11,10 @@ data NodeRef   = NodeRef Int (Int,Int) -- tuplet is number of input and output channels for this node   | NodeInputRef Int Int -- for a numbered node, a specific numbered input of that node-  | NodeOutputRef Int Int -- for a numbered node, a specific numbered input of that node+  | NodeOutputRef Int Int -- for a numbered node, a specific numbered output of that node+  | ExternalNodeRef Node (Int,Int) -- a node that prexists a synth instantiation, not disconnected etc when synth ends+  | ExternalNodeInputRef Node Int -- for an external node, a specific numbered input of that node+  | ExternalNodeOutputRef Node Int -- for an external node, a specific numbered output of that node   | ParamRef Int ParamType   | DestinationRef   deriving (Show)@@ -20,6 +23,9 @@ nodeRefInputCount (NodeRef _ (i,_)) = i nodeRefInputCount (NodeInputRef _ _) = 1 nodeRefInputCount (NodeOutputRef _ _) = 0+nodeRefInputCount (ExternalNodeRef _ (i,_)) = i+nodeRefInputCount (ExternalNodeInputRef _ _) = 1+nodeRefInputCount (ExternalNodeOutputRef _ _) = 0 nodeRefInputCount (ParamRef _ _) = 1 nodeRefInputCount DestinationRef = 2 -- hard-coding of stereo output here might be problematic or irrelevant... @@ -27,6 +33,9 @@ nodeRefOutputCount (NodeRef _ (_,o)) = o nodeRefOutputCount (NodeInputRef _ _) = 0 nodeRefOutputCount (NodeOutputRef _ _) = 1+nodeRefOutputCount (ExternalNodeRef _ (_,o)) = o+nodeRefOutputCount (ExternalNodeInputRef _ _) = 0+nodeRefOutputCount (ExternalNodeOutputRef _ _) = 1 nodeRefOutputCount (ParamRef _ _) = 0 nodeRefOutputCount DestinationRef = 0 @@ -73,8 +82,12 @@ connect :: Monad m => NodeRef -> NodeRef -> SynthDef m () connect from to = modify $ \s -> s { connections = connections s ++ [(from,to)] } +-- given two NodeRefs that are either NodeRef or ExternalNodeRef, connect specific inputs and outputs of them to eachother connect' :: Monad m => NodeRef -> Int -> NodeRef -> Int -> SynthDef m ()-connect' (NodeRef fromNode (_,_)) fromIndex (NodeRef toNode (_,_)) toIndex = connect (NodeOutputRef fromNode fromIndex) (NodeInputRef toNode toIndex)+connect' (NodeRef from _) fromIndex (NodeRef to _) toIndex = connect (NodeOutputRef from fromIndex) (NodeInputRef to toIndex)+connect' (NodeRef from _) fromIndex (ExternalNodeRef to _) toIndex = connect (NodeOutputRef from fromIndex) (ExternalNodeInputRef to toIndex)+connect' (ExternalNodeRef from _) fromIndex (NodeRef to _) toIndex = connect (ExternalNodeOutputRef from fromIndex) (NodeInputRef to toIndex)+connect' (ExternalNodeRef from _) fromIndex (ExternalNodeRef to _) toIndex = connect (ExternalNodeOutputRef from fromIndex) (ExternalNodeInputRef to toIndex) connect' _ _ _ _ = error "unsupported connection type in connect'"  addChange :: Monad m => Change -> SynthDef m ()@@ -182,7 +195,13 @@ audioIn = addNodeBuilder (0,1) $ createGain 1.0 -- TODO: hard-coded single input channel will be problematic later  microphone :: AudioIO m => SynthDef m NodeRef-microphone = addNodeBuilder (0,1) createMicrophone +microphone = addNodeBuilder (0,1) createMicrophone++externalNode :: AudioIO m => Node -> SynthDef m NodeRef+externalNode x = do+  let iChnls = numberOfInputs x+  let oChnls = numberOfOutputs x+  return $ ExternalNodeRef x (iChnls,oChnls)  resink :: AudioIO m => NodeRef -> NodeRef -> SynthDef m NodeRef resink target input = connect input target >> return target