Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring: name factories consistently
- Loading branch information
Showing
27 changed files
with
1,088 additions
and
598 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as R from 'ramda' | ||
|
||
export const createBitCrusher = (audioContext) => { | ||
const bufferSize = 512 | ||
const scriptProcessor = audioContext.createScriptProcessor(bufferSize, 1, 1) | ||
let bits = 16 | ||
let normFreq = 0.05 | ||
const step = 0.5 ** bits | ||
let phaser = 0 | ||
let last = 0 | ||
scriptProcessor.onaudioprocess = (event) => { | ||
const input = event.inputBuffer.getChannelData(0) | ||
const output = event.outputBuffer.getChannelData(0) | ||
R.times((i) => { | ||
phaser += normFreq | ||
if (phaser >= 1) { | ||
phaser -= 1 | ||
last = step * Math.floor((input[i] / step) + 0.5) | ||
} | ||
output[i] = last | ||
}, bufferSize) | ||
} | ||
|
||
return { | ||
connect({ connect, getInput }) { | ||
scriptProcessor.connect(getInput()) | ||
return { connect } | ||
}, | ||
getInput() { | ||
return scriptProcessor | ||
}, | ||
setFrequencyValue(value) { | ||
normFreq = value | ||
return this | ||
}, | ||
setBitsValue(value) { | ||
bits = value | ||
return this | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
import test from 'ava' | ||
import sinon from 'sinon' | ||
import { Delay } from './delay' | ||
import { createDelay } from './delay' | ||
|
||
import { AudioContextMock } from '../../mock/audio-context.mock' | ||
|
||
test('Delay factory returns an object', (t) => { | ||
test('createDelay factory returns an object', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
t.true(typeof delay === 'object') | ||
}) | ||
|
||
test('Delay connect method returns an object with a connect method', (t) => { | ||
test('createDelay connect method returns an object with a connect method', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
const nextInChain = { | ||
getInput: () => audioContext.createGain(), | ||
connect() {}, | ||
} | ||
t.true(typeof delay.connect(nextInChain).connect === 'function') | ||
}) | ||
|
||
test('Delay connect method returns an object with a getInput method', (t) => { | ||
test('createDelay connect method returns an object with a getInput method', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
t.true(typeof delay.getInput() === 'object') | ||
}) | ||
|
||
test('Delay factory returns object with a tempo getter and setter', (t) => { | ||
test('createDelay factory returns object with a tempo getter and setter', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
delay.setTempoValue(120) | ||
t.is(120, delay.getTempoValue()) | ||
}) | ||
|
||
test('Delay factory returns object with a division getter and setter', (t) => { | ||
test('createDelay factory returns object with a division getter and setter', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
delay.setDivisionValue(4) | ||
t.is(4, delay.getDivisionValue()) | ||
}) | ||
|
||
test('Delay factory returns object with a feedback getter and setter', (t) => { | ||
test('createDelay factory returns object with a feedback getter and setter', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
delay.setFeedbackValue(0.8) | ||
t.is(0.8, delay.getFeedbackValue()) | ||
}) | ||
|
||
test('Delay factory returns object with a frequency getter and setter', (t) => { | ||
test('createDelay factory returns object with a frequency getter and setter', (t) => { | ||
const audioContext = AudioContextMock(sinon.sandbox.create()) | ||
const delay = Delay(audioContext) | ||
const delay = createDelay(audioContext) | ||
delay.setFrequencyValue(200) | ||
t.is(200, delay.getFrequencyValue()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,52 @@ | ||
export const Distortion = (audioContext) => { | ||
import * as R from 'ramda' | ||
|
||
export const createDistortion = (audioContext) => { | ||
const makeDistortionCurve = (amount) => { | ||
const k = typeof amount === 'number' ? amount : 50 | ||
const nSamples = 44100 | ||
const { sampleRate } = audioContext | ||
const curve = new Float32Array(44100) | ||
const deg = Math.PI / 180 | ||
for (let i = 0; i < nSamples; i += 1) { | ||
const x = (i * 2) / (nSamples - 1) | ||
curve[i] = ((3 + k) * x * 20 * deg) / ((Math.PI + k) * Math.abs(x)) | ||
} | ||
R.times((i) => { | ||
const x = (i * 2) / (sampleRate - 1) | ||
const a = (3 + amount) * x * 20 * deg | ||
const b = Math.PI + (amount * Math.abs(x)) | ||
curve[i] = a / b | ||
}, sampleRate) | ||
return curve | ||
} | ||
|
||
const preGain = audioContext.createGain() | ||
const postGain = audioContext.createGain() | ||
const dist = audioContext.createWaveShaper() | ||
dist.curve = makeDistortionCurve(100) | ||
preGain.connect(dist).connect(postGain) | ||
dist.curve = makeDistortionCurve(400) | ||
dist.oversample = '4x' | ||
preGain.gain.value = 50 | ||
postGain.gain.value = 1 | ||
return { | ||
connect({ connect, getInput }) { | ||
dist.connect(getInput()) | ||
return connect | ||
postGain.connect(getInput()) | ||
return { connect } | ||
}, | ||
getInput() { | ||
return dist | ||
return preGain | ||
}, | ||
setCurve(amount) { | ||
setCurveAmount(amount) { | ||
dist.curve = makeDistortionCurve(amount) | ||
return this | ||
}, | ||
setPreGainValue(value) { | ||
preGain.gain.value = value | ||
return this | ||
}, | ||
getPreGainValue() { | ||
return preGain.gain.value | ||
}, | ||
setPostGainValue(value) { | ||
postGain.gain.value = value | ||
return this | ||
}, | ||
getPostGainValue() { | ||
return postGain.gain.value | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as R from 'ramda' | ||
|
||
export const createNoiseConvolver = (audioContext) => { | ||
const convolver = audioContext.createConvolver() | ||
const bufferSize = audioContext.sampleRate | ||
const buffer = audioContext.createBuffer(2, bufferSize / 2, bufferSize) | ||
const left = buffer.getChannelData(0) | ||
const right = buffer.getChannelData(1) | ||
R.times((i) => { | ||
left[i] = Math.random() | ||
right[i] = Math.random() | ||
}, buffer.length) | ||
convolver.buffer = buffer | ||
|
||
return { | ||
connect({ connect, getInput }) { | ||
convolver.connect(getInput()) | ||
return { connect } | ||
}, | ||
getInput() { | ||
return convolver | ||
}, | ||
} | ||
} |
Oops, something went wrong.