Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mic-sender.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import record from 'node-record-lpcm16'
import mic from 'mic'
import WebSocket from 'ws'

const url = process.argv[2] || 'ws://localhost:8081'
const ws = new WebSocket(url)

ws.on('open', () => {
const mic = record.start({ sampleRate: 44100, channels: 1 })
mic.on('data', chunk => ws.send(chunk))
const micInst = mic({ rate: '44100', channels: '1' })
const micStream = micInst.getAudioStream()
micInst.start()
micStream.on('data', chunk => ws.send(chunk))
micStream.on('error', console.error)
})

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"d3-interpolate": "^3.0.1",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"fft-js": "^0.0.12",
"music-tempo": "^1.0.3",
"node-record-lpcm16": "^1.0.1",
"fft-js": "^0.0.12",
"mic": "^2.1.1",
"essentia.js": "^0.1.3",
"react": "^19.1.0",
"react-colorful": "^5.6.1",
"react-dom": "^19.1.0",
Expand Down
44 changes: 24 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 25 additions & 7 deletions src/backend/pattern/extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,27 @@ export const getStrobeColor: IColorGetter = (_, time) => {
}

export const getPulseColor: IColorGetter = (_, time) => {
const cycle = settings.syncToMusic && audioState.bpm ? 60000 / audioState.bpm : 1000
const t = (time * settings.effectSpeed) % cycle
if (t < lastPulse) {
const target = settings.syncToMusic && audioState.bpm ? 60000 / audioState.bpm : 1000

if (!lastPulseTime) {
lastPulseTime = time
pulseCycle = target
}

const dt = (time - lastPulseTime) * settings.effectSpeed
lastPulseTime = time

const elapsed = pulsePhase * pulseCycle + dt
pulseCycle = target
pulsePhase = elapsed / pulseCycle

if (pulsePhase >= 1) {
const cycles = Math.floor(pulsePhase)
pulsePhase -= cycles
pulseColor = hslToRgb(Math.random() * 360, 1, 0.5)
}
lastPulse = t
const intensity = Math.sin((t / cycle) * Math.PI)

const intensity = Math.sin(pulsePhase * Math.PI)
return pulseColor.map(c => Math.round(c * intensity)) as IArrColor
}

Expand Down Expand Up @@ -82,7 +96,9 @@ export const getMultiPulseColor: IColorGetter = (index, time) => {
let strobeColor: IArrColor = [255, 255, 255]
let lastStrobe = 0
let pulseColor: IArrColor = [255, 0, 0]
let lastPulse = 0
let pulseCycle = 1000
let pulsePhase = 0
let lastPulseTime = 0
let multiPulseColors: IArrColor[] = Array(4)
.fill(null)
.map(() => hslToRgb(Math.random() * 360, 1, 0.5))
Expand All @@ -95,7 +111,9 @@ export function resetExtraPatterns() {
strobeColor = [255, 255, 255]
lastStrobe = 0
pulseColor = [255, 0, 0]
lastPulse = 0
pulseCycle = 1000
pulsePhase = 0
lastPulseTime = 0
multiPulseColors = Array(4)
.fill(null)
.map(() => hslToRgb(Math.random() * 360, 1, 0.5))
Expand Down
18 changes: 13 additions & 5 deletions src/backend/wsAudio.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebSocketServer } from 'ws'
import fftjs from 'fft-js'
import MusicTempo from 'music-tempo'
import { Essentia, EssentiaWASM } from 'essentia.js'
// eslint-disable-next-line import/default
import RingBufferTs from 'ring-buffer-ts'
const RingBuffer = RingBufferTs.RingBuffer
Expand Down Expand Up @@ -29,8 +29,10 @@ const sampleRateDefault = 44100
const bpmWindow = sampleRateDefault * 8
const sampleBuffer = new RingBuffer<number>(bpmWindow)
let lastBpmUpdate = 0
const essentiaPromise = Promise.resolve(new Essentia(EssentiaWASM))
let bpmSmoothed = 0

export function processAudio(buffer: Buffer, sampleRate = sampleRateDefault) {
export async function processAudio(buffer: Buffer, sampleRate = sampleRateDefault) {
const samples = new Int16Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 2)
const input = Array.from(samples, s => s / 32768)
for (const s of input) sampleBuffer.add(s)
Expand All @@ -56,9 +58,14 @@ export function processAudio(buffer: Buffer, sampleRate = sampleRateDefault) {
if (now - lastBpmUpdate > 2000 && sampleBuffer.getBufferLength() >= sampleRate * 4) {
lastBpmUpdate = now
try {
const mt = new MusicTempo(Float32Array.from(sampleBuffer.toArray()))
const tempo = parseFloat(String(mt.tempo))
if (!Number.isNaN(tempo)) audioState.bpm = tempo
const essentia = await essentiaPromise
const signal = essentia.arrayToVector(Float32Array.from(sampleBuffer.toArray()))
const res = essentia.RhythmExtractor2013(signal, 208, 'multifeature', 40)
const tempo = res.bpm
if (!Number.isNaN(tempo)) {
bpmSmoothed = bpmSmoothed ? bpmSmoothed * 0.8 + tempo * 0.2 : tempo
audioState.bpm = bpmSmoothed
}
} catch {
// ignore errors
}
Expand All @@ -73,4 +80,5 @@ export function resetAudioState() {
audioState.bins = []
sampleBuffer.clear()
lastBpmUpdate = 0
bpmSmoothed = 0
}
1 change: 1 addition & 0 deletions src/types/essentia.js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'essentia.js'
8 changes: 4 additions & 4 deletions tests/wsAudio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ describe('processAudio', () => {
beforeEach(() => {
resetAudioState()
})
test('detects frequency', () => {
test('detects frequency', async () => {
const buf = genSine(440, 1234, 44100)
processAudio(buf)
await processAudio(buf)
expect(audioState.freq).toBeGreaterThan(430)
expect(audioState.freq).toBeLessThan(450)
})

test('detects bpm', () => {
test('detects bpm', async () => {
const buf = genBeat(120, 4, 44100)
processAudio(buf)
await processAudio(buf)
expect(audioState.bpm).toBeGreaterThan(110)
expect(audioState.bpm).toBeLessThan(130)
})
Expand Down