-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
example.js
36 lines (28 loc) · 1.18 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require('fs')
const Speaker = require('speaker')
const createMusicStream = require('create-music-stream')
const {MusicBeatDetector, MusicBeatScheduler, MusicGraph} = require('.')
const musicSource = process.argv[2] //gets the first argument on cli
//MusicGraph generates a SVG graph that displays every detected peak
const musicGraph = new MusicGraph()
//MusicBeatScheduler syncs any detected peak with the listened audio. It's useful to control some bulbs or any other effect
const musicBeatScheduler = new MusicBeatScheduler(pos => {
console.log(`peak at ${pos}ms`) //do your effect here
})
//MusicBeatDetector analyzes the music
const musicBeatDetector = new MusicBeatDetector({
plotter: musicGraph.getPlotter(),
scheduler: musicBeatScheduler.getScheduler(),
})
//get any raw pcm_16le stream
createMusicStream(musicSource)
//pipe on analyzer
.pipe(musicBeatDetector.getAnalyzer())
.on('peak-detected', (pos, bpm) => console.log(`peak-detected at ${pos}ms, detected bpm ${bpm}`))
.on('end', () => {
fs.writeFileSync('graph.svg', musicGraph.getSVG())
console.log('end')
})
//pipe on speaker
.pipe(new Speaker())
.on('open', () => musicBeatScheduler.start())