-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.js
110 lines (85 loc) · 3.04 KB
/
11.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
"use strict";
// Theremin control functions
function thereminOn(oscillator) {
oscillator.play();
}
function thereminControl(e, oscillator, theremin) {
let x = e.offsetX;
let y = e.offsetY;
console.log(x, y);
let minFrequency = 220.0;
let maxFrequency = 880.0;
let freqRange = maxFrequency - minFrequency;
let thereminFreq = minFrequency + (x / theremin.clientWidth) * freqRange;
let thereminVolume = 1.0 - (y / theremin.clientHeight);
if (document.getElementById("pFifth").checked)
{
thereminVolume /= 2;
}
let roundedFreq = midiToFrequency(Math.round(frequencyToMidi(thereminFreq)));
if (document.getElementById("autotune").checked)
{
thereminFreq = roundedFreq;
}
let dispFreq = Math.round(thereminFreq * 100) / 100;
let noteName = noteFromFrequency(thereminFreq);
console.log("Frequency: ", thereminFreq);
console.log("Volume: ", thereminVolume);
oscillator.frequency = thereminFreq;
oscillator.volume = thereminVolume;
document.getElementById("dispFreq").innerHTML = `Frequency: ${dispFreq}`;
document.getElementById("noteName").innerHTML = `Note: ${noteName}`;
}
function thereminFifth(e, oscillator, theremin) {
let x = e.offsetX;
let y = e.offsetY;
console.log(x, y);
let minFrequency = 220.0;
let maxFrequency = 880.0;
let freqRange = maxFrequency - minFrequency;
let thereminFreq = minFrequency + (x / theremin.clientWidth) * freqRange;
thereminFreq = interval(thereminFreq, 7);
let thereminVolume = (1.0 - (y / theremin.clientHeight)) / 2;
let roundedFreq = midiToFrequency(Math.round(frequencyToMidi(thereminFreq)));
if (document.getElementById("autotune").checked)
{
thereminFreq = roundedFreq;
}
console.log("Frequency fifth: ", thereminFreq);
console.log("Volume fifth: ", thereminVolume);
oscillator.frequency = thereminFreq;
oscillator.volume = thereminVolume;
}
function thereminOff(oscillator) {
oscillator.stop();
}
// Initialize theremin on page load
function runAfterLoadingPage() {
let waveform = "sine";
let urlParameters = new URL(document.location).searchParams;
if (urlParameters.has('waveform'))
{
waveform = urlParameters.get('waveform');
}
document.getElementById(waveform).checked = true;
const oscillator = createThereminSound(waveform);
const oscFifth = createThereminSound(waveform);
const theremin = document.getElementById("thereminZone");
theremin.addEventListener("mouseenter", function () {
thereminOn(oscillator);
if (document.getElementById("pFifth").checked)
{
thereminOn(oscFifth);
}
console.log("Theremin is ON");
});
theremin.addEventListener("mousemove", function (e) {
thereminControl(e, oscillator, theremin);
thereminFifth(e, oscFifth, theremin);
});
theremin.addEventListener("mouseleave", function () {
thereminOff(oscillator);
thereminOff(oscFifth);
});
}
window.onload = runAfterLoadingPage;