### Build Tone.js Project Examples (Shell) Source: https://github.com/tonejs/tonejs.github.io/blob/master/next/examples/README.md Install project dependencies and build the Tone.js examples using npm commands. This step is required before the examples can be served and viewed. ```Shell $ npm install ... $ npm run build ``` -------------------------------- ### Start Local HTTP Server for Examples (Shell) Source: https://github.com/tonejs/tonejs.github.io/blob/master/next/examples/README.md Use Python's built-in SimpleHTTPServer module to start a local web server on port 8000. This allows the built Tone.js examples to be accessed and viewed in a web browser. ```Shell $ python -m SimpleHTTPServer 8000 ``` -------------------------------- ### Install Dependencies and Build Project (Shell) Source: https://github.com/tonejs/tonejs.github.io/blob/master/examples/README.md Installs project dependencies using npm and then builds the project. This is a necessary step to prepare the examples for local viewing after cloning the repository. ```Shell $ npm install ... $ npm run build ``` -------------------------------- ### Start Local HTTP Server (Shell) Source: https://github.com/tonejs/tonejs.github.io/blob/master/examples/README.md Starts a simple HTTP server using Python's built-in module on port 8000. This allows you to serve the built examples locally from your browser at http://localhost:8000/examples. ```Shell $ python -m SimpleHTTPServer 8000 ``` -------------------------------- ### Starting Tone.js AudioContext on User Click Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Provides the recommended pattern for starting the Tone.js AudioContext in response to a user interaction, such as a button click. This is necessary because browsers require a user gesture to enable audio playback. ```JavaScript //attach a click listener to a play button document.querySelector("button")?.addEventListener("click", async () => { await Tone.start(); console.log("audio is ready"); }); ``` -------------------------------- ### Installing Tone.js via npm Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Installs the Tone.js library using the npm package manager. Provides options to install either the latest stable version or the 'next' development version. ```Bash npm install tone // Install the latest stable version npm install tone@next // Or, alternatively, use the 'next' version ``` -------------------------------- ### Scheduling Multiple Notes with triggerAttackRelease Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Illustrates scheduling multiple notes ('C4', 'E4', 'G4') at different times relative to `Tone.now()`. This demonstrates basic sequencing by offsetting the start time for each note using `triggerAttackRelease`. ```JavaScript const synth = new Tone.Synth().toDestination(); const now = Tone.now(); synth.triggerAttackRelease("C4", "8n", now); synth.triggerAttackRelease("E4", "8n", now + 0.5); synth.triggerAttackRelease("G4", "8n", now + 1); ``` -------------------------------- ### Loading and Playing Audio Sample with Tone.Player (JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Creates a Tone.Player instance, loading an audio file from a URL. It uses Tone.loaded() to wait for the audio buffer to load before starting playback, connecting the player directly to the destination. ```javascript const player = new Tone.Player( "https://tonejs.github.io/audio/berklee/gong_1.mp3" ).toDestination(); Tone.loaded().then(() => { player.start(); }); ``` -------------------------------- ### Automating Oscillator Frequency with Signal.rampTo (Tone.js, JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md This snippet demonstrates how to use the 'frequency' Signal property of a Tone.js Oscillator to create a smooth frequency ramp. It initializes an oscillator, sets a starting frequency, and then uses the 'rampTo' method to smoothly transition to a target frequency over a specified duration. ```javascript const osc = new Tone.Oscillator().toDestination(); // start at "C4" osc.frequency.value = "C4"; // ramp to "C2" over 2 seconds osc.frequency.rampTo("C2", 2); // start the oscillator for 2 seconds osc.start().stop("+3"); ``` -------------------------------- ### Logging AudioContext Time with Tone.now() Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Demonstrates how to retrieve the current time of the Web Audio AudioContext using `Tone.now()`. This example uses `setInterval` to log the time every 100 milliseconds. ```JavaScript setInterval(() => console.log(Tone.now()), 100); ``` -------------------------------- ### Scheduling Events with Tone.Transport and Tone.Loop (JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Creates two monophonic synths and schedules notes using Tone.Loop synchronized with the Tone.Transport. It shows how to start loops at specific times relative to the transport and how to control the transport's BPM. ```javascript // create two monophonic synths const synthA = new Tone.FMSynth().toDestination(); const synthB = new Tone.AMSynth().toDestination(); //play a note every quarter-note const loopA = new Tone.Loop((time) => { synthA.triggerAttackRelease("C2", "8n", time); }, "4n").start(0); //play another note every off quarter-note, by starting it "8n" const loopB = new Tone.Loop((time) => { synthB.triggerAttackRelease("C4", "8n", time); }, "4n").start("8n"); // all loops start when the Transport is started Tone.getTransport().start(); // ramp up to 800 bpm over 10 seconds Tone.getTransport().bpm.rampTo(800, 10); ``` -------------------------------- ### Creating Multi-Sample Instrument with Tone.Sampler (JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Initializes a Tone.Sampler with multiple sample URLs mapped to specific notes. It sets a base URL and release time. It uses Tone.loaded() to ensure samples are ready before triggering multiple notes simultaneously with a specified duration. ```javascript const sampler = new Tone.Sampler({ urls: { C4: "C4.mp3", "D#4": "Ds4.mp3", "F#4": "Fs4.mp3", A4: "A4.mp3", }, release: 1, baseUrl: "https://tonejs.github.io/audio/salamander/", }).toDestination(); Tone.loaded().then(() => { sampler.triggerAttackRelease(["Eb4", "G4", "Bb4"], 4); }); ``` -------------------------------- ### Creating and Playing a Basic Tone.Synth Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Demonstrates creating a simple Tone.Synth instance, connecting it to the main audio output, and playing a single note ('C4') for a specified duration ('8n') using the combined `triggerAttackRelease` method. ```JavaScript //create a synth and connect it to the main output (your speakers) const synth = new Tone.Synth().toDestination(); //play a middle 'C' for the duration of an 8th note synth.triggerAttackRelease("C4", "8n"); ``` -------------------------------- ### Creating and Playing Polyphonic Synth (JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Initializes a Tone.PolySynth with a basic Tone.Synth. It demonstrates triggering multiple notes simultaneously (triggerAttack) and releasing multiple notes simultaneously (triggerRelease) at specific times using Tone.now(). ```javascript const synth = new Tone.PolySynth(Tone.Synth).toDestination(); const now = Tone.now(); synth.triggerAttack("D4", now); synth.triggerAttack("F4", now + 0.5); synth.triggerAttack("A4", now + 1); synth.triggerAttack("C5", now + 1.5); synth.triggerAttack("E5", now + 2); synth.triggerRelease(["D4", "F4", "A4", "C5", "E5"], now + 4); ``` -------------------------------- ### Routing Audio Through Parallel Effects (JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Creates a Tone.Player with an audio file set to autostart. It then creates a Tone.Filter and a Tone.FeedbackDelay. The player's output is connected to both the filter and the delay simultaneously, routing the signal through both effects in parallel to the destination. ```javascript const player = new Tone.Player({ url: "https://tonejs.github.io/audio/drum-samples/loops/ominous.mp3", autostart: true, }); const filter = new Tone.Filter(400, "lowpass").toDestination(); const feedbackDelay = new Tone.FeedbackDelay(0.125, 0.5).toDestination(); // connect the player to the feedback delay and filter in parallel player.connect(filter); player.connect(feedbackDelay); ``` -------------------------------- ### Using triggerAttack and triggerRelease with Tone.Synth Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Shows how to control a note's envelope using separate `triggerAttack` and `triggerRelease` calls. The release is scheduled one second after the attack using the current AudioContext time obtained via `Tone.now()`. ```JavaScript const synth = new Tone.Synth().toDestination(); const now = Tone.now(); // trigger the attack immediately synth.triggerAttack("C4", now); // wait one second before triggering the release synth.triggerRelease(now + 1); ``` -------------------------------- ### Routing Audio Through Single Effect (JavaScript) Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Creates a Tone.Player with an audio file set to loop and autostart. It then creates a Tone.Distortion effect and connects the player's output to the distortion effect's input, which is connected to the destination. ```javascript const player = new Tone.Player({ url: "https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3", loop: true, autostart: true, }); //create a distortion effect const distortion = new Tone.Distortion(0.4).toDestination(); //connect a player to the distortion player.connect(distortion); ``` -------------------------------- ### Importing Tone.js in JavaScript Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Imports the Tone.js library into a JavaScript project using standard ES module syntax, making all Tone.js components available under the 'Tone' namespace. ```JavaScript import * as Tone from "tone"; ``` -------------------------------- ### Including Tone.js via CDN Source: https://github.com/tonejs/tonejs.github.io/blob/master/index.md Includes the Tone.js library directly in an HTML document using a script tag. This method fetches the library from a Content Delivery Network (CDN) like unpkg. ```HTML ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.