File size: 10,195 Bytes
5268b4e |
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
/**
* ------------------------------------------------
* Harmonic class.
* ------------------------------------------------
*/
var backgroundDots = [];
var Harmonic = function(indPm, controllerPm) {
// my index number
this.ind = indPm;
this.m = controllerPm;
// my harmonic series number
this.series = this.ind + 1;
// how many periods will I draw
this.periods = (this.series / 2);
// Stroke size.
this.ratioStroke = 0.0;
this.strokeMin = 2.8; this.strokeMax = 3.3;
// width and height of the bounding box drawing area of each harmonic
this.boxWidth; this.boxHeight;
// my position
this.xp; this.yp;
// Amplitude range.
this.amplitudeMin; this.amplitudeMax;
// Current amplitude is stored as a ratio from 0 to 1 between min and max
this.amplitudeRatio = 0;
// Period size - length of a full sine wave cycle
this.halfPeriodLength;
// color range - r, g, b, a - stored as both current color and ratio 0 to 1
var arrColorRange = [
[190, 190, 190, 1.0], // Starting color: grey
[255, 183, 41, 1.0] // Ending color: when highlighted
];
this.colorMin = arrColorRange[0];
this.colorMax = arrColorRange[1];
this.colorCurr = this.colorMin;
// Current color is stored as a ratio in that range
this.colorRatio = 0;
// counter
this.oscCt = 0 -this.ind * 0.6;
// Am I currently playing?
this.isPlaying = false;
// My volume range (decibels, e.g. 0, -1, -2 etc.)
this.volumeMax = -2; this.volumeMin = -2;
this.volumeRatio = 1;
// Easing ratio (0 to 1)
this.easingRelease = 0.04;
this.easingAttack = 0.2;
this.easing = this.easingAttack;
// My overall ratio of pressed amount from 0 to 1
this.ratioTarget = 0.0;
this.ratioCurr = 0.0;
// store whether a touch event is currently touching me
this.isATouchOverMe = false;
//
this.init();
}
/**
* ------------------------------------------------
* Initialize.
* ------------------------------------------------
*/
Harmonic.prototype.init = function() {
// set the canvas I draw to
this.cv = ctx;
// create synth object
this.synth = new Tone.Synth().toMaster();
// What kind of waveform? "sine" "triangle" etc. Passed in via URL e.g. /?sound=sine
// If not passed in, it defaults to "sine"
this.synth.oscillator.type = SOUND; // "triangle";
this.synth.envelope.attack = 100;
this.synth.envelope.decay = 4;
this.synth.envelope.sustain = 0.9;
this.synth.envelope.release = 10;
// Add sub-octave synth setup
this.subSynth = new Tone.Synth().toMaster();
this.subSynth.oscillator.type = "sawtooth2";
this.subSynth.envelope.attack = 4;
this.subSynth.envelope.decay = 0.5;
this.subSynth.envelope.sustain = 0.9;
this.subSynth.envelope.release = 1.1;
this.subSynth.volume.value = -10;
this.setVolume(0);
}
/**
* ------------------------------------------------
* Set volume.
* ------------------------------------------------
*/
Harmonic.prototype.setVolume = function(r) {
this.volumeRatio = r;
var harmonicStrength = 1 - (this.ind / this.m.harmonics);
var mainVolume = lerp(this.volumeMin, this.volumeMax, this.volumeRatio) + harmonicStrength * 6;
var subVolume = lerp(this.volumeMin, this.volumeMax - 10, this.volumeRatio) + harmonicStrength * 6;
this.synth.volume.value = mainVolume;
this.subSynth.volume.value = subVolume;
}
/**
* ------------------------------------------------
* Update function.
* ------------------------------------------------
*/
Harmonic.prototype.upd = function() {
this.oscCt += 0.9; // ensure background dots animate regardless of playback
this.ratioCurr += (this.ratioTarget - this.ratioCurr) * this.easing;
this.amplitudeRatio = this.colorRatio = this.strokeRatio = this.ratioCurr;
// Sin goes from -1 to 1. Normalize to a 0 to 1 range
var ct = (1 + Math.sin(this.oscCt)) / 2;
// make it quiver
if (this.isPlaying) {
this.amplitudeRatio += ct * 0.15;
}
// Calculate current color and size
this.colorCurr = lerpColor(this.colorMin, this.colorMax, this.colorRatio);
var strokeBase = lerp(this.strokeMin, this.strokeMax, this.strokeRatio);
var strokeCurr = strokeBase;
if (this.isPlaying) {
strokeCurr = strokeBase + Math.sin(this.oscCt * 2) * 1.2; // sine wave modulated stroke
}
var amplitudeCurr = lerp(this.amplitudeMin, this.amplitudeMax, this.amplitudeRatio);
// Arrange circles in concentric layout
var centerX = width / 2;
var centerY = height / 2;
var baseRadius = 30;
var stepRadius = 15;
var radius = 3 * (baseRadius + (this.m.harmonics - this.ind - 1) * stepRadius + (amplitudeCurr * 0.5));
var x = centerX;
var y = centerY;
var transparentColor = [this.colorCurr[0], this.colorCurr[1], this.colorCurr[2], 0.1];
if (this.isPlaying) {
// Draw sine wave
var wavelength = 440;
var amplitude = radius * 0.05;
var segments = 1000;
this.cv.beginPath();
for (let i = 0; i <= segments; i++) {
var angle = (i / segments) * Math.PI * 2;
var r = radius + Math.sin(angle * this.series + this.oscCt * 0.05) * amplitude;
var px = centerX + Math.cos(angle) * r;
var py = centerY + Math.sin(angle) * r;
if (i === 0) {
this.cv.moveTo(px, py);
} else {
this.cv.lineTo(px, py);
}
}
this.cv.closePath();
this.cv.save();
this.cv.shadowColor = getColor(this.colorCurr);
this.cv.shadowBlur = 20;
this.cv.fillStyle = getColor(transparentColor);
this.cv.fill();
this.cv.lineWidth = strokeCurr;
this.cv.strokeStyle = getColor(this.colorCurr);
this.cv.stroke();
this.cv.restore();
} else {
// Draw a filled circle
this.cv.save();
this.cv.shadowColor = getColor(this.colorCurr);
this.cv.shadowBlur = 20;
this.cv.beginPath();
this.cv.arc(x, y, radius, 0, Math.PI * 2);
this.cv.fillStyle = getColor(transparentColor);
this.cv.fill();
this.cv.lineWidth = strokeCurr;
this.cv.strokeStyle = getColor(this.colorCurr);
this.cv.stroke();
this.cv.closePath();
this.cv.restore();
}
if (this.isPlaying) {
var dotSpacing = 5;
var dotSize = 3.5;
var segments = 300;
var freq = this.series;
var amp = amplitudeCurr * 30;
for (let i = 0; i <= segments; i++) {
let angle = (i / segments) * Math.PI * 2;
let sineOffset = Math.sin(angle * freq + this.oscCt * 0.05) * amp;
let r = radius + sineOffset;
let px = centerX + Math.cos(angle) * r;
let py = centerY + Math.sin(angle) * r;
this.cv.beginPath();
this.cv.arc(px, py, dotSize, 0, Math.PI * 2);
this.cv.fillStyle = getColor([255, 255, 255, 1.0]);
this.cv.fill();
}
}
}
/**
* ------------------------------------------------
* Update the size.
* ------------------------------------------------
*/
Harmonic.prototype.updateSizeAndPosition = function() {
//
var drawAreaWidth = (width - leftMargin - rightMargin);
var drawAreaHeight = (height - topMargin - bottomMargin);
// What's the aspect ratio? Width / height, e.g. 2:1 = 2.0.
var aspectRatio = drawAreaWidth / drawAreaHeight;
// Don't let it get too wide
var aspectRatioMax = TOP_ASPECT_RATIO_MAX;
var aspectRatioMin = TOP_ASPECT_RATIO_MIN;
if (aspectRatio > aspectRatioMax) {
drawAreaWidth = drawAreaHeight * aspectRatioMax;
} else if (aspectRatio < aspectRatioMin) {
drawAreaHeight = drawAreaWidth / aspectRatioMin;
}
// where is the upper left corner of this
var xLeft = (width - drawAreaWidth) / 2;
var yTop = topMargin;
// set length of the harmonic to the window height
this.boxHeight = drawAreaHeight;
this.boxWidth = (drawAreaWidth / this.m.harmonics);
// Size of the half-period - how many px for a single crest to go up and down
this.periodLength = (this.boxHeight / this.series) * 2;
this.halfPeriodLength = (this.boxHeight / this.series);
// set max amplitude size. Just make it almost fill up the box.
this.amplitudeMax = (this.boxHeight * 0.04) / 2;
// Don't let them get bigger than the box width though
var maxLimit = this.boxWidth * 0.5;
var minLimit = this.boxWidth * 0.2;
if (this.amplitudeMax > maxLimit) {
this.amplitudeMax = maxLimit;
} else if (this.amplitudeMax < minLimit) {
this.amplitudeMax = minLimit;
}
//this.amplitudeMax = this.boxWidth * 0.5;
// Set the minimum amplitude to a little bit smaller than the max.
this.amplitudeMin = this.amplitudeMax * 0.75;
// Set my position
this.xp = xLeft + this.boxWidth * this.ind;
this.yp = yTop;
}
/**
* ------------------------------------------------
* Check if given x,y value intersects with this one (radial detection)
* ------------------------------------------------
*/
Harmonic.prototype.checkIfATouchIsOverMe = function(x, y) {
var centerX = width / 2;
var centerY = height / 2;
var baseRadius = 30;
var stepRadius = 15;
var radius = 2 * (baseRadius + (this.m.harmonics - this.ind - 1) * stepRadius);
var dx = x - centerX;
var dy = y - centerY;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= radius) {
this.isATouchOverMe = true;
if (!this.isPlaying) {
this.startPlaying();
}
} else {
this.isATouchOverMe = false;
if (this.isPlaying) {
this.stopPlaying();
}
}
}
/**
* ------------------------------------------------
* Check if given x,y value intersects with this one
* ------------------------------------------------
*/
Harmonic.prototype.checkIfIShouldBePlaying = function() {
if (this.isATouchOverMe) {
if (!this.isPlaying) {
this.startPlaying();
}
} else {
if (this.isPlaying) {
this.stopPlaying();
}
}
}
/**
* ------------------------------------------------
* Start playing
* ------------------------------------------------
*/
Harmonic.prototype.startPlaying = function() {
this.isPlaying = true;
var baseFreq = 136.10;
var freq = baseFreq * (this.ind + 1);
this.synth.triggerAttack(freq);
this.subSynth.triggerAttack(freq / 4);
this.easing = this.easingAttack;
this.ratioTarget = 1.0;
}
/**
* ------------------------------------------------
* Stop playing
* ------------------------------------------------
*/
Harmonic.prototype.stopPlaying = function() {
this.isPlaying = false;
if (audioContext.state == "running") this.synth.triggerRelease();
this.subSynth.triggerRelease();
this.easing = this.easingRelease;
this.ratioTarget = 0.0;
}
|