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

react-drum-machine

v1.8.0

Published

react drum machine

Downloads

23

Readme

React drum machine Build Status Dependency Status devDependency Status

Drum machine made with :heart: and :muscle:.

Can be used in ReactJS applications.

Supports browsers with web audio

How to use it ?

NPM

With npm:

npm install --save react-drum-machine

Then, you can use a module bundler like webpack or browserify to import package

// using an ES6 transpiler, like babel
import ReactDrumMachine from 'react-drum-machine'

// not using an ES6 transpiler
var ReactDrumMachine = require('react-drum-machine');

UMD

The UMD build is also available on unpkg:

<script src="https://unpkg.com/react-drum-machine/lib/react-drum-machine.min.js"></script>

You can find the library on window.ReactDrumMachine.

Also you have to import React, ReactDOM and perhaps babel-polyfill from CDNs.

<head>
...
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.9.1/polyfill.js"></script>
<script src="https://fb.me/react-15.2.0.js"></script>
<script src="https://fb.me/react-dom-15.2.0.js"></script>
<script src="https://unpkg.com/react-drum-machine/lib/react-drum-machine.min.js"></script>
...
</head>

To use JSX syntax, you can use Babel

Test it now !

Play with drum machine easily with this JSBin

Let's code

Here is a simple react app using ES6 module and syntax (Babel, webpack or browserify can help you)

import React from 'react';
import ReactDOM from 'react-dom';

import DrumMachine from 'react-drum-machine';

const song = {
	"title": "example",
	"beatpermeasure": 4,
	"bpm": 79,
	"divisionperbeat": 4,
	"instruments": [
  	{
		"title": "hihat",
		"image": "img/hihat.png",
		"sound": "https://content.dropboxapi.com/1/files/auto/CyCdh_K3ClHat-01.wav",
		"bearer": "JfnDpAnZcQ8AAAAAAAABYbt6Zvq6-U10DeFgzcZEbz7XYZrTv9ugPuuRl0ai9BFR",
		"bits": [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]
  	},
  	{
		"title": "snare",
		"image": "http://i.imgur.com/NwDw9lZ.png",
		"sound": "https://content.dropboxapi.com/1/files/auto/snare.mp3",
		"bearer": "JfnDpAnZcQ8AAAAAAAABYbt6Zvq6-U10DeFgzcZEbz7XYZrTv9ugPuuRl0ai9BFR",
		"bits": [0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1]
  	},
  	{
		"title": "kick",
		"image": "http://i.imgur.com/CmsdE9k.png",
		"sound": "https://content.dropboxapi.com/1/files/auto/kick.mp3",
		"bearer": "JfnDpAnZcQ8AAAAAAAABYbt6Zvq6-U10DeFgzcZEbz7XYZrTv9ugPuuRl0ai9BFR",
		"bits": [1,1,1,1,0,1,0,1,0,0,0,0,0,1,1,1,1]
  	}
  ]
}

ReactDOM.render(
	<div>
		<button onClick={() => PubSub.publish('drum',{action:'play'})}>Play</button>
		<button onClick={() => PubSub.publish('drum',{action:'stop'})}>Stop</button>
		<DrumMachine song={song} />
	</div>,
	document.getElementById('main')
);

Ensure you have an element with id main in your html file

Run demo

  1. Clone repository git clone https://github.com/studiolasso/react-drum-machine.git

  2. Install dependencies cd react-drum-machine && npm i && cd demo && npm i

  3. Execute demo npm start

API

<ReactDrumMachine />

Drum machine component. Display and play songs

Props

song

An object representing the song. Here is an example.

Song structure :

{
	"title": <String>, // song title
	"beatpermeasure": <Number>, // beats per measure
	"bpm": <Number>,
	"divisionperbeat": <Number>, // divisions per beat
	"instruments": [
	{
		"title": <String>, // instrument name
		"image": <String>, // image url, optional
		"sound": <String>, // sound url
		"bearer": <String> // token to get sound, optional,
		"bits": <[<Number>]> // array of bits (0 or 1)
	}]
}

After component is mounted, song can be changed.

divisionSize

Size in pixel of a division. Default is 20 pixels.

id

Drum machine can be controlled with publish/subscribe mechanism.

If you have multiple instances of drum machine, you can define an id for each one and target a specific instance when you publish an action. Here is an example :

<ReactDrumMachine id="dm1" />
<ReactDrumMachine id="dm2" />

...

PubSub.publish('dm1', {action:'play'});
PubSub.publish('dm2', {action:'stop'});

onLoaded

Callback triggered when <ReactDrumMachine /> is mounted. It passes 3 arguments :

  • state: actual state of the component. Read reducers code to have more details on structure
  • actions: all actions. Useful to interact with drum machine.
  • infoAPI: a set of methods to get informations about elapsed time, current beat, song size, etc ...
    • getElapsedTime() : returns elapsed time in seconds
    • getCurrentBeat() : returns current beat
    • getCurrentDivision() : returns current division
    • elapsedTimeToSize() : returns convertion of elapsed time to pixels
    • sizeToTime(<Number>) : returns convertion of size in argument into seconds
    • getSongSize() : returns size of song in pixels

onChange

Callback triggered when state changes. It passes state in argument.

PubSub

publish/subscribe object to trigger drum machine actions. Here is some examples:

// trigger action play
PubSub.publish('drum',{action:'play'});

// trigger action stop
PubSub.publish('drum',{action:'stop'});

First argument of PubSub.publish() helps to target drum machine instances. <ReactDrumMachine /> have 'drum' as default id. It can specified using id props (ex: <ReactDrumMachine id="dm1" />)

Second argument of PubSub.publish() take a object like this :

{
	action: <string> // action method name
	args: <object> // action payload 
}

Read reducers code to have details on payload sturctures

License

This project is licensed under the GPL3 license