# Wavetable zone waveform



## Claud9 (Feb 23, 2019)

Hello,
I'm doing some script experiments with the new wavetable functions in K6.

Everything is working fine with 1 group/zone

But if I try with a 4 group/zones instrument I have some problems showing the correct zone/waveform...

This is the code I have in the on init


```
declare ui_wavetable $wt
set_control_par(get_ui_id($wt), $CONTROL_PAR_WT_VIS_MODE, $NI_WT_VIS_2D)
make_persistent($wt)
read_persistent_var($wt)

declare ui_menu $waveform
while ($i < $NUM_GROUPS)
  add_menu_item($waveform,group_name($i),$i)
     inc($i)
   end while

if ($waveform=0)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,1)
end if
if ($waveform=1)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,2)
end if
if ($waveform=2)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,3)
end if
if ($waveform=3)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,4)
end if

make_persistent($waveform)
read_persistent_var($waveform)
end on
```
 
in the on ui_control I have this:


```
on ui_control ($waveform)
  $i :=0
  while ($i < $NUM_GROUPS)
   purge_group($i,0)
   inc($i)
end while
purge_group($waveform,1)
if ($waveform=0)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,1)
end if
if ($waveform=1)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,2)
end if
if ($waveform=2)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,3)
end if
if ($waveform=3)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,4)
end if
end on
```

Thanks in advance for any help


----------



## EvilDragon (Feb 23, 2019)

You need to know the zone ID of the waveform you want to show. You cannot just use raw numbers without consequence. The way to do this is the same as with ui_waveform control... You need those zone IDs for each group you want to display the waveform/wavetable for. Best use a lookup table once you get zone IDs (preferably after your instrument structure is completed as far as sample mapping is concerned).


----------



## Claud9 (Feb 23, 2019)

EvilDragon said:


> You need to know the zone ID of the waveform you want to show. You cannot just use raw numbers without consequence. The way to do this is the same as with ui_waveform control... You need those zone IDs for each group you want to display the waveform/wavetable for. Best use a lookup table once you get zone IDs (preferably after your instrument structure is completed as far as sample mapping is concerned).


Thanks for the help. 
Zone ID 1 2 3 4 are the one I can find in the "expert - zone" tab. 
The script I posted work. I can see the waveforms but is not working properly.
First problem: If I save an instrument that shows for example waveform of Zone ID 1 when I reload the instrument the waveform does not show up.
Second problem: when I select one of the groups with the menu the first time the waveform is not loaded and stay the same of the previous group, It's loaded only after I re-select that group with the menu.


----------



## EvilDragon (Feb 24, 2019)

You need to set the waveform AFTER read_persistent_var for the menu in init callback. But better yet, do it in persistence_changed callback and then you don't need to use read_persistent_var at all.


----------



## Claud9 (Feb 24, 2019)

EvilDragon said:


> You need to set the waveform AFTER read_persistent_var for the menu in init callback. But better yet, do it in persistence_changed callback and then you don't need to use read_persistent_var at all.


I have already tried both your suggestions but the problem remains the same: when I reload the saved instrument the waveform does not show up and if I change group/zone with the menu the waveform change only if I select with the menu the group/zone twice, It never changes at the first attempt. Once I have correctly shown all waveforms than they show up correctly at the first attempt when I change group/zone with the menu.


----------



## EvilDragon (Feb 24, 2019)

It has to work after moving the $CONTROL_PAR_WT_ZONE part in "on persistence_changed". Tried that here and it works. You're doing something else wrong, then.


