-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrumcontainer.js
executable file
·170 lines (155 loc) · 6.29 KB
/
spectrumcontainer.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//Spectrum container
//implements partial and spectrum classes
//----------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------//
//Partial class
//Constructor of the partial class
//----------------------------------------------------------------------------------------//
function partial(f, a) {
this.freq = f;
this.amp = a;
this.startDate = 0; //in milliseconds//
}
//----------------------------------------------------------------------------------------//
//This method gets the frequency of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.getFreq = function() {
return this.freq;
}
//----------------------------------------------------------------------------------------//
//This method gets the amplitude of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.getAmp = function() {
return this.amp;
}
//----------------------------------------------------------------------------------------//
//This method gets the startdate of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.getStartDate = function() {
return this.startDate;
}
//----------------------------------------------------------------------------------------//
//This method sets the frequency of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.setFreq = function(f) {
this.freq = f;
}
//----------------------------------------------------------------------------------------//
//This method sets the amplitude of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.setAmp = function(a) {
this.amp = a;
}
//----------------------------------------------------------------------------------------//
//This method sets the startdate of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.setStartDate = function(d) {
this.startDate = d;
}
//----------------------------------------------------------------------------------------//
//This method draws randomly the startdate of the partial
//----------------------------------------------------------------------------------------//
partial.prototype.drawStartDate = function(dateMax) {
this.startDate = Math.floor(Math.random() * dateMax);
}
//----------------------------------------------------------------------------------------//
//This method shifts the frequency of the class from f0 (in Hz)
//----------------------------------------------------------------------------------------//
partial.prototype.freqshift = function(f0) {
this.freq = this.freq + f0;
}
//----------------------------------------------------------------------------------------//
//This method multiplies the frequency of the class by a factor
//----------------------------------------------------------------------------------------//
partial.prototype.freqmult = function(factor) {
this.freq = this.freq * factor;
}
//----------------------------------------------------------------------------------------//
//This method prints in the console the content of the partial class
//----------------------------------------------------------------------------------------//
partial.prototype.print = function() {
post("frequency=", this.freq, " amplitude=", this.amp, " startDate=", this.startDate, "\n");
}
//----------------------------------------------------------------------------------------//
//To test partial class
//----------------------------------------------------------------------------------------//
function partial_test() {
var f, a, d;
p1 = new partial(440, 0.3);
post("p1_____", "\n");
f = p1.getFreq();
post("getFreq sur p1=", f, "\n");
a = p1.getAmp();
post("getAmp sur p1=", a, "\n");
d = p1.getStartDate();
post("getStartDate sur p1=", d, "\n");
p1.print();
post("modification freq, amp, startDate\n");
p1.setFreq(450);
p1.setAmp(0.4);
p1.setStartDate(100);
p1.print();
post("draws randomnly the startDate\n");
p1.drawStartDate(5000);
p1.print();
post("_____après freqshift de 100\n");
p1.freqshift(100);
p1.print();
post("_____après freqmult de 3\n");
p1.freqmult(3);
p1.print();
}
//----------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------//
//Spectrum class
//Constructor of the partial class
function spectrum(N) {
var i;
this.partialNumber = N;
this.partialList = new Array(N);
for (i = 0; i < N; i++) {
this.partialList[i] = new partial(i*50, 0);
}
}
//----------------------------------------------------------------------------------------//
//This method removes the partial at # index from the spectrum
//----------------------------------------------------------------------------------------//
spectrum.prototype.removePartial = function(index) {
var n;
n = this.partialNumber;
if ((index !== -1) && (n > 1)) {
this.partialList.splice(index, 1);
this.partialNumber = n - 1;
}
}
//----------------------------------------------------------------------------------------//
//This method draws randomly the startdates of the partials of the spectrum
//----------------------------------------------------------------------------------------//
spectrum.prototype.drawStartDates = function(dateMax) {
var i, n;
n = this.partialNumber;
for (i = 0; i < n; i++) {
this.partialList[i].drawStartDate(dateMax);
}
}
//----------------------------------------------------------------------------------------//
//This method prints in the console the content of the spectrum class
//----------------------------------------------------------------------------------------//
spectrum.prototype.print = function() {
var i, n;
n = this.partialNumber;
post("______Number of partials =", n, "\n");
for (i = 0; i < n; i++) {
this.partialList[i].print();
}
}
function spectrum_test() {
s1 = new spectrum(50);
s1.print();
post("Draws startDates\n");
s1.drawStartDates(2000);
s1.print();
post("Removes partial#25\n");
s1.removePartial(25);
s1.print();
}