forked from finale-lua/lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
note_cluster_determinate.lua
381 lines (341 loc) · 13.5 KB
/
note_cluster_determinate.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
function plugindef()
finaleplugin.RequireSelection = true
finaleplugin.Author = "Jacob Winkler" -- With help & advice from CJ Garcia, Nick Mazuk, and Jan Angermüller. Thanks guys!
finaleplugin.Copyright = "©2019 Jacob Winkler"
finaleplugin.AuthorEmail = "[email protected]"
finaleplugin.Version = "1.0.1"
finaleplugin.Date = "11/02/2019"
return "Cluster - Determinate", "Cluster - Determinate", "Creates a determinate cluster."
end
local note_entry = require("library.note_entry")
local region = finenv.Region()
local layer = {}
local layer_one_note = {}
local layer_two_note = {}
local measure = {}
local horizontal_offset = -20
-- FUNCTION 1: Delete and Hide Notes
local function process_notes(music_region)
local stem_dir = {}
-- First build up a table of the initial stem direction information
for entry in eachentrysaved(region) do
entry.FreezeStem = false
table.insert(stem_dir, entry:CalcStemUp())
end
layer.copy(1, 2)
layer.copy(1, 3)
local i = 1 -- To iterate stem direction table for Layer 1
local j = 1 -- To iterate stem direction table for Layer 2
for entry in eachentrysaved(music_region) do
local span = entry:CalcDisplacementRange(nil)
local stem_direction
if entry.LayerNumber == 1 then
stem_direction = stem_dir[i]
if entry:IsNote() then
if span > 2 then
delete_bottom_notes(entry)
else
delete_middle_notes(entry)
entry.FreezeStem = true
entry.StemUp = stem_direction
end
elseif entry:IsRest() then
entry:SetRestDisplacement(6)
end
if stem_direction == false and span > 2 then
hide_stems(entry, stem_direction)
end
i = i + 1
elseif entry.LayerNumber == 2 then
stem_direction = stem_dir[j]
if entry:IsNote() and span > 2 then
delete_top_notes(entry)
else
entry:MakeRest()
entry.Visible = false
entry:SetRestDisplacement(4)
end
if stem_direction == true then
hide_stems(entry, stem_direction)
end
j = j + 1
elseif entry.LayerNumber == 3 then
if entry:IsNote() then
for note in each(entry) do
note.AccidentalFreeze = true
note.Accidental = false
end
entry.FreezeStem = true
entry.StemUp = true
hide_stems(entry, true)
delete_top_bottom_notes(entry)
elseif entry:IsRest() then
entry:SetRestDisplacement(2)
end
entry.Visible = false
end
entry.CheckAccidentals = true
if entry:IsNote() then
local n = 1
for note in each(entry) do
note.NoteID = n
n = n + 1
end
end
end
end
-- Function 2: Hide Stems (from JW's Harp Gliss script, modified)
function hide_stems(entry, stem_direction)
local stem = finale.FCCustomStemMod()
stem:SetNoteEntry(entry)
if stem_direction then -- Reverse "stemDir"
stem_direction = false
else
stem_direction = true
end
stem:UseUpStemData(stem_direction)
if stem:LoadFirst() then
stem.ShapeID = 0
stem:Save()
else
stem.ShapeID = 0
stem:SaveNew()
end
entry:SetBeamBeat(true) -- Since flags get hidden, use this instead of trying tro change beam width
end
-- Function 3 - Copy Layer "src" to Layer "dest"
function layer.copy(source, destination) -- source and destination layer numbers, 1 based
local region = finenv.Region() -- luacheck: ignore region
local start = region.StartMeasure
local stop = region.EndMeasure
local system_staves = finale.FCSystemStaves()
system_staves:LoadAllForRegion(region)
-- Set variables for 0-based layers
source = source - 1
destination = destination - 1
for system_staff in each(system_staves) do
local staff_number = system_staff.Staff
local note_entry_layer_source = finale.FCNoteEntryLayer(source, staff_number, start, stop)
note_entry_layer_source:Load()
local noteentrylayerDest = note_entry_layer_source:CreateCloneEntries(destination, staff_number, start)
noteentrylayerDest:Save()
noteentrylayerDest:CloneTuplets(note_entry_layer_source)
noteentrylayerDest:Save()
end
end
-- Function 4 - Delete the bottom notes, leaving only the top
function delete_bottom_notes(entry)
while entry.Count > 1 do
local lowest_note = entry:CalcLowestNote(nil)
note_entry.delete_note(lowest_note)
end
end
-- Function 5 - Delete the top notes, leaving only the bottom
function delete_top_notes(entry)
while entry.Count > 1 do
local highest_note = entry:CalcHighestNote(nil)
note_entry.delete_note(highest_note)
end
end
-- Function 6 - Delete the Top and Bottom Notes
function delete_top_bottom_notes(entry)
local highest_note = entry:CalcHighestNote(nil)
note_entry.delete_note(highest_note)
local lowest_note = entry:CalcLowestNote(nil)
note_entry.delete_note(lowest_note)
end
-- Function 6.1 - Delete the middle notes
function delete_middle_notes(entry)
while entry.Count > 2 do
local n = 1
for note in each(entry) do
note.NoteID = n
n = n + 1
end
for note in each(entry) do
if note.NoteID == 2 then
note_entry.delete_note(note)
end
end
end
end
-- Function 7: Create the custom line to use (or choose it if it already exists)
local function create_cluster_line()
-- Check to see if the right line exists. If it does, get the line ID
local line_exists = false
local my_line = 0
local my_line_width = 64 * 24 * .5 -- 64 EFIXes * 24EVPUs * .5 = 1/2 space
local custom_start_line_defs = finale.FCCustomSmartLineDefs()
custom_start_line_defs:LoadAll()
for csld in each(custom_start_line_defs) do
if csld.LineStyle == finale.CUSTOMLINE_SOLID and csld.LineWidth == my_line_width then -- 1st if: Solid line, 740
if csld.StartArrowheadStyle == finale.CLENDPOINT_NONE and csld.EndArrowheadStyle == finale.CLENDPOINT_NONE then -- 2nd if (arrowhead styles)
if csld.Horizontal == false then
my_line = csld.ItemNo
line_exists = true
end
end
end
end
-- if the line does not exist, create it and get the line ID
if line_exists == false then
local csld = finale.FCCustomSmartLineDef()
csld.Horizontal = false
csld.LineStyle = finale.CUSTOMLINE_SOLID
csld.StartArrowheadStyle = finale.CLENDPOINT_NONE
csld.EndArrowheadStyle = finale.CLENDPOINT_NONE
csld.LineWidth = my_line_width
csld:SaveNew()
my_line = csld.ItemNo
end
return my_line
end
-- Function 7.1: Create the short-span custom line to use (or choose it if it already exists)
local function create_short_cluster_line()
-- Check to see if the right line exists. If it does, get the line ID
local line_exists = false
local my_line = 0
local my_line_width = 64 * 24 * .333 -- 64 EFIXes * 24EVPUs * .333 = 1/3 space
local custom_smart_line_defs = finale.FCCustomSmartLineDefs()
custom_smart_line_defs:LoadAll()
for csld in each(custom_smart_line_defs) do
if csld.LineStyle == finale.CUSTOMLINE_SOLID and csld.LineWidth == my_line_width then -- 1st if: Solid line, 740
if csld.StartArrowheadStyle == finale.CLENDPOINT_NONE and csld.EndArrowheadStyle == finale.CLENDPOINT_NONE then -- 2nd if (arrowhead styles)
if csld.Horizontal == false then
my_line = csld.ItemNo
line_exists = true
end
end
end
end
-- if the line does not exist, create it and get the line ID
if line_exists == false then
local csld = finale.FCCustomSmartLineDef()
csld.Horizontal = false
csld.LineStyle = finale.CUSTOMLINE_SOLID
csld.StartArrowheadStyle = finale.CLENDPOINT_NONE
csld.EndArrowheadStyle = finale.CLENDPOINT_NONE
csld.LineWidth = my_line_width
csld:SaveNew()
my_line = csld.ItemNo
end
return my_line
end
-- Function 8: Attach the cluster line to the score
function add_cluster_line(left_note, right_note, line_id)
if left_note:IsNote() and left_note.Count == 1 and right_note:IsNote() then
local smartshape = finale.FCSmartShape()
local layer_one_highest = left_note:CalcHighestNote(nil)
local note_width = layer_one_highest:CalcNoteheadWidth()
local layer_one_note_y = layer_one_highest:CalcStaffPosition()
local layer_two_highest = right_note:CalcHighestNote(nil)
local layer_two_note_y = layer_two_highest:CalcStaffPosition()
local top_pad = 0
local bottom_pad = 0
if left_note.Duration >= 2048 and left_note.Duration < 4096 then -- for half notes...
top_pad = 9
bottom_pad = top_pad
elseif left_note.Duration >= 4096 then -- for whole notes and greater...
top_pad = 10
bottom_pad = 11.5
end
layer_one_note_y = (layer_one_note_y * 12) - top_pad
layer_two_note_y = (layer_two_note_y * 12) + bottom_pad
smartshape.ShapeType = finale.SMARTSHAPE_CUSTOM
smartshape.EntryBased = false
smartshape.MakeHorizontal = false
smartshape.BeatAttached = true
smartshape.PresetShape = true
smartshape.Visible = true
smartshape.LineID = line_id
local left_segment = smartshape:GetTerminateSegmentLeft()
left_segment:SetMeasure(left_note.Measure)
left_segment:SetStaff(left_note.Staff)
left_segment:SetMeasurePos(left_note.MeasurePos)
left_segment:SetEndpointOffsetX(note_width / 2)
left_segment:SetEndpointOffsetY(layer_one_note_y)
local right_segment = smartshape:GetTerminateSegmentRight()
right_segment:SetMeasure(right_note.Measure)
right_segment:SetStaff(right_note.Staff)
right_segment:SetMeasurePos(right_note.MeasurePos)
right_segment:SetEndpointOffsetX(note_width / 2)
right_segment:SetEndpointOffsetY(layer_two_note_y)
smartshape:SaveNewEverything(nil, nil)
end
end
-- Function 8.1: Attach the short cluster line to the score
function add_short_cluster_line(entry, short_lineID)
if entry:IsNote() and entry.Count > 1 then
local smartshape = finale.FCSmartShape()
local left_note = entry:CalcHighestNote(nil)
local left_note_y = left_note:CalcStaffPosition() * 12 + 12
local right_note = entry:CalcLowestNote(nil)
local right_note_y = right_note:CalcStaffPosition() * 12 - 12
smartshape.ShapeType = finale.SMARTSHAPE_CUSTOM
smartshape.EntryBased = false
smartshape.MakeHorizontal = false
smartshape.PresetShape = true
smartshape.Visible = true
smartshape.BeatAttached = true
smartshape.LineID = short_lineID
local left_segment = smartshape:GetTerminateSegmentLeft()
left_segment:SetMeasure(entry.Measure)
left_segment:SetStaff(entry.Staff)
left_segment:SetMeasurePos(entry.MeasurePos)
left_segment:SetEndpointOffsetX(horizontal_offset)
left_segment:SetEndpointOffsetY(left_note_y)
local right_segment = smartshape:GetTerminateSegmentRight()
right_segment:SetMeasure(entry.Measure)
right_segment:SetStaff(entry.Staff)
right_segment:SetMeasurePos(entry.MeasurePos)
right_segment:SetEndpointOffsetX(horizontal_offset)
right_segment:SetEndpointOffsetY(right_note_y)
smartshape:SaveNewEverything(nil, nil)
end
end
local line_id = create_cluster_line()
local short_lineID = create_short_cluster_line()
for add_staff = region:GetStartStaff(), region:GetEndStaff() do
local count = 0
for k in pairs(layer_one_note) do
layer_one_note[k] = nil
end
for k in pairs(layer_two_note) do
layer_two_note[k] = nil
end
for k in pairs(measure) do
measure[k] = nil
end
region:SetStartStaff(add_staff)
region:SetEndStaff(add_staff)
local measures = finale.FCMeasures()
measures:LoadRegion(region)
process_notes(region)
for entry in eachentrysaved(region) do
if entry.LayerNumber == 1 then
table.insert(layer_one_note, entry)
table.insert(measure, entry.Measure)
count = count + 1
elseif entry.LayerNumber == 2 then
table.insert(layer_two_note, entry)
end
end
for i = 1, count do
add_short_cluster_line(layer_one_note[i], short_lineID)
add_cluster_line(layer_one_note[i], layer_two_note[i], line_id)
end
end
-- separate move accidentals function for short clusters that encompass a 3rd
for entry in eachentrysaved(finenv.Region()) do
if entry:IsNote() and entry.Count > 1 then
for note in each(entry) do
if note.Accidental == true then
local accidental_mod = finale.FCAccidentalMod()
accidental_mod:SetNoteEntry(entry)
accidental_mod:SetUseCustomVerticalPos(true)
accidental_mod:SetHorizontalPos(horizontal_offset * 1.5)
accidental_mod:SaveAt(note)
end
end
end
end