# Structure of creating a legato instrument



## stevemazzaro (Mar 5, 2011)

I was wondering if anyone could explain how a legato instrument is structured/recorded.

The way I'm guessing it works is this:

Samples are recorded of each note switching to every note within an octave or so (varies depending on the library, I suppose). Then in the script, it's made clear that if note A (any note) is held, and note B (any note) is pressed, it switches to the "legato" sample which consists of the very end of note A to the transition to note B. If nothing is pressed, and you hit note B, it hits the regular sustained note B sample, etc. etc. etc.

Is this right? Or have they simplified this? (or have I simplified it and it's actually harder?...)

Thanks!


----------



## MacQ (Mar 5, 2011)

I think that's pretty much it. There's a threshold usually of how much the notes have to overlap to trigger that legato sample. The VSL approach was to have the legato transition be both a transition and hold at the new note. This makes the patches RAM intensive, since the transitions themselves are so long. Modern techniques used in a bunch of current libraries involve micro-crossfading into the transition sample, and then micro-crossfading out of that into a sustain sample at the new note (usually with a sample-offset at the new note so as not to hear the new note's attack portion). It's quite effective, but sadly not a "plug-and-play-and-it-sounds-awesome" kind of thing. It's still very much a "tune-it-by-hand-1000-times" kind of thing. Which is where we're at, unfortunately. 

~Stu


----------



## Mike Greene (Mar 5, 2011)

That's basically how I did Realivox. If it turns out there was an easier way, I don't want to know it! :mrgreen: 

I don't think it's necessary for most instruments to actually sample every single legato interval, by the way. It's certainly nice, but since there's no real "economy of scale" when it comes to editing samples, you have to weigh how much time you want to spend perfecting 25 samples (12 intervals up plus 12 intervals down plus the straight one) for every single note. My opinion is that when it gets that huge, attention to detail will suffer. (Unless you have an enormous budget and staff like VSL.)


----------



## stevemazzaro (Mar 5, 2011)

So could you potentially use the transition samples as the regular sustain samples to save on RAM (just duplicate the sample in Kontakt and change the start time)?


----------



## Mike Greene (Mar 5, 2011)

MacQ @ Sat Mar 05 said:


> Modern techniques used in a bunch of current libraries involve micro-crossfading into the transition sample, and then micro-crossfading out of that into a sustain sample at the new note (usually with a sample-offset at the new note so as not to hear the new note's attack portion).


I wanted to do that, but couldn't make it sound good. It might be because vocals are wildly inconsistent, as opposed to a clarinet. I'm not saying it *can't* be done, but I couldn't manage it.


----------



## stevemazzaro (Mar 5, 2011)

Good to know. 

How intensive is the script generally? I mean, is it hundreds of lines that an amateur like me wouldn't even understand what's going on?


----------



## MacQ (Mar 5, 2011)

Mike Greene @ Sat Mar 05 said:


> I wanted to do that, but couldn't make it sound good. It might be because vocals are wildly inconsistent, as opposed to a clarinet. I'm not saying it *can't* be done, but I couldn't manage it.



Yeah ... vocals are super inconsistent. And so touchy when it comes to "realism". How are those demos coming? 

~Stu


----------



## MacQ (Mar 5, 2011)

stevemazzaro @ Sat Mar 05 said:


> Good to know.
> 
> How intensive is the script generally? I mean, is it hundreds of lines that an amateur like me wouldn't even understand what's going on?



It's definitely in the dozens of lines. It's not rocket-science as far as the conventions of programming are concerned, but if you're not really familiar with the concepts of computer programming, it's a bit of a slog. Personally, I'm hiring out the really complicated stuff. There are some great programmers out there that I'm happy to pay for what would be a time-suck for me (in terms of getting my head around things and debugging sloppy code). 

That said, there's a "VSL-style" example of code floating around here somewhere ... or you can find it over at Northernsounds, I think. At least, I think I found it there. Blake Robinson (one of the aforementioned great programmers) has a free example of legato transition code here: http://dummyworld.net/programs/

It's not for the faint of heart!

~Stu


----------



## stevemazzaro (Mar 5, 2011)

Hah, as I figured!

Thanks, guys.

And Mike, I second on "where's the demos?"


----------



## stevemazzaro (Mar 5, 2011)

Okay - I ran into a little issue. 

I made a quick octave with a flute just to try it out to see how it works. The notes play legato via the voice groups in the Kontakt editor, however I'm not sure how to make it so if D switches to E, it plays group "x", but if E is pressed without any previous note, it plays group "z" (or if this is something the player can even do!)

Also, does each sample need to be in a separate group? Or can I group all of the "D" samples together (meaning d, e to d, f to d, c to d, etc.)


----------



## Mike Greene (Mar 6, 2011)

There's no easy way to set this up. You're going to have use scripting to do this. While it's not necessarily a complicated script, it's still going to take you some time. It took me a few days to write the basics of mine. Mind you, most of that time was just learning the KSP language, but that's going to be necessary even for the simplest of scripts. (Unless you hire it out, of course.)

If you do decide to learn KSP (Kontakt's scripting language,) I can send you the PDF KSP manual for Kontakt 2, which was a much better "beginning" manual, because it gave some simple examples of very rudimentary scripts. Obviously there are a lot of new things you'll want/need in Kontakt 4, but I still think the K2 manual is better for beginners. The KSP manual for K4 is much dryer with less explanation.


----------



## stevemazzaro (Mar 6, 2011)

I had a feeling :(

I believe I have the kontakt manual (not sure if it's the same or not though, it's the one that came with K4) - I have a good grasp on some of the easy scripting stuff (declaring ui functions, setting different parameters within the ui, etc.) The whole thing I don't quite understand is a lot of the more advanced stuff - setting engine parameters, arrays, on note stuff, etc.


----------



## stevemazzaro (Mar 11, 2011)

I've read both the K4 and K2 ksp manuals backwards and forwards and still have no indications on how to properly script this, so I came up with an idea and wanted to know what you guys thought:

Would it be possible to set real-legato up by using the group start options "on key"?

Basically you would load each sample into its own group (yes, there would hundreds, if not thousands of groups depending on the instrument). Each group would have a separate on key group start option.

So in example, on the D2 to C2 transition sample (which would lie on C2), the group start option would require the "on key" to be D2. For the E2 to C2 transition sample (which would lie on C2 as well in a separate group) the group start option would require the "on key" to be E2.

I suppose all you'd have to script is to make it so when no key is held, all the groups aside from the non-legato are disallowed?

Would something like this work?


----------



## stevemazzaro (Mar 12, 2011)

Ah, I see what you mean. I'll take a look at yours then  Thanks, Blake!


----------



## stevemazzaro (Mar 19, 2011)

Ok, I've played around a LOT with the script on Blake's site, and it makes sense and seems to do the exact thing most real-legato libraries do (xfading between the transition sample and the sustain).

For reference, here's the script:



> {On script initialisation}on init
> 
> {Variables we store legato information with}
> declare $legato_id := 0
> ...



Now, here's my issue. If playing fast notes, if the transition from the legato sample to the sustain doesn't complete, no note will sound (due to this line, I presume): 

{In the middle of legato? Ignore this note if so}
if ($legato_id = 255)

ignore_event ($EVENT_ID)
exit
end if

Any ideas on what I could replace the ignore_event / exit lines with to make it so switching to a new note in the middle of a legato transition restarts the legato transition? I've tried taking the fadein/fadeout start times down a lot, which did help, but the samples still aren't 100% solid like LASS or HW.


----------



## stevemazzaro (Mar 21, 2011)

Does anyone have any idea on this?

I tried copy/pasting the entire code into the if ($legato_id = 255) statement, but that didn't seem to work either.

Would it be better to change the code so the only crossfading taking place is between the currently played note and the transition sample (which would require each transition sample to have the sustain patch already on it)?

Any help would be greatly appreciated!


----------



## Mike Greene (Mar 21, 2011)

Steve, this is the hard part of scripting, because you're more or less on your own at this stage. Speaking for myself, I'd love to help, but troubleshooting is really time consuming for an outsider like me to not only figure out what your code is doing, but then to find solutions which will invariably be more than just one or two lines of changes.

If it makes you feel any better, I encounter problems like this with my own scripts on a nearly daily basis. You just have to battle through the logic until you find a solution.

FWIW, my vocal library uses the method where the legato transition also contains the full landing sustain note, rather than a short legato sample transitioning into a separate landing note. I did it that way not because the scripting would be easier, but because vocals are so inconsistent that the extra transitions would be a nightmare to sound natural. If whatever you're sampling can sound natural with just the short legato transition and then separate hold samples, then I say stick with that method, because it will make for much less time recording. (It takes a ton more time to record a transition from C to D where you include a long note on the D, not only because of the time of the note itself, but also because if there's a wobble or hiccup in the D hold, you have to do it again.)


----------



## Mike Greene (Mar 21, 2011)

Nick Batzdorf @ Mon Mar 21 said:


> Mike, I don't quite follow - are you saying you just pitch shift to create the transition samples you don't actually sample?


No, the legato transitions are indeed recorded and not faked. It's just that they then hold on that note, rather than then transitioning into *another* sample that is just the hold note. Basically there are two methods being described in this thread:

1. Long Original Note >> Short Transition (Legato) Snippet >> Long Hold Note

2. Long Original Note >> Transition (legato) snippet that then includes the entire long landing (hold) note

Steve is using the first method, which involves two crossfades. I use the second method which involves only one crossfade, but requires more megabytes for the longer samples. I think most libraries (VSL for instance) use the same method I do, but the first method is more elegant, *if* you can make it sound smooth.


----------



## stevemazzaro (Mar 21, 2011)

Not a problem, Mike.



> FWIW, my vocal library uses the method where the legato transition also contains the full landing sustain note, rather than a short legato sample transitioning into a separate landing note.



This does seem like a smarter solution, scripting wise. However, couldn't you then just record the transition samples as a very short transition without the sustained note, record the sustained notes separate, and then crossfade them yourself in a waveform editor application? Since Kontakt's crossfade mechanics, they won't sound perfect, but with free reign over the samples, you should definitely be able to make it unnoticeable.


----------



## Mike Greene (Mar 21, 2011)

stevemazzaro @ Mon Mar 21 said:


> This does seem like a smarter solution, scripting wise.


It's a little easier, but I don't think your snag will be that hard to overcome. The long term benefit, if you can make it work, will be huge in the amount of recording and editing you'll have to do later.



stevemazzaro @ Mon Mar 21 said:


> However, couldn't you then just record the transition samples as a very short transition without the sustained note, record the sustained notes separate, and then crossfade them yourself in a waveform editor application?


I didn't have any success with this method, either. Mostly because vocals are insanely variable and it's really rare that that a singer doesn't have slight changes in her timbre from one note to the next (even if they're the same pitch and articulation) that will be instantly heard if you make an edit in ProTools or whatever. I know it sounds crazy, but vocals have the tiniest little things that are completely unnoticeable until you try to splice two performances together, at which point you will instantly hear the slight difference in breathiness or whatever that you didn't even realize was there before.

Not to toot my own horn, but doing this vocal library has taught me why no one else has done it before.

With that said, if you're sampling something more consistent, it might totally work.


----------



## stevemazzaro (Mar 21, 2011)

Yeah I could imagine doing vocals would be tough . It should work in theory with wind instruments though. Ideally you would incorporate the sustain right at the end of the transition to make it less noticable (instead of a second AFTER the transition). 

Anyway, I'll give it a shot. Thanks for your help, Mike!


----------



## Mike Greene (Mar 21, 2011)

One other thought on that is that you might still want to keep that half second or so after the transition if there is any "settling" into the note that might make the whole transition sound more natural.

With vocalists, for example, they typically slightly overshoot or undershoot their target note, so keeping that little initial wobble makes the legato sound a little more believable.


----------



## Nick Batzdorf (Mar 21, 2011)

Okay, I got it (Mike).

Yes, VSL does the second. There was a lot of talk about that with their first orchestra, and then I think they released looped versions so you could hold string pedals - or simulate tuba-playing zombies who can hold their breaths.


----------



## germancomponist (Mar 21, 2011)

LOL


----------



## stevemazzaro (Mar 21, 2011)

In my honest opinion, never-ending breath is better than the samples being too short (I'm looking at you, Requiem).


----------



## Tod (Mar 21, 2011)

> This does seem like a smarter solution, scripting wise. However, couldn't you then just record the transition samples as a very short transition without the sustained note, record the sustained notes separate, and then crossfade them yourself in a waveform editor application? Since Kontakt's crossfade mechanics, they won't sound perfect, but with free reign over the samples, you should definitely be able to make it unnoticeable.



Hi steve,

I did this several years ago with K2 and used this approach. I mean createing all fadeins on the sustains and fadeouts on the intervals in an editor an then saveing those samples. It worked but was also a nightmare. First you have to get the fadeins on the sustains so that they're almost exactly the same. Then you have to match those fadeins with the fadeouts for each interval going up and down. This was a daunting task. However, I have to admit I'm a real perfectionist and wouldn't settle for anything that was less then almost perfect.

If I had it to do again today I'd try to do it differently. I notice in your script you have a "fade_in" and "fade_out". Since you don't have any functions I'm assumeing this is something that K4 has now? I'm just getting into K4 scripting so I've got a lot to learn about it. :oops:  



> In my honest opinion, never-ending breath is better than the samples being too short (I'm looking at you, Requiem).



Heh heh, I agree.  

Tod


----------



## Mike Greene (Mar 21, 2011)

Nick Batzdorf @ Mon Mar 21 said:


> Yes, VSL does the second. There was a lot of talk about that with their first orchestra, and then I think they released looped versions so you could hold string pedals - or simulate tuba-playing zombies who can hold their breaths.


To be clear, I do indeed loop the sustain after the legato transition.


----------



## Nick Batzdorf (Mar 21, 2011)

Oh! I didn't mean to imply that you're a scoundrel! (Or at least I didn't mean to blow your cover.)

Actually, my comment was in reference to an argument on the VSL site about their tuba. Someone was complaining that it didn't sustain long enough, and of course the VSL guys replied that you aren't going to get too many tuba players to survive longer notes than they recorded.


----------



## MozillaUser (Mar 21, 2011)

Heya Steve,
Although I think Mike and Nick and others gave you already lots of hints about your general question, please allow me to put my 2 pence on your topic, and to adress a few of your problems or answer to your other questions.
Before I start, I think you could find lots of answers if you search for a topic called "Custom legato interval samples like in LASS" and look to my first post there. It would enlighten you to a certain degree, I guess.
Now to your questions:



stevemazzaro said:


> How intensive is the script generally? I mean, is it hundreds of lines that an amateur like me wouldn't even understand what's going on?


Well yes, the very basic legato script (that is, a script which works SOMEHOW  ) would require that amount of lines. However, a script which adresses - and solves - most of the "hidden" problems would require much more lines, I guess.



stevemazzaro said:


> So could you potentially use the transition samples as the regular sustain samples to save on RAM (just duplicate the sample in Kontakt and change the start time)?



Yes you could use them this way, in most of the cases. However, as Mike pointed out, for good results it would be better to have dedicated sustain samples. 



stevemazzaro said:


> ...however I'm not sure how to make it so if D switches to E, it plays group "x", but if E is pressed without any previous note, it plays group "z" (or if this is something the player can even do!)
> Also, does each sample need to be in a separate group? Or can I group all of the "D" samples together (meaning d, e to d, f to d, c to d, etc.)


You should group all "same transition type" samples in one group: for instance one group which contains all "1 halftone up" samples, another group which contains all "2 halftones up", etc. You'll end up with 12 "transition up" + 12 "transition down" + 1 "first note" group.
There are ways to reduce this number. It's definitely possible to group all 24 transition types in one single group, for instance - but this involves lots of work to map them properly and lots of advanced scripting and ingenuity to choose the right transitions. 



stevemazzaro said:


> Would it be possible to set real-legato up by using the group start options "on key"?
> Basically you would load each sample into its own group (yes, there would hundreds, if not thousands of groups depending on the instrument). Each group would have a separate on key group start option.
> So in example, on the D2 to C2 transition sample (which would lie on C2), the group start option would require the "on key" to be D2. For the E2 to C2 transition sample (which would lie on C2 as well in a separate group) the group start option would require the "on key" to be E2.


No hundres and no thousands of groups, as I just pointed out. 25 groups would suffice.
Well...VSL uses kind of "starting on key", but not in the sense you described. They have dedicated keys placed in the very low part of the keyboard (note 0, note 1 etc) which are usually hidden to you, no keyboard sends that low midi messages. They use those keys to select one group or another, and are triggered within the script. The notes themselves (in the "playing zone"), they should never be used to "start themselves" like you imply (if I understood correctly what you asked). To validate a group or another, most people here prefer to use the "allow" and disallow" commands. I, for one, I prefer the "start on controller" option. In the script, I validate a group simply by setting a controller to an unique value, which in turn lets just that group start, and block all others. (that is, with a single line of code I "allow" one group and "disallow" all others.)



stevemazzaro said:


> Now, here's my issue. If playing fast notes, if the transition from the legato sample to the sustain doesn't complete, no note will sound (due to this line, I presume):


Well, you just pointed to one of those "hidden problems" I was talking about in the beginning. I have seen scripts where only adressing this problem took a hundred of lines
If you prefer the version "separate transitions" and "separate sustains following the transitions", I'd suggest a simple solution to your problem. Let both, the transition and the sustain, start at once. On the sustain group though, set its FLEXIBLE envelope in such manner, that during the transition it doesn't sound yet; when the transition completes, only then the sustain starts to fade in, and so, it crossfades with the end of the transition. But if you done this, you still face the problem "if playing FAST notes"....oh yes, you shouldn't play that fast if you want the sustains to appear in the picture  . In other words, give the sustains a chance to be heard 
No need to be said: it's possible to write the script such, that it forces the complete transition to be heard (no matter if you played the second note too fast) and only when it completes, the sustain is started. This involves having some not polyphonic variables used as "still busy" flags. 

I hope I have adressed your questions. If not...my Latin ends here :-(
Mozil.


----------



## stevemazzaro (Mar 22, 2011)

Hey Mozil,

Thanks for this! I think what I will end up doing is just creating each transition as an entire sample instead of cross-fading to the the transition and back to the sustain, partly due to the fact that I'm not an expert programmer like some of these guys (in fact, I'm not even sure I'm an intermediate programmer! ). Hopefully this should take out most, if not all of those "hidden" problems.


Tod,

I'm trying not to think of how daunting that task will be...


----------



## marcotronic (Jan 9, 2013)

Hi guys,

the thread is almost two years old now and I jut wanted to ask if the described methods are still "state of the art" today when it comes to legato programming in Kontakt (especially Kontakt 5) - or has it gotten easier these days? (e.g. by new features in later Kontakt versions, scripting options etc...)?

I'd love to dig deeper into creating my own Kontakt libs...

thanks
Marco


----------



## jesusginard (Aug 9, 2013)

I'm struggling with it right now :D 

I know this is an old thread but I think it hasn't been solved yet. 

I have an instrument containing sustains, legato transitions followed by sustains. If it's a legato note I want the script to play the plain sustain -> Fade out this sustain -> Fade in the legato transition and let sounding the looped sustain that follows the transition until another legato note is triggered or until the note is released. 

The problem with the script pasted above is the famous "if I play fast there are no samples triggered". I will try to find a fix for this, I'll post whatever I come up with. 

If you have some time please help me! I'm sure this hasn't to be so difficult.


----------



## d.healey (Aug 10, 2013)

Something like this


```
on init
	
	declare const FADE_TIME
	declare const HOLD_TIME
	declare sustain_id
	declare legato_id
	declare interval
	declare current_note := -1
	sustain_group := 0 
	legato_group
	
end on

on note

	if current_note > -1
	
		interval := abs(EVENT_NOTE - current_note)
		
		{Determine correct legato group here
		legato_group := ?}

		disallow_group(ALL_GROUPS)
		allow_group(legato_group)
		fade_out(legato_id, 10, 1) {To avoid hanging notes, make sure old legato note is stopped}

		legato_id := play_note(current_note, EVENT_VELOCITY, 0, -1)
		
		{Fade out sustain note, fade in legato}
		fade_out(sustain_id, FADE_TIME, 1)
		fade_in(legato_id, FADE_TIME)

		wait(HOLD_TIME)		
		current_note := EVENT_NOTE

		disallow_group(ALL_GROUPS)
		allow_group(sustain_group)
		sustain_id := play_note(EVENT_NOTE, EVENT_VELOCITY, 0, -1)		
		{Fade out legato note, fade in sustain}
		fade_out(legato_id, FADE_TIME, 1)
		fade_in(sustain_id, FADE_TIME)		

	else
		disallow_group(ALL_GROUPS)
		allow_group(sustain_group)
		sustain_id := play_note(EVENT_NOTE, EVENT_VELOCITY, 0 , -1)
		current_note := EVENT_NOTE
	end if
end on

on release

	if EVENT_NOTE = current_note
		current_note := -1
	end if
end on
```


----------



## jesusginard (Aug 12, 2013)

Hi TotalComposure, I was referring on modifying this script by Blake Robinson: 


```
{On script initialisation}on init 

{Variables we store legato information with} 
declare $legato_id := 0 
declare $legato_now := 0 
declare $legato_dif 
declare $legato_group := 0 

{Constants we can tweak} 
declare const $LEGATO_FADEINTIME := 150000 
declare const $LEGATO_FADEOUTTIME := 100000 
declare const $LEGATO_SUSTAIN_TIMEBEFORESTART := 500000 {Quarter of a second} 


end on 



{On note press} 
on note 

{Disallow all groups} 

disallow_group($ALL_GROUPS) 

{In the middle of legato? Ignore this note if so} 
if ($legato_id = 255) 

ignore_event ($EVENT_ID) 
exit 
end if 

{If we are already holding our legato note} 
if ($legato_now # 0) 

{Work out how many notes we are transversing. The abs ensures it's 
always a positive number} 
$legato_dif := abs($EVENT_NOTE - $legato_now) 

{Enable the correct up or down legato group depending on whether we 
are performing an up or down legato interval. Groups are named 
specifically} 
message ("Legato " & $legato_dif) 
$legato_group := 0 
if ($EVENT_NOTE > $legato_now) 
$legato_group := find_group("Legato Up " & $legato_dif) 
else 
$legato_group := find_group("Legato Down " & $legato_dif) 
end if 

{No legato instrument exists for this, simply stop the current 
and play the next note} 
if ($legato_group = 0) 
allow_group(0) 
note_off($legato_id) 
$legato_id := $EVENT_ID 
$legato_now := $EVENT_NOTE 
exit 
end if 

{Allow the group of the legato interval sample} 
allow_group($legato_group) 

{Fade out the currently held sustain and fade in the interval} 

fade_out($legato_id, $LEGATO_FADEOUTTIME, 1) 

fade_in ($EVENT_ID, $LEGATO_FADEINTIME) 


{Now we wait to fade in the new sustain. If the user released the 
sustain key before the wait, simply exit this callback} 
$legato_id := 255 
$legato_now := $EVENT_NOTE 
wait($LEGATO_SUSTAIN_TIMEBEFORESTART) 
if ($legato_now = 0 or $legato_id # 255) 
exit 
end if 

{Disallow all groups again and then allow the sustains} 
disallow_group($ALL_GROUPS) 
allow_group(0) 

{Fade out the legato interval sample and play the new sustain, 
making sure that it fades in. Finally, store it as the new held 
note so that the next transition is a legato.} 
$legato_id := play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, -1) 
fade_out($EVENT_ID, $LEGATO_FADEOUTTIME, 1) 

fade_in($legato_id, $LEGATO_FADEINTIME) 


else 

{This is the first note we're playing. Simply activate the 
sustain group and store the note and id of this event} 
allow_group(0) 
$legato_id := $EVENT_ID 
$legato_now := $EVENT_NOTE 


end if 

end on 



{On note release} 
on release 

{If we released our held sustain, set us to not performing 
legato and reenable all groups to make it easier to edit} 
if ($EVENT_NOTE = $legato_now) 
$legato_id := 0 
$legato_now := 0 
allow_group($ALL_GROUPS) 
end if 

end on
```


----------



## d.healey (Aug 12, 2013)

Mine is a variation of that script - if you put a fade and hold time of about 0.1s you should be able to play quicker without getting notes cutting out.


----------



## jdawg (Aug 14, 2013)

@Total Composure, how would you suggest setting up a patch so your code would best work?


thanking you o=< o=<


----------



## d.healey (Aug 14, 2013)

It depends a bit on the sample material and the way the instrument is to work. For instance I'm currently working on an instrument with multiple mic positions and several dynamics that are crossfaded via the modwheel. Each dynamic for each mic position requires a group and there are twelve transition samples for each note for each mic position at each dynamic. If I created an individual group for each of the 24 transitions (12 up 12 down) I would end up with a huge number of groups which makes the whole thing quite impractical - however with a smaller instrument this is the approach I would use.

What I have done is put all the transition samples in one group for each dynamic, defined by velocity. So if I had a transition sample named "instrument_down_a3_g#3" I know its a down sample and has an interval of one semi tone, so I map this to key a3 at velocity 1. If it was "a3_g3" it would go on key a3 at velocity 2 and so on. The up transition samples are mapped in the same way except I add 64 to all the velocities.

Now in the code above the place where I've commented 'determine correct legato group here' you can use the interval that has already been derived and simply put


```
if (EVENT_NOTE > current_note) {Going up!}
    interval := interval + 64
end if
```

This will determine if the interval will require an up transition.

Then at the point when the legato transition is played


```
legato_id := play_note(current_note, interval, 0, -1)
```

And providing the correct group is enabled this should play the correct legato transition sample.

I recommend adding the interval of the transition sample to the end of the samples name to make it easier to use Kontakt's automap feature, otherwise it will take hours to map each transition sample manually.


----------

