-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
176 lines (148 loc) · 5.66 KB
/
scripts.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
170
171
172
173
174
175
let audioContext;
let oscillator;
let gainNode;
let lfo;
let lfoGainNode;
let isMouseDown = false;
const waveforms = ['sine', 'square', 'sawtooth', 'triangle'];
let oscillatorWaveformIndex = 0;
let lfoWaveformIndex = 0;
function createAudioContext() {
if (audioContext) return;
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
function initializeAudio() {
gainNode = audioContext.createGain();
gainNode.gain.value = 0.1;
gainNode.connect(audioContext.destination);
lfoGainNode = audioContext.createGain();
lfoGainNode.gain.value = 50; // Adjust this value to control the modulation depth
}
function startTone(frequency, lfoFrequency) {
oscillator = audioContext.createOscillator();
oscillator.type = waveforms[oscillatorWaveformIndex]; // Add this line
oscillator.frequency.value = frequency;
lfo = audioContext.createOscillator();
lfo.type = waveforms[lfoWaveformIndex]; // Add this line
lfo.frequency.value = lfoFrequency;
lfo.connect(lfoGainNode);
lfoGainNode.connect(oscillator.frequency);
oscillator.connect(gainNode);
oscillator.start();
lfo.start();
}
function stopTone() {
if (oscillator) {
oscillator.stop();
oscillator.disconnect();
}
if (lfo) {
lfo.stop();
lfo.disconnect();
}
}
function getFrequencyFromMousePosition(event) {
const windowHeight = window.innerHeight;
const mouseY = event.clientY;
const frequencyRange = 1000 - 100;
const frequency = 1000 - (mouseY / windowHeight) * frequencyRange;
return frequency;
}
function getLfoFrequencyFromMousePosition(event) {
const windowWidth = window.innerWidth;
const mouseX = event.clientX;
const lfoFrequencyRange = 10 - .1;
const lfoFrequency = .1 + (mouseX / windowWidth) * lfoFrequencyRange;
return lfoFrequency;
}
// mouse down
document.addEventListener('mousedown', (event) => {
isMouseDown = true;
createAudioContext();
initializeAudio();
const frequency = getFrequencyFromMousePosition(event);
const lfoFrequency = getLfoFrequencyFromMousePosition(event);
startTone(frequency, lfoFrequency);
document.body.style.backgroundColor = getColorFromMousePosition(event);
});
// touching screen
document.addEventListener('touchstart', async (event) => {
isMouseDown = true;
event.preventDefault();
createAudioContext();
initializeAudio();
const frequency = getFrequencyFromMousePosition(event.touches[0]);
const lfoFrequency = getLfoFrequencyFromMousePosition(event.touches[0]);
startTone(frequency, lfoFrequency);
}, { passive: false });
// moving mouse
document.addEventListener('mousemove', (event) => {
if (!oscillator || !lfo || !isMouseDown) return;
const frequency = getFrequencyFromMousePosition(event);
const lfoFrequency = getLfoFrequencyFromMousePosition(event);
oscillator.frequency.value = frequency;
lfo.frequency.value = lfoFrequency;
oscillatorFrequencyValue.textContent = frequency.toFixed(0);
lfoFrequencyValue.textContent = lfoFrequency.toFixed(1);
document.body.style.backgroundColor = getColorFromMousePosition(event);
});
// moving finger
document.addEventListener('touchmove', (event) => {
event.preventDefault();
if (!oscillator || !lfo || !isMouseDown) return;
const frequency = getFrequencyFromMousePosition(event.touches[0]);
const lfoFrequency = getLfoFrequencyFromMousePosition(event.touches[0]);
oscillator.frequency.value = frequency;
lfo.frequency.value = lfoFrequency;
oscillatorFrequencyValue.textContent = frequency.toFixed(2);
lfoFrequencyValue.textContent = lfoFrequency.toFixed(2);
document.body.style.backgroundColor = getColorFromMousePosition(event.touches[0]);
}, { passive: false });
// lifting mouse
document.addEventListener('mouseup', () => {
isMouseDown = false;
stopTone();
});
// lifting finger
document.addEventListener('touchend', () => {
isMouseDown = false;
stopTone();
});
document.addEventListener('touchcancel', () => {
isMouseDown = false;
stopTone();
});
const oscillatorWaveformBtn = document.getElementById('oscillatorWaveformBtn');
const lfoWaveformBtn = document.getElementById('lfoWaveformBtn');
const oscillatorFrequencyValue = document.getElementById('oscillatorFrequencyValue');
const lfoFrequencyValue = document.getElementById('lfoFrequencyValue');
function onOscillatorWaveformBtnClick() {
console.log('click')
oscillatorWaveformIndex = (oscillatorWaveformIndex + 1) % waveforms.length;
if (oscillator) {
oscillator.type = waveforms[oscillatorWaveformIndex];
}
let oscillatorformLabel = document.getElementById('oscillatorWaveformLabel');
oscillatorformLabel.textContent = waveforms[oscillatorWaveformIndex];
}
function onLfoWaveformBtnClick() {
lfoWaveformIndex = (lfoWaveformIndex + 1) % waveforms.length;
if (lfo) {
lfo.type = waveforms[lfoWaveformIndex];
}
let lfoWaveformLabel = document.getElementById('lfoWaveformLabel');
lfoWaveformLabel.textContent = waveforms[lfoWaveformIndex];
}
oscillatorWaveformBtn.addEventListener('click', onOscillatorWaveformBtnClick);
oscillatorWaveformBtn.addEventListener('touchstart', onOscillatorWaveformBtnClick);
lfoWaveformBtn.addEventListener('click', onLfoWaveformBtnClick);
lfoWaveformBtn.addEventListener('touchstart', onLfoWaveformBtnClick);
// Background color fun!
function getColorFromMousePosition(event) {
const xRatio = event.clientX / window.innerWidth;
const yRatio = event.clientY / window.innerHeight;
const r = Math.floor(xRatio * 255);
const g = Math.floor(yRatio * 255);
const b = Math.floor((1 - xRatio) * (1 - yRatio) * 255);
return `rgb(${r}, ${g}, ${b})`;
}