Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / doppler.js

Raw 2.0 KB on 8b38e375b3e16f98454b93adca8e1fbd4fc534c5
1WE.mount('doppler', function (S) {
2 var C = S.C, ctx = S.ctx, x = 0, rings = [], acc = 0, Cs = 120;
3 var v = S.slider('Speed of the source', 0, 1.6, .5, .05, function (m) { return m === 0 ? 'Standing still' : (m * 100).toFixed(0) + '% of the speed of sound'; });
4 S.button('Hear it pass', function () { var m = Math.min(.9, v.v); try { var o = S.tone(440 / (1 - m), 2.2, 'sawtooth', .06); o.frequency.setValueAtTime(440 / (1 - m), o.context.currentTime); o.frequency.exponentialRampToValueAtTime(440 / (1 + m), o.context.currentTime + 1.3); } catch (e) { } });
5 S.loop(function (dt) {
6 var w = S.W, h = S.H, m = v.v; x += m * Cs * dt; if (x > w + 80) { x = -80; rings = []; }
7 acc += dt; while (acc > .22) { acc -= .22; rings.push({ x: x, r: 0 }); }
8 rings.forEach(function (r) { r.r += Cs * dt; }); rings = rings.filter(function (r) { return r.r < w; });
9 S.clear(); rings.forEach(function (r) { S.ring(r.x, h / 2, r.r, 'rgba(111,177,230,' + Math.max(.08, .7 - r.r / w) + ')', 1.5); });
10 if (m > 1) { var a = Math.asin(1 / m), L = w; S.ln(x, h / 2, x - L * Math.cos(a), h / 2 - L * Math.sin(a), C.red, 2); S.ln(x, h / 2, x - L * Math.cos(a), h / 2 + L * Math.sin(a), C.red, 2); }
11 S.dot(x, h / 2, 9, C.acc); S.arrow(x + 12, h / 2, x + 22 + m * 30, h / 2, C.acc, 2);
12 var f = 440, ahead = m < 1 ? f / (1 - m) : null, behind = f / (1 + m);
13 S.txt('Behind: ' + behind.toFixed(0) + ' Hz, lower', 14, 22, C.blue, 13.5, 'left', 600); S.txt(ahead ? 'Ahead: ' + ahead.toFixed(0) + ' Hz, higher' : 'Ahead: silence, then a boom', w - 60, 22, C.red, 13.5, 'right', 600);
14 S.say(m === 0 ? 'A still source sends out even rings: a steady <b>440 Hz</b> note in every direction.' : m < 1 ? 'The source chases its own waves. They bunch up in front, so the pitch is <b>higher</b>, and stretch out behind, so it is <b>lower</b>. That is the drop you hear as a siren passes.' : 'Faster than sound. The waves cannot get ahead, so they pile into a cone: a <b>shock wave</b>, heard on the ground as a sonic boom.');
15 });
16});