xxxxxxxxxx
147
class Module {
constructor(posX, posY) {
this.posX = posX;
this.posY = posY;
this.prevModules = [];
this.modFunc = function(x) { console.log("this is a module " + x)};
this.inputNum = 0;
this.outputNum = 0;
this.inputs = [];
this.outputs = [];
}
link(modOutput, slotOut, slotIn)
{
modOutput.outputs[slotOut] = this;
this.inputs[slotIn] = modOutput;
}
initIO()
{
for(var i = 0; i < this.inputNum; i++)
this.inputs.push(null);
for(var i = 0; i < this.outputNum; i++)
this.outputs.push(null);
}
}
class Osc extends Module {
constructor(posX, posY)
{
super(posX, posY);
this.phase = 0;
this.detune = 0;
this.waveform = 0;
this.wavetable = [16, 25, 30, 31, 30, 29, 26, 25, 25, 28, 31, 28, 18, 11, 10, 13, 17, 20, 22, 20, 15, 6, 0, 2, 6, 5, 3, 1, 0, 0, 1, 4];
this.tl = 1;
this.atck = 0;
this.dec = 0;
this.sus = 1; // Amogus
this.inputNum = 0;
this.outputNum = 1;
super.modFunc = function(x) {
var a = Math.sin(x * 2 * Math.PI) * this.tl;
console.log("sine : " + a);
return a};
super.initIO()
}
}
class Mixer extends Module {
constructor(posX, posY)
{
super(posX, posY);
super.inputNum = 4;
super.outputNum = 1;
super.modFunc = function(x) {
var out = 0;
for(var i = 0; i < this.inputNum; i++)
{
this.inputs[i] == null ? out += 0 : out += this.inputs[i].modFunc(x);
}
console.log("Mixed signal : " + out);
return out;
};
super.initIO()
}
}
class PhaseMod extends Module {
constructor(posX, posY)
{
super(posX, posY);
super.inputNum = 2;
super.outputNum = 1;
this.modFunc = function(x) {
var fmModulation = 0;
this.inputs[0] == null ? fmModulation = 0 : fmModulation = this.inputs[0].modFunc(x);
console.log(x);
return this.inputs[1] == null ? 0 : this.inputs[1].modFunc(x + fmModulation);
}
super.initIO()
}
}
class Out extends Module {
constructor(posX, posY)
{
super(posX, posY);
super.inputNum = 1;
super.outputNum = 0;
this.buffer = [];
super.modFunc = function(x) {
return this.inputs[0] == null ? 0 : this.inputs[0].modFunc(x);
}
super.initIO()
}
fillBuffer(){
this.buffer = [];
for(var i = 0; i < 256; i++)
this.buffer.push(this.modFunc(i/256));
}
}
function test()
{
var oscillator1 = new Osc(0, 0);
var oscillator2 = new Osc(0, 0);
var oscillator3 = new Osc(0, 0);
var mix = new Mixer(0, 0);
var pm = new PhaseMod(0, 0);
var outpt = new Out(0, 0);
oscillator1.tl = 0.5;
oscillator2.tl = 0.5;
oscillator3.tl = 1;
mix.link(oscillator1, 0, 0);
mix.link(oscillator2, 0, 1);
pm.link(mix, 0, 0);
pm.link(oscillator3, 0, 1);
outpt.link(pm, 0, 0);
outpt.fillBuffer();
var str = ""
for(var i = 0; i < 256; i++)
str += (Math.round(outpt.buffer[i] * 15)) + " ";
console.log(str);
}
function setup() {
createCanvas(1, 1);
var genMacButton = createButton("Generate waves!")
genMacButton.mousePressed(function() {
test();
})
}
function draw() {
//background(220);
}