Commit 4c4c3d13 authored by mzed's avatar mzed
Browse files

More examples for maxiLib

parent e6011e29
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Javascript Audio Processing
</title>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=js&amp;skin=sunburst"></script>
<!-- show all line numbers-->
<style type="text/css">.prettyprint ol.linenums > li { list-style-type: decimal }</style>
<script src="maxiLib.js"></script>
<!-- Our javascript code -->
<script type="text/javascript">
var maxiLib = window.MaxiLib();
var audio = new maxiLib.maxiAudio();
audio.init();
var a = new maxiLib.maxiOsc();
var xs = [0,0,0], ys = [0,0,0];
var a0, a1, a2, b0, b1, b2;
var Q = 1.0;
var mySwitchableOsc = new maxiLib.maxiOsc();
var cos = Math.cos;
// Band-pass reson:
// b0 = alpha;
// b1 = 0;
// b2 = -1 * alpha;
// a0 = 1 + alpha;
// a1 = -2*cos(w0);
// a2 = 1 - alpha;
//Notch:
// b0 = 1;
// b1 = -2*cos(w0);
// b2 = 1;
// a0 = 1 + alpha;
// a1 = -2*cos(w0);
// a2 = 1 - alpha;
//LPF:
var f0 = 20;
audio.play = function() {//this is where the magic happens. Very slow magic.
f0 = (f0 + 0.001)%8000; //THE FREQUENCY
var w0 = 2*Math.PI*f0/44100;
var alpha = Math.sin(w0)/(2*Q);
b0 = (1 - cos(w0))/2;
b1 = 1 - cos(w0);
b2 = (1 - cos(w0))/2;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
xs[0] = Math.random();
ys[0] = (b0/a0)*xs[0] + (b1/a0)*xs[1] + (b2/a0)*xs[2]
- (a1/a0)*ys[1] - (a2/a0)*ys[2];
var out = ys[0];
ys[2] = ys[1]; ys[1] = ys[0];
xs[2] = xs[1]; xs[1] = xs[0];
this.output = out;
}
</script>
</head>
<body>
<h1> Advanced Filters </h1>
<p>
</p>
<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" >
var maxiLib = window.MaxiLib();
var audio = new maxiLib.maxiAudio();
audio.init();
var a = new maxiLib.maxiOsc();
var xs = [0,0,0], ys = [0,0,0];
var a0, a1, a2, b0, b1, b2;
var f0 = 400; //THE FREQUENCY
var Q = 1.0;
var mySwitchableOsc = new maxiLib.maxiOsc();
var cos = Math.cos;
var w0 = 2*Math.PI*f0/44100;
var alpha = Math.sin(w0)/(2*Q);
// Band-pass reson:
// b0 = alpha;
// b1 = 0;
// b2 = -1 * alpha;
// a0 = 1 + alpha;
// a1 = -2*cos(w0);
// a2 = 1 - alpha;
//Notch:
b0 = 1;
b1 = -2*cos(w0);
b2 = 1;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
//LPF:
// b0 = (1 - cos(w0))/2;
// b1 = 1 - cos(w0);
// b2 = (1 - cos(w0))/2;
// a0 = 1 + alpha;
// a1 = -2*cos(w0);
// a2 = 1 - alpha;
audio.play = function() {//this is where the magic happens. Very slow magic.
xs[0] = mySwitchableOsc.sawn(400);
ys[0] = (b0/a0)*xs[0] + (b1/a0)*xs[1] + (b2/a0)*xs[2]
- (a1/a0)*ys[1] - (a2/a0)*ys[2];
this.output = ys[0];
ys[2] = ys[1]; ys[1] = ys[0];
xs[2] = xs[1]; xs[1] = xs[0];
}
</pre>
</body></html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Javascript Audio Processing
</title>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=js&amp;skin=sunburst"></script>
<!-- show all line numbers-->
<style type="text/css">.prettyprint ol.linenums > li { list-style-type: decimal }</style>
<script src="maxiLib.js"></script>
<!-- Our javascript code -->
<script type="text/javascript">
var maxiLib = window.MaxiLib();
var audio = new maxiLib.maxiAudio();
audio.init();
//This shows how to use maximilian to build a monophonic synth
//These are the synthesiser bits
var VCO1 = new maxiLib.maxiOsc();
var VCO2 = new maxiLib.maxiOsc();
var LFO1 = new maxiLib.maxiOsc();
var LFO2 = new maxiLib.maxiOsc();
var VCF = new maxiLib.maxiFilter();
var ADSR = new maxiLib.maxiEnv();
//This is a bunch of control signals so that we can hear something
var timer = new maxiLib.maxiOsc();//this is the metronome
var currentCount,lastCount;//these values are used to check if we have a new beat this sample
//and these are some variables we can use to pass stuff around
var VCO1out,VCO2out,LFO1out,LFO2out,VCFout,ADSRout;
ADSR.setAttack(1000);
ADSR.setDecay(1);
ADSR.setSustain(1);
ADSR.setRelease(1000);
audio.play = function(){
//so this first bit is just a basic metronome so we can hear what we're doing.
currentCount=Math.round(timer.phasor(0.5));//this sets up a metronome that ticks every 2 seconds
if (lastCount!=currentCount) {//if we have a new timer int this sample, play the sound
ADSR.trigger = 1;//trigger the envelope from the start
// console.log("tick\n");//the clock ticks
lastCount=0;//set lastCount to 0
}
//and this is where we build the synth
ADSRout=ADSR.adsr(1.0,ADSR.trigger);
LFO1out=LFO1.sinebuf(0.2);//this lfo is a sinewave at 0.2 hz
VCO1out=VCO1.pulse(55,0.6);//here's VCO1. it's a pulse wave at 55 hz, with a pulse width of 0.6
VCO2out=VCO2.pulse(110+LFO1out,0.2);//here's VCO2. it's a pulse wave at 110hz with LFO modulation on the frequency, and width of 0.2
VCFout=VCF.lores((VCO1out+VCO2out)*0.5, ADSRout*10000, 10);//now we stick the VCO's into the VCF, using the ADSR as the filter cutoff
var finalSound=VCFout*ADSRout;//finally we add the ADSR as an amplitude modulator
ADSR.trigger=0;
this.output=finalSound;
}
</script>
</head>
<body>
<h1> monosynth </h1>
<p>
This shows how to use maximilian to build a monophonic synth
</p>
<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" >
var maxiLib = window.MaxiLib();
var audio = new maxiLib.maxiAudio();
audio.init();
//This shows how to use maximilian to build a monophonic synth
//These are the synthesiser bits
var VCO1 = new maxiLib.maxiOsc();
var VCO2 = new maxiLib.maxiOsc();
var LFO1 = new maxiLib.maxiOsc();
var LFO2 = new maxiLib.maxiOsc();
var VCF = new maxiLib.maxiFilter();
var ADSR = new maxiLib.maxiEnv();
//This is a bunch of control signals so that we can hear something
var timer = new maxiLib.maxiOsc();//this is the metronome
var currentCount,lastCount;//these values are used to check if we have a new beat this sample
//and these are some variables we can use to pass stuff around
var VCO1out,VCO2out,LFO1out,LFO2out,VCFout,ADSRout;
ADSR.setAttack(1000);
ADSR.setDecay(1);
ADSR.setSustain(1);
ADSR.setRelease(1000);
audio.play = function(){
//so this first bit is just a basic metronome so we can hear what we're doing.
currentCount=Math.round(timer.phasor(0.5));//this sets up a metronome that ticks every 2 seconds
if (lastCount!=currentCount) {//if we have a new timer int this sample, play the sound
ADSR.trigger = 1;//trigger the envelope from the start
// console.log("tick\n");//the clock ticks
lastCount=0;//set lastCount to 0
}
//and this is where we build the synth
ADSRout=ADSR.adsr(1.0,ADSR.trigger);
LFO1out=LFO1.sinebuf(0.2);//this lfo is a sinewave at 0.2 hz
VCO1out=VCO1.pulse(55,0.6);//here's VCO1. it's a pulse wave at 55 hz, with a pulse width of 0.6
VCO2out=VCO2.pulse(110+LFO1out,0.2);//here's VCO2. it's a pulse wave at 110hz with LFO modulation on the frequency, and width of 0.2
VCFout=VCF.lores((VCO1out+VCO2out)*0.5, ADSRout*10000, 10);//now we stick the VCO's into the VCF, using the ADSR as the filter cutoff
var finalSound=VCFout*ADSRout;//finally we add the ADSR as an amplitude modulator
ADSR.trigger=0;
this.output=finalSound;
}
</pre>
</body></html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Javascript Audio Processing
</title>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=js&amp;skin=sunburst"></script>
<!-- show all line numbers-->
<style type="text/css">.prettyprint ol.linenums > li { list-style-type: decimal }</style>
<script src="maxiLib.js"></script>
<!-- Our javascript code -->
<script type="text/javascript">
var maxiLib = window.MaxiLib();
var audio = new maxiLib.maxiAudio();
audio.init();
// change output to an array
audio.outputIsArray(true, 2);
//This shows how to use maximilian to build a polyphonic synth.
// Chrome doesn't like this one for some reason
var VCO_ArraySize = 6;
//These are the synthesiser bits
var VCO1 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
VCO1.push(new maxiLib.maxiOsc());
}
var VCO2 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
VCO2.push(new maxiLib.maxiOsc());
}
var LFO1 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
LFO1.push(new maxiLib.maxiOsc());
}
var LFO2 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
LFO2.push(new maxiLib.maxiOsc());
}
var VCF = [];
for(var i = 0; i < VCO_ArraySize; ++i){
VCF.push(new maxiLib.maxiFilter());
}
var ADSR = [];
for(var i = 0; i < VCO_ArraySize; ++i){
ADSR.push(new maxiLib.maxiEnv());
}
//and these are some variables we can use to pass stuff around
var VCO1out = [], VCO2out = [], LFO1out = [],LFO2out = [], VCFout = [], ADSRout = [];
var pitch = [];
// fill arrays with 0s
for(var i = 0; i < VCO_ArraySize; i++){
VCO1out.push(0);
VCO2out.push(0);
LFO1out.push(0);
LFO2out.push(0);
VCFout.push(0);
ADSRout.push(0);
pitch.push(0);
}
//This is a bunch of control signals so that we can hear something
var timer = new maxiLib.maxiOsc();//this is the metronome
var currentCount = 0,lastCount = 0, voice = 0;//these values are used to check if we have a new beat this sample
var mix;
for (var i=0;i<VCO_ArraySize;i++) {
ADSR[i].setAttack(0);
ADSR[i].setDecay(200);
ADSR[i].setSustain(0.2);
ADSR[i].setRelease(2000);
}
audio.play = function(){
mix=0;//we're adding up the samples each update and it makes sense to clear them each time first.
//so this first bit is just a basic metronome so we can hear what we're doing.
currentCount=Math.floor(timer.phasor(8));//this sets up a metronome that ticks 8 times a second
if (lastCount!=currentCount) {//if we have a new timer int this sample, play the sound
if (voice>=VCO_ArraySize) {
voice=0;
}
ADSR[voice].trigger=1;//trigger the envelope from the start
pitch[voice]=voice+1;
voice++;
// lastCount = 0;
}
//and this is where we build the synth
for (var i=0; i<VCO_ArraySize; i++) {
ADSRout[i]=ADSR[i].adsr(1.,ADSR[i].trigger);//our ADSR env is passed a constant signal of 1 to generate the transient.
LFO1out[i]=LFO1[i].sinebuf(0.2);//this lfo is a sinewave at 0.2 hz
VCO1out[i]=VCO1[i].pulse(55*pitch[i],0.6);//here's VCO1. it's a pulse wave at 55 hz, with a pulse width of 0.6
VCO2out[i]=VCO2[i].pulse((110*pitch[i])+LFO1out[i],0.2);//here's VCO2. it's a pulse wave at 110hz with LFO modulation on the frequency, and width of 0.2
VCFout[i]=VCF[i].lores((VCO1out[i]+VCO2out[i])*0.5, 250+((pitch[i]+LFO1out[i])*1000), 10);//now we stick the VCO's into the VCF, using the ADSR as the filter cutoff
mix+=VCFout[i]*ADSRout[i]/VCO_ArraySize;//finally we add the ADSR as an amplitude modulator
}
this.output[0]=mix*0.5;//left channel
this.output[1]=mix*0.5;//right channel
// This just sends note-off messages.
for (var i=0; i<VCO_ArraySize; i++) {
ADSR[i].trigger=0;
}
}
</script>
</head>
<body>
<h1> polysynth </h1>
<p>
This shows how to use maximilian to build a polyphonic synth.
</p>
<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" >
var maxiLib = window.MaxiLib();
var audio = new maxiLib.maxiAudio();
audio.init();
// change output to an array
audio.outputIsArray(true, 2);
//This shows how to use maximilian to build a polyphonic synth.
// Chrome doesn't like this one for some reason
var VCO_ArraySize = 6;
//These are the synthesiser bits
var VCO1 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
VCO1.push(new maxiLib.maxiOsc());
}
var VCO2 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
VCO2.push(new maxiLib.maxiOsc());
}
var LFO1 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
LFO1.push(new maxiLib.maxiOsc());
}
var LFO2 = [];
for(var i = 0; i < VCO_ArraySize; ++i){
LFO2.push(new maxiLib.maxiOsc());
}
var VCF = [];
for(var i = 0; i < VCO_ArraySize; ++i){
VCF.push(new maxiLib.maxiFilter());
}
var ADSR = [];
for(var i = 0; i < VCO_ArraySize; ++i){
ADSR.push(new maxiLib.maxiEnv());
}
//and these are some variables we can use to pass stuff around
var VCO1out = [], VCO2out = [], LFO1out = [],LFO2out = [], VCFout = [], ADSRout = [];
var pitch = [];
// fill arrays with 0s
for(var i = 0; i < VCO_ArraySize; i++){
VCO1out.push(0);
VCO2out.push(0);
LFO1out.push(0);
LFO2out.push(0);
VCFout.push(0);
ADSRout.push(0);
pitch.push(0);
}
//This is a bunch of control signals so that we can hear something
var timer = new maxiLib.maxiOsc();//this is the metronome
var currentCount = 0,lastCount = 0, voice = 0;//these values are used to check if we have a new beat this sample
var mix;
for (var i=0;i<VCO_ArraySize;i++) {
ADSR[i].setAttack(0);
ADSR[i].setDecay(200);
ADSR[i].setSustain(0.2);
ADSR[i].setRelease(2000);
}
audio.play = function(){
mix=0;//we're adding up the samples each update and it makes sense to clear them each time first.
//so this first bit is just a basic metronome so we can hear what we're doing.
currentCount=Math.floor(timer.phasor(8));//this sets up a metronome that ticks 8 times a second
if (lastCount!=currentCount) {//if we have a new timer int this sample, play the sound
if (voice>=VCO_ArraySize) {
voice=0;
}
ADSR[voice].trigger=1;//trigger the envelope from the start
pitch[voice]=voice+1;
voice++;
// lastCount = 0;
}
//and this is where we build the synth
for (var i=0; i<VCO_ArraySize; i++) {
ADSRout[i]=ADSR[i].adsr(1.,ADSR[i].trigger);//our ADSR env is passed a constant signal of 1 to generate the transient.
LFO1out[i]=LFO1[i].sinebuf(0.2);//this lfo is a sinewave at 0.2 hz
VCO1out[i]=VCO1[i].pulse(55*pitch[i],0.6);//here's VCO1. it's a pulse wave at 55 hz, with a pulse width of 0.6
VCO2out[i]=VCO2[i].pulse((110*pitch[i])+LFO1out[i],0.2);//here's VCO2. it's a pulse wave at 110hz with LFO modulation on the frequency, and width of 0.2
VCFout[i]=VCF[i].lores((VCO1out[i]+VCO2out[i])*0.5, 250+((pitch[i]+LFO1out[i])*1000), 10);//now we stick the VCO's into the VCF, using the ADSR as the filter cutoff
mix+=VCFout[i]*ADSRout[i]/VCO_ArraySize;//finally we add the ADSR as an amplitude modulator
}
this.output[0]=mix*0.5;//left channel
this.output[1]=mix*0.5;//right channel
// This just sends note-off messages.
for (var i=0; i<VCO_ArraySize; i++) {
ADSR[i].trigger=0;
}
}
</pre>
</body></html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment