@errozero/beatstepper
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -40,4 +40,4 @@ //Based on article: https://html5rocks.com/en/tutorials/audio/scheduling/ | ||
private tempoWorker: Worker; | ||
private scheduleAheadTime = 0.1; //How far ahead to schedule events (in seconds) | ||
private lookAhead = 25.0; //How frequently to call scheduling (in ms) | ||
private scheduleAheadTime = 0.18; //How far ahead to schedule events (in seconds) | ||
private lookAhead = 20.0; //How frequently to call scheduling (in ms) | ||
private nextStepTime = 0; | ||
@@ -130,5 +130,9 @@ private currentStep = 0; | ||
this.playing = false; | ||
this.tempoWorker.postMessage({message: 'pause'}); | ||
this.tempoWorker.postMessage({message: 'stop'}); | ||
} | ||
getStepsPerBeat(){ | ||
return this.stepsPerBeat; | ||
} | ||
setStepsPerBeat(steps:number){ | ||
@@ -139,2 +143,6 @@ this.stepsPerBeat = steps; | ||
getBeatsPerBar(){ | ||
return this.beatsPerBar; | ||
} | ||
setBeatsPerBar(beats:number){ | ||
@@ -145,2 +153,6 @@ this.beatsPerBar = beats; | ||
getTempo(){ | ||
return this.tempo; | ||
} | ||
setTempo(tempo:number){ | ||
@@ -147,0 +159,0 @@ this.tempo = tempo; |
@@ -7,5 +7,5 @@ { | ||
}, | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"main": "dist/Beatstepper.js", | ||
"scripts": { | ||
@@ -12,0 +12,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
116
README.md
@@ -1,1 +0,115 @@ | ||
Web audio event scheduler | ||
# Beatstepper | ||
This is a webaudio event scheduler inspired by the article: | ||
https://html5rocks.com/en/tutorials/audio/scheduling/ | ||
Supports different time divisions: 4/4 or 3/4 for example. | ||
## Usage | ||
Install Beatstepper: | ||
`npm install @errozero/beatstepper` | ||
Create an instance and pass in a web audio context, and a callback function: | ||
```javascript | ||
import Beatstepper from '@errozero/beatstepper'; | ||
const ctx = new AudioContext(); | ||
const callback = data => { | ||
console.log('Step!', data); | ||
}; | ||
const beatstepper= new Beatstepper(ctx, callback); | ||
``` | ||
Now that is setup, call the start method to begin scheduling: | ||
```javascript | ||
beatstepper.start() | ||
``` | ||
This will run the default tempo of 130bpm, with 4/4 time division. | ||
Your callback function will run on every step and receive an object with the following structure: | ||
```typescript | ||
{ | ||
step: number, //The current step, starting at 0, default max is 15 | ||
beat: number, //The current beat, starts at 0, default max is 3 | ||
bar: number, //Current bar, starts at 0, default max is 3 | ||
startTime: number, //The webaudio clock time that events for this step should start | ||
stepLength: number //Calculated length of one step, useful for sub-step timing | ||
} | ||
``` | ||
Your callback function could be used to trigger notes or samples from a pattern array etc. | ||
## Methods | ||
**start**\ | ||
Starts the clock | ||
```javascript | ||
beatstepper.start(); | ||
``` | ||
\ | ||
**stop**\ | ||
Stops the clock and resets the current step, beat and bar to 0 | ||
```javascript | ||
beatstepper.stop(); | ||
``` | ||
\ | ||
**pause**\ | ||
Pauses the clock | ||
```javascript | ||
beatstepper.pause(); | ||
``` | ||
\ | ||
**setTempo**\ | ||
Sets the tempo in bpm | ||
Param: `tempo:number` | ||
```javascript | ||
beatstepper.setTempo(160); | ||
``` | ||
\ | ||
**setStepsPerBeat**\ | ||
Sets the number of steps that make up a beat, default is 4. | ||
This works together with beatsPerBar to set the timing of the clock. | ||
It represents the first 4 in **4**/4. | ||
Param: `steps:number` | ||
```javascript | ||
beatstepper.setStepsPerBeat(4); | ||
``` | ||
\ | ||
**setBeatsPerBar**\ | ||
Sets the number of beats that make up a bar. | ||
This works together with stepPerBeat to set the timing of the clock. | ||
It represents the second 4 in 4/**4**. | ||
Param: `beats:number` | ||
```javascript | ||
beatstepper.setBeatsPerBar(4); | ||
``` | ||
## Metronome example | ||
```javascript | ||
import Beatstepper from '@errozero/beatstepper'; | ||
const ctx = new AudioContext(); | ||
const stepsPerBeat = 4; | ||
const tempo = 90; | ||
const callback = data => { | ||
const freq = (data.step % stepsPerBeat == 0) ? 880 : 440; | ||
const oscillator = context.createOscillator(); | ||
oscillator.type = 'square'; | ||
oscillator.frequency.setValueAtTime(freq, context.currentTime); // value in hertz | ||
oscillator.connect(context.destination); | ||
oscillator.start(data.startTime); | ||
oscillator.stop(data.startTime + (data.stepLength/2)); | ||
} | ||
const beatstepper = new Beatstepper(context, callback); | ||
beatstepper.setTempo(tempo); | ||
beatstepper.setStepsPerBeat(stepsPerBeat); | ||
beatstepper.start(); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23794
359
116