1.1
Browse files- advanced_demo.js → play-midi.js +4 -111
advanced_demo.js → play-midi.js
RENAMED
|
@@ -20,7 +20,7 @@ fetch(
|
|
| 20 |
).then(async (response) => {
|
| 21 |
// Load the soundfont into an array buffer
|
| 22 |
let soundFontBuffer = await response.arrayBuffer();
|
| 23 |
-
document.getElementById("message").innerText = "O
|
| 24 |
|
| 25 |
// Create the context and add audio worklet
|
| 26 |
const context = new AudioContext();
|
|
@@ -73,9 +73,9 @@ fetch(
|
|
| 73 |
}, 100);
|
| 74 |
|
| 75 |
seq.addOnSongChangeEvent((e) => {
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
slider.onchange = () => {
|
| 81 |
seq.currentTime = (slider.value / 1000) * seq.duration;
|
|
@@ -106,110 +106,3 @@ fetch(
|
|
| 106 |
gainNode.gain.value = parseFloat(volumeControl.value); // Adjust the volume
|
| 107 |
});
|
| 108 |
});
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
/*var creditsyear = new Date();
|
| 113 |
-
document.getElementById("datacedencer").innerHTML = creditsyear.getFullYear();*/
|
| 114 |
-
|
| 115 |
-
/*
|
| 116 |
-
|
| 117 |
-
// import the modules
|
| 118 |
-
import { WORKLET_URL_ABSOLUTE } from "./spessasynth_lib/synthetizer/worklet_url.js";
|
| 119 |
-
import { Sequencer } from "./spessasynth_lib/sequencer/sequencer.js";
|
| 120 |
-
import { Synthetizer } from "./spessasynth_lib/synthetizer/synthetizer.js";
|
| 121 |
-
|
| 122 |
-
// load the soundfont
|
| 123 |
-
fetch("./spessasynth_lib/soundfonts/GeneralUserGS.sf3").then(async response =>
|
| 124 |
-
{
|
| 125 |
-
// load the soundfont into an array buffer
|
| 126 |
-
let soundFontBuffer = await response.arrayBuffer();
|
| 127 |
-
document.getElementById("message").innerText = "SoundFont has been loaded!";
|
| 128 |
-
|
| 129 |
-
// create the context and add audio worklet
|
| 130 |
-
const context = new AudioContext();
|
| 131 |
-
await context.audioWorklet.addModule(new URL("./spessasynth_lib/" + WORKLET_URL_ABSOLUTE, import.meta.url));
|
| 132 |
-
const synth = new Synthetizer(context.destination, soundFontBuffer); // create the synthetizer
|
| 133 |
-
let seq;
|
| 134 |
-
|
| 135 |
-
// add an event listener for the file inout
|
| 136 |
-
document.getElementById("midi_input").addEventListener("change", async event =>
|
| 137 |
-
{
|
| 138 |
-
// check if any files are added
|
| 139 |
-
if (!event.target.files[0])
|
| 140 |
-
{
|
| 141 |
-
return;
|
| 142 |
-
}
|
| 143 |
-
// resume the context if paused
|
| 144 |
-
await context.resume();
|
| 145 |
-
// parse all the files
|
| 146 |
-
const parsedSongs = [];
|
| 147 |
-
for (let file of event.target.files)
|
| 148 |
-
{
|
| 149 |
-
const buffer = await file.arrayBuffer();
|
| 150 |
-
parsedSongs.push({
|
| 151 |
-
binary: buffer, // binary: the binary data of the file
|
| 152 |
-
altName: file.name // altName: the fallback name if the MIDI doesn't have one. Here we set it to the file name
|
| 153 |
-
});
|
| 154 |
-
}
|
| 155 |
-
if (seq === undefined)
|
| 156 |
-
{
|
| 157 |
-
seq = new Sequencer(parsedSongs, synth); // create the sequencer with the parsed midis
|
| 158 |
-
seq.play(); // play the midi
|
| 159 |
-
}
|
| 160 |
-
else
|
| 161 |
-
{
|
| 162 |
-
seq.loadNewSongList(parsedSongs); // the sequencer is already created, no need to create a new one.
|
| 163 |
-
}
|
| 164 |
-
seq.loop = false; // the sequencer loops a single song by default
|
| 165 |
-
|
| 166 |
-
// make the slider move with the song
|
| 167 |
-
let slider = document.getElementById("progress");
|
| 168 |
-
setInterval(() =>
|
| 169 |
-
{
|
| 170 |
-
// slider ranges from 0 to 1000
|
| 171 |
-
slider.value = (seq.currentTime / seq.duration) * 1000;
|
| 172 |
-
}, 100);
|
| 173 |
-
|
| 174 |
-
// on song change, show the name
|
| 175 |
-
seq.addOnSongChangeEvent(e =>
|
| 176 |
-
{
|
| 177 |
-
document.getElementById("message").innerText = "Now playing: " + e.midiName;
|
| 178 |
-
}, "example-time-change"); // make sure to add a unique id!
|
| 179 |
-
|
| 180 |
-
// add time adjustment
|
| 181 |
-
slider.onchange = () =>
|
| 182 |
-
{
|
| 183 |
-
// calculate the time
|
| 184 |
-
seq.currentTime = (slider.value / 1000) * seq.duration; // switch the time (the sequencer adjusts automatically)
|
| 185 |
-
};
|
| 186 |
-
|
| 187 |
-
// add button controls
|
| 188 |
-
document.getElementById("previous").onclick = () =>
|
| 189 |
-
{
|
| 190 |
-
seq.previousSong(); // go back by one song
|
| 191 |
-
};
|
| 192 |
-
|
| 193 |
-
// on pause click
|
| 194 |
-
document.getElementById("pause").onclick = () =>
|
| 195 |
-
{
|
| 196 |
-
if (seq.paused)
|
| 197 |
-
{
|
| 198 |
-
document.getElementById("pause").innerText = "Pause";
|
| 199 |
-
seq.play(); // resume
|
| 200 |
-
}
|
| 201 |
-
else
|
| 202 |
-
{
|
| 203 |
-
document.getElementById("pause").innerText = "Resume";
|
| 204 |
-
seq.pause(); // pause
|
| 205 |
-
|
| 206 |
-
}
|
| 207 |
-
};
|
| 208 |
-
document.getElementById("next").onclick = () =>
|
| 209 |
-
{
|
| 210 |
-
seq.nextSong(); // go to the next song
|
| 211 |
-
};
|
| 212 |
-
});
|
| 213 |
-
});
|
| 214 |
-
|
| 215 |
-
*/
|
|
|
|
| 20 |
).then(async (response) => {
|
| 21 |
// Load the soundfont into an array buffer
|
| 22 |
let soundFontBuffer = await response.arrayBuffer();
|
| 23 |
+
document.getElementById("message").innerText = "O PLAY MIDI FOI CARREGADO!";
|
| 24 |
|
| 25 |
// Create the context and add audio worklet
|
| 26 |
const context = new AudioContext();
|
|
|
|
| 73 |
}, 100);
|
| 74 |
|
| 75 |
seq.addOnSongChangeEvent((e) => {
|
| 76 |
+
document.getElementById("message").innerHTML =
|
| 77 |
+
"Tocando agora: " + e.midiName + "<img type='button 'title='CAPA' src='./d-framework/icon/144/favicon.png' class='img-thumbnail rounded-circle border-2 p-0 bg-hover border-warning ms-2' width='40' height='40' />";
|
| 78 |
+
}, "example-time-change");
|
| 79 |
|
| 80 |
slider.onchange = () => {
|
| 81 |
seq.currentTime = (slider.value / 1000) * seq.duration;
|
|
|
|
| 106 |
gainNode.gain.value = parseFloat(volumeControl.value); // Adjust the volume
|
| 107 |
});
|
| 108 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|