|
46 | 46 | <script lang="ts"> |
47 | 47 | import { createEventDispatcher } from "svelte"; |
48 | 48 | import { MeltCombo } from "@intechstudio/grid-uikit"; |
49 | | - import { GridScript } from "@intechstudio/grid-protocol"; |
| 49 | + import { |
| 50 | + ElementType, |
| 51 | + EventType, |
| 52 | + GridScript, |
| 53 | + NumberToEventType, |
| 54 | + } from "@intechstudio/grid-protocol"; |
50 | 55 | import { midiCC } from "./_midi.js"; |
51 | 56 | import { Script } from "./_script_parsers.js"; |
52 | 57 | import { LocalDefinitions } from "../runtime/runtime.store"; |
53 | | - import { GridAction, GridEvent } from "./../runtime/runtime"; |
| 58 | + import { GridAction, GridElement, GridEvent } from "./../runtime/runtime"; |
54 | 59 | import SendFeedback from "../main/user-interface/SendFeedback.svelte"; |
55 | 60 | import TabButton from "../main/user-interface/TabButton.svelte"; |
56 | 61 | import { MusicalNotes } from "../main/panels/MidiMonitor/MidiMonitor.store"; |
57 | 62 | import { Validator } from "./validators"; |
58 | 63 | import { valid } from "semver"; |
59 | 64 | import { Grid } from "../lib/_utils.js"; |
| 65 | + import ActionHelper from "../main/panels/configuration/components/ActionHelper.svelte"; |
60 | 66 |
|
61 | 67 | export let config: GridAction; |
62 | 68 |
|
63 | 69 | let event = config.parent as GridEvent; |
| 70 | + let element = event.parent as GridElement; |
64 | 71 |
|
65 | 72 | const dispatch = createEventDispatcher(); |
66 | 73 |
|
|
117 | 124 | }); |
118 | 125 | } |
119 | 126 |
|
120 | | - const channels = (length) => { |
121 | | - let arr = []; |
122 | | - for (let i = 0; i < length; i++) { |
123 | | - arr[i] = { value: i, info: `Channel ${i + 1}` }; |
124 | | - } |
125 | | - return arr; |
126 | | - }; |
| 127 | + $: if ($event) { |
| 128 | + renderSuggestions(); |
| 129 | + } |
| 130 | +
|
| 131 | + const tabs = [ |
| 132 | + { name: "MIDI", short: "gms" }, |
| 133 | + { name: "14 bit MIDI", short: "gmsh" }, |
| 134 | + { name: "SysEX", short: "gmss" }, |
| 135 | + { name: "NRPN MIDI", short: "gmnp" }, |
| 136 | + ]; |
| 137 | +
|
| 138 | + type SuggestionValue = { value: string; info: string; key: string }; |
| 139 | +
|
| 140 | + // --- helpers --- |
| 141 | + const makeAuto = (info: string, key = "auto"): SuggestionValue => ({ |
| 142 | + value: "-1", |
| 143 | + info, |
| 144 | + key, |
| 145 | + }); |
| 146 | +
|
| 147 | + const makeChannels = (n: number) => |
| 148 | + Array.from({ length: n }, (_, i) => ({ |
| 149 | + value: String(i), |
| 150 | + info: `Channel ${i + 1}`, |
| 151 | + key: `ch_${i}`, |
| 152 | + })); |
127 | 153 |
|
128 | | - const _suggestions = [ |
129 | | - // channels |
130 | | - [...channels(16)], |
131 | | - // commands |
| 154 | + const makeCCs = () => |
| 155 | + Object.entries(midiCC).map(([value, info]) => ({ |
| 156 | + value, |
| 157 | + info, |
| 158 | + key: `cc_${value}`, |
| 159 | + })); |
| 160 | +
|
| 161 | + const makeNotes = () => |
| 162 | + Array.from({ length: 128 }, (_, i) => ({ |
| 163 | + value: String(i), |
| 164 | + info: MusicalNotes.FromInt(i), |
| 165 | + key: `note_${i}`, |
| 166 | + })); |
| 167 | +
|
| 168 | + const baseSuggestions: Array<SuggestionValue[]> = [ |
| 169 | + [ |
| 170 | + makeAuto( |
| 171 | + `Auto (${Grid.Auto.getMidi(config, Grid.Auto.Value.MIDI_CHANNEL)})`, |
| 172 | + ), |
| 173 | + ...makeChannels(16), |
| 174 | + ], |
132 | 175 | [ |
133 | 176 | { value: "176", info: "Control Change", key: "control_change_messages" }, |
134 | 177 | { value: "144", info: "Note On", key: "note_on_event" }, |
135 | 178 | { value: "128", info: "Note Off", key: "note_off_event" }, |
136 | 179 | { value: "192", info: "Program Change", key: "program_change_messages" }, |
137 | 180 | ], |
138 | | - // param 1 |
139 | | - [], |
140 | | - // param 2 |
141 | | - [ |
142 | | - //{value: '', info: 'to do...'} |
143 | | - ], |
| 181 | + [], // param1 (dynamic) |
| 182 | + [makeAuto("Auto")], |
144 | 183 | ]; |
145 | 184 |
|
146 | | - let suggestions = []; |
147 | | - let suggestionsAuto = []; |
148 | | -
|
149 | | - $: if ($event) { |
150 | | - renderSuggestions(); |
151 | | - } |
| 185 | + let suggestions: SuggestionValue[][] = []; |
152 | 186 |
|
153 | 187 | function renderSuggestions() { |
154 | | - // removed ?. as terser didn't work |
155 | | - let selectedCommand = _suggestions[1].find( |
156 | | - (s) => s.value == scriptSegments[1], |
| 188 | + const currentCommand = Grid.Auto.getMidi( |
| 189 | + config, |
| 190 | + Grid.Auto.Value.MIDI_COMMAND, |
157 | 191 | ); |
158 | | - if (selectedCommand) { |
159 | | - selectedCommand = selectedCommand.key; |
160 | | - } else { |
161 | | - selectedCommand = "control_change_messages"; |
162 | | - } |
163 | 192 |
|
164 | | - try { |
165 | | - let param_1: any[] = []; |
166 | | - if (selectedCommand === "control_change_messages") { |
167 | | - param_1 = Object.entries(midiCC).map(([value, info]) => ({ |
168 | | - value: Number(value), |
169 | | - info, |
170 | | - })); |
171 | | - } else if ( |
172 | | - ["note_on_event", "note_off_event"].includes(selectedCommand) |
173 | | - ) { |
174 | | - param_1 = [...Array(128).keys()].map((e) => { |
175 | | - return { value: String(e), info: MusicalNotes.FromInt(e) }; |
176 | | - }); |
177 | | - } |
| 193 | + // find corresponding command once |
| 194 | + const commandEntry = baseSuggestions[1].find( |
| 195 | + (e) => +e.value === currentCommand, |
| 196 | + ); |
| 197 | + const autoCommand = makeAuto( |
| 198 | + `Auto (${commandEntry?.info ?? "?"})`, |
| 199 | + commandEntry?.key ?? "control_change_messages", |
| 200 | + ); |
178 | 201 |
|
179 | | - suggestionsAuto = [ |
180 | | - { |
181 | | - value: "-1", |
182 | | - info: `Auto (${Grid.Auto.getMidi(config, Grid.Auto.Value.MIDI_CHANNEL)})`, |
183 | | - key: "auto", |
184 | | - }, |
185 | | - { |
186 | | - value: "-1", |
187 | | - info: `Auto (${Grid.Auto.getMidi(config, Grid.Auto.Value.MIDI_COMMAND)})`, |
188 | | - key: "auto", |
189 | | - }, |
190 | | - { |
191 | | - value: "-1", |
192 | | - info: `Auto (${Grid.Auto.getMidi(config, Grid.Auto.Value.MIDI_P1)})`, |
193 | | - key: "auto", |
194 | | - }, |
195 | | - { |
196 | | - value: "-1", |
197 | | - info: `Auto`, |
198 | | - key: "auto", |
199 | | - }, |
200 | | - ]; |
201 | | - suggestions = [ |
202 | | - [..._suggestions[0]], |
203 | | - [..._suggestions[1]], |
204 | | - [...param_1], |
205 | | - [..._suggestions[3]], |
206 | | - ]; |
207 | | - } catch (error) { |
208 | | - console.warn("error while creating midi suggetions"); |
209 | | - suggestions = _suggestions; |
| 202 | + const selectedCommand = |
| 203 | + [autoCommand, ...baseSuggestions[1]].find( |
| 204 | + (s) => s.value == scriptSegments[1], |
| 205 | + )?.key ?? "control_change_messages"; |
| 206 | +
|
| 207 | + // param1 depends on selected command |
| 208 | + let param1: SuggestionValue[]; |
| 209 | + switch (selectedCommand) { |
| 210 | + case "control_change_messages": |
| 211 | + param1 = [ |
| 212 | + makeAuto( |
| 213 | + `Auto (${Grid.Auto.getMidi(config, Grid.Auto.Value.MIDI_P1)})`, |
| 214 | + ), |
| 215 | + ...makeCCs(), |
| 216 | + ]; |
| 217 | + break; |
| 218 | + case "note_on_event": |
| 219 | + case "note_off_event": |
| 220 | + const autoNote = Grid.Auto.getMidi(config, Grid.Auto.Value.MIDI_P1); |
| 221 | + param1 = [ |
| 222 | + makeAuto(`Auto (${MusicalNotes.FromInt(autoNote)})`), |
| 223 | + ...makeNotes(), |
| 224 | + ]; |
| 225 | + break; |
| 226 | + default: |
| 227 | + param1 = []; |
210 | 228 | } |
211 | 229 |
|
| 230 | + // fetch local definitions |
212 | 231 | const actions = $event.config; |
213 | 232 | const index = actions.findIndex((e) => e.id === config.id); |
214 | 233 | const localDefinitions = LocalDefinitions.getFrom({ |
215 | 234 | configs: actions, |
216 | | - index: index, |
| 235 | + index, |
217 | 236 | }); |
218 | | - suggestions = suggestions.map((s, index) => [ |
219 | | - suggestionsAuto[index], |
220 | | - ...localDefinitions, |
221 | | - ...s, |
222 | | - ]); |
| 237 | +
|
| 238 | + // assemble suggestions with "auto" always first |
| 239 | + suggestions = [ |
| 240 | + [ |
| 241 | + baseSuggestions[0][0], |
| 242 | + ...localDefinitions, |
| 243 | + ...baseSuggestions[0].slice(1), |
| 244 | + ], // channels |
| 245 | + [autoCommand, ...localDefinitions, ...baseSuggestions[1]], // commands |
| 246 | + param1.length > 0 |
| 247 | + ? [param1[0], ...localDefinitions, ...param1.slice(1)] |
| 248 | + : [...localDefinitions], // param1 |
| 249 | + [ |
| 250 | + baseSuggestions[3][0], |
| 251 | + ...localDefinitions, |
| 252 | + ...baseSuggestions[3].slice(1), |
| 253 | + ], // param2 |
| 254 | + ]; |
223 | 255 | } |
224 | 256 |
|
225 | | - const tabs = [ |
226 | | - { name: "MIDI", short: "gms" }, |
227 | | - { name: "14 bit MIDI", short: "gmsh" }, |
228 | | - { name: "SysEX", short: "gmss" }, |
229 | | - { name: "NRPN MIDI", short: "gmnp" }, |
230 | | - ]; |
| 257 | + $: if ($event) { |
| 258 | + renderSuggestions(); |
| 259 | + } |
231 | 260 |
|
232 | 261 | function handleTabButtonClicked(element) { |
233 | 262 | dispatch("replace", { short: element.short }); |
|
0 commit comments