Friday, January 1, 2016

On reversing instruments in Kontakt

I wonder how would that piano sound backwards... That probably crossed every sampler user mind, at least once. Well, Kontakt has 'reverse' button, press it and Kontakt will play the samples backwards. However, the problem is, samples often have very long tails, so it takes a while until you can hear the sound and it's useless for dynamic playing. You would want to play the sample, but not from the beginning (or the end, depending on how we look at it). For this 'offset' parameter can be used, it tells the sampler from which point in the sample it should start playback. Now, another problem is, that samples have different lengths. To have more or less consistent volume envelope, when playing chords, you would want all played samples to reach the end of sample (reversed beginning) at the same time. Since offset is calculated from the beginning of the sample, and for reversed playback, from it's end, it's problematic. You would need to know the sample length for each note to calculate the proper offset. Considering velocity layers, it can be a lot of coding.
I wanted it, to be done automatically for any instrument, so I could play around with sound reversing. I came with somewhat crude solution, I wrote a script which will retrieve sample length for every note and every velocity in a group, remember it and apply offset accordingly when playing. To fetch the data for any sample set, I needed the script to actually play each note, so it could retrieve zone id and then sample length. Scanning takes a while, but in the end it's way faster, than doing it by hand. Here's a little presentation of reversing Kontakt's factory sitar.


You can get the script here. Both a preset file (which need to be copied to your Kontakt/presets/scripts folder) and as plain text that can be copied into Kontakt. It will work in Kontakt 4 or newer.

The shortcomings of this solutions are:
  • You can only use one group for reverse playing, so no round robin backwards.
  • It will work best on instruments build on sample per note basis. If samples are re-tuned, playback speed will vary and since offset is provided in microseconds, it will get misaligned. The more tune change, the more misalignment. To compensate for this, script would need to learn root key for each sample, which I don't know how to automatically retrieve in KSP.

About using the script:
  • First locate the group you want to reverse and set 'targ.group' accordingly. First group has index of zero.
  • Change sampler mode to 'sampler'. Disadvantage here is, that it will load all samples to memory, but offset parameter doesn't work in DFD mode. If you need to use DFD, you would have to edit 's.mod' value in wave editor to match sample length. And do that for every sample. No fun.
  • Switch reverse button!
  • You can adjust 'interval' setting, it's time to take to scan single note/velocity combination in microseconds. Lower the setting to scan faster. However I found that at low settings, the script failed to retrieve data correctly, 500 didn't work for me, 1000 did, but this may depend on settings/setup. I would consider 3000 a safe setting.
  • Now press scan button and wait for it to complete. If you need to break the process press it again.
  • Scanned data will be remembered in DAW project / patch, so if instrument/project is saved, you don't need to scan again.
  • Note that, the scanner will skip any note that hasn't a sample mapped at velocity of 1, to speed up scanning process.
  • Position knob sets the offset value, that is how far from the beginning should playback start. You can adjust this value while playing. Try not to exceed shortest sample length, or shorter samples will get misaligned.
  • When duration knob is set to zero, samples will play as long as the key is held (or until they reach the beginning of a sample). Any other setting will generate notes of fixed duration, defined in percentage of offset setting. You can use it, to prevent playback to reach the beginning of the sample and for smoother sound tail, if combined with volume envelope.
  • This script can interfere with other scripts, particularly those which also do ignore_event, play_note sequence, like factory portamento script, so you may need to disable other scripts, if it doesn't work.
  • Have fun reversing!