OK, I tried to merge these two scripts. You should be able to drop samples when first zone is selected.
local zones = this.parent:findZones()
zoneNames = {}
for i = 1, #zones do
zoneNames[i] = zones[i].name
end
function zoneSelectChanged()
getSampleLength()
scope = "@0:"..zones[ZoneSelect].name.."/"
end
defineParameter("ZoneSelect", nil, 1, zoneNames, zoneSelectChanged)
defineParameter("scope", nil, "")
defineParameter("SampleStart", nil, 0, 0, 0x7fffffff, 1)
defineParameter("SampleEnd", nil, 0, 0, 0x7fffffff, 1)
defineParameter("Filename", nil, "", function() onFilenameChanged() end)
defineParameter("PitchDetectionProgress", nil, 0, 0, 100)
defineParameter("PitchDetection", nil, false)
function sampleStartChanged()
local zone = zones[ZoneSelect]
zone:setParameter("SampleOsc.SampleStart", SampleStart)
if SampleEnd < SampleStart then
SampleEnd = SampleStart
zone:setParameter("SampleOsc.SampleEnd", SampleEnd)
end
end
function sampleEndChanged()
local zone = zones[ZoneSelect]
zone:setParameter("SampleOsc.SampleEnd", SampleEnd)
if SampleStart > SampleEnd then
SampleStart = SampleEnd
zone:setParameter("SampleOsc.SampleStart", SampleStart)
end
end
function setRootkey(sample)
local zone = zones[ZoneSelect]
local pitch, voiced = sample:getPitch(0, -1)
if voiced and voiced then
local rootkey, detune = math.modf(pitch)
if detune >= 0.5 then
rootkey = rootkey + 1
detune = detune - 1
end
detune = math.floor((detune * 100) + 0.5)
print(pitch, voiced, rootkey, detune)
zone:setParameter("SampleOsc.Rootkey", rootkey)
zone:setParameter("SampleOsc.Tune", detune)
end
end
function keyTextFromSampleName(st)
local notes = {c = 0, d = 2, e = 4, f = 5, g = 7, a = 9, b = 11}
local mn = {}
st = string.match(st, "([^\\/]+)%.%a+$")
print(st)
for k, s, n in string.gmatch(st, "[_%-%s]([A-Ga-g])(%#?)(%-?%d)") do
if k and n then
local midinn = notes[string.lower(k)] + (s == "#" and 1 or 0) + (n + 2) * 12
table.insert(mn, midinn)
print(k, s, n, midinn)
end
end
return mn[#mn]
end
function onFilenameChanged()
local zone = zones[ZoneSelect]
local sample = AudioFile.open(Filename)
if sample.valid then
zone:setParameter("SampleOsc.Filename", sample.fileName)
zone:setParameter("SampleOsc.SampleStart", 0)
zone:setParameter("SampleOsc.SampleEnd", sample.length)
-- sample start and end parameters
defineParameter("SampleStart", nil, 0, 0, sample.length, 1, sampleStartChanged)
defineParameter("SampleEnd", nil, 0, 0, sample.length, 1, sampleEndChanged)
SampleStart = 0
SampleEnd = sample.length
-- rootkey and detune
local rootKey = keyTextFromSampleName(sample.fileName)
zone:setParameter("SampleOsc.Rootkey", rootKey or 60)
zone:setParameter("SampleOsc.Tune", sample.detune or 0)
if not rootKey and PitchDetection then
sample:analyzePitch(setRootkey)
while sample:getPitchAnalysisProgress() < 1 do
PitchDetectionProgress = sample:getPitchAnalysisProgress() * 100
wait(250)
end
PitchDetectionProgress = 0
end
end
end
function getSampleLength()
local zone = zones[ZoneSelect]
local fn = zone:getParameter("SampleOsc.Filename")
local sample = AudioFile.open(fn)
if sample.valid then
defineParameter("SampleStart", nil, 0, 0, sample.length, 1, sampleStartChanged)
defineParameter("SampleEnd", nil, 0, 0, sample.length, 1, sampleEndChanged)
SampleStart = zone:getParameter("SampleOsc.SampleStart")
SampleEnd = zone:getParameter("SampleOsc.SampleEnd")
end
end
function onLoad()
zoneSelectChanged()
end
function onNote(e)
playNote(e.note, e.velocity, -1, zones[ZoneSelect])
end
SampleStartEnd Drop.vstpreset (12.8 KB)