Skip to content

Instantly share code, notes, and snippets.

View miselaytes-anton's full-sized avatar

Anton Miselaytes miselaytes-anton

  • Netherlands
View GitHub Profile
/**
Main thread (e.g. index.js)
**/
const audioCtx = new AudioContext()
// Loads module script via AudioWorklet.
audioCtx.audioWorklet.addModule('reverb-processor.js')
.then(() => getLiveAudio(audioCtx))
.then((liveIn) => {
// After the resolution of module loading, an AudioWorkletNode can be constructed.
const inputSignal = readSignalFromSomewhere()
const outputSignal = new Array(inputSignal.length)
const delayInSamples = 44100
const delayBuffer = new Array(delayInSamples)
const decay = 0.5
let pointer = 0
inputSignal.forEach(sample => {
const previousSample = delayBuffer[pointer % delayInSamples]
delayBuffer[pointer] = sample + previousSample * decay
const getImpulseBuffer = (audioCtx, impulseUrl) => {
return fetch(impulseUrl)
.then(response => response.arrayBuffer())
.then(arrayBuffer => audioCtx.decodeAudioData(arrayBuffer))
}
const getLiveAudio = (audioCtx) => {
return navigator.mediaDevices.getUserMedia({audio: true})
.then(stream => audioCtx.createMediaStreamSource(stream));
}
//in this example we assume guitar sound and impulse response are loaded into arrays somehow
const samplingRate = 44100
// 1 second of an input sound (e.g. guitar chord)
const X = new Array(samplingRate * 1)
// 2 seconds of an impulse response (e.g echos of a clap in a room)
const H = new Array(samplingRate * 2)
//output sound (how chord would sound in this room)
const Y = new Array(X.length + H.length)
X.forEach((x, i) => {
// Allows to set a parameter value for multiple nodes (of same kind) at the same time
function mergeParams(params){
const singleParam = params[0]
const parameter = {};
const audioNodeMethods = Object.getOwnPropertyNames(AudioParam.prototype)
.filter(prop => typeof singleParam[prop] === 'function')
//allows things like parameter.setValueAtTime(x, ctx.currentTime)
audioNodeMethods.forEach(method => {
parameter[method] = (...argums) => {
class Freeverb extends CompositeAudioNode {
get wetGain () {
return this._wet.gain;
}
get dryGain () {
return this._dry.gain;
}
get roomSize() {
return mergeParams(this._combFilters.map(comb => comb.resonance))
}
class LowpassCombFilter extends CompositeAudioNode {
get resonance () {
return this._gain.gain;
}
get dampening () {
return this._lowPass.frequency;
}
get delayTime () {
return this._delay.delayTime;
}
// static Low Pass Feedback Comb Filter
const audioCtx = new AudioContext()
const input = getOsc(audioCtx)
const combFilter = getCombFilter(audioCtx)
const output = audioCtx.destination
input
.connect(combFilter)
.connect(output)
@miselaytes-anton
miselaytes-anton / nginx.conf
Created April 3, 2017 15:12 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@miselaytes-anton
miselaytes-anton / GraphicsMagick.sh
Created November 6, 2015 17:58 — forked from paul91/GraphicsMagick.sh
How to install GraphicsMagick on CentOS 6.4
#!/bin/bash
# Install build dependencies
yum install -y gcc libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel
# Get GraphicsMagick source
wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.9.tar.gz
tar zxvf GraphicsMagick-1.3.9.tar.gz
# Configure and compile