beattool
Sync scripts to your phat beats in javascript with howlerjs and web worker timers.
AudioSync Class Documentation
Overview
npm i beattool
import {AudioSync} from 'beattool
The beattool
library is a tool for synchronizing audio playback with events on specific time intervals and signatures. Utilizing the Howler.js library for audio control and a web-worker hack for browser timers to keep events synced, this class enables the creation of dynamic audio experiences in web applications.
Methods
BeatSettings
Defines the settings for beat synchronization, including intervals, tempo, and callbacks for beat events.
type BeatSettings = {
interval: Timing;
tempo?: number;
onBeat?: (sound: any, now: number, startTime: number, intervals: any) => void;
} & {
[key:string]:{
interval: Timing;
tempo?: number;
duration?: number;
onBeat: (sound: any, now: number, startTime: number, intervals: any) => void;
repeatEvery?: number;
repeatUntil?: number;
}|any
};
constructor
import { AudioSync, BeatSettings } from "beattool";
const beatSettings = {
interval: '4/4',
tempo:144,
100:{
interval:'1/16',
tempo:144,
onBeat:() => {
console.timeEnd('da');
console.time('da');
toggleBeatColor();
},
duration:1200,
repeatEvery:1600,
repeatUntil:3800
},
'00:05.800':{
interval: '1/4',
onBeat:(sound,now,startTime)=>{
if(b) console.timeEnd('dum');
console.time('dum'); b = true;
toggleBeatColor()
}
}
} as BeatSettings;
const audioSync = new AudioSync(
['./entertainer.mp3'],
beatSettings,
true
);
init()
Initializes or re-initializes the audio synchronization settings.
audioSync.init(src, settings, playAll);
Parameters are the same as the constructor.
play()
Begins audio playback, optionally at a specific sound ID.
audioSync.play(onplay, id);
- onplay (optional): A callback function to be executed when playback starts.
- id (optional): The sound ID to play. If omitted, all sounds are played.
stop()
Stops audio playback, optionally for a specific sound ID.
audioSync.stop(onstop, id);
- onstop (optional): A callback function to be executed when playback stops.
- id (optional): The sound ID to stop. If omitted, all sounds are stopped.
type Timing = number|`${number}/${number}`;
The rest is just using howlerjs normally, so you can dig into that at will. The audioSync will follow the song start/stop/end events and re-arm itself for you.
Example index.ts
import { AudioSync, BeatSettings } from "beattool";
document.body.insertAdjacentHTML('beforeend', `
Press F12 for timer results<br/>
<canvas id="beat-visualization" width="200" height="200" style="border:1px solid #000; margin: 5px;"></canvas>
<hr/>
<button id="play-button">Play</button>
<button id="stop-button">Stop</button>
`);
const canvas = document.getElementById('beat-visualization') as HTMLCanvasElement;
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
function toggleBeatColor() {
if(ctx.fillStyle === '#ff0000') {
ctx.fillStyle = '#00ff00';
} else if (ctx.fillStyle === '#00ff00') {
ctx.fillStyle = '#0000ff';
} else ctx.fillStyle = '#ff0000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, canvas.width, canvas.height);
let b = false;
const beatSettings = {
interval: '4/4',
tempo:144,
100:{
interval:'1/16',
tempo:144,
onBeat:() => {
console.timeEnd('da');
console.time('da');
toggleBeatColor();
},
duration:1200,
repeatEvery:1600,
repeatUntil:3800
},
'00:05.800':{
interval: '1/4',
onBeat:(sound,now,startTime)=>{
if(b) console.timeEnd('dum');
console.time('dum'); b = true;
toggleBeatColor()
}
}
} as BeatSettings;
document.addEventListener('DOMContentLoaded', () => {
const audioSync = new AudioSync(
['./entertainer.mp3'],
beatSettings,
true
);
(document.getElementById('play-button') as HTMLElement).addEventListener('click', () => {
audioSync.play();
});
(document.getElementById('stop-button') as HTMLElement).addEventListener('click', () => {
audioSync.stop();
});
});
(Dev) Dependencies
- Howler.js: A JavaScript library for audio playback. Ensure you include Howler.js in your project to use the
AudioSync
class. - HackTimer.js: An optional dependency for improving timer precision and ensuring functionality when the browser tab is not in focus.
Build and run sample
With tinybuild
installed globally (npm i -g tinybuild
): npm start
Configuration
See ./tinybuild.config.js
for settings.
Add build:true for build-only, add serve:true for serve-only, or set bundle or server to false alternatively.