load Samplemap and play note
-
I know how to load a samplemap, but I would like to know how to play certain note within the sampler.
example: C3, D3, E3 = when load samplemap C [C3 note should play] but when I load samplemap E [E3 note should play]
this is what I got and is not working
function onNoteOn() { // This makes the button go down if you play a MIDI note switch(Message.getNoteNumber()) { case 60: C.setValue(true); break; case 62: D.setValue(true); break; case 64: E.setValue(true); break; case 65: F.setValue(true); break; case 67: G.setValue(true); break; case 69: A.setValue(true); break; case 71: B.setValue(true); break; case 72: C2.setValue(true); break; case 74: D2.setValue(true); break; } }
function onNoteOff() { // This makes the button go up if you let go of a MIDI note switch(Message.getNoteNumber()) { case 60: C.setValue(false); break; case 62: D.setValue(false); break; case 64: E.setValue(false); break; case 65: F.setValue(false); break; case 67: G.setValue(false); break; case 69: A.setValue(false); break; case 71: B.setValue(false); break; case 72: C2.setValue(false); break; case 74: D2.setValue(false); break; } }
function onControl(number, value) { switch(number) { case C: { if(value) // Button press // Play a C-1 and store the event id for the note off eventIds[0] = Synth.playNote(60, 127); else // Use the event id to kill the note when you release the button Synth.noteOffByEventId(eventIds[0]); break; } case C2: { if(value) eventIds[1] = Synth.playNote(72, 127); else Synth.noteOffByEventId(eventIds[1]); break; } case D: { if(value) eventIds[2] = Synth.playNote(62, 127); else Synth.noteOffByEventId(eventIds[2]); break; } case D2: { if(value) eventIds[3] = Synth.playNote(74, 127); else Synth.noteOffByEventId(eventIds[3]); break; } case E: { if(value) eventIds[4] = Synth.playNote(64, 127); else Synth.noteOffByEventId(eventIds[4]); break; } case F: { if(value) eventIds[5] = Synth.playNote(65, 127); else Synth.noteOffByEventId(eventIds[5]); break; } case G: { if(value) eventIds[7] = Synth.playNote(67, 127); else Synth.noteOffByEventId(eventIds[6]); break; } case A: { if(value) eventIds[9] = Synth.playNote(69, 127); else Synth.noteOffByEventId(eventIds[7]); break; } case B: { if(value) eventIds[11] = Synth.playNote(71, 127); else Synth.noteOffByEventId(eventIds[8]); break; } { case CmbSampleMap: analogousPlayer.loadSampleMap(CmbSampleMap.getItemText()); break; } }
onInit // This is what I got onInit // Store the event id of the generated note for the note on. const var eventIds = [-1, -1, -1, -1, -1, -1, -1, -1];
-
Let's start by getting rid of the horrible switch statement in onControl. Instead you should assign a custom callback function in on init. Put all the buttons in an array and use the index of the array for
eventIds[array index]
Now your actual question
example: C3, D3, E3 = when load samplemap C [C3 note should play] but when I load samplemap E [E3 note should play]
I don't understand what you want. Are you saying that when the C samplemap is loaded playing C3, D3, or E3 should trigger note C3?
-
@d-healey All of my samplemaps only have 1 note each with a sample in it, I just want to be able to select the samplemaps and make trigger the sample indefinitely until I switch to another samplemap. I don't know if it make sense
-
I think the samplemap system is overkill for you needs :p
So if samplemap = C you want every switch to trigger C3?
-
@d-healey what can I use instead?
-
@Jay You could just put all the samples in one sample map and only allow the notes to be played that you want by ignoring the others in the on note callback. But I still don't know what you're trying to do :p
-
@d-healey yes I was thinking of doing that but don't know how, can you share a little snippet?
-
Yes I will post a snippet but it would be better if I understand exactly what behaviour you need so I can create a more complete snippet. I still don't understand exactly what you want the instrument to do.
-
@d-healey ok here is the idea I have.
Let's say I got an audio file that I want to incorporate into my songs as a background noise like the old LP, I want to create a plugin with that audio playing, I insert that plugin in my master bus and voila the old LP noise is now part of my song, make sense now?
-
I know exactly what you want now. I'll send you a snippet soon :)
-
I've made a little demo project for you. It should explain things a little more clearly than just a snippet.
The file is actually a .zip but the forum doesn't allow such files. So when you download it just change the extension to .zip and it should open.
-
@d-healey Thank you brother I'll read through it and try to learn as much as I can, I appreciate it
-
@d-healey is Good, but is there a way of no having to press the Play Button?, but when selecting any of the option in this case the notes it start automatically?
-
@Jay said in load Samplemap and play note:
no having to press the Play Button?, but when selecting any of the opti
Yeah of course, just put it in the combo box callback instead.
-
@d-healey said in load Samplemap and play note:
Yeah of course, just put it in the combo box callback instead.
Don't know what to change
-
@Jay Ok, I'll walk you through it because if I just change it it won't explain why.
So this is the whole script (except for the one line in the on note callback)
Content.makeFrontInterface(600, 500); //Combo box const var cmbSound = Content.getComponent("cmbSound"); //Play button inline function onbtnPlayControl(component, value) { var sound = cmbSound.getValue()-1; //Get combobox value (-1 to start at 0) value ? Synth.playNote(60+sound, 64) : Engine.allNotesOff(); }; Content.getComponent("btnPlay").setControlCallback(onbtnPlayControl);
The first line
Content.makeFrontInterface(600, 500);
Just sets the width and height of the main interface. Nothing special here, HISE adds this for us automatically.In this line
const var cmbSound = Content.getComponent("cmbSound");
We are getting a reference to the combo box that is on the interface. The combo box has an id of cmbSound and we are saving it in a variable with the same name.Next we have the callback function for the play button. This is like when you use a switch statement in the generic on control callback. But this callback is only triggered when the play button
btnPlay
is pressed.var sound = cmbSound.getValue()-1; //Get combobox value (-1 to start at 0)
This line gets the current value of the combo box. We use the reference we saved earlier - cmbSound. The first item in a combo box always has an index of 1 but we want it to be 0 so we subtract 1.value ? Synth.playNote(60+sound, 64) : Engine.allNotesOff();
This uses the ternary operator?
. It's a way of making a 1 line if/else statement. We can understand it better if we write it out in full.if (value) { Synth.playNote(60+sound, 64); } else { Engine.allNotesOff(); }
Let's go through this.
So firstly we only play a note when the value is 1 -
if (value)
Then we play the note
Synth.playNote(60+sound, 64);
We use 60 as the note number because the first sample is mapped to middle C which is MIDI note 60. We add to this the current value ofcmbSound
. We saved this value in thesound
variable earlier. So if the first sound is selected then the note that triggers will be 60, if the second sound is selected then the triggered note will be 61, etc.In the else clause (it the button value is 0) we just turn off all playing notes. That way we don't have to keep track of individual note IDs. There will only ever be one note playing at a time.
Ok so to get this to play when selecting from the comboBox instead of by pressing the play button we add a callback function for the combo box. The callback function is auto-generated by HISE if we right click on the control in the widget list and select
create custom callback definition
then paste into the script editor.Now our script will look like this:
Content.makeFrontInterface(600, 500); //Play button inline function onbtnPlayControl(component, value) { Engine.allNotesOff(); }; Content.getComponent("btnPlay").setControlCallback(onbtnPlayControl); inline function oncmbSoundControl(component, value) { Synth.playNote(60+value-1, 64); }; Content.getComponent("cmbSound").setControlCallback(oncmbSoundControl);
The play button is now a stop button - you'll probably want to rename it and set it to be momentary.
The note is played in the cmbSound callback. We no longer need to store this control or its value in a separate variable. We can just use the value directly in the
Synth.playNote()
function. You'll want to disablesaveInPreset
for the combo box otherwise it will always play when you hit compile. -
@d-healey got it up to here, but every time I select any of the other notes it play on top of the one already playing, how do I mute the last note playing and start playing the new select it one?
-
@Jay Oh yeah I didn't think of that. Well you know how to turn the note off, right ;) so just do that before you play a note.
-
@d-healey yeah cool, but I would like to be able to do it automatically, I will be trying to do it myself to see if I can pull it off, wish me luck.
Thanks man
-
Just use the same code from the stop button -
Engine.allNotesOff();