Skip to content

Getting Started

Installation

Install rescript-tone and its peer dependency tone via your package manager:

1npm install rescript-tone tone

Or with yarn:

1yarn add rescript-tone tone

ReScript 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 Tone
2
3// Create a synth and connect it to the speakers
4let synth = Synth.make()
5synth->Synth.asAudioNode->AudioNode.toDestination
6
7// Tone.js requires a user gesture to start audio
8let startAudio = async () => {
9 await Core.start()
10 synth->Synth.triggerAttackRelease("C4", "8n")
11}
Note

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 -> Destination
2let synth = Synth.make()
3let reverb = Reverb.make()
4
5synth->Synth.asAudioNode
6->AudioNode.connect(reverb->Reverb.asAudioNode)
7->AudioNode.toDestination

The 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.t
2synth->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 transport
2Transport.setBpm(120.0)
3Transport.start()
4
5// Schedule a callback
6Transport.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
Was this page helpful?