forked from finale-lua/lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathostinato_maker.lua
411 lines (388 loc) · 14.8 KB
/
ostinato_maker.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
function plugindef()
finaleplugin.RequireSelection = false
finaleplugin.HandlesUndo = true
finaleplugin.Author = "Carl Vine after Michael McClennan & Jacob Winkler"
finaleplugin.AuthorURL = "https://carlvine.com/lua/"
finaleplugin.Copyright = "https://creativecommons.org/licenses/by/4.0/"
finaleplugin.Version = "v0.19"
finaleplugin.Date = "2024/01/31"
finaleplugin.MinJWLuaVersion = 0.62
finaleplugin.Notes = [[
Copy the current selection and paste it consecutively
to the right a nominated number of times.
The replicas can span barlines ignoring time signatures.
The same effect can be achieved with Edit → Paste Multiple
but this script is simpler to use and works intuitively
on the current music selection in a single step.
To repeat the last action without confirmation
dialog hold down [shift] when starting the script.
Choose to independently include or remove articulations,
expressions, smartshapes, lyrics or chords from the repeats.
This script grew from the "region_replicate_music.lua" script in
the FinaleLua.com repository by Michael McClennan and Jacob Winkler.
]]
return "Ostinato Maker...", "Ostinato Maker",
"Copy the current selection and paste it consecutively to the right a number of times"
end
local info_notes = [[
Copy the current selection and paste it consecutively
to the right a nominated number of times.
The replicas can span barlines ignoring time signatures.
The same effect can be achieved with Edit → Paste Multiple
but this script is simpler to use and works intuitively
on the current music selection in a single step.
**
To repeat the last action without confirmation
dialog hold down [shift] when starting the script.
Choose to independently include or remove articulations,
expressions, smartshapes, lyrics or chords from the repeats.
**
This script grew from the "region_replicate_music.lua" script in
the FinaleLua.com repository by Michael McClennan and Jacob Winkler.
**
Key Commands:
*• q @t show these script notes
*• w @t flip [copy articulations]
*• e @t flip [copy expressions]
*• r @t flip [copy smartshapes]
*• t @t flip [copy lyrics]
*• y @t flip [copy chords]
*– – –
*• a @t copy all
*• z @t copy none
]]
info_notes = info_notes:gsub("\n%s*", " "):gsub("*", "\n"):gsub("@t", "\t")
.. "\n(" .. finaleplugin.Version .. ")"
local configuration = require("library.configuration")
local mixin = require("library.mixin")
local library = require("library.general_library")
local script_name = library.calc_script_name()
local dialog, current_selection
local config = {
num_repeats = 1,
timer_id = 1,
window_pos_x = false,
window_pos_y = false,
}
local dialog_options = { -- and populate config values (unchecked)
"copy_articulations", "copy_expressions", "copy_smartshapes",
"copy_lyrics", "copy_chords"
}
for _, v in ipairs(dialog_options) do config[v] = 0 end -- (default unchecked)
local bounds = { -- primary region selection boundary properties
"StartStaff", "StartMeasure", "StartMeasurePos",
"EndStaff", "EndMeasure", "EndMeasurePos",
}
local function copy_region_bounds()
local copy = {}
for _, v in ipairs(bounds) do
copy[v] = finenv.Region()[v]
end
return copy
end
local function dialog_set_position()
if config.window_pos_x and config.window_pos_y then
dialog:StorePosition()
dialog:SetRestorePositionOnlyData(config.window_pos_x, config.window_pos_y)
dialog:RestorePosition()
end
end
local function dialog_save_position()
dialog:StorePosition()
config.window_pos_x = dialog.StoredX
config.window_pos_y = dialog.StoredY
configuration.save_user_settings(script_name, config)
end
local function measure_duration(measure_number)
local m = finale.FCMeasure()
return m:Load(measure_number) and m:GetDuration() or 0
end
local function selection_id()
if finenv.Region():IsEmpty() then return " - no selection - " end
local rgn = finenv.Region()
local ratio = rgn.StartMeasure + (rgn.StartMeasurePos / measure_duration(rgn.StartMeasure))
local id = "m" .. string.format("%.2f", ratio) .. "-"
local m = measure_duration(rgn.EndMeasure)
ratio = rgn.EndMeasure + (math.min(rgn.EndMeasurePos, m) / m)
id = id .. "m" .. string.format("%.2f", ratio)
return id
end
local function staff_id()
if finenv.Region():IsEmpty() then return "" end
local staff = finale.FCStaff()
staff:Load(finenv.Region().StartStaff)
local str = staff:CreateDisplayFullNameString()
local id = "Staff: " .. str.LuaString .. " → "
staff:Load(finenv.Region().EndStaff)
str = staff:CreateDisplayFullNameString()
return id .. str.LuaString
end
local function add_duration(measure_number, position, add_edu)
local m_width = measure_duration(measure_number)
if m_width == 0 then -- measure didn't load
return 0, 0
end
if position > m_width then
position = m_width
end
local remaining_to_add = position + add_edu
while remaining_to_add > m_width do
remaining_to_add = remaining_to_add - m_width
local next_width = measure_duration(measure_number + 1) -- another measure?
if next_width == 0 then -- no more measures
remaining_to_add = m_width -- finished calculating
else
measure_number = measure_number + 1 -- next measure
m_width = next_width
end
end
return measure_number, remaining_to_add
end
local function shift_region_by_EDU(rgn, add_edu)
rgn.EndMeasure, rgn.EndMeasurePos =
add_duration(rgn.EndMeasure, rgn.EndMeasurePos, add_edu)
if rgn.EndMeasure == 0 then return false end
rgn.StartMeasure, rgn.StartMeasurePos =
add_duration(rgn.StartMeasure, rgn.StartMeasurePos, add_edu)
if rgn.StartMeasure == 0 then return false end
return true
end
local function round_measure_position(measure_num, pos)
-- round off measure position to nearest reasonable sub-beat
local measure = finale.FCMeasure()
local beat_edu = finale.NOTE_QUARTER -- default for composite T_Sig
measure:Load(measure_num)
local time_sig = measure:GetTimeSignature()
if not time_sig.CompositeTop then -- simple T_Sig beat value
beat_edu = time_sig.BeatDuration
if (beat_edu % 3 == 0) then beat_edu = beat_edu / 3 end -- compound
end
local ok, count = false, 1
while not ok and count <= 3 do -- scan down to 1/8th sub-beat
local remainder = pos % beat_edu
if remainder == 0 then
ok = true -- found integer multiple
else
local ratio = remainder / beat_edu
local num_beats = math.floor(pos / beat_edu)
if ratio >= 7/8 or ratio <= 1/8 then
if ratio >= 7/8 then num_beats = num_beats + 1 end
pos = num_beats * beat_edu
ok = true -- within 1/8th of total beat
end
end
if not ok then
count = count + 1 -- one more cycle
beat_edu = beat_edu / 2 -- halve note value
end
end
return pos
end
local function region_duration(rgn)
local meas = {
start = rgn.StartMeasure,
stop = rgn.EndMeasure
}
local pos = {
start = round_measure_position(meas.start, rgn.StartMeasurePos),
stop = round_measure_position(meas.stop, rgn.EndMeasurePos)
}
local diff, duration
if meas.start == meas.stop then -- simple EDU offset
diff = pos.stop - pos.start
else
duration = -pos.start
while meas.start < meas.stop do
duration = duration + measure_duration(meas.start)
meas.start = meas.start + 1
end
diff = duration + pos.stop
end
return diff
end
local function region_erasures(rgn)
-- region-based markings
if config.copy_smartshapes == 0 then -- erase SMART SHAPES
local sh_rgn = finale.FCMusicRegion()
sh_rgn:SetRegion(rgn) -- extend erasure region for stray hairpins
sh_rgn.EndMeasure, sh_rgn.EndMeasurePos =
add_duration(sh_rgn.EndMeasure, sh_rgn.EndMeasurePos, 256)
for mark in loadallforregion(finale.FCSmartShapeMeasureMarks(), sh_rgn) do
local shape = mark:CreateSmartShape()
if shape then shape:DeleteData() end
end
end
if config.copy_expressions == 0 then -- erase EXPRESSIONS
local expressions = finale.FCExpressions()
expressions:LoadAllForRegion(rgn)
for exp in eachbackwards(expressions) do
if exp.StaffGroupID == 0 then exp:DeleteData() end
end
end
if config.copy_chords == 0 then -- erase CHORDS
local chords = finale.FCChords()
chords:LoadAllForRegion(rgn)
for chord in eachbackwards(chords) do
if chord then chord:DeleteData() end
end
end
-- then entry-based markings
if config.copy_articulations == 0 or config.copy_lyrics == 0 then
for entry in eachentrysaved(rgn) do
if entry.ArticulationFlag and config.copy_articulations == 0 then -- erase ARTICULATIONS
for articulation in eachbackwards(entry:CreateArticulations()) do
articulation:DeleteData()
end
entry:SetArticulationFlag(false)
end
if entry.LyricFlag and config.copy_lyrics == 0 then -- erase LYRICS
for _, v in ipairs{"FCChorusSyllable", "FCSectionSyllable", "FCVerseSyllable"} do
local lyric = finale[v]()
lyric:SetNoteEntry(entry)
while lyric:LoadFirst() do
lyric:DeleteData()
end
end
end
end
end
end
local function paste_many_copies()
if finenv.Region():IsEmpty() then
finenv.UI():AlertError("Please select some music before\n"
.. "running this script.", plugindef() .. " Error" )
return
end
local rgn = finenv.Region()
local origin = copy_region_bounds() -- copy current bounds
local undo_str = "Ostinato Maker " .. selection_id()
finenv.StartNewUndoBlock(undo_str, false)
-- ---- DO THE WORK
local m_d = measure_duration(rgn.EndMeasure)
if rgn.EndMeasurePos >= m_d then
rgn.EndMeasurePos = m_d - 1
end
rgn:CopyMusic() -- save a copy of the current selection
local duration = region_duration(rgn)
local first_rpt = nil -- save start position of first repeat
for _ = 1, config.num_repeats do
if not shift_region_by_EDU(rgn, duration) then break end
rgn:PasteMusic()
if not first_rpt then -- only save the first repeat region
first_rpt = { measure = rgn.StartMeasure, pos = rgn.StartMeasurePos }
end
end
rgn:ReleaseMusic()
-- erase unwanted markings across whole ostinato passage
rgn.StartMeasure = first_rpt.measure
rgn.StartMeasurePos = first_rpt.pos
region_erasures(rgn)
-- reset to start of operation
for _, v in ipairs(bounds) do rgn[v] = origin[v] end
rgn:SetInDocument()
-- ----
if finenv.EndUndoBlock then
finenv.EndUndoBlock(true)
finenv.Region():Redraw()
else
finenv.StartNewUndoBlock(undo_str, true)
end
end
local function on_timer()
local changed = false
for _, v in ipairs(bounds) do
if current_selection[v] ~= finenv.Region()[v] then
changed = true
break
end
end
if changed then
current_selection = copy_region_bounds()
dialog:GetControl("info")
:SetText("Selection " .. selection_id() .. "\n" .. staff_id())
end
end
local function create_dialog()
local edit_x, e_wide, y_step = 110, 40, 18
local y_offset = finenv.UI():IsOnMac() and 3 or 0
local save_rpt = config.num_repeats
local name = plugindef():gsub("%.%.%.", "")
dialog = mixin.FCXCustomLuaWindow():SetTitle(name)
local y = 0
local function flip_check(id)
local ctl = dialog:GetControl(dialog_options[id])
ctl:SetCheck((ctl:GetCheck() + 1) % 2)
end
local function check_all_state(state)
for _, v in ipairs(dialog_options) do
dialog:GetControl(v):SetCheck(state)
end
end
local function info_dialog()
finenv.UI():AlertInfo(info_notes, "About " .. name)
end
local function key_check(ctl) -- some stray key commands
local s = ctl:GetText():lower()
if s:find("[^0-9]") then
if s:find("q") then info_dialog()
elseif s:find("w") then flip_check(1)
elseif s:find("e") then flip_check(2)
elseif s:find("r") then flip_check(3)
elseif s:find("t") then flip_check(4)
elseif s:find("y") then flip_check(5)
elseif s:find("a") then check_all_state(1)
elseif s:find("z") then check_all_state(0)
end
ctl:SetText(save_rpt):SetKeyboardFocus()
else
s = s:sub(-3) -- 3-digit limit
ctl:SetText(s)
save_rpt = s
end
end
dialog:CreateStatic(0, y, "info")
:SetText("Selection " .. selection_id() .. "\n" .. staff_id())
:SetWidth(edit_x * 2):SetHeight(30)
y = y + 35
dialog:CreateStatic(0, y):SetText("Repeat ostinato:"):SetWidth(edit_x)
dialog:CreateStatic(edit_x, y):SetText("Include:"):SetWidth(90)
y = y + y_step
local num_repeats = dialog:CreateEdit(0, y + 2 - y_offset)
:SetWidth(e_wide - 4):SetText(config.num_repeats)
:AddHandleCommand(function(self) key_check(self) end)
dialog:CreateStatic(e_wide, y + 2):SetText("times"):SetWidth(edit_x)
for _, v in ipairs(dialog_options) do
dialog:CreateCheckbox(edit_x, y, v):SetCheck(config[v])
:SetText(v:sub(6, -1):gsub("^%l", string.upper)):SetWidth(90)
y = y + y_step
end
local q = dialog:CreateButton(0, y - 19):SetText("?"):SetWidth(20)
:AddHandleCommand(function() info_dialog() end)
dialog:CreateOkButton()
dialog:CreateCancelButton()
dialog_set_position()
dialog:RegisterHandleTimer(on_timer)
dialog:RegisterInitWindow(function()
q:SetFont(q:CreateFontInfo():SetBold(true))
num_repeats:SetKeyboardFocus()
dialog:SetTimer(config.timer_id, 125)
end)
dialog:RegisterCloseWindow(function()
dialog_save_position()
dialog:StopTimer(config.timer_id)
end)
dialog:RegisterHandleOkButtonPressed(function()
config.num_repeats = num_repeats:GetInteger()
for _, v in ipairs(dialog_options) do
config[v] = dialog:GetControl(v):GetCheck()
end
paste_many_copies()
end)
end
local function make_ostinato()
configuration.get_user_settings(script_name, config, true)
current_selection = copy_region_bounds()
create_dialog()
dialog:RunModeless()
end
make_ostinato()