Coloured Keys not quite behaving...
-
Evening everyone hope you are all well. So code incoming, I've got a dropdown box to change loaded sample maps - they've got quite a varying range depending on the selected map and was colour coding the keys for the user so its clear what you've got to work with.
The issue is that it seems a bit inaccurate and to be honest I'm not sure why some are very close 1 or 2 notes missed low and high others are missing 16-20 notes left uncoloured completely, as always I suspect there is something I've missed!
/// Dropdown Sample Maps Content.getComponent("cmbsamplemap").setControlCallback(oncmbsamplemapControl); const var sampleMaps = Sampler.getSampleMapList(); const var cmbsamplemap = Content.getComponent("cmbsamplemap"); cmbsamplemap.set("items", sampleMaps.join("\n")); inline function oncmbsamplemapControl(component, value) { Sampler1.asSampler().loadSampleMap(sampleMaps[value-1]); for (i = 0; i < 127; i++) if (Sampler1.asSampler().isNoteNumberMapped(i)) Engine.setKeyColour(i, Colours.withAlpha(Colours.green, 0.3)); else Engine.setKeyColour(i, Colours.withAlpha(Colours.white, 0.0)); };``` -
@JamesC The sample map loading is asynchronous so your colour setting code is running before the map is fully loaded. You need to use a loading callback to handle this.
-
@JamesC said in Coloured Keys not quite behaving...:
if (Sampler1.asSampler().isNoteNumberMapped(i))
Engine.setKeyColour(i, Colours.withAlpha(Colours.green, 0.3));
else
Engine.setKeyColour(i, Colours.withAlpha(Colours.white, 0.0));
};Im not sure this will work without the braces, try this:
if(Sampler1.asSampler().isNoteNumberMapped(i)) { Engine.setKeyColour(i, Colours.withAlpha(Colours.green, 0.3)); }else{ Engine.setKeyColour(i, Colours.withAlpha(Colours.white, 0.0)); }; };if nothing else it makes it more readable...but whilst we are here....
if(Sampler1.asSampler().isNoteNumberMapped(i))so you are looping round all possible notes (0 ->127) and every time you are asking HISE to load the Sampler1.asSampler()
Its more efficient and better to do this:
/// Dropdown Sample Maps Content.getComponent("cmbsamplemap").setControlCallback(oncmbsamplemapControl); const var sampleMaps = Sampler.getSampleMapList(); //<-- shouldnt this be Sampler1 ?? const var cmbsamplemap = Content.getComponent("cmbsamplemap"); const var Sampler1AsASampler = Sampler1.asSampler(); //<--assuming you've got a reference to Sampler1 earlier... cmbsamplemap.set("items", sampleMaps.join("\n")); inline function oncmbsamplemapControl(component, value) { Sampler1AsASampler.loadSampleMap(sampleMaps[value-1]); //so put all this code in the OnLoad callback for (i = 0; i < 127; i++) { if (Sampler1AsASampler.isNoteNumberMapped(i)) { Engine.setKeyColour(i, Colours.withAlpha(Colours.green, 0.3)); }else{ Engine.setKeyColour(i, Colours.withAlpha(Colours.white, 0.0)); }; }; // to here... }; -
-
@Lindon thanks both for the help, I'm not sure I'm understanding the example given in the documentation
What element is this case are we setting the loading call back for in the exmaple I see its the panel in this case is it the sampler? (I'm also fairly sure my layout of brackets etc is completely out on this also!:
//so put all this code in the OnLoad callback sampler.setLoadingCallback(function(isPreloading) { for (i = 0; i < 127; i++) { if (Sampler1AsASampler.isNoteNumberMapped(i)) { Engine.setKeyColour(i, Colours.withAlpha(Colours.green, 0.3)); }else{ Engine.setKeyColour(i, Colours.withAlpha(Colours.white, 0.0)); }; }; // to here... -
@JamesC You need to use a panel, the callback is triggered by the sample map being loaded. From within the callback you can call your key colouring function.
-
@JamesC Apparently I made a video about it
-
@d-healey awesome will stick it on my watch list and regroup tomorrow!
-
@d-healey Seeing it and what its actually doing makes so much more sense now, thanks so much again for brilliant video!