Skip to content

Commit

Permalink
test soundfont-player piano if it crackles in mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
TeemuKoivisto committed Apr 6, 2024
1 parent 6936a6e commit 7eed770
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
9 changes: 5 additions & 4 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
"vitest": "^1.0.4"
},
"dependencies": {
"@iconify-icons/fe": "^1.2.5",
"@iconify-icons/feather": "^1.2.3",
"@iconify-icons/mdi": "^1.2.48",
"@iconify/svelte": "^3.1.4",
"@/chords-and-scales": "workspace:*",
"@/games": "workspace:*",
"@/keyboard": "workspace:*",
"@/midi-piano": "workspace:*",
"@/types": "workspace:*",
"@iconify-icons/fe": "^1.2.5",
"@iconify-icons/feather": "^1.2.3",
"@iconify-icons/mdi": "^1.2.48",
"@iconify/svelte": "^3.1.4",
"soundfont-player": "^0.12.0",
"tailwindcss": "^3.3.3",
"vexflow": "^4.2.3",
"webmidi": "^3.1.8"
Expand Down
1 change: 0 additions & 1 deletion packages/client/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>MIDI Note Trainer</title>

<script>
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const WHITE_INDECES = [0, 2, 4, 5, 7, 9, 11]
const C0_MIDI = 12
const C8_MIDI = 108
const C7_MIDI = 96
let innerWidth = window.innerWidth
$: pianoWidth = Math.min(innerWidth, 780)
Expand Down Expand Up @@ -50,7 +50,7 @@
}
function shiftRange(up: boolean) {
bottomNoteMidi.update(v => (up ? Math.min(v + 12, C8_MIDI) : Math.max(v - 12, C0_MIDI)))
bottomNoteMidi.update(v => (up ? Math.min(v + 12, C7_MIDI) : Math.max(v - 12, C0_MIDI)))
}
function handleKeyPressed(e: CustomEvent<{ idx: number; row: number }>) {
Expand Down
5 changes: 4 additions & 1 deletion packages/client/src/routes/(site)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
midiGranted,
midiInput,
piano,
sf,
useVirtualPiano
} from '$stores/inputs'
import { modalActions } from '$stores/modal'
Expand Down Expand Up @@ -81,7 +82,9 @@
scoreActions.pushPlayed(value)
}
if ($piano) {
$piano.noteOn(value, $inputs.fixedVelocity ?? velocity)
// $piano.noteOn(value, $inputs.fixedVelocity ?? velocity)
// @ts-ignore
$sf?.play(value, undefined, { gain: 4 })
}
}
function gameUpdate() {
Expand Down
4 changes: 4 additions & 0 deletions packages/client/src/stores/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { derived, get, writable } from 'svelte/store'
import { WebMidi } from 'webmidi'
import Soundfont from 'soundfont-player'

import { platform } from './media'
import { persist } from './persist'
Expand Down Expand Up @@ -36,6 +37,7 @@ export const midiRangeNotes = derived(
)
export const audioContext = writable<AudioContext | undefined>(undefined)
export const piano = writable<Piano | undefined>(undefined)
export const sf = writable<Soundfont.Player | undefined>(undefined)
export const useVirtualPiano = writable<boolean>(get(platform) === 'mobile')
export const inputs = persist(
writable<Inputs>({
Expand Down Expand Up @@ -101,6 +103,8 @@ export const inputsActions = {
const sounds = await fetchSounds(`${GH_BASE_URL}audio`, ctx)
p.load(sounds)
piano.set(p)
const gpiano = await Soundfont.instrument(ctx, 'acoustic_grand_piano')
sf.set(gpiano)
}
}
}
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7eed770

Please sign in to comment.