diff options
| author | Martin Hafskjold Thoresen <martin@vind.ai> | 2025-01-04 23:22:29 +0100 |
|---|---|---|
| committer | Martin Hafskjold Thoresen <martin@vind.ai> | 2025-01-04 23:22:29 +0100 |
| commit | e249e3003b569c7e3b270c06e02b87cad86ea2fb (patch) | |
| tree | 3e63d459a4146144f49fd13b9bda46b97b5fd924 /src/main.rs | |
| parent | c5fce19c22ec32129a8b8135c35798e430c5fc53 (diff) | |
| download | musicgame-e249e3003b569c7e3b270c06e02b87cad86ea2fb.tar.gz musicgame-e249e3003b569c7e3b270c06e02b87cad86ea2fb.zip | |
Add some tunes
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 92bda43..3c98db2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -402,6 +402,36 @@ async fn route_index(State(st): State<Server>) -> Response { (game_list) button hx-post="/game" { "Create game" } + + button #soundtest { "Sound test" } + + script { (PreEscaped(r#" + +async function soundtest() { + const context = new AudioContext(); + + function loadSample(url) { + return fetch(url) + .then(response => response.arrayBuffer()) + .then(buffer => context.decodeAudioData(buffer)); + } + + function playSample(sample, sampleNote, noteToPlay) { + const source = context.createBufferSource(); + source.buffer = sample; + source.playbackRate.value = 2 ** ((noteToPlay - sampleNote) / 12); + source.connect(context.destination); + source.start(0); + } + + const sample = await loadSample('/static/cat.mp3'); + const audio = new Audio("/static/dog.mp3"); + [0, 2, 4, 5, 7, 9, 11, 12].forEach((p, i) => setTimeout(() => + playSample(sample, 60, 60 + p), 500 * i) + ); +} +document.getElementById("soundtest").onclick = soundtest; +"#)) } }; html_response(html! { |
