-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressive Vibrato.lua
421 lines (347 loc) · 11.8 KB
/
Progressive Vibrato.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
412
413
414
415
416
417
418
419
420
421
SCRIPT_TITLE = "Progressive Vibrato"
-- Standard header
function getClientInfo()
return {
name = SV:T(SCRIPT_TITLE),
author = "David Cuny",
versionNumber = 1,
minEditorVersion = 65537
}
end
-- Internationalization
-- Note: Translated via Google, may not be accurate
function getTranslations(langCode)
if langCode == "ja-jp" then
return {
{"Progressive Vibrato", "プログレッシブビブラート"},
{"Start Vibrato Frequency", "ビブラート周波数を開始"},
{"Final Vibrato Frequency", "最終的なビブラート周波数"},
{"Vibrato Depth", "ビブラートの深さ"},
{"Vibrato Start Time", "ビブラート開始時間"},
{"Vibrato Left Time", "ビブラート左時間"},
{"Vibrato Right Time", "ビブラートの適切なタイミング"},
{"Vibrato Volume", "ビブラートボリューム"},
{"Use Note Defaults", "ノートのデフォルトを使用"},
}
end
-- no options found
return {}
end
-- main function
function main()
-- create a form
local form = {
title = SV:T(SCRIPT_TITLE),
message = "",
buttons = "OkCancel",
widgets = {
-- vibrato start frequency slider
{
name = "vibStartFrq",
type = "Slider",
label = SV:T("Start Vibrato Frequency"),
format = "%3.0f",
minValue = 0,
maxValue = 100,
interval = 1,
default = 30
},
-- vibrato frequency slider
{
name = "vibFrq",
type = "Slider",
label = SV:T("Final Vibrato Frequency"),
format = "%3.0f",
minValue = 0,
maxValue = 100,
interval = 1,
default = 55
},
-- vibrato depth
{
name = "vibDepth",
type = "Slider",
label = SV:T("Vibrato Depth"),
format = "%3.0f",
minValue = 0,
maxValue = 20,
interval = 1,
default = 10
},
-- vibrato start slider
{
name = "vibStart",
type = "Slider",
label = SV:T("Vibrato Start Time"),
format = "%3.0f",
minValue = 0,
maxValue = 600,
interval = 10,
default = 20
},
-- vibrato ramp up slider
{
name = "vibLeft",
type = "Slider",
label = SV:T("Vibrato Left Time"),
format = "%3.0f",
minValue = 0,
maxValue = 400,
interval = 10,
default = 80
},
-- vibrato ramp down slider
{
name = "vibRight",
type = "Slider",
label = SV:T("Vibrato Right Time"),
format = "%3.0f",
minValue = 0,
maxValue = 200,
interval = 10,
default = 20
},
-- volume slider
{
name = "vibLoudScale",
type = "Slider",
label = SV:T("Vibrato Volume"),
format = "%3.0f",
minValue = 0,
maxValue = 60,
interval = 1,
default = 20
},
-- use note defaults
{
name = "useNoteDefaults",
type = "CheckBox",
text = SV:T("Use Note Defaults"),
default = false
}
}
}
-- render the dialog
local results = SV:showCustomDialog(form)
-- not cancelled?
if results.status then
-- pass values to the progressiveVibrato() function
progressiveVibrato(results.answers)
end
-- clean up and exit
SV:finish()
end
-- Get an array of blick ranges for connected notes in the selection
function getSelectedRanges(options)
-- get the selected items from the Editor object
local selection = SV:getMainEditor():getSelection()
-- get the selected notes from the selected items
local selectedNotes = selection:getSelectedNotes()
-- exit routine if no notes selected
if #selectedNotes == 0 then
return {}
end
-- get the voice, if default values are needed
local voice = SV:getMainEditor():getCurrentGroup():getVoice()
-- sort notes based on onset time
table.sort(selectedNotes, function(noteA, noteB)
return noteA:getOnset() < noteB:getOnset()
end)
-- holds the ranges
local ranges = {}
-- TimeAxis:getBlickFromSeconds(t) → {number}
-- TimeAxis:getSecondsFromBlick(b) → {number}
-- Project:getTimeAxis() → {TimeAxis}
-- SV:getProject() → {Project}
-- If attribute is NIL, need to look in the Voice to find the value
-- NoteGroupReference:getVoice() {object}
-- tF0Left: number pitch transition - duration left (seconds)
-- tF0Right: number pitch transition - duration right (seconds)
-- dF0Left: number pitch transition - depth left (semitones)
-- dF0Right: number pitch transition - depth right (semitones)
-- tF0VbrStart: number vibrato - start (seconds) default = .250
-- tF0VbrLeft: number vibrato - left (seconds) default = .20
-- tF0VbrRight: number vibrato - right (seconds) default = .20
-- dF0Vbr: number vibrato - depth (semitones) default = 1.0
-- fF0Vbr: number vibrato - frequency (Hz) default = 5.5
-- paramLoudness: number parameters - loudness (dB) default = 0.0
-- paramTension: number parameters - tension default = 0.0
-- paramBreathiness: number parameters - breathiness default = 0.0
-- paramGender: number parameters - gender default = 0.0
-- FIXME: Note left and right transitions don't appear to be used in
-- in the vibrato calcuations, so they are commented out for note
local timeAxis = SV:getProject():getTimeAxis()
-- iterate through notes, don't handle overlapping notes properly
for i = 1, #selectedNotes do
-- get the note
local theNote = selectedNotes[i]
-- get the note attributes
local theNoteAttribs = theNote:getAttributes()
-- get start of note in blicks, convert to seconds
local bStart = theNote:getOnset()
local tStart = timeAxis:getSecondsFromBlick( bStart )
-- get the vibrato offset in seconds from the attributes
local tF0VbrStart = theNoteAttribs.tF0VbrStart or voice.tF0VbrStart or 0.250
-- use dialog option instead?
if not options.useNoteDefaults then
-- get the value from the dialog
tF0VbrStart = options.vibStart / 100
end
-- add left offset to get start of vibrato
local tVibStart = tStart + tF0VbrStart
local bVibStart = timeAxis:getBlickFromSeconds( tVibStart )
-- get the end from the note
local bEnd = theNote:getEnd()
local tEnd = timeAxis:getSecondsFromBlick( bEnd )
-- use dialog option instead?
if not options.useNoteDefaults then
-- get the value from the dialog
tF0VbrEnd = options.vibRight / 100
end
-- vibrato depth
local vibDepth = theNoteAttribs.dF0Vbr or voice.dF0Vbr or 1
-- if vibDepth parameter has been turned off, set to 1
if vibDepth == 0 then
vibDepth = voice.dF0Vbr or 1
end
vibDepth = vibDepth * 1.4 / 10
if not options.useNoteDefaults then
-- use the value from the dialog
vibDepth = options.vibDepth * 1.4 / 100
end
-- vibrato start frequency
vibStartFrq = options.vibStartFrq / 10
-- vibrato target frequency
local vibFrq = theNoteAttribs.fF0Vbr or voice.fF0Vbr or 5.5
if not options.useNoteDefaults then
-- use the value from the dialog
vibFrq = options.vibFrq / 10
end
-- vibrato phase
local vibPhase = theNoteAttribs.pF0Vbr or voice.pF0Vbr or 0
-- vibrato left
local vibLeft = theNoteAttribs.tF0VbrLeft or voice.tF0VbrLeft or 0.20
if not options.useNoteDefaults then
-- use the value from the dialog
vibLeft = options.vibLeft / 100
end
-- vibrato right
local vibRight = theNoteAttribs.tF0VbrRight or voice.tF0VbrRight or 0.20
if not options.useNoteDefaults then
-- use the value from the dialog
vibRight = options.vibRight / 100
end
-- save the values
ranges[i] = {bStart, bEnd, tStart, tEnd, bVibStart, tVibStart, vibDepth, vibStartFrq, vibFrq, vibPhase, vibLeft, vibRight}
-- set the note vibrato depth to zero
theNote:setAttributes({dF0Vbr=0})
end
-- return the ranges table for each selected note
return ranges
end
function progressiveVibrato(options)
-- get the current group from the main editor {NoteGroupReference}
local scope = SV:getMainEditor():getCurrentGroup()
-- get the target {NoteGroup}
local group = scope:getTarget()
-- call getSelectedRanges() to get the ranges
local ranges = getSelectedRanges(options)
-- get the {Automation} for pitchDelta
local amPitch = group:getParameter("pitchDelta")
-- get the {Automation} for loudness
local amLoud = group:getParameter("loudness")
-- vibrato envelope automation
local amVibEnvelope = SV:create("Automation", "vibratoEnv")
local step = math.floor(SV.QUARTER / 64 )
-- iterate through the ranges
for i, r in ipairs(ranges) do
-- read the parameters
local bStart = r[1]
local bEnd = r[2]
local tStart = r[3]
local tEnd = r[4]
local bVibStart = r[5]
local tVibStart = r[6]
local vibDepth = r[7] * 50 -- FIXME
local vibStartFrq = r[8]
local vibFrq = r[9]
local vibPhase = r[10]
local vibLeft = r[11]
local vibRight = r[12]
-- amount vibrato changes amplitude
local vibLoudScale = options.vibLoudScale / 1000
-- get the values to the left and right
local origLeft, origRight = amPitch:get(bStart - step), amPitch:get(bEnd + step)
-- set anchor to the left and right
amPitch:add(bStart - step, origLeft)
amPitch:add(bEnd + step, origRight)
-- remove everything between
amPitch:remove(bStart, bEnd)
-- get the loudness values to the left and right
local origLeft, origRight = amLoud:get(bStart - step), amLoud:get(bEnd + step)
-- set anchor to the left and right
amLoud:add(bStart - step, origLeft)
amLoud:add(bEnd + step, origRight)
-- remove everything between
amLoud:remove(bStart, bEnd)
-- clear vibrato envelope
amVibEnvelope:add(bStart - step, 0)
amVibEnvelope:add(bEnd + step, 0)
amVibEnvelope:remove(bStart, bEnd)
-- vibrato width
bVibWidth = bEnd - bVibStart
tVibWidth = tEnd - tVibStart
-- phase change per blick
local totalRadians = math.pi * 2 * tVibWidth * vibFrq
local totalSteps = bVibWidth / step
local radiansPerStep = totalRadians / totalSteps
-- ramp up per step
local ratio = math.min( vibLeft / tVibWidth, 1.0 )
local rampUpSteps = math.floor(totalSteps * ratio)
local rampUpDelta = vibDepth/rampUpSteps
-- ramp down
ratio = math.min( vibRight / tVibWidth, 1.0 )
local rampDownSteps = math.floor(totalSteps * ratio)
local rampDownCounter = math.floor(totalSteps * (1-ratio))
-- initial radians
local startRadians = math.pi * 2 * tVibWidth * vibStartFrq
local startRadiansPerStep = startRadians / totalSteps
-- rate of change for radian step
local phaseDelta = (radiansPerStep-startRadiansPerStep) / rampUpSteps
-- get start position
local b = bVibStart
-- advance until past end
local envelope = 0
local phaseRate = startRadiansPerStep
local rampDownDelta = 0
while b < bEnd do
-- calculate height of vibrato
local value = math.sin(vibPhase)*vibDepth*envelope
-- insert into pitch and amplitude parameters
amPitch:add(b, value)
-- vibrato modulates the amplitude, but only in the positive direction
amLoud:add(b, math.abs(value * vibLoudScale) )
-- advance by step size
b = b + step
vibPhase = vibPhase + phaseRate
-- ramp up?
if rampUpSteps > 0 then
-- increase the amplitude envelope
envelope = envelope + rampUpDelta
-- increase the phase
phaseRate = phaseRate + phaseDelta
-- decrement the number of ramp up steps remaining
rampUpSteps = rampUpSteps - 1
end
-- decrement ramp down counter
rampDownCounter = rampDownCounter - 1
if rampDownCounter == -1 then
-- determine rate of change for envelope
rampDownDelta = envelope / rampDownSteps
elseif rampDownCounter < 0 then
-- reduce envelope
envelope = math.max( envelope - rampDownDelta, 0 )
end
end
end
end