Skip to content

Commit

Permalink
Merge pull request #66 from nudibranchrecords/fix/lfo-timing
Browse files Browse the repository at this point in the history
Fix/lfo timing
  • Loading branch information
funwithtriangles authored May 26, 2018
2 parents 411792e + 270035a commit db866ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
24 changes: 15 additions & 9 deletions src/store/clock/sagas.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'babel-polyfill'

import now from 'performance-now'
import { takeEvery, put, call } from 'redux-saga/effects'
import * as a from './actions'
import { inputFired } from '../inputs/actions'
import now from 'performance-now'
import * as a from './actions'

const ppqn = 24
let deltaInc = Math.PI / ppqn
let pulses, delta, beats, lastBar
let pulses, delta, beats, lastBar, totalBeats
let seqStepCount = 0 // Sequencer step count
const ppSeqStep = ppqn / 8 // Pulses per 8th beat
const seqStepPerBar = ppSeqStep * 8 * 4
Expand All @@ -16,26 +14,32 @@ export const clockReset = () => {
pulses = 0
delta = 0
beats = 0
totalBeats = 0
lastBar = now()
}

export const newPulse = () => {
pulses++
delta += deltaInc
seqStepCount++

if (seqStepCount > seqStepPerBar - 1) {
seqStepCount = 0
}

if (pulses > 23) {
pulses = 0
beats++
totalBeats++
if (beats > 3) {
beats = 0
}
}
return { pulses, beats, delta, seqStepCount }
delta = pulses / ppqn + totalBeats
return {
pulses,
beats,
delta,
seqStepCount
}
}

export const calcBpm = () => {
Expand All @@ -47,7 +51,9 @@ export const calcBpm = () => {

export function* clockUpdate () {
const info = yield call(newPulse)
yield put(inputFired('lfo', info.delta, { type: 'lfo' }))
yield put(inputFired('lfo', info.delta, {
type: 'lfo'
}))

if (info.seqStepCount % ppSeqStep === 0) {
yield put(inputFired('seq-step', info.seqStepCount / ppSeqStep))
Expand Down
12 changes: 6 additions & 6 deletions src/utils/lfoProcess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ export default (delta, shape, rate) => {

switch (shape) {
case 'sine':
y = Math.sin(x)
y = Math.cos(x * 6.28318530718) * 0.5 + 0.5
break
case 'sawtooth':
y = (x - Math.floor(x + 0.5)) * 2
y = x % 1
break
case 'rSawtooth':
y = -(x - Math.floor(x + 0.5)) * 2
y = 1 - (x % 1)
break
case 'square':
y = Math.sign(Math.sin(x))
y = Math.floor((x % 1) * 2)
break
case 'triangle':
y = Math.abs((x - Math.floor(x + 0.5)) * 2)
y = Math.abs((x % 1) * 2 - 1)
break
}

return (y + 1) / 2 // convert from -1 ~ 1 to 0 ~ 1
return y
}

0 comments on commit db866ec

Please sign in to comment.