-
-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathmidi-load.html
More file actions
112 lines (100 loc) · 3.59 KB
/
Copy pathmidi-load.html
File metadata and controls
112 lines (100 loc) · 3.59 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>dawcore + Tone.js — Load MIDI</title>
<style>
body {
font-family: system-ui, sans-serif;
background: #0f0f1a;
color: #e0d4c8;
padding: 24px;
}
h1 { font-size: 1.2rem; margin-bottom: 16px; }
daw-editor {
--daw-wave-color: #c49a6c;
--daw-playhead-color: #d08070;
--daw-background: #1a1a2e;
--daw-track-background: #16213e;
--daw-ruler-color: #c49a6c;
--daw-ruler-background: #0f0f1a;
--daw-piano-roll-note-color: #2a7070;
--daw-piano-roll-selected-note-color: #3d9e9e;
--daw-piano-roll-background: #1a1a2e;
margin-bottom: 12px;
}
.toolbar { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }
daw-transport { display: flex; gap: 8px; }
#log {
margin-top: 12px;
font-family: monospace;
font-size: 0.75rem;
color: #888;
max-height: 160px;
overflow-y: auto;
}
button { padding: 6px 12px; }
</style>
</head>
<body>
<h1>dawcore + Tone.js — Load a .mid file</h1>
<script type="module">import '@dawcore/components';</script>
<div class="toolbar">
<button id="load-url">Load from URL</button>
<label>
Load file:
<input id="load-file" type="file" accept=".mid,.midi" />
</label>
</div>
<daw-editor id="editor" samples-per-pixel="2048" wave-height="120" timescale>
<daw-keyboard-shortcuts playback></daw-keyboard-shortcuts>
</daw-editor>
<daw-transport for="editor">
<daw-play-button></daw-play-button>
<daw-pause-button></daw-pause-button>
<daw-stop-button></daw-stop-button>
</daw-transport>
<div id="log"></div>
<script type="module">
import { createToneAdapter } from '@waveform-playlist/playout';
const editor = document.getElementById('editor');
editor.adapter = createToneAdapter({ ppqn: 960 });
const log = document.getElementById('log');
function addLog(msg) {
const line = document.createElement('div');
line.textContent = msg;
log.prepend(line);
while (log.children.length > 30) log.lastChild.remove();
}
editor.addEventListener('daw-track-ready', (e) => addLog('track-ready: ' + e.detail.trackId));
editor.addEventListener('daw-error', (e) => addLog('error: ' + e.detail.operation + ' — ' + String(e.detail.error)));
async function loadMidi(source) {
try {
const result = await editor.loadMidi(source);
addLog('loaded ' + result.trackIds.length + ' track(s); bpm=' + result.bpm.toFixed(1) + ' ts=' + result.timeSignature.join('/'));
// Apply tempo / time signature from the file (caller decides).
editor.bpm = result.bpm;
editor.timeSignature = result.timeSignature;
} catch (err) {
// Cancellation isn't a failure — surface it differently.
if (err && err.name === 'AbortError') {
addLog('loadMidi cancelled');
return;
}
// Log the full Error (with stack + cause chain) to DevTools — the
// on-screen log is plain text and loses structured detail.
console.error('[loadMidi]', err);
addLog('loadMidi failed: ' + String(err));
}
}
document.getElementById('load-url').addEventListener('click', () => {
loadMidi('/media/midi/RedHotChiliPeppers-Otherside.mid');
});
document.getElementById('load-file').addEventListener('change', (e) => {
const file = e.target.files?.[0];
if (file) loadMidi(file);
});
</script>
</body>
</html>