purescript-hylograph-music
Audio interpreter for Hylograph - data sonification (proof of concept)
Live interactive demo can be seen as part of the suite of Hylograph library demos. Click the screenshot below for Anscombe’s Quartet sonified.
What this is
interpretAudio walks a HATS Tree and
schedules one Web Audio note for each element in it, reading the
element’s attributes as sound: time, pitch,
duration, volume, timbre. A
forEach over data becomes a sequence of notes. It is a
third interpreter of the same tree type the DOM and English interpreters
read, with sound as its output.
What is published and what is not: the interpreter exists and
compiles, but nothing calls it yet. The Anscombe’s Quartet demo linked
above schedules its notes directly through this package’s Web Audio
bindings (scheduleNote) and draws its charts with Halogen
SVG. It is a sonification of the data, not yet a HATS chart played by
the audio interpreter.
Installation
spago install hylograph-musicMotivation
1. Accessibility
Data visualization is inherently visual, excluding: - Unsighted or visually impaired users - Users with visual attention occupied (pilots, drivers, surgeons) - Situations where visual displays are impractical
Audio provides an alternative modality for data comprehension. Just as a scatter plot reveals patterns spatially, a sonification can reveal patterns temporally and spectrally.
2. A tree is data, so it can be played
A HATS tree describes what to build and leaves the how to an
interpreter. The DOM interpreter turns it into SVG, the English
interpreter into text, and this one into sound. (This package began
against the earlier finally-tagless SelectionM API, which
is what older versions of this README described; the interpreter now
reads HATS trees.)
3. Foundation for Music DSL
While focused on sonification, this work explores patterns that could inform a future music composition DSL inspired by: - Tidal Cycles’ mini-notation - Modular synthesis patching philosophy - The data join pattern for separating temporal structure from musical content
Conceptual Mapping
A design sketch of the correspondence. What is implemented is the
five attribute names time, pitch,
duration, volume and timbre;
nothing maps cx or fill onto sound
automatically.
| D3/Visual Domain | Audio/Music Domain |
|---|---|
select "svg" |
Create audio context |
selectAll "circle" |
Create array of sound events |
| Data join | Bind data to sonic parameters |
attr "cx" (x position) |
Time offset (when to play) |
attr "cy" (y position) |
Pitch (frequency in Hz) |
attr "r" (radius) |
Duration or volume |
attr "fill" (color) |
Timbre (waveform shape) |
| Parent/child hierarchy | Sequential/parallel composition |
| Enter/Update/Exit | Sound onset/sustain/release |
Example
import Prelude
import Data.Array (mapWithIndex)
import Data.Int (toNumber)
import Effect (Effect)
import Hylograph.HATS (Tree, ElementType(..), elem, forEach, staticNum, staticStr)
import Hylograph.Music.Interpreter.WebAudio (initMusicContext, interpretAudio)
-- One note per datum: when it sounds, how high, how long.
melody :: Array Number -> Tree
melody values =
forEach "notes" Circle (mapWithIndex (\i v -> { i, v }) values) (show <<< _.i) \d ->
elem Circle
[ staticNum "time" (toNumber d.i * 0.4)
, staticNum "pitch" (220.0 + d.v * 20.0)
, staticNum "duration" 0.3
, staticStr "timbre" "sine"
] []
-- Call from a click handler: browsers only start audio on a user gesture.
play :: Effect Unit
play = initMusicContext (interpretAudio (melody [ 4.0, 7.0, 5.0, 9.0 ]))The interpreter reads attribute names, so a tree written for
the DOM (with cx, cy, r) plays as
default notes until it is given time and
pitch. Mapping visual attributes onto sound automatically,
as in the table above, is not implemented.
Anscombe’s Quartet
Anscombe’s Quartet demonstrates why this matters. Four datasets with identical statistics: - Same mean - Same variance - Same correlation
But when visualized, they look completely different. And when sonified, they would sound completely different: - Dataset 1: Linear progression (smooth ascending/descending melody) - Dataset 2: Quadratic (curved melodic contour) - Dataset 3: Linear with outlier (melody interrupted by jarring note) - Dataset 4: Vertical line with outlier (repeated note with one exception)
The patterns that vision reveals spatially, hearing reveals temporally and spectrally.
Use Cases
- Data Exploration - Listen to data distributions, outliers, trends
- Accessibility - Make charts and visualizations available to blind users
- Monitoring - Audio dashboards for system metrics (like Geiger counters for data)
- Multimodal Analysis - Use both vision and hearing simultaneously for richer understanding
- Education - Teach data literacy through multiple sensory modes
Architecture
hylograph-selection/ # HATS: the Tree type and its DOM interpreter
└── Hylograph/HATS.purs
hylograph-music/ # this package
└── Hylograph/Music/
├── Interpreter/WebAudio.purs # interpretAudio :: Tree -> MusicM Unit
├── Attributes.purs # attribute helpers for the older typed-attribute API
└── Internal/FFI.purs # Web Audio bindings (createAudioContext, scheduleNote)
Modules
Hylograph.Music.Interpreter.WebAudio- WebAudio interpreterHylograph.Music.Attributes- Sonic attribute mappingsHylograph.Music.Internal.FFI- Web Audio API bindings
Relationship to Music Composition DSL (Future)
This sonification work is distinct from but related to a potential music composition DSL. Key differences:
hylograph-music (this package): - Data → Sound (sonification) - Input: Arbitrary data arrays - Output: Audio representation of data patterns - Goal: Accessibility and data comprehension
Future Music DSL: - Musical ideas → Sound (composition) - Input: Musical structures (mini-notation, chord progressions, melodies) - Output: Music - Goal: Creative music making and generative composition
Shared patterns: - Typed data joins (structure + content) - Multiple interpreters of one description (audio, notation, visualization, English) - Separation of “what/when” (temporal structure) from “how” (sonic content)
Both benefit from exploring how the join pattern works in the temporal/auditory domain.
Status
Proof of concept. The Web Audio bindings work and are what the
Anscombe demo uses. The tree interpreter (interpretAudio)
compiles against the current HATS types and has no test and no demo that
calls it. Enter/update/exit have no audio meaning yet.
Part of Hylograph
- hylograph-music - Audio interpreter (this package)
- hylograph-selection - HATS trees and DOM rendering
- hylograph-simulation - Force simulation
- hylograph-graph - Graph algorithms
References
- Sonification Handbook - Academic resource on data sonification
- Web Audio API - Browser audio capabilities
- Tidal Cycles - Live coding music with mini-notation
- Strudel - Tidal in the browser (potential compilation target)
License
MIT