npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dittytoy

v1.0.7

Published

Compile and play code (ditties) from [Dittytoy.net](https://dittytoy.net), an online platform that allows you to create generative music using a minimalistic javascript API. Zero dependencies.

Downloads

950

Readme

Dittytoy

Compile and play code (ditties) from Dittytoy.net, an online platform that allows you to create generative music using a minimalistic javascript API. Zero dependencies.

The API syntax of Dittytoy is loosely based on the syntax of Sonic Pi. You can find the full Dittytoy API Reference here.

Demo

This is a build from the repository's example/ directory. To start playback, press the play button at the top left of your screen.

Getting started

Installing

Add dittytoy to your project:

npm i dittytoy

Basic usage

Compile a ditty and play.

import {Dittytoy} from 'dittytoy';

const dittytoy = new Dittytoy();

dittytoy.compile(`
ditty.bpm = 120;

loop( () => {

    for (let i=0; i<4; i++) {
        sine.play(c4, { attack: 0.01, release: 0.25,  duration: 0.125, pan: Math.random() * 2 - 1, amp: 1.0 });
        sleep( 0.25 );
    }

    sine.play(d4, { attack: 0.01, release: 0.25,  duration: 0.25 }); // attack and release in seconds, duration in ticks
    sleep(0.5); // sleep in ticks

    sine.play(f4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);

}, { name: 'my first loop' });
`).then(() => {
  dittytoy.play();
})

Note: Most browsers only allow audio after a user interacts with it. You should use the play method to start the audio after a user interaction.

Controlling playback

You can control the playback of the ditty using the following methods:

dittytoy.play(); // start playing
dittytoy.pause(); // pause playing
dittytoy.stop(); // stop playing
dittytoy.resume(); // resume playing

Change the volume

You can change the volume of the ditty using the setVolume method.

// set the volume to 50%
dittytoy.setVolume({master: {amp: 0.5}}); 

It is also possible to set the volume of two separate loops using the same method.

// set the volume of loop1 to 50% and loop2 to 75%
dittytoy.setVolume({loops: [{name: loop1, amp: 0.5}, {name: loop2, amp: 0.75}]});

Set Input Parameters

Dittytoy allows you to set input parameters for the ditty using the setInputParameters method. For example, to set two parameters, threshold and gain, to -15 and 4, respectively, you can use the following code:

dittytoy.setInputParameters([{key: 'threshold', value: -15}, {key: 'gain', value: 4}]);

Events

Dittytoy emits events you can listen to by subscribing to the addListener method. For example, to listen to the MSG_PLAY event, you can use the following code:

dittytoy.addListener(MSG_PLAY, () => {
  console.log('Dittytoy starts playing');
});

Initialization

The MSG_INIT event is emitted when the ditty is compiled successfully and ready to play.

dittytoy.addListener(MSG_INIT, (data:any) => {
  console.log('Dittytoy is initialized, ready to play');
  console.log('Structure of compiled ditty:', data.structure);
});

Playback

During playback, the MSG_UPDATE event is emitted each time the ditty is updated. This will be ~60 times per second.

dittytoy.addListener(MSG_UPDATE, (data:any) => {
  // data.amp contains information about the volume of the ditty and the separate loops
  const state = data.state;
  if (state) {
    console.log(`tick: ${(state.tick || 0).toFixed(3)}, time: ${(state.time || 0).toFixed(3)} (${state.bpm.toFixed(0)} bpm)`);
  }
});

Each time a note is played, the MSG_NOTE_PLAYED event is emitted.

dittytoy.addListener(MSG_NOTE_PLAYED, (data:any) => {
  console.log(`♪ tick: ${data.tick.toFixed(3)}, note: ${data.note} (${data.loop}.${data.synth})`);
});

Logging

Different messages are emitted using the MSG_LOG and MSG_ERROR events.

dittytoy.addListener(MSG_LOG, (data: any) => {
  console.log(data.message);
});

dittytoy.addListener(MSG_ERROR, (data: any) => {
  console.error(data.message);
});

Flow

Finally, the MSG_PLAY, MSG_PAUSE, MSG_STOP, and MSG_RESUME events are emitted when the ditty is played, paused, stopped, or resumed.

dittytoy.addListener(MSG_PLAY, () => {
  console.log('play');
});
dittytoy.addListener(MSG_PAUSE, () => {
  console.log('pause');
});
dittytoy.addListener(MSG_STOP, () => {
  console.log('stop');
});
dittytoy.addListener(MSG_RESUME, () => {
  console.log('resume');
});

Building

To build Dittytoy, ensure that you have Git and Node.js installed.

Clone a copy of the repo:

git clone https://github.com/reindernijhoff/dittytoy-package.git

Change to the dittytoy directory:

cd dittytoy-package

Install dev dependencies:

npm i

Build package:

npm run build