-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremix.html
98 lines (92 loc) · 3.41 KB
/
remix.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Virtual Quantum Flute</title>
<style>
body {
font-family: 'Times New Roman', serif;
background-color: #ffffff;
color: #000000;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
padding: 10px;
}
#controls input[type="range"] {
width: 50%;
}
#quantum-canvas {
width: 90%;
height: 200px;
border: 1px solid #000000;
}
</style>
</head>
<body>
<div id="controls">
<button onclick="startQuantumFlute()">Play Quantum Flute</button>
<input type="range" min="0.5" max="1.5" step="0.01" value="1" oninput="updateModulation(this.value)" role="slider" aria-label="Modulation Factor" />
</div>
<canvas id="quantum-canvas"></canvas>
<script>
let audioContext;
let gainNode;
let quantumLoop;
let modulationFactor = 1;
let quantumStates = [];
const canvas = document.getElementById('quantum-canvas');
const ctx = canvas.getContext('2d');
const widthPerState = canvas.width / 4000;
function initializeAudioContext() {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
gainNode = audioContext.createGain();
gainNode.connect(audioContext.destination);
}
window.startQuantumFlute = function() {
if (!audioContext) {
initializeAudioContext();
}
clearInterval(quantumLoop);
quantumLoop = setInterval(runQuantumEmulation, 200);
}
function runQuantumEmulation() {
if (quantumStates.length >= 4000) {
quantumStates.shift(); // Remove the oldest state
}
quantumStates.push(Math.floor(Math.random() * 3));
displayWaveform();
playQuantumSound(quantumStates[quantumStates.length - 1]);
}
function displayWaveform() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
quantumStates.forEach((state, index) => {
ctx.beginPath();
ctx.strokeStyle = state === 0 ? 'blue' : state === 1 ? 'green' : 'red';
ctx.moveTo(widthPerState * index, canvas.height / 2);
ctx.lineTo(widthPerState * (index + 1), canvas.height / 2);
ctx.stroke();
});
}
function playQuantumSound(state) {
if (audioContext.state === 'suspended') {
audioContext.resume();
}
const oscillator = audioContext.createOscillator();
oscillator.type = state === 0 ? 'sine' : state === 1 ? 'square' : 'triangle';
oscillator.frequency.setValueAtTime((440 + state * 110) * modulationFactor, audioContext.currentTime);
oscillator.connect(gainNode);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.4);
}
window.updateModulation = function(value) {
modulationFactor = value;
gainNode.gain.value = Math.exp(value - 1);
}
</script>
</body>
</html>