Skip to main content

Drawing the Instrument tab with your own UI

StrudelBassTab draws its buttons, select, sliders, checkbox and instrument picker with the editor's own components. A host with a UI kit of its own passes the ones it wants in the ui prop, and anything left out keeps the editor's. The tab is compact by default; add advanced for the whole tab (see Embedding the Strudel editor).

The compact Instrument tab drawn with an example kit: coral round buttons, a pill dropdown and a coral slider

What each component receives​

Each one is a component with the editor's own props and events, so a wrapper maps them onto your kit. The type is StrudelBassTabUi.

KeyProps it receivesEvents and slotsUsed for
Buttonvariant (default or outline), size (sm), disabled, title, aria-pressedclick, a default slot with the labelThe calibration notes, export and import
IconButtontitle, aria-label, active, size, variantclick, a default slot with an iconPlay and the tuner
SelectmodelValue (a string), options ({ value, label }), disabledupdate:modelValueThe audio input
SlidermodelValue (a list of one number), min, max, step, labelupdate:modelValue with a listThe gate, Monitor, Synth and the string guess delay
CheckboxmodelValue (a boolean)update:modelValueAll frets and MIDI, in the advanced tab
SoundPickerlabel, modelValue (a sound name), groups ({ label, samples: { value, label }[] }[])update:modelValue with a sound nameThe synth instrument; the editor passes its sounds as voiceGroups

Turning parts off​

The Instrument tab's props, beside ui and advanced:

PropDefaultMeaning
instrumentnonebass or guitar; the tab follows it. Without it, the player's last pick, else the bass
demofalsePlays synthetic notes instead of an audio input, to try the tab without an instrument
instrumentPickertrueShows the instrument dropdown. update:instrument reports each pick
monitortrueOffers Monitor, the raw bass through the speakers. false neither shows nor plays it
synthtrueOffers Synth, each note played on a Strudel sound. false neither shows nor plays it
soundPickertrueShows the synth's instrument picker. Turn it off when the host chooses the sound
synthSoundnoneThe sound the synth plays, chosen by the host, such as gm_acoustic_grand_piano
voiceGroupsnoneThe sounds the picker lists, under headings; useStrudelSounds() from the helpers gives them
autoStartfalseStarts listening as soon as the tab is shown, without waiting for its start button
defaultMonitorPercent100Monitor's volume until the player moves it, 0 to 200
defaultSynthPercent0Synth's volume until the player moves it, 0 to 200

With autoStart there may have been no tap yet, and a phone then keeps the sound off. The first tap anywhere on the page turns it on.

Fonts​

The tab reads two CSS custom properties, so a host sets its own type without reaching into the tab's classes:

PropertyUsed forDefault
--strudel-bass-note-fontThe note heard, the tuner and the fretboard's string namesthe mono font
--strudel-bass-label-fontThe slider labels, such as Gate, Monitor and Synth, and notesinherited
.my-bass-panel {
--strudel-bass-note-font: 'My Game Font', sans-serif;
--strudel-bass-label-font: 'My Game Font', sans-serif;
}

The text colours come from the editor's theme tokens, --color-foreground and --color-muted-foreground, which a host can set on the same element.

The example​

The files are in documentation/examples/bass-tab-ui: one small wrapper per component and the tab that uses them. This is the tab:

<script setup lang="ts">
import { StrudelBassTab } from '@webgamekit/strudel-editor'
import type { StrudelBassTabUi } from '@webgamekit/strudel-editor'
import ExampleButton from './ExampleButton.vue'
import ExampleCheckbox from './ExampleCheckbox.vue'
import ExampleIconButton from './ExampleIconButton.vue'
import ExampleSelect from './ExampleSelect.vue'
import ExampleSlider from './ExampleSlider.vue'
import ExampleSoundPicker from './ExampleSoundPicker.vue'

defineProps<{ advanced?: boolean }>()

// Anything left out keeps the editor's own component.
const ui: StrudelBassTabUi = {
Button: ExampleButton,
IconButton: ExampleIconButton,
Select: ExampleSelect,
Slider: ExampleSlider,
Checkbox: ExampleCheckbox,
SoundPicker: ExampleSoundPicker
}
</script>

<template>
<StrudelBassTab :ui="ui" :advanced="advanced" />
</template>

A wrapper only turns the props above into your kit's. The slider is the one that needs a little translation, because the tab speaks in lists of one number:

<script setup lang="ts">
defineProps<{ modelValue?: number[]; min?: number; max?: number; step?: number; label?: string }>()
const emit = defineEmits<{ 'update:modelValue': [value: number[]] }>()
</script>

<template>
<input
type="range"
:value="modelValue?.[0]"
:min="min"
:max="max"
:step="step"
:aria-label="label"
@input="emit('update:modelValue', [Number(($event.target as HTMLInputElement).value)])"
/>
</template>

The advanced tab, and the moving picture​

With advanced the checkboxes, the export and import buttons and the delay slider use your components too. The animation below ticks All frets and MIDI and drags the delay slider.

The advanced Instrument tab drawn with the example kit

Opening the compact tab, then the advanced one, ticking its options and dragging the delay

In a game​

games-lobby does the same with its LobbyUI components: a Button wrapper over LobbyUIButton, a Slider wrapper over LobbyUISlider and so on, so the Bass section under the Rhythm controls looks like the rest of the lobby.

Playing the bass during a round​

The tab stops listening when it is closed, so a game that reads the bass during a round opens its own input with createBassMidi. These helpers let the round sound as the tab did:

HelperGives
readBassPlayback(defaults?)The Monitor gain and Synth level the player set; defaults apply until a slider moves
createBassSynthPlayer(options)The tab's own note by note synth: prepare() from a click, then noteOn and noteOff
playStrudelSong(code, { mutedSounds })The song live with some sounds unheard, named as its MIDI tracks
preloadStrudelSong(code, onProgress)Fetches the song's recordings ahead, so it sounds from its first note
import { createBassMidi } from '@webgamekit/bass-midi'
import {
createBassSynthPlayer,
playStrudelSong,
readBassPlayback
} from '@webgamekit/strudel-editor/helpers'

const { monitorVolume, synthVolume } = readBassPlayback({ monitorPercent: 0, synthPercent: 100 })
const synth = createBassSynthPlayer({
readSound: () => 'gm_electric_bass_finger',
readVolume: () => synthVolume,
onError: console.error
})
synth.prepare()

// The player's bass replaces the song's own when it can be heard; otherwise the song plays whole.
const isHeard = monitorVolume > 0 || synthVolume > 0
await playStrudelSong(songCode, { mutedSounds: isHeard ? [bassTrackName] : [] })

await createBassMidi({
monitorVolume,
onNoteOn: ({ midi, velocity }) => synth.noteOn(midi, velocity),
onNoteOff: () => synth.noteOff()
})

bassTrackName is the track's name in the MIDI from renderStrudelCodeToNotes: the sound it plays, such as gm_electric_bass_finger. Drums share one drums track and cannot be muted this way.