15_Polysnth.html 4.65 KB
Newer Older
Dr-Dan's avatar
Dr-Dan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

<!--
Copyright 2010, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
    * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!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>

<!-- <link rel="stylesheet" type="text/css" href="javascript-processing_files/simple.css"> -->


<script src="../maxiLib.js"></script>
43
<!-- <script src="../maxi_webAudio.js"></script> -->
Dr-Dan's avatar
Dr-Dan committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

<!-- Our javascript code -->
<script type="text/javascript">
//This shows how to use maximilian to build a polyphonic synth.

var VCO_ArraySize = 6;
//These are the synthesiser bits
var VCO1 = [], VCO2 = [], LFO1 = [], LFO2 = [];
var VCF = [];
var ADSR = [];

//and these are some variables we can use to pass stuff around
var VCO1out = [], VCO2out = [], LFO1out = [],LFO2out = [], VCFout = [], ADSRout = [];

for(var i = 0; i < VCO_ArraySize; i++){
59
60
61
62
	VCO1.push(new maximJs.maxiOsc());
	VCO2.push(new maximJs.maxiOsc());
	LFO1.push(new maximJs.maxiOsc());
	LFO2.push(new maximJs.maxiOsc());
Dr-Dan's avatar
Dr-Dan committed
63

64
	VCF.push(new maximJs.maxiFilter());
Dr-Dan's avatar
Dr-Dan committed
65

66
	ADSR.push(new maximJs.maxiEnvelope());
Dr-Dan's avatar
Dr-Dan committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80

	VCO1out.push(0);
	VCO2out.push(0);
	LFO1out.push(0);
	LFO2out.push(0);
	VCFout.push(0);
	ADSRout.push(0);
}

//These are the control values for the envelope
// var adsrEnvArray = [1,5,0.125,100,0.125,200,0,1000];
var adsrEnv = GetArrayAsVectorDbl([1,5,0.125,100,0.125,200,0,1000]);

//This is a bunch of control signals so that we can hear something
81
var timer = new maximJs.maxiOsc();//this is the metronome
Dr-Dan's avatar
Dr-Dan committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var currentCount,lastCount, voice = 0;//these values are used to check if we have a new beat this sample


var mix;
var pitch = [1,2,3,4,5,6]; // for some reason this must be done


function setup(){
setNumChannels(true, 2);
}

function play(){	
	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>=6) {
			voice=0;
		}
		
		ADSR[voice].trigger(0, adsrEnv.get(0));//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<6; i++) {

	ADSRout[i]=ADSR[i].line(8,adsrEnv);//our ADSR env has 8 value/time pairs.
	
	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
	
	// some kind of NaN problem here !!!
	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]/6;//finally we add the ADSR as an amplitude modulator 
}

	output[0]=mix*0.5;//left channel
	output[1]=mix*0.5;//right channel
}


</script>

</head>

<body>

	<h1> polysynth </h1>

	<p>
		This shows how to use maximilian to build a polyphonic synth.
	</p>

</body></html>