-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
158 lines (148 loc) · 5.96 KB
/
index.html
File metadata and controls
158 lines (148 loc) · 5.96 KB
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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test MIDI Web App avec Réverb</title>
<style>
body {
font-family: monospace;
margin: 20px;
}
#console {
height: 300px;
overflow-y: scroll;
border: 1px solid #ccc;
padding: 10px;
background: #f9f9f9;
margin-bottom: 20px;
}
button {
padding: 8px 16px;
font-family: monospace;
}
</style>
</head>
<body>
<h1>Test MIDI Web App avec Réverb</h1>
<button id="start">Démarrer MIDI</button>
<div id="console"></div>
<script>
// Variables globales
let midiAccess;
const sounds = {};
let audioContext;
let masterGain;
let reverb;
let dryGain;
let wetGain;
let globalVolume = 0.7;
let globalPitch = 1.0;
let reverbAmount = 0.3; // Niveau de réverb (0 à 1)
// Fonction pour ajouter un message à la console
function log(message) {
const consoleEl = document.getElementById('console');
consoleEl.innerHTML += message + "<br>";
consoleEl.scrollTop = consoleEl.scrollHeight;
}
// Fonction pour précharger les sons
async function loadSounds() {
for (let note = 36; note <= 51; note++) {
const num = (note - 35).toString().padStart(4, '0');
const url = `https://lasonotheque.org/UPLOAD/mp3/${num}.mp3`;
sounds[note] = new Audio(url);
}
log("Sons chargés !");
}
// Fonction pour initialiser l'AudioContext et les effets
function initAudioContext() {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Nœud de gain principal
masterGain = audioContext.createGain();
masterGain.gain.value = globalVolume;
masterGain.connect(audioContext.destination);
// Création de la réverb (convolution)
const impulseResponseUrl = "https://raw.githubusercontent.com/mohayonao/web-audio-api-samples/master/samples/impulse/impulse-rev.wav";
fetch(impulseResponseUrl)
.then(response => response.arrayBuffer())
.then(buffer => audioContext.decodeAudioData(buffer))
.then(impulse => {
reverb = audioContext.createConvolver();
reverb.buffer = impulse;
reverb.connect(masterGain);
// Gains dry/wet pour mélanger son original et réverbéré
dryGain = audioContext.createGain();
wetGain = audioContext.createGain();
dryGain.gain.value = 1 - reverbAmount;
wetGain.gain.value = reverbAmount;
dryGain.connect(masterGain);
wetGain.connect(reverb);
})
.catch(err => log(`Erreur chargement réverb : ${err}`));
}
// Fonction pour démarrer MIDI
async function startMIDI() {
try {
midiAccess = await navigator.requestMIDIAccess();
log("MIDI démarré avec succès !");
initAudioContext();
await loadSounds();
for (const input of midiAccess.inputs.values()) {
input.onmidimessage = handleMIDIMessage;
log(`Écoute sur : ${input.name}`);
}
} catch (err) {
log(`Erreur MIDI : ${err}`);
}
}
// Fonction pour gérer les messages MIDI
function handleMIDIMessage(message) {
const [status, data1, data2] = message.data;
const command = status >> 4;
const channel = status & 0xf;
const note = data1;
const velocity = data2;
log(`Message MIDI : command=${command}, channel=${channel}, note=${note}, velocity=${velocity}`);
// Note On (144 = 0x90)
if (command === 9 && velocity > 0) {
log(`Note ON : ${note} (vélocité: ${velocity})`);
if (note >= 36 && note <= 51) {
if (sounds[note]) {
const source = audioContext.createMediaElementSource(sounds[note]);
source.connect(dryGain);
if (wetGain) source.connect(wetGain);
sounds[note].currentTime = 0;
sounds[note].volume = (velocity / 127) * globalVolume;
sounds[note].playbackRate = globalPitch;
sounds[note].play();
}
}
}
// Control Change (176 = 0xB0)
else if (command === 11) {
log(`Control Change : CC#${data1} (valeur: ${data2})`);
// CC#7 = Volume global
if (data1 === 7) {
globalVolume = data2 / 127;
masterGain.gain.value = globalVolume;
log(`Volume global : ${Math.round(globalVolume * 100)}%`);
}
// CC#1 = Pitch
else if (data1 === 1) {
globalPitch = 0.5 + (data2 / 127) * 1.5;
log(`Pitch : ${globalPitch.toFixed(2)}`);
}
// CC#30 = Réverb
else if (data1 === 30) {
reverbAmount = data2 / 127;
if (dryGain) dryGain.gain.value = 1 - reverbAmount;
if (wetGain) wetGain.gain.value = reverbAmount;
log(`Réverb : ${Math.round(reverbAmount * 100)}%`);
}
}
}
// Événements
document.getElementById('start').addEventListener('click', startMIDI);
</script>
</body>
</html>