# Simple Sequencer + Tempo Sync



## DJSH-Music (Jun 9, 2015)

Hey Guys need a little help!

I have been scripting a little sequencer in kontakt.
Its working great so for except two things.

1.It doesn't follow the tempo of my daw (when changed)

2.How would I make it so that say if your playing a chord all the notes lock to the same tempo. Right now if you are slightly off with the timing the (pulse) of each note is out of sync with the others. I would like a more gated effect rather than every note have its own timing etc.

Thanks


on init

declare ui_table %steps[16] (4,4,127)
make_persistent(%steps)

declare polyphonic $counter

declare ui_menu $length
make_persistent($length)
add_menu_item($length, "1/4 Note", $DURATION_QUARTER)
add_menu_item($length, "1/8 Note", $DURATION_EIGHTH)
add_menu_item($length, "1/16 Note", $DURATION_SIXTEENTH)
declare ui_knob $numSteps(1,16,1)
$numSteps := 16
make_persistent($numSteps)
declare ui_knob $divisor(1,4,1)
$divisor := 2
make_persistent($divisor)
make_perfview
end on

on note
ignore_event($EVENT_ID)
$counter := 0

while($NOTE_HELD = 1)
play_note($EVENT_NOTE,%steps[$counter],0,$length/$divisor)
wait($length)

$counter := $counter + 1
if($counter >= $numSteps)
$counter := 0
end if
end while
end on


DJSH


----------



## mk282 (Jun 9, 2015)

If you want it to follow the tempo of your DAW, you must not use the wait() method in a while loop, you should use the listener callback instead.

If you own Retro Machines II (and most everyone owning Kontakt 5 should own it), check out how it's scripted for clues on how to do it.


----------



## Lindon (Jun 9, 2015)

The quick and dirty way is this:


```
on init 

	declare ui_table %steps[16] (4,4,127) 
	make_persistent(%steps) 

	declare polyphonic $counter 

	declare ui_menu $length 
	make_persistent($length) 
	add_menu_item($length, "1/4 Note", 1) 
	add_menu_item($length, "1/8 Note", 2) 
	add_menu_item($length, "1/16 Note",3)

	declare ui_knob $numSteps(1,16,1) 
	$numSteps := 16 
	make_persistent($numSteps) 

	declare ui_knob $divisor(1,4,1) 
	$divisor := 2 
	make_persistent($divisor) 
	make_perfview 
	
end on 

on note 
	ignore_event($EVENT_ID) 
	$counter := 0 

	while($NOTE_HELD = 1) 
		play_note($EVENT_NOTE,%steps[$counter],0,$length/$divisor) 
		select $length
			case 1
				wait(DURATION_QUARTER)
			case 2
				wait(DURATION_EIGHTH)
			case 3
				wait(DURATION_SIXTEENTH)
		end select
		$counter := $counter + 1 
		if($counter >= $numSteps) 
			$counter := 0 
		end if 
	end while 
end on
```

But Mario is right best to use the LCB...


----------



## DJSH-Music (Jun 9, 2015)

Hey Thanks guys for the speedy reply !

Lindon I pasted in the code but this pops up ? (check the attached picture)

where can I find more info on implementing LCB's

David [/img]


----------



## Lindon (Jun 9, 2015)

odd...

try replacing the select with this:


```
if $length = 1 
         wait(DURATION_QUARTER) 
      end if
      if $length = 2
         wait(DURATION_EIGHTH) 
      end if 
      if $length = 3
         wait(DURATION_SIXTEENTH) 
      end if
```

The LCB is in the callbacks section of the manual listed as

on listener

There's example code there...


----------



## Lindon (Jun 9, 2015)

you are using Nils editor right?


----------



## Lindon (Jun 9, 2015)

'cause if not then you need to replace

select $length


with

select ($length)


----------



## DJSH-Music (Jun 9, 2015)

I went and compiled the first code you sent in Nils editor and now its working!

Thanks so much :D

I will check out the manual now.


David


----------

