Formerly pd-andy/elm-web-audio. Search finds this package by either name.
The most general way to construct Web Audio nodes. This is used to create all the helper functions below.
You can use this function to define custom nodes by partially applying just the
type parameter. This is handy if you're using a library like Tone.js and want
to use those nodes in Elm.
import WebAudio
import WebAudio.Property exposing (Property)
omniOscillator : List Property -> List WebAudio.Node -> WebAudio.Node
omniOscillator =
WebAudio.node "Tone-OmniOscillatorNode"
myOsc : WebAudio.Node
myOsc =
omniOscillator
[ WebAudio.Property.frequency 440 ]
[ WebAudio.dac ]A ref node is used to refer to some other audio node by id. We can do things like create a feedback loop by connecting a node back into itself.
import WebAudio
import WebAudio.Property exposing (Property)
feedbackDelay : Float -> Float -> List WebAudio.Node -> WebAudio.Node
feedbackDelay amount time connections =
WebAudio.keyed "feedback-delay" <|
WebAudio.delay
[ WebAudio.Property.delayTime time ]
[ WebAudio.gain
[ WebAudio.Property.gain gain ]
-- Reference the delay node above by its key. This sends
-- the output of the gain node back into the delay node,
-- creating a feedback loop!
(WebAudio.ref "feedback-delay" :: connections)
]
synth : Float -> Float -> WebAudio.Node
synth freq gain =
WebAudio.oscillator
[ WebAudio.Parameters.frequency freq ]
[ WebAudio.gain
[ WebAudio.Parameters.gain gain ]
[ feedbackDelay 0.45 250 <|
[ WebAudio.dac ]
]
]Attach a key or id to an existing node. This is commonly used in conjunction
with ref nodes to create feedback loops and other more complex signal
graphs.
Typically we chain audio nodes together either implicitly by nesting them or
explicitly by using ref nodes but there is a third way to connect
audio nodes: by connecting them to certain audio params!
This is useful for common synthesis techniques like AM and FM synthesis, or modulating parameters like cutoff frequencer of a filter using an oscillator.
import WebAudio
import WebAudio.Property
audio : List WebAudio.Node
audio =
[ WebAudio.oscillator
[ WebAudio.Property.frequency 5 ]
-- Connect to the "frequency" parameter of the "carrier" node
[ WebAudio.param "carrier" "frequency" ]
, WebAudio.keyed "carrier" <|
WebAudio.oscillator []
[ WebAudio.dac ]
]Common properties:
See: https://developer.mozilla.org/en-US/docs/Web/API/OscillatorNode
An alias for oscillator.
It turns out oscillators are pretty common in lots of audio signal graphs. It
also turns out that "oscillator" is a pretty long word, so you can use osc
instead to save your fingers and your eyes.
The audio destination is... the destination of our audio! Connect to this node to get sound coming out of your speakers.
It doesn't have any parameters itself, and it doesn't make sense to connect the destination to anything else; it's the end of the line.
import WebAudio
import WebAudio.Property
audio : List WebAudio.Node
audio =
[ WebAudio.oscillator
[ WebAudio.Property.frequency 440 ]
[ WebAudio.audioDestination ]
]See: https://developer.mozilla.org/en-US/docs/Web/API/AudioDestinationNode
An alias for audioDestination. "dac" is another common name for an output:
it stands for digital to analog converter.
Common properties:
See: https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
Common properties:
See: https://developer.mozilla.org/en-US/docs/Web/API/ConvolverNode
Common properties:
See: https://developer.mozilla.org/en-US/docs/Web/API/WaveShaperNode
Represents a node in an audio processing singal graph. There are a handful of basic constructors:
nodefor creating typical audio nodes,keyedfor attaching a key or id to an existing node, andreffor referencing a node elsewhere in the graph by id.There are also a number of constructors for specific audio nodes, such as
oscillatorandgain. These constructors mirror the low-level nodes provided by the Web Audio API: they make great building blocks!