-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
155 lines (135 loc) · 3.74 KB
/
index.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
'use strict';
// Soundfont collection name (e.g. FluidR3_GM)
const col_name = "FluidR3_GM";
// Soundfont extension (ogg/mp3)
var sf_ext;
// Automatic extension selection
if (document.getElementById("audio-format-test").canPlayType("audio/ogg"))
{
sf_ext = "ogg";
}
else
{
sf_ext = "mp3";
}
// Parsed MIDI contents
var MIDIContents;
// Sound state (muted/unmuted)
var sound_enabled = false;
// Load and parse a MIDI file into MIDIContents, then call a function on success
function loadMIDIFile(call_func)
{
return new Promise(function(resolve, reject)
{
// Get file
const file = document.getElementById("file-in").files[0];
console.info("Loading \"" + file.name + "\" (" + file.size + " B)");
// Check for non-MIDIs
if (file.type != "audio/midi")
{
console.error("The inputted file is not a MIDI file");
reject("The inputted file is not a MIDI file");
}
// Read file
file.arrayBuffer().then(
// Read successful
function(contents)
{
console.log(contents);
// Must be seperate
MIDIContents = new Uint8Array(contents);
MIDIContents = parseMidi(MIDIContents);
console.info(MIDIContents);
// Call the function
call_func.call();
resolve("MIDI file loaded successfully");
}
).catch(
// Read failed
function()
{
console.error("Failed to read MIDI file");
reject("Failed to read MIDI file");
}
);
});
}
// Download a file (https://stackoverflow.com/questions/54626186/how-to-download-file-with-javascript)
function downloadURI(uri, name)
{
var link = document.createElement("a");
link.download = name;
link.href = uri;
link.click();
link.remove();
}
// Parse MIDIContents and generate a downloadable MIDI File
function saveMIDIFile(name)
{
let rectifiedArray = new Uint8Array(writeMidi(MIDIContents));
console.log(rectifiedArray);
let blob = new File([rectifiedArray], {type: "audio/midi"});
console.info(blob);
saveAs(blob, name);
}
// Toggle the mute/unmute button in the navbar
function toggleMute()
{
// Get element
var ind = document.getElementById("sound-toggle");
// Toggle
if (!sound_enabled)
{
// Unmute
if (!ac)
{
audioCtxInit();
}
else
{
ac.resume();
}
ind.setAttribute("class", "bi bi-volume-up-fill");
sound_enabled = true;
}
else
{
// Mute
ac.suspend();
ind.setAttribute("class", "bi bi-volume-mute");
sound_enabled = false;
}
}
// Set state of progress bar
function setProgress(value, vmin, vmax, visible, message)
{
var barEl = document.getElementById("progress");
var txtEl = document.getElementById("progress-txt");
// Set percentage
const val = ((value - vmin) / (vmax - vmin) * 100).toFixed(3) + "%";
barEl.firstElementChild.style.width = val;
barEl.firstElementChild.textContent = val;
txtEl.textContent = message;
// Set visibility
if (visible)
{
txtEl.style.display = "inline-block";
barEl.style.display = "flex";
}
else
{
txtEl.style.display = "none";
barEl.style.display = "none";
}
barEl.setAttribute("aria-valuenow", value);
barEl.setAttribute("aria-valuemin", vmin);
barEl.setAttribute("aria-valuemax", vmax);
}
// Automatically initialize components on window load
window.onload = function()
{
loadNavs();
createCurChDisp();
updateCurChDisp();
setProgress(0, 0, 0, false, "");
}