-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
239 additions
and
20 deletions.
There are no files selected for viewing
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,27 @@ | ||
import { linToLog, logToLin } from "../../utils/conversion"; | ||
|
||
const Decay = ({ state, setState }) => { | ||
const time = { | ||
min: 0.1, | ||
max: 10, | ||
}; | ||
|
||
const handleChange = (ev) => { | ||
const linValue = ev.target.value; | ||
const logValue = linToLog(linValue, time.min, time.max); | ||
setState((prevState) => ({ | ||
...prevState, | ||
decay: +logValue.toFixed(2), | ||
})); | ||
}; | ||
|
||
return ( | ||
<input | ||
type="range" | ||
onChange={handleChange} | ||
value={logToLin(state.decay, time.min, time.max)} | ||
/> | ||
); | ||
}; | ||
|
||
export default Decay; |
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,11 @@ | ||
const Toggle = ({ state, setState }) => { | ||
const handleClick = () => { | ||
setState((prevState) => ({ | ||
...prevState, | ||
on: !prevState.on, | ||
})); | ||
}; | ||
return <button onClick={handleClick}>{state.on ? "On" : "Off"}</button>; | ||
}; | ||
|
||
export default Toggle; |
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,19 @@ | ||
import { useContext } from "react"; | ||
|
||
import { SettingsContext } from "../../contexts/SettingsContext"; | ||
|
||
import Toggle from "./Toggle"; | ||
import Decay from "./Decay"; | ||
|
||
const Reverb = () => { | ||
const { reverbConfig, setReverbConfig } = useContext(SettingsContext); | ||
return ( | ||
<div> | ||
Reverb | ||
<Toggle state={reverbConfig} setState={setReverbConfig} /> | ||
<Decay state={reverbConfig} setState={setReverbConfig}/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Reverb; |
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,32 @@ | ||
import { createContext, useState, useContext } from "react"; | ||
|
||
import { SettingsContext } from "./SettingsContext"; | ||
import { AudioContext } from "./AudioContext"; | ||
|
||
import { createImpulseResponse } from "../utils/audio"; | ||
|
||
export const EffectsContext = createContext(); | ||
|
||
export const EffectsContextProvider = ({ children }) => { | ||
const { reverbConfig } = useContext(SettingsContext); | ||
const { actx, master } = useContext(AudioContext); | ||
|
||
console.log(reverbConfig); | ||
|
||
// Reverb node | ||
const [reverb] = useState(new ConvolverNode(actx)); | ||
reverb.buffer = createImpulseResponse(actx, 3, reverbConfig.decay); | ||
|
||
// Routing | ||
master.connect(actx.destination); | ||
if (reverbConfig.on) { | ||
master.connect(reverb); | ||
reverb.connect(actx.destination); | ||
} else { | ||
reverb.disconnect(); | ||
} | ||
|
||
return ( | ||
<EffectsContext.Provider value={{}}>{children}</EffectsContext.Provider> | ||
); | ||
}; |
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,115 @@ | ||
/** | ||
* Simple Reverb constructor. | ||
* | ||
* @param {AudioContext} context | ||
* @param {object} opts | ||
* @param {number} opts.seconds | ||
* @param {number} opts.decay | ||
* @param {boolean} opts.reverse | ||
*/ | ||
|
||
export function SimpleReverb(context, opts) { | ||
this.input = this.output = context.createConvolver(); | ||
this._context = context; | ||
|
||
var p = this.meta.params; | ||
opts = opts || {}; | ||
this._seconds = opts.seconds || p.seconds.defaultValue; | ||
this._decay = opts.decay || p.decay.defaultValue; | ||
this._reverse = opts.reverse || p.reverse.defaultValue; | ||
this._buildImpulse(); | ||
} | ||
|
||
SimpleReverb.prototype = Object.create(null, { | ||
connect: { | ||
value: function (dest) { | ||
this.output.connect(dest.input ? dest.input : dest); | ||
}, | ||
}, | ||
|
||
disconnect: { | ||
value: function () { | ||
this.output.disconnect(); | ||
}, | ||
}, | ||
|
||
_buildImpulse: { | ||
value: function () { | ||
var rate = this._context.sampleRate, | ||
length = rate * this.seconds, | ||
decay = this.decay, | ||
impulse = this._context.createBuffer(2, length, rate), | ||
impulseL = impulse.getChannelData(0), | ||
impulseR = impulse.getChannelData(1), | ||
n, | ||
i; | ||
|
||
for (i = 0; i < length; i++) { | ||
n = this.reverse ? length - i : i; | ||
impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay); | ||
impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay); | ||
} | ||
|
||
this.input.buffer = impulse; | ||
}, | ||
}, | ||
|
||
meta: { | ||
value: { | ||
name: "SimpleReverb", | ||
params: { | ||
seconds: { | ||
min: 1, | ||
max: 50, | ||
defaultValue: 3, | ||
type: "float", | ||
}, | ||
decay: { | ||
min: 0, | ||
max: 100, | ||
defaultValue: 2, | ||
type: "float", | ||
}, | ||
reverse: { | ||
min: 0, | ||
max: 1, | ||
defaultValue: 0, | ||
type: "bool", | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
seconds: { | ||
enumerable: true, | ||
get: function () { | ||
return this._seconds; | ||
}, | ||
set: function (value) { | ||
this._seconds = value; | ||
this._buildImpulse(); | ||
}, | ||
}, | ||
|
||
decay: { | ||
enumerable: true, | ||
get: function () { | ||
return this._decay; | ||
}, | ||
set: function (value) { | ||
this._decay = value; | ||
this._buildImpulse(); | ||
}, | ||
}, | ||
|
||
reverse: { | ||
enumerable: true, | ||
get: function () { | ||
return this._reverse; | ||
}, | ||
set: function (value) { | ||
this._reverse = value; | ||
this._buildImpulse(); | ||
}, | ||
}, | ||
}); |