(Also, there's no point in making the ui_wavetable control persistent at all.)


----------



## Claud9 (Feb 24, 2019)

this is working now when I reload the instrument but is not working in the ui_control callback. I have always to reload twice the group in order to have Kontakt show the correct waveform. is this the correct way to show waveform in the ui_control callback?


```
on init
make_perfview
set_ui_width_px(170)
 
declare $i
declare ui_wavetable $wt
set_control_par(get_ui_id($wt), $CONTROL_PAR_WT_VIS_MODE, $NI_WT_VIS_2D)
 
declare ui_menu $waveform
make_persistent($waveform)
read_persistent_var($waveform)

while ($i < $NUM_GROUPS)
add_menu_item($waveform,group_name($i),$i)
inc($i)
end while
end on
on persistence_changed
if ($waveform=0)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,1)
end if
if ($waveform=1)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,2)
end if
if ($waveform=2)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,3)
end if
if ($waveform=3)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,4)
end if
end on

on ui_control ($waveform)
$i :=0
while ($i < $NUM_GROUPS)
purge_group($i,0)
inc($i)
end while
purge_group($waveform,1)
if ($waveform=0)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,1)
end if
if ($waveform=1)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,2)
end if
if ($waveform=2)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,3)
end if
if ($waveform=3)
set_control_par(get_ui_id($wt),$CONTROL_PAR_WT_ZONE,4)
end if
end on
```


----------



## EvilDragon (Feb 24, 2019)

It should work, I'm not having any problems changing the waveforms from ui_control callback.


----------



## Claud9 (Feb 24, 2019)

EvilDragon said:


> It should work, I'm not having any problems changing the waveforms from ui_control callback.


I really don't understand


EvilDragon said:


> It should work, I'm not having any problems changing the waveforms from ui_control callback.


Just tested again on a second computer, same problem. I have to load twice the same group/zone with the menu in order to load the correct waveform. On the first attempt, the waveform is not loaded. Have you tried to save and reopen the instrument? When I do so I always have the waveform problem. Is there another method to load wavetable waveform that I can try in the ui_control callback? Thank in advance for any help


----------



## EvilDragon (Feb 24, 2019)

Claud9 said:


> Have you tried to save and reopen the instrument?



Yes, works fine here.



Claud9 said:


> Is there another method to load wavetable waveform that I can try in the ui_control callback?



No. That's the proper/only command to load wavetable waveforms into ui_wavetable widget. You have some other problem in the code, then, because it all works just fine here.


```
on init
   set_ui_height(2)

   declare $i

   declare ui_menu $Wave
   declare ui_wavetable $WT

    $i := 0
    while ($i < $NUM_GROUPS)
       add_menu_item($Wave, "Group " & $i + 1, $i)
       inc($i)
   end while
   
   make_persistent($Wave)

   set_control_par(get_ui_id($WT), $CONTROL_PAR_WIDTH, 200)
   set_control_par(get_ui_id($WT), $CONTROL_PAR_HEIGHT, 75)
   move_control_px($WT, 158, 2)
end on

function LoadWT()
   set_control_par(get_ui_id($WT), $CONTROL_PAR_WT_ZONE, $Wave)
end function

on persistence_changed
   call LoadWT()
end on

on ui_control ($Wave)
    $i := 0
    while ($i < $NUM_GROUPS)
       purge_group($i, 0)
       inc($i)
   end while
   purge_group($i, $Wave)

   call LoadWT()
end on
```

In the above example, wavetables have zone IDs 0, 1, 2, 3.


----------



## Claud9 (Feb 25, 2019)

Thanks for the code. It is supposed to work as it is or I need to add something?
I have created a new instrument from scratch - 4 groups and exactly the same zone IDs of your example, but is not working and I have exactly the same problems as before.
Also, groups were not playing at all, I had to change this line of your code " 
purge_group($i, $Wave)" with this : "purge_group($Wave,1)" in order to have the selected group playing. But as before if I save the instrument and I releoad it no waveform is shown and in order to view the correct waveform I have to select twice each group/zone. The same problem also with saved Snapshots.



EvilDragon said:


> Yes, works fine here.
> 
> 
> 
> ...


----------



## EvilDragon (Feb 25, 2019)

It's working fine over here. Yeah I made a typo there in purge_group(), but otherwise it's working just fine over here.


----------



## Claud9 (Feb 25, 2019)

Thanks for sharing, but it's still not working here (pls see attached video). I have tested with both Full version and Player on two different Mac, the problem is always the same: I have to select twice the group in order to see the correct waveform. It's a bug of K6 Mac version?


----------



## EvilDragon (Feb 25, 2019)

Yeah that's bizarre, it works totally fine over here...


Can any other Mac users here test the NKI I posted, please?


----------



## polypx (Feb 25, 2019)

Mac here. Strange behaviour for me as well. It updates if you select the menu item twice, or else after you select you hit the exclamation mark.

Once all four have been selected successfully once it works fine after that.


----------



## geronimo (Feb 25, 2019)

If we must see a Waveform interface, I see just the Menu, with this Script .
I'm under SIERRA 10.12.6. _

EDIT:sorry, it's work fine with your .NKI _


----------



## Claud9 (Feb 25, 2019)

geronimo said:


> If we must see a Waveform interface, I see just the Menu, with this Script .
> I'm under SIERRA 10.12.6. _
> 
> EDIT:sorry, it's work fine with your .NKI _


Load the .NKI and then try to change the group. The waveform should update only if you select the menu item twice. You have this problem only right after loading the instrument, as polypx wrote after you have correctly selected all groups twice then the waveform is loaded correctly the first time you change group.


----------



## EvilDragon (Feb 25, 2019)

Weirdity.


Can you try my NKI but completely remove the purging part (and make sure all groups are loaded into memory)? See what happens.

If it works then, I guess it might be that purging somehow influences retrieving the waveform for some reason. Try then adding a short wait(50000) after purging but BEFORE setting the waveform.


----------



## Claud9 (Feb 25, 2019)

adding wait(50000) made it and now It works fine! remain a mystery why on PC K6 is working without!


----------



## EvilDragon (Feb 25, 2019)

Beats me.


----------



## Claud9 (Feb 25, 2019)

EvilDragon said:


> Beats me.


But why now if I change your samples with mine I don't see any waveform anymore? I noticed that zones are changed to 4 - 5 - 6 - 7. It works only if zones are 0 - 1 - 2 - 3? And it works only with 4 groups/zones? I'm confused what's going on here!


----------



## EvilDragon (Feb 25, 2019)

Yes, when you remove zones, it doesn't reclaim back zone IDs so that they could be reused, it just keeps incrementing them. There's a trick to reset zone ID counter back to zero, though. Remove all zones from the instrument, then load an effect, any effect, to any slot, then undo one step back. Now when you drop new samples in they will start from zone ID 0.

In ANY case, messing with zone IDs like this is never a good idea. You should always properly retrieve zone IDs and put them in a lookup array _only after your instrument is completely mapped out_!


----------



## polypx (Feb 25, 2019)

Weird. Same here, the wait solves it.


----------



## EvilDragon (Feb 25, 2019)

I guess you don't have a Windows machine handy to test it out there too, mayhaps?


----------



## polypx (Feb 25, 2019)

Only a VM, probably not the best place to test.


----------



## EvilDragon (Feb 25, 2019)

No, it's worth a shot! Especially because it's ran on the same hardware... An even better test would be bootcamped Windows on the same Mac, but that's an obvious pain in the ass.


----------



## Claud9 (Feb 25, 2019)

EvilDragon said:


> Yes, when you remove zones, it doesn't reclaim back zone IDs so that they could be reused, it just keeps incrementing them. There's a trick to reset zone ID counter back to zero, though. Remove all zones from the instrument, then load an effect, any effect, to any slot, then undo one step back. Now when you drop new samples in they will start from zone ID 0.
> 
> In ANY case, messing with zone IDs like this is never a good idea. You should always properly retrieve zone IDs and put them in a lookup array _only after your instrument is completely mapped out_!


The effect trick worked but I have no idea how to do what you suggested with the lookup array. 
I have to do a sort of list with all ID's and corresponding groups?
Also just to understand, the code as is in the NKI you posted works properly only if Zone ID 0 corresponds to Group 0 and so on.... correct?


----------



## polypx (Feb 25, 2019)

Ok, I ran it in Bootcamp (Win 10), on the same Mac, and the same problem happens in Windows. Without the wait I have to select twice, with the wait it works properly.

This on a pretty old MacBookPro.... 2015.


----------



## EvilDragon (Feb 25, 2019)

Weird. I can't repro it here, honestly. Waveforms change the first time you change the menu selection... And this is not even on an SSD...



Claud9 said:


> Also just to understand, the code as is in the NKI you posted works properly only if Zone ID 0 corresponds to Group 0 and so on.... correct?



Yes.



Claud9 said:


> I have to do a sort of list with all ID's and corresponding groups?



You need just one zone ID per group. If you have multiple layers that all have the same sounds, you don't have to have ALL groups in the lookup array, you can just use the first set of groups (for layer 1) to show different waveforms for all x layers that you have.


----------



## EvilDragon (Feb 25, 2019)

Also just to show that I'm not crazy:


----------



## EvilDragon (Feb 26, 2019)

OK it seems there is a direct correlation between loading the samples and waveforms being drawn. If the sample was still in the process of loading while you executed setting the waveform in script, the waveform won't be updated. That's why the wait() helps. There's also a difference between regular HDDs and SSDs in this case - SSDs are faster so, it's obvious they'd probably work better in this case.


Actually last night I managed to make it not work for me too, but only in one particular case: after loading the NKI, go straight for Group 3. It wouldn't refresh the waveform. But if I selected ANY other group except group 3 after loading the NKI, it would update the waveform properly.

Then I moved the NKI to an SSD, and Group 3 changed waveform immediately after loading the NKI, so that was all fine!


It seems it would be great if purge_group() command were made async aware, so that we actually know when stuff is done loading, so that we can do GUI-related stuff that ties into sample loading (like showing waveforms) properly.


----------



## Claud9 (Feb 26, 2019)

EvilDragon said:


> OK it seems there is a direct correlation between loading the samples and waveforms being drawn. If the sample was still in the process of loading while you executed setting the waveform in script, the waveform won't be updated. That's why the wait() helps. There's also a difference between regular HDDs and SSDs in this case - SSDs are faster so, it's obvious they'd probably work better in this case.
> 
> 
> Actually last night I managed to make it not work for me too, but only in one particular case: after loading the NKI, go straight for Group 3. It wouldn't refresh the waveform. But if I selected ANY other group except group 3 after loading the NKI, it would update the waveform properly.
> ...


I tried with longer samples, 3D waveform and bigger and I had to increase the wait time to 200000 in order to make it work properly.


----------



## EvilDragon (Feb 26, 2019)

Yeah, that's no good. Well, I'm told there have been some issues in showing wavetable waveforms in certain cases, and some of those cases have been remedied (but this is not in the official 6.0.4 release), so in the next Kontakt update we _might_ see some improvements here.


----------



## Claud9 (Feb 26, 2019)

EvilDragon said:


> Yeah, that's no good. Well, I'm told there have been some issues in showing wavetable waveforms in certain cases, and some of those cases have been remedied (but this is not in the official 6.0.4 release), so in the next Kontakt update we _might_ see some improvements here.


I was also experimenting with adding an envelope or LFO to the position knob. Is there a way to actually see those position changes reflected in the ui waveform as you see when you manually move the position knob with the ui_control callback?


----------



## EvilDragon (Feb 26, 2019)

No. Only direct change of WT position engine parameter will be displayed, but not modulation.


----------



## geronimo (Feb 26, 2019)

Claud9 said:


> I was also experimenting with adding an envelope or LFO to the position knob. Is there a way to actually see those position changes reflected in the ui waveform as you see when you manually move the position knob with the ui_control callback?



It would have been too good !


----------



## Claud9 (Feb 27, 2019)

EvilDragon said:


> No. Only direct change of WT position engine parameter will be displayed, but not modulation.


and at least seeing the "position" knob moving when I activate the LFO as it is moving when I enter in editing mode?


----------



## EvilDragon (Feb 27, 2019)

The light around the knob will move according to modulation, in instrument edit mode. That's normal for any modulatable parameter.


----------



## Claud9 (Feb 27, 2019)

EvilDragon said:


> The light around the knob will move according to modulation, in instrument edit mode. That's normal for any modulatable parameter.


I was meaning if it is possible with a script to see that knob movement we see in instrument edit mode also in our own GUI when we activate an LFO?


----------



## EvilDragon (Feb 27, 2019)

No.


----------



## Claud9 (Mar 14, 2019)

EvilDragon said:


> No.


Ok but so how is possible to do this as is shown in this instrument?

If I add an LFO to a parameter my custom knob is not moving, how is possible that in this instrument is moving?


----------



## EvilDragon (Mar 15, 2019)

That's just showing the volume level of each loaded group. It doesn't directly show the LFO modulation - LFO just modulates group volume 


Interesting that you linked to the library I actually scripted


----------



## geronimo (May 8, 2019)

Claud9 said:


> First problem: If I save an instrument that shows for example waveform of Zone ID 1 when I reload the instrument the waveform does not show up.


Solved with the KONTAKT update 6.1.0. _


----------



## mscp (May 8, 2019)

EvilDragon said:


> That's just showing the volume level of each loaded group. It doesn't directly show the LFO modulation - LFO just modulates group volume
> 
> 
> Interesting that you linked to the library I actually scripted



EvilDragon - just felt like you should know. Every time I read your posts, I get drawn to look at your profile picture - which often takes part of my soul away like Voldemort does in HP.


----------



## EvilDragon (May 8, 2019)

Well, I need to feed myself _somehow_!


----------



## Rancine (Jan 23, 2021)

Claud9 said:


> Ok but so how is possible to do this as is shown in this instrument?
> 
> If I add an LFO to a parameter my custom knob is not moving, how is possible that in this instrument is moving?



it seems possible with the update of the kontakt play series - would love to know how this is done


----------



## soundtrax (Jan 25, 2021)

Rancine said:


> it seems possible with the update of the kontakt play series - would love to know how this is done


You'd need to script a custom LFO for that.


----------

