Getting Started
Installation
Install rescript-tone and its peer dependency tone via your package manager:
1npm install rescript-tone toneOr with yarn:
1yarn add rescript-tone toneReScript Configuration
Add rescript-tone to your bs-dependencies in rescript.json:
1{2 "bs-dependencies": [3 "rescript-tone"4 ]5}Your First Sound
All Tone.js modules are available under the Tone namespace. Here's how to play a simple note:
1open Tone23// Create a synth and connect it to the speakers4let synth = Synth.make()5synth->Synth.asAudioNode->AudioNode.toDestination67// Tone.js requires a user gesture to start audio8let startAudio = async () => {9 await Core.start()10 synth->Synth.triggerAttackRelease("C4", "8n")11}Browsers require a user interaction (click, tap) before audio can play. Always call Core.start() inside an event handler.
Core Concepts
Audio Nodes
Every sound-producing or sound-modifying module in Tone.js is an AudioNode. Nodes can be connected together to form a signal chain:
1// Synth -> Reverb -> Destination2let synth = Synth.make()3let reverb = Reverb.make()45synth->Synth.asAudioNode6->AudioNode.connect(reverb->Reverb.asAudioNode)7->AudioNode.toDestinationThe asAudioNode Pattern
Each module provides an asAudioNode function to cast its type to AudioNode.t. This is the common interface for connecting, disconnecting, and routing audio:
1// Every module can be cast to AudioNode.t2synth->Synth.asAudioNode->AudioNode.connect(...)3reverb->Reverb.asAudioNode->AudioNode.connect(...)4delay->FeedbackDelay.asAudioNode->AudioNode.connect(...)Transport
The Transport is Tone.js's main timekeeper. Use it to schedule events, set tempo, and control playback:
1// Set BPM and start the transport2Transport.setBpm(120.0)3Transport.start()45// Schedule a callback6Transport.schedule(~callback=_time => {7 synth->Synth.triggerAttackRelease("C4", "8n")8}, ~time="0")Requirements
- ReScript >= 12.0.0
- Tone.js >= 15.0.0
- A browser with Web Audio API support