some love for session-scripts.
[ardour.git] / scripts / stop_at_marker.lua
1 ardour {
2         ["type"]    = "session",
3         name        = "Stop at Marker",
4         license     = "MIT",
5         author      = "Robin Gareus",
6         email       = "robin@gareus.org",
7         site        = "http://gareus.org",
8         description = [[An example session script which stops the transport on every location marker when rolling forward.]]
9 }
10
11 function factory ()
12         return function (n_samples)
13                 if (not Session:transport_rolling ()) then
14                         -- not rolling, nothing to do.
15                         return
16                 end
17
18                 local pos = Session:transport_frame () -- current playhead position
19                 local loc = Session:locations () -- all marker locations
20
21                 -- find first marker after the current playhead position, ignore loop + punch ranges
22                 -- (this only works when rolling forward, to extend this example see
23                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Locations )
24                 local m = loc:first_mark_after (pos, false)
25
26                 if (m == -1) then
27                         -- no marker was found
28                         return
29                 end
30
31                 -- since ardour may split the process cycle for events,
32                 -- n_samples may be smaller.
33                 local blk = Session:get_block_size ()
34
35                 -- transport stop can only happen on a process-cycle boundary.
36                 -- This callback happens from within the process callback,
37                 -- so we need to queue it ahead of time.
38                 if (pos + n_samples + blk >= m and pos + n_samples < m) then
39                         Session:request_transport_speed (0.0, true)
40                 end
41         end
42 end