-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequencer.lua
297 lines (271 loc) · 9.55 KB
/
sequencer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
local sequencer = {}
local log = hs.logger.new('sequencer', 'debug')
sequencer.hotkeys = {}
sequencer.eventtaps = {}
sequencer.zoomSum = 0
sequencer.lastZoomTime = 0
sequencer.zoomThreshold = 0.10
sequencer.colorPicker = dofile(hs.spoons.resourcePath('color_picker.lua'))
local function applescript(x)
hs.execute('osascript -e ' .. [[']] .. string.gsub(x, [[']], [['\'']]) .. [[']])
end
-----------
-- setup --
-----------
function sequencer:bindHotkeys(maps)
table.insert(sequencer.hotkeys, sequencer:bounceClip(maps))
table.insert(sequencer.hotkeys, sequencer:flatten(maps))
table.insert(sequencer.hotkeys, sequencer:joinClips(maps))
table.insert(sequencer.hotkeys, sequencer:doubleTempo(maps))
table.insert(sequencer.hotkeys, sequencer:halfTempo(maps))
table.insert(sequencer.hotkeys, sequencer:legato(maps))
table.insert(sequencer.hotkeys, sequencer:quantize(maps))
table.insert(sequencer.hotkeys, sequencer:reverse(maps))
table.insert(sequencer.hotkeys, sequencer:setLoopAndPlay(maps))
table.insert(sequencer.hotkeys, sequencer:color(maps))
table.insert(sequencer.eventtaps, sequencer:mouse4Mute())
table.insert(sequencer.eventtaps, sequencer:pinchZoom())
end
function sequencer:activate(app)
for _, v in pairs(sequencer.hotkeys) do v:enable() end
for _, v in pairs(sequencer.eventtaps) do v:start() end
sequencer.app = app
log.d('sequencer activated')
end
function sequencer:deactivate()
for _, v in pairs(sequencer.hotkeys) do v:disable() end
for _, v in pairs(sequencer.eventtaps) do v:stop() end
log.d('sequencer deactivated')
end
-----------
-- mouse --
-----------
-- sequencer:mouse4Mute()
-- Method
-- Maps MOUSE4 to "M", for muting clips and notes in the sequencer
--
-- Returns:
-- * An hs.eventtap object, to be addded to this module's eventtaps table
function sequencer:mouse4Mute()
return hs.eventtap.new(
{ hs.eventtap.event.types.otherMouseUp }, function(event)
local buttonNumber = tonumber(hs.inspect(event:getProperty(hs.eventtap.event.properties
.mouseEventButtonNumber)))
if buttonNumber == 3 then
hs.eventtap.event.newKeyEvent('m', true):setFlags({}):post()
hs.eventtap.event.newKeyEvent('m', false):setFlags({}):post()
log.d('mouse4')
end
end)
end
-- sequencer:pinchZoom()
-- Method
-- Zooms the timeline in and out with a pinch gesture on the trackpad
-- Hold cmd while pinching to zoom vertically
-- Hold shift while pinching to zoom in on the playhead instead of the cursor
--
-- Returns:
-- * An hs.eventtap object, to be addded to this module's eventtaps table
function sequencer:pinchZoom()
return hs.eventtap.new({ hs.eventtap.event.types.gesture }, function(event)
local gestureType = event:getType(true)
if gestureType ~= hs.eventtap.event.types.magnify then return end
local zoomTime = hs.timer.absoluteTime()
if zoomTime - sequencer.lastZoomTime > 1000000000 then sequencer.zoomSum = 0 end
sequencer.lastZoomTime = zoomTime
-- the zoom key commands are much less sensitive than the scroll wheel
-- so we lower the threshold when using them
local threshold = sequencer.zoomThreshold
if event:getFlags()['shift'] then threshold = threshold * 0.5 end
local zoomLevel = event:getTouchDetails().magnification
sequencer.zoomSum = sequencer.zoomSum + zoomLevel
local offsets = {}
if sequencer.zoomSum >= threshold then
offsets = { 1, 0 }
sequencer.zoomSum = 0
elseif sequencer.zoomSum <= -threshold then
offsets = { -1, 0 }
sequencer.zoomSum = 0
else
return
end
if event:getFlags()['shift'] or event:getFlags()['cmd'] then
local flags = { ['cmd'] = true, ['shift'] = true }
if event:getFlags()['cmd'] then flags['alt'] = true end
local key = offsets[1] == 1 and '=' or '-'
hs.eventtap.event.newKeyEvent(key, true):setFlags(flags):post()
hs.eventtap.event.newKeyEvent(key, false):setFlags(flags):post()
else
hs.eventtap.event.newScrollEvent(offsets, { 'cmd', 'shift' }, 'pixel'):post()
end
end)
end
--------------
-- keybinds --
--------------
-- sequencer:bounceClip(m)
-- Method
-- Opens the dialog to bounce the currently selected clip(s) to disk
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:bounceClip(m)
return hs.hotkey.new(m.bounceClip[1], m.bounceClip[2], function()
sequencer.app:selectMenuItem({ 'Edit', 'Bounce', 'Bounce Clip to Disk…' })
log.d('bouncing clip to disk')
end)
end
-- sequencer:flatten(m)
-- Method
-- Performs a sequence of actions to "flatten" a clip and match the current tempo
-- In order, it does:
-- * Disable Stretch
-- * Bounce Clips to New Recordings
-- * Enable Stretch
-- * Delete Unused Recordings
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:flatten(m)
return hs.hotkey.new(m.flatten[1], m.flatten[2], function()
sequencer.app:selectMenuItem({ 'Edit', 'Disable Stretch' })
sequencer.app:selectMenuItem({ 'Edit', 'Bounce', 'Bounce Clips to New Recordings' })
sequencer.app:selectMenuItem({ 'Edit', 'Bounce', 'Enable Stretch' })
local ok = sequencer.app:selectMenuItem({ 'Edit', 'Delete Unused Recordings' })
applescript(
[[tell application "System Events" to click button "Delete" of front window of application process "Reason"]])
log.d('flattened clips')
end)
end
-- sequencer:joinClips(m)
-- Method
-- Joins the selected clips
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:joinClips(m)
return hs.hotkey.new(m.joinClips[1], m.joinClips[2], function()
sequencer.app:selectMenuItem({ 'Edit', 'Join Clips' })
log.d('joined clips')
end)
end
-- sequencer:doubleTempo(m)
-- Method
-- Doubles the tempo of the selected clips/MIDI notes
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:doubleTempo(m)
return hs.hotkey.new(m.doubleTempo[1], m.doubleTempo[2], function()
applescript(
'tell application "System Events" to click button 7 of window "Tool Window" of application process "Reason"')
log.d('doubled tempo')
end)
end
-- sequencer:halfTempo(m)
-- Method
-- Halves the tempo of the selected clips/MIDI notes
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:halfTempo(m)
return hs.hotkey.new(m.halfTempo[1], m.halfTempo[2], function()
applescript(
'tell application "System Events" to click button 8 of window "Tool Window" of application process "Reason"')
log.d('halved tempo')
end)
end
-- sequencer:legato(m)
-- Method
-- Applies the currently selected legato adjustment in the tool window
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:legato(m)
return hs.hotkey.new(m.legato[1], m.legato[2], function()
applescript(
'tell application "System Events" to click button 5 of window "Tool Window" of application process "Reason"')
log.d('legato')
end)
end
-- sequencer:quantize(m)
-- Method
-- Applies the currently selected quantize settings in the tool window
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:quantize(m)
return hs.hotkey.new(m.quantize[1], m.quantize[2], function()
sequencer.app:selectMenuItem({ 'Edit', 'Quantize' })
log.d('quantized')
end)
end
-- sequencer:reverse(m)
-- Method
-- Reverses the currently selected clips/MIDI notes
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:reverse(m)
return hs.hotkey.new(m.reverse[1], m.reverse[2], function()
sequencer.app:selectMenuItem({ 'Edit', 'Reverse' })
log.d('reversed')
end)
end
-- sequencer:setLoopAndPlay(m)
-- Method
-- Sets the loop to the current selection and starts playback
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:setLoopAndPlay(m)
return hs.hotkey.new(m.setLoopAndPlay[1], m.setLoopAndPlay[2], function()
sequencer.app:selectMenuItem({ 'Edit', 'Set Loop to Selection and Start Playback' })
log.d('set loop and play')
end)
end
-- mixer:color(m)
-- Method
-- Activates a colorPicker (hs.chooser) with available track colors
-- The selected color is then applied to the selected tracks
--
-- Parameters:
-- * m - A table of hotkey mappings
--
-- Returns:
-- * An hs.hotkey object, to be addded to this module's hotkeys table
function sequencer:color(m)
return hs.hotkey.new(m.color[1], m.color[2], function()
local picker = sequencer.colorPicker:setup(sequencer.app, 'Track Color')
sequencer.colorPicker:show()
log.d('showing sequencer device color picker')
end)
end
return sequencer