local SCRIPT_TITLE = 'Extract velocity from midi file V1.0'

--[[

Synthesizer V Studio Pro Script
 
lua file name: ExtractVelocityFromMidi.lua

This script will extract the velocity notes (or CC controller) coming a midi file
and copy them to loudness SynthV parameter.

Midi file path:
To avoid copy/pasting the midi file path each time,
midi file path is also retrieved from: Clipboard or trackname

Midi extracting source code comming from:
https://github.com/Possseidon/lua-midi/blob/main/lib/midi.lua

2025 - JF AVILES
--]]

-- Generated by JFA TranslateScripts.lua
function getTranslations(langCode)
	return getArrayLanguageStrings()[langCode]
end

-- Generated by JFA TranslateScripts.lua
function getArrayLanguageStrings()
	return {
		["en-us"] = {
			{"Track", "Track"},
			{"Tempo pos: ", "Tempo pos: "},
			{"ticks: ", "ticks: "},
			{"tempo: ", "tempo: "},
			{"Select source & target track to update loudness,", "Select source & target track to update loudness,"},
			{"Seleted track must match the midi file track!", "Seleted track must match the midi file track!"},
			{"Select midi track source", "Select midi track source"},
			{"Select target track to update loudness", "Select target track to update loudness"},
			{"Notes velocity gain", "Notes velocity gain"},
			{"Reduce controller gain %", "Reduce controller gain %"},
			{"Nothing found during processing the MIDI file!", "Nothing found during processing the MIDI file!"},
			{"tracks count:", "tracks count:"},
			{"No notes found in tracks!", "No notes found in tracks!"},
			{"Nothing found!", "Nothing found!"},
			{"Enter the full path to your MIDI file.", "Enter the full path to your MIDI file."},
		},
	}
end

function getClientInfo()
	return {
		name = SV:T(SCRIPT_TITLE),
		category = "_JFA_MIDI",
		author = "JFAVILES",
		versionNumber = 1,
		minEditorVersion = 65540
	}
end

-- A flick (frame-tick) is a very small unit of time. It is 1/705600000 of a second, exactly.
local blicksPerQuarter = SV.QUARTER -- 705600000 
local ticksPerQuarter = nil
local DEBUG = false
local DEBUG_RESULT = ""
local midiFileNameFromTrack = ""
local midiFileExtension = ".mid"
local DEFAULT_FILE_PATH = ""

local ticks = 0

-- Times infos
local tempoActive = 120
local metronomeActive = 24
local signatureActive = 4 -- numerator
local denominatorActive = 4
local dottedActive = 8

local notesTable = {}
local tempoMarkers = {}
local midiTrackNames = {}
local controllersTable = {}
local channelPressure = {}
local modeMessage = {}
local timeSignature = {}
local listAllTracks = {}
local controllerCC = 1  -- CC 1

local CURRENT_LYRIC = ""
local INDEX_NOTE = 0
local CURRENT_TRACK = 0
local PREVIOUS_TRACK = 0
local DELTA_TIME = 0
local project = SV:getProject()
local timeAxis = project:getTimeAxis()
local formatCount = "Count: %04d"
local formatNumber = "%4d"
local formatTrack = "%2d"
local handlers = {}

-- Get string format from seconds
function SecondsToClock(timestamp)
	return string.format("%02d:%06.3f", 
	  math.floor(timestamp/60)%60, 
	  timestamp%60):gsub("%.",",")
end

-- Store data from midi file
function recordDeltaTimes(timeBegin, ticks, timeSecond, channel, key, velocity)
	table.insert(notesTable, {
		index = INDEX_NOTE,
		track = CURRENT_TRACK,
		ticksBegin  =  ticks,
		ticksEnd  = nil,
		timeBegin  =  timeBegin,
		timeEnd  =  nil,
		timeSecondBegin = timeSecond,
		timeSecondEnd = nil,
		channel = channel,
		key = key, 
		velocity = velocity,
		lyric = CURRENT_LYRIC
	})
end

-- Store controller data from midi file
function recordControllerDeltaTimes(timeBegin, ticks, timeSecond, channel, number, value)
    table.insert(controllersTable, {
        index = INDEX_NOTE,
		track = CURRENT_TRACK,
		ticksBegin  =  ticks,
		timeBegin  =  timeBegin,
		timeSecondBegin = timeSecond,
		channel = channel,
		number = number, 
		value = value
    })
end

-- Store mode Message data from midi file
function recordModeMessageDeltaTimes(timeBegin, ticks, timeSecond, channel, number, value)
    table.insert(modeMessage, {
        index = INDEX_NOTE,
		track = CURRENT_TRACK,
		ticksBegin  =  ticks,
		timeBegin  =  timeBegin,
		timeSecondBegin = timeSecond,
		channel = channel,
		number = number, 
		value = value
    })
end

-- Store channel pressure data from midi file
function recordChannelPressureDeltaTimes(timeBegin, ticks, timeSecond, channel, pressure)
    table.insert(channelPressure, {
        index = INDEX_NOTE,
		track = CURRENT_TRACK,
		ticksBegin  =  ticks,
		timeBegin  =  timeBegin,
		timeSecondBegin = timeSecond,
		channel = channel,
		pressure = pressure
    })
end

-- Store time Signature data from midi file
function recordtimeSignatureDeltaTimes(timeBegin, ticks, timeSecond, numerator, denominator, metronome, dotted)
    table.insert(timeSignature, {
        index = INDEX_NOTE,
		track = CURRENT_TRACK,
		ticksBegin  =  ticks,
		timeBegin  =  timeBegin,
		timeSecondBegin = timeSecond,
		numerator = numerator,
		denominator = denominator,
		metronome = metronome,
		dotted = dotted
    })
end

-- Get header from midi file
function handlers.header(format, trackCount, tickCount)
    ticksPerQuarter = tickCount
end

-- Get delta times from midi file
function handlers.deltatime(delta)
    ticks = ticks + delta
	DELTA_TIME = delta
end

-- Get noteOn from midi file
function handlers.noteOn(channel, key, velocity)
    INDEX_NOTE = INDEX_NOTE + 1
	
	local timeSecond = 0
	if ticks > 0 then
		local secNotePos = timeAxis:getSecondsFromBlick(ticksToBlicks(ticks))
		timeSecond = SecondsToClock(secNotePos)
	end
	recordDeltaTimes(DELTA_TIME, ticks, timeSecond, channel, key, velocity)
end

-- Get noteOff from midi file
function handlers.noteOff(channel, key, velocity)
	notesTable[INDEX_NOTE].timeEnd = DELTA_TIME
	
	local timeSecond = 0
	if ticks > 0 then
		local secNotePos = timeAxis:getSecondsFromBlick(ticksToBlicks(ticks))
		timeSecond = SecondsToClock(secNotePos)
	end
	notesTable[INDEX_NOTE].timeSecondEnd = timeSecond
	notesTable[INDEX_NOTE].ticksEnd = ticks
end

-- Get controller from midi file
function handlers.controller(channel, number, value)
	local timeSecond = 0
	if ticks > 0 then
		local secNotePos = timeAxis:getSecondsFromBlick(ticksToBlicks(ticks))
		timeSecond = SecondsToClock(secNotePos)
	end
	recordControllerDeltaTimes(DELTA_TIME, ticks, timeSecond, channel, number, value)
end

-- Get modeMessage from midi file
function handlers.modeMessage(channel, number, value)
	local timeSecond = 0
	if ticks > 0 then
		local secNotePos = timeAxis:getSecondsFromBlick(ticksToBlicks(ticks))
		timeSecond = SecondsToClock(secNotePos)
	end
	recordModeMessageDeltaTimes(DELTA_TIME, ticks, timeSecond, channel, number, value)
end

-- Get channelPressure from midi file
function handlers.channelPressure(channel, pressure)
	local timeSecond = 0
	if ticks > 0 then
		local secNotePos = timeAxis:getSecondsFromBlick(ticksToBlicks(ticks))
		timeSecond = SecondsToClock(secNotePos)
	end
	recordChannelPressureDeltaTimes(DELTA_TIME, ticks, timeSecond, channel, pressure)
end

-- Get time Signature from midi file
function handlers.timeSignature(numerator, denominator, metronome, dotted)
	local timeSecond = 0
	if ticks > 0 then
		local secNotePos = timeAxis:getSecondsFromBlick(ticksToBlicks(ticks))
		timeSecond = SecondsToClock(secNotePos)
	end
	recordtimeSignatureDeltaTimes(DELTA_TIME, ticks, timeSecond, numerator, denominator, metronome, dotted)
end

-- Get lyrics from midi file
function handlers.lyric(data)
	CURRENT_LYRIC = data
end

-- Get track number from midi file
function handlers.track(track) 
	if CURRENT_TRACK ~= track then
		PREVIOUS_TRACK = CURRENT_TRACK
		DELTA_TIME = 0
		ticks = 0
	end
	CURRENT_TRACK = track
end

-- Get tempo midi file data
function handlers.setTempo(tempo)
    table.insert(tempoMarkers, {
        position = ticksToBlicks(ticks),
        ticksBegin = ticks,
        tempo = tempo
    })
end

-- Get sequencer or track name from midi file
function handlers.sequencerOrTrackName(data)
    table.insert(midiTrackNames, {
		track = CURRENT_TRACK,
        trackName = data
    })
end

-- Convert ticks to Blicks
function ticksToBlicks(ticks)
    return (ticks / ticksPerQuarter) * blicksPerQuarter
end

-- Convert blicks to ticks
function blicksToTicks(blicks)
	return (blicks / blicksPerQuarter) * ticksPerQuarter
end

-- Call back to dispatch content data from midi file
local function callback(name, ...)
    local handler = handlers[name]
	-- DEBUG_RESULT = DEBUG_RESULT .. "handlers:" .. name .. "\r"
    if handler then
		handler(...)
    end
end

-- Check existing external file
function checkExternalFile(filename)
    local filetoOpen = io.open(filename, "r")	
	if filetoOpen ~= nil then io.close(filetoOpen) end
    return filetoOpen
end

-- Get name for track
function getTrackName(iTrackPos)
	local trackName = ""
	for iTrack = 1, #midiTrackNames do
		if midiTrackNames[iTrack].track == iTrackPos then
			trackName = midiTrackNames[iTrack].trackName
		end
	end
	return trackName
end

-- Get Midi track list
function getMidiTrackList()
	local list = {}
	local listTracks = {}
	local trackNotesCount = 0
	local currentTrack = 1
	local positionTrack = 0
	local previousTrack = -1
	local firstTrackWithNotes = -1
	
	for iMidiNote = 1, #notesTable do
		local note = notesTable[iMidiNote]
		currentTrack = note.track
				
		if previousTrack >= 0 and previousTrack ~= currentTrack then
			if trackNotesCount > 0 then
				addTrackList(list, listTracks, previousTrack, trackNotesCount, positionTrack)
				
				if firstTrackWithNotes < 0 then
					firstTrackWithNotes = positionTrack
				end
				positionTrack = positionTrack  + 1			
				trackNotesCount = 0
			end
		end
		
		previousTrack = currentTrack
		trackNotesCount = trackNotesCount + 1
	end
	
	if firstTrackWithNotes < 0 and trackNotesCount > 0 then
		firstTrackWithNotes = positionTrack
	end
	addTrackList(list, listTracks, previousTrack, trackNotesCount, positionTrack)
	
	-- if DEBUG then SV:showMessageBox("", "firstTrackWithNotes: " .. firstTrackWithNotes) end
	
	return list, listTracks, firstTrackWithNotes
end

-- Add track list
function addTrackList(list, listTracks, iTrack, trackNotesCount, positionTrack)
	local trackLabel = SV:T("Track")
	
	table.insert(list, trackLabel 
				.. string.format(formatTrack, iTrack)
				.. " '" .. getTrackName(iTrack) .. "'"
				.. " (" .. string.format(formatCount, trackNotesCount) .. ")")
	table.insert(listTracks,  iTrack)
end

-- Get list of tempo infos
function getTempoList()
	local list = ""
	
	for iTempo = 1, #tempoMarkers do
		local tempoMarker = tempoMarkers[iTempo]
		list = list 
						.. SV:T("Tempo pos: ") .. tempoMarker.position .. ", " 
						.. SV:T("ticks: ")     .. tempoMarker.ticksBegin .. ", " 
						.. SV:T("tempo: ")     .. tempoMarker.tempo .. "\r"
	end
	if DEBUG then SV:showMessageBox(SV:T(SCRIPT_TITLE), "list tempo: " .. list) end
	return list
end

-- Get Midi controller track list
function getMidiControllersTrackList(trackFilterMidi, allTracks)
	local list = {}
	local listAllTracks = {}
	local listOutput = {}
	local value = 0
	local currentTrack = 1
	local previousTrack = 0
	local ctrlTrackCount = 0
	local ctrlInfosCount = 0
	local previousTicksBegin = 0
	local previousTimeSecondBegin = 0
	local trackLabel = SV:T("Track")
	
	for iMidiInfo = 1, #controllersTable do
		local ctrlData = controllersTable[iMidiInfo]
		currentTrack = ctrlData.track
		
		if previousTrack == 0 then 
			previousTrack = currentTrack
		end
		
		if previousTrack ~= currentTrack then
			table.insert(listAllTracks, trackLabel 
				.. string.format(formatTrack, previousTrack)
				.. " (" .. previousTicksBegin .. "/"
				.. ": " .. previousTimeSecondBegin .. ": "
				.. " Count: " .. string.format(formatNumber, ctrlInfosCount)
				)
				ctrlTrackCount = ctrlTrackCount + 1
		end

		if trackFilterMidi == currentTrack then
			table.insert(list, trackLabel 
				.. string.format(formatTrack, currentTrack)
				.. " (" .. ctrlData.ticksBegin .. "/"
				.. ": " .. ctrlData.timeSecondBegin .. ": "
				.. " CC: " .. string.format(formatNumber, ctrlData.number)
				.. " val: " .. string.format(formatNumber, ctrlData.value)
			)
			ctrlInfosCount = ctrlInfosCount + 1
		end
		
		previousTicksBegin = ctrlData.ticksBegin
		previousTimeSecondBegin = ctrlData.timeSecondBegin
		previousTrack = currentTrack
	end
	
	table.insert(listAllTracks, trackLabel 
		.. string.format(formatTrack, previousTrack)
		.. " (" .. previousTicksBegin .. "/"
		.. ": " .. previousTimeSecondBegin .. ": "
		.. " Count: " .. string.format(formatNumber, ctrlInfosCount)
		)

	if allTracks then
		listOutput = listAllTracks
	else
		listOutput = list
	end
	
	if DEBUG then
		SV:showMessageBox(SV:T(SCRIPT_TITLE), 
			"ControllersTable count: " .. #controllersTable .. "\r" 
			.. "tracks count: " .. ctrlTrackCount .. "\r" 
			.. "track (" .. trackFilterMidi .. ") count: " .. ctrlInfosCount .. "\r" 
			.. "MidiControllers:" .. "\r" 
			..  string.sub(table.concat(listOutput, "\r"), 1, 1000) .. "\r"
			.. "length: " ..  #listOutput
			)
	end
	return list
end

-- Get time Signature track list
function getMidiTimeSignatureTrackList()
	local list = {}
	local value = 0
	local currentTrack = 1
	local trackLabel = SV:T("Track")
	
	for iMidiInfo = 1, #timeSignature do
		local data = timeSignature[iMidiInfo]
		currentTrack = data.track
		table.insert(list, trackLabel 
					.. string.format(formatTrack, currentTrack)
					.. " (" .. ticksToBlicks(data.ticksBegin) .. "/"
					.. "" .. data.ticksBegin .. "/"
					.. ""   .. data.timeBegin .. ")"
					.. ": " .. data.timeSecondBegin .. ": "
					.. " numerator: " .. string.format(formatNumber, data.numerator)
					.. " denominator: " .. string.format(formatNumber, data.denominator)
					.. " metronome: " .. string.format(formatNumber, data.metronome)
					.. " dotted: " .. string.format(formatNumber, data.dotted)
					)		
	end

	SV:showMessageBox(SV:T(SCRIPT_TITLE), 
		"timeSignature count: " .. #timeSignature .. "\r" 
		.. "content:\r" ..  table.concat(list, "\r")
		)
	return list
end

--- Get first note lyrics
function getFirstNotesLyrics(numNotes, groupNotesMain)
	local lyrics = ""
		if numNotes > 0 then
			local firstNote = groupNotesMain:getNote(1) -- First note
			if firstNote ~= nil then
				lyrics = firstNote:getLyrics()
			end
		end 
		if numNotes > 1 then
			local secondNote = groupNotesMain:getNote(2)
			if secondNote ~= nil then
				lyrics = lyrics .. " " .. secondNote:getLyrics()	
			end
		end
		
		if string.len(lyrics) > 0 then
			lyrics = " : " .. string.sub(lyrics, 1, 10)
		end
	return lyrics
end

-- Trim string
function trim(s)
	return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
end

--- Get track list
function getTracksList()
	
	local list = {}
	local listTrackNumber = {}
	local tracks = project:getNumTracks()
	for iTrack = 1, tracks do
		local track = project:getTrack(iTrack)
		local trackName = track:getName()
		local mainGroupRef = track:getGroupReference(1) -- main group
		local groupNotesMain = mainGroupRef:getTarget()
		local numNotes = groupNotesMain:getNumNotes()
		
		
		if numNotes > 0 then
			groupNotes = groupNotesMain
		else
			local numGroups = track:getNumGroups()
			for iGroup = 1, numGroups do
				local groupRef = track:getGroupReference(iGroup)
				local groupNotesFound = groupRef:getTarget()
				numNotes = groupNotesFound:getNumNotes()
				if numNotes > 0 then
					groupNotes = groupNotesFound
					break
				end
			end
		end
		local lyrics = getFirstNotesLyrics(numNotes, groupNotes)
		
		if (string.find(trackName, midiFileExtension, 1, true) == nil and numNotes > 0) then
			table.insert(list, trackName
								.. " (" .. string.format(formatCount, numNotes) .. ")"
								.. lyrics
								)
			table.insert(listTrackNumber, iTrack)
		end
		if string.find(trackName, midiFileExtension, 1, true) ~= nil then
			midiFileNameFromTrack = getCleanFilename(trackName)
		end
	end
	return list, listTrackNumber
end

-- Create user input form
function getForm(midiTrackList, trackList, firstTrackWithNotes)
	local velocityGainDefaultValue = 5
	local velocityGainMinValue =  1
	local velocityGainMaxValue =  20
	local velocityGainInterval =  5
	local reduceControllerGainDefaultValue = 80
	local reduceControllerGainMinValue =  0
	local reduceControllerGainMaxValue =  95
	local reduceControllerGainInterval =  10
	local trackDefault = 0
	
	local form = {
		title = SV:T(SCRIPT_TITLE),
		message =  SV:T("Select source & target track to update loudness,") .. "\r" 
				.. SV:T("Seleted track must match the midi file track!"),
		buttons = "OkCancel",
		widgets = {
			{
				name = "trackMidi", type = "ComboBox",
				label = SV:T("Select midi track source"),
				choices = midiTrackList, 
				default = firstTrackWithNotes
			},
			{
				name = "trackSynthV", type = "ComboBox",
				label = SV:T("Select target track to update loudness"),
				choices = trackList, 
				default = trackDefault
			},
			{
				name = "velocityGain", type = "Slider",
				label = SV:T("Notes velocity gain"),
				format = "%3.0f",
				minValue = velocityGainMinValue, 
				maxValue = velocityGainMaxValue, 
				interval = velocityGainInterval, 
				default = velocityGainDefaultValue
			},			
			{
				name = "controller", type = "CheckBox", text = SV:T("Replace loudness by midi controller CC = " .. controllerCC),
				default = true
			},
			{
				name = "reduceControllerGain", type = "Slider",
				label = SV:T("Reduce controller gain %"),
				format = "%3.0f",
				minValue = reduceControllerGainMinValue, 
				maxValue = reduceControllerGainMaxValue, 
				interval = reduceControllerGainInterval, 
				default = reduceControllerGainDefaultValue
			},			
			{
				name = "separator", type = "TextArea", label = "", height = 0
			}
		}
	}
	return SV:showCustomDialog(form)
end

-- Set first parameter Loudness in track
function setFirstParameterLoudnessInTrack(trackFilterSynthV)
	local groupNotesMain, numNotes = getGroupMainFromTrack(trackFilterSynthV)
	local loudness = groupNotesMain:getParameter("loudness")
	local firstNote = groupNotesMain:getNote(1) -- First note
	
	-- Decay before applying first loudness value in next steps
	local newTimeOffset = firstNote:getOnset() - timeAxis:getBlickFromSeconds(0.1)
	if newTimeOffset <  0 then newTimeOffset = 0 end  -- Negative is not possible
	
	loudness:add(newTimeOffset, 0)
	
	return firstNote
end

-- Set last parameter Loudness in track
function setLastParameterLoudnessInTrack(trackFilterSynthV, lastValue)
	local groupNotesMain, numNotes = getGroupMainFromTrack(trackFilterSynthV)
	local lastNote = groupNotesMain:getNote(numNotes) -- last note

	local loudness = groupNotesMain:getParameter("loudness")
	
	loudness:add(lastNote:getEnd(), lastValue)
	-- Decay after applying last loudness value
	local time = lastNote:getEnd() + timeAxis:getBlickFromSeconds(2)
	loudness:add(time, 0)
	
	return lastNote
end

-- Get tempo
function getTempo(ticks)
	local newTempo = tempoActive
	
	for iTempo = 1, #tempoMarkers do
		local tempoMarker = tempoMarkers[iTempo]
		if tempoMarker.ticksBegin <= ticks then
			newTempo = tempoMarker.tempo
		end
	end
	return newTempo
end

-- Get metronome
function getMetronome(ticks)
	local newMetronome = metronomeActive
	
	for iSignature = 1, #timeSignature do
		local infoSignature = timeSignature[iSignature]
		if infoSignature.ticksBegin <= ticks then
			newMetronome = infoSignature.metronome
		end
	end
	return newMetronome
end

-- Get signature
function getSignature(ticks)
	local numerator = signatureActive
	local denominator = denominatorActive
	local metronome = metronomeActive
	local dotted = dottedActive
	
	for iSignature = 1, #timeSignature do
		local infoSignature = timeSignature[iSignature]
		if infoSignature.ticksBegin <= ticks then
			numerator = infoSignature.numerator
			denominator = infoSignature.denominator
			metronome = infoSignature.metronome
			dotted = infoSignature.dotted
		end
	end
	return numerator, denominator, metronome, dotted
end

-- Get loudness from track
function getLoudnessFromTrack(trackFilterSynthV)
	local groupNotesMain, numNotes = getGroupMainFromTrack(trackFilterSynthV)
	local loudness = groupNotesMain:getParameter("loudness")
	
	return loudness
end

-- Get group main from track
function getGroupMainFromTrack(trackFilterSynthV)
	local groupNotes = nil
	local track = project:getTrack(trackFilterSynthV)
	local mainGroupRef = track:getGroupReference(1) -- main group
	local groupNotesMain = mainGroupRef:getTarget()
	local numNotes = groupNotesMain:getNumNotes()
	
	if numNotes > 0 then
		groupNotes = groupNotesMain
	else
		local numGroups = track:getNumGroups()
		local lyrics = ""
		for iGroup = 1, numGroups do
			local groupRef = track:getGroupReference(iGroup)
			local groupNotesFound = groupRef:getTarget()
			numNotes = groupNotesFound:getNumNotes()
			-- SV:showMessageBox(SV:T(SCRIPT_TITLE), "track: " .. track:getName() 
				-- .. ", groupNotesFound: " .. groupNotesFound:getName() .. ", numNotes: " .. numNotes)
			if numNotes > 0 then
				groupNotes = groupNotesFound
				break
			end
		end
	end
	
	return groupNotes, numNotes
end

-- Set volume notes for tracks
function setLoudnessOnTracks(trackFilterMidi, trackFilterSynthV, 
								velocityGain, controller, reduceControllerGain)
	local lyricContent = ""
	local currentTrack = 1
	local previousTrack = 1
	local iSynthVNote = 0
	local trackNotesCount = 0
	local lyrics = ""
	local result = ""
	local loudness = getLoudnessFromTrack(trackFilterSynthV)
	loudness:removeAll()
	
	-- Loudness from midi controller CC1
	if controller then
		local currentTrack = 1
		local ctrlInfosCount = 0
		
		for iMidiInfo = 1, #controllersTable do
			local ctrlData = controllersTable[iMidiInfo]
			currentTrack = ctrlData.track
			
			if currentTrack == trackFilterMidi then
			
				-- Controller CC = 1 modulation wheel
				if ctrlData.number == controllerCC then
					
					-- get Blicks with new tempo
					local timeInfo = ticksToBlicks(ctrlData.ticksBegin)
					
					-- 0...127 => 0..24
					local coef = (24/127)
					local newValue = ctrlData.value * coef
					newValue = newValue - (newValue * (reduceControllerGain/100))
					
					-- Get track infos for loudness
					loudness:add(timeInfo, newValue) -- Add loudness
					ctrlInfosCount = ctrlInfosCount + 1
				end
			end
		end
	else
		-- Set first parameter loudness default value (0)
		local firstNote = setFirstParameterLoudnessInTrack(trackFilterSynthV)
		
		-- Loudness from midi notes velocity
		for iMidiNote = 1, #notesTable do
			local midiNote = notesTable[iMidiNote]
			currentTrack = midiNote.track
			
			if previousTrack ~= currentTrack then
				lyrics = ""
			end
			lyrics = midiNote.lyric
			
			if currentTrack == trackFilterMidi then
				iSynthVNote = iSynthVNote + 1
				
				-- Get track infos for loudness
				local groupNotesMain, numNotes = getGroupMainFromTrack(trackFilterSynthV)
				
				if iSynthVNote <= numNotes then
					local noteSynthV = groupNotesMain:getNote(iSynthVNote)

					if noteSynthV ~= nil then
						local midiNoteVelocity = midiNote.velocity
						if midiNoteVelocity == nil then
							midiNoteVelocity = 0
						end
						
						-- local timeNote = ticksToBlicks(ctrlData.ticksBegin)
						local timeNote = noteSynthV:getOnset()
						local newValue = midiNoteVelocity * velocityGain
						local loudness = groupNotesMain:getParameter("loudness")
						loudness:add(timeNote, newValue)
					end
				end
			end
			previousTrack = currentTrack
			trackNotesCount = trackNotesCount + 1
		end
	end
	
	-- Set last parameter loudness to default value  (0)
	local lastNote = setLastParameterLoudnessInTrack(trackFilterSynthV, newValue)

	-- if DEBUG then 
		-- SV:showMessageBox(SV:T(SCRIPT_TITLE), "result: " .. string.sub(result, 1, 1000)) 
	-- end
	
	return result
end

-- Extract data from midi file
function extractMidiData(MidiReader, midiFilename)
	local file = io.open(midiFilename, 'rb')
	local result = {}
	
	-- Protected external call function
	local status, retval = pcall(function() resultTrack = MidiReader.process(file, callback) return resultTrack end)
    if not status then
		io.close(file)
        SV:showMessageBox(SV:T(SCRIPT_TITLE), 'Failed to process MIDI file:' .. midiFilename .. "\r" .. retval)
	else
		io.close(file)
	end
	
	-- Track count in result
	result =  {
		status = status,
		tracksCount = retval
	}
	return result
end

-- Get notes from midi file
function getNotesFromMidiFile(MidiReader, midiFilename, trackList, listTrackNumber)
	local project = SV:getProject()
	local timeSecondEndPhrase = ""
	local lyricsIndice = 0
	local endTrack = false
	local trackFilterSynthV = 1
	local trackFilterMidi = 1
	local velocityGain = 0
	local reduceControllerGain = 0
	
	if not checkExternalFile(midiFilename) then 
		SV:showMessageBox(SV:T(SCRIPT_TITLE), 'Failed to open MIDI from ' .. midiFilename)
		return false
	end
	
	local resultExtractMidi = extractMidiData(MidiReader, midiFilename)
	local resultExtractMidiStatus = resultExtractMidi.status
	local tracksCount = resultExtractMidi.tracksCount
	
	if not resultExtractMidiStatus  then
		SV:showMessageBox(SV:T(SCRIPT_TITLE), SV:T("Nothing found during processing the MIDI file!"))
		return false
	end
	
	-- Result infos
	if #notesTable > 0 then
		-- Message result
		local resultMessage = ""
		
		if string.len(tracksCount) > 0 then
			resultMessage = resultMessage .. SV:T("tracks count:") .. " " .. tracksCount .. "\r"
		end
		
		local midiTrackList, listTracks, firstTrackWithNotes = getMidiTrackList()

		local userInput = getForm(midiTrackList, trackList, firstTrackWithNotes)
		
		if userInput.status then
			
			trackFilterMidi = listTracks[userInput.answers.trackMidi + 1]
			trackFilterSynthV = listTrackNumber[userInput.answers.trackSynthV + 1]

			velocityGain = userInput.answers.velocityGain
			controller = userInput.answers.controller
			reduceControllerGain = userInput.answers.reduceControllerGain
			
			-- check track contents
			local groupNotesMain, numNotes = getGroupMainFromTrack(trackFilterSynthV)
			if numNotes == 0 then
				SV:showMessageBox(SV:T(SCRIPT_TITLE), SV:T("No notes found in tracks!"))
				return false
			end
		
			-- Set loudness from midi velocity
			local result = setLoudnessOnTracks(trackFilterMidi, trackFilterSynthV, 
								velocityGain, controller, reduceControllerGain)
		end

		--if DEBUG then SV:showMessageBox(SV:T(SCRIPT_TITLE), resultMessage ) end
	else
		SV:showMessageBox(SV:T(SCRIPT_TITLE), SV:T("Nothing found!"))
	end
	
	return true
end

-- getCleanFilename
function getCleanFilename(file)
	local filename = file
	if string.len(filename) > 0 then
		if string.find(filename, '"') ~= nil then
			filename = filename:gsub('"', '')
		end
	end
	return filename
end

-- Main procedure
function main()
	local contentInfo = SV:getHostClipboard()
	local filenameInit = DEFAULT_FILE_PATH
	
	-- Get file name from last clipboard
	if string.find(contentInfo, midiFileExtension, 1, true) ~= nil then
		filenameInit = getCleanFilename(contentInfo)
	end
	
	local trackList, listTrackNumber = getTracksList()
	
	-- Get file name with path from a track name in SynthV
	if string.len(midiFileNameFromTrack) > 0 then
		filenameInit = midiFileNameFromTrack
	end
	
	local midiFilename = SV:showInputBox(SV:T(SCRIPT_TITLE), SV:T("Enter the full path to your MIDI file."), filenameInit)
	
	if string.len(midiFilename) > 0 then
		local filename = getCleanFilename(midiFilename)
		getNotesFromMidiFile(getMidiReader(), filename, trackList, listTrackNumber)
	end

	SV:finish()
end

--[[
  Avoiding to manage dependencies,
  getMidiReader is imported from https://github.com/Possseidon/lua-midi/blob/main/lib/midi.lua
  version from 04/2024
--]]

function getMidiReader()
	---Reads exactly count bytes from the given stream, raising an error if it can't.
	---@param stream file* The stream to read from.
	---@param count integer The count of bytes to read.
	---@return string data The read bytes.
	local function read(stream, count)
		local result = ""
		while #result ~= count do
			result = result .. assert(stream:read(count), "missing value")
		end
		return result
	end

	---Reads a variable length quantity from the given stream, raising an error if it can't.
	---@param stream file* The stream to read from.
	---@return integer value The read value.
	---@return integer length How many bytes were read in total.
	local function readVLQ(stream)
		local value = 0
		local length = 0
		repeat
			local byte = assert(stream:read(1), "incomplete or missing variable length quantity"):byte()
			value = value << 7
			value = value | byte & 0x7F
			length = length + 1
		until byte < 0x80
		return value, length
	end
	
	local midiEvent = {
		[0x80] = function(stream, callback, channel, fb)
			local key, velocity = ("I1I1"):unpack(fb .. stream:read(1))
			callback("noteOff", channel, key, velocity / 0x7F)
			return 2
		end,
		[0x90] = function(stream, callback, channel, fb)
			local key, velocity = ("I1I1"):unpack(fb .. stream:read(1))
			callback("noteOn", channel, key, velocity / 0x7F)
			return 2
		end,
		[0xA0] = function(stream, callback, channel, fb)
			local key, pressure = ("I1I1"):unpack(fb .. stream:read(1))
			callback("keyPressure", channel, key, pressure / 0x7F)
			return 2
		end,
		[0xB0] = function(stream, callback, channel, fb)
		local number, value = ("I1I1"):unpack(fb .. stream:read(1))
			if number < 120 then
			  callback("controller", channel, number, value)
			else
			  callback("modeMessage", channel, number, value)
			end
			return 2
		end,
		[0xC0] = function(stream, callback, channel, fb)
			local program = fb:byte()
			callback("program", channel, program)
			return 1
		end,
		[0xD0] = function(stream, callback, channel, fb)
			local pressure = fb:byte()
			callback("channelPressure", channel, pressure / 0x7F)
			return 1
		end,
		[0xE0] = function(stream, callback, channel, fb)
			local lsb, msb = ("I1I1"):unpack(fb .. stream:read(1))
			callback("pitch", channel, (lsb | msb << 7) / 0x2000 - 1)
			return 2
		end
	}

	---Processes a manufacturer specific SysEx event.
	---@param stream file* The stream, pointing to one byte after the start of the SysEx event.
	---@param callback function The feedback providing callback function.
	---@param fb string The first already read byte, representing the manufacturer id.
	---@return integer length The total length of the read SysEx event in bytes (including fb).
	local function sysexEvent(stream, callback, fb)
		local manufacturer = fb:byte()
		local data = {}
		repeat
			local char = stream:read(1)
			table.insert(data, char)
		until char:byte() == 0xF7
		callback("sysexEvent", data, manufacturer, table.concat(data))
		return 1 + #data
	end

	---Creates a simple function, forwarding the provided name and read data to a callback function.
	---@param name string The name of the event, which is passed to the callback function.
	---@return function function The function, calling the provided callback function with name and read data.
	local function makeForwarder(name)
		return function(data, callback)
			callback(name, data)
		end
	end

	local metaEvents = {
		[0x00] = makeForwarder("sequenceNumber"),
		[0x01] = makeForwarder("text"),
		[0x02] = makeForwarder("copyright"),
		[0x03] = makeForwarder("sequencerOrTrackName"),
		[0x04] = makeForwarder("instrumentName"),
		[0x05] = makeForwarder("lyric"),
		[0x06] = makeForwarder("marker"),
		[0x07] = makeForwarder("cuePoint"),
		[0x20] = makeForwarder("channelPrefix"),
		[0x2F] = makeForwarder("endOfTrack"),
		[0x51] = function(data, callback)
			local rawTempo = (">I3"):unpack(data)
			callback("setTempo", 6e7 / rawTempo)
		end,
		[0x54] = makeForwarder("smpteOffset"),
		[0x58] = function(data, callback)
			local numerator, denominator, metronome, dotted = (">I1I1I1I1"):unpack(data)
			callback("timeSignature", numerator, 1 << denominator, metronome, dotted)
		end,
		[0x59] = function(data, callback)
			local count, minor = (">I1I1"):unpack(data)
			callback("keySignature", math.abs(count), count < 0 and "flat" or count > 0 and "sharp" or "C", minor == 0 and "major" or "minor")
		end,
		[0x7F] = makeForwarder("sequenceEvent")
	}

	---Processes a midi meta event.
	---@param stream file* A stream pointing one byte after the meta event.
	---@param callback function The feedback providing callback function.
	---@param fb string The first already read byte, representing the meta event type.
	---@return integer length The total length of the read meta event in bytes (including fb).
	local function metaEvent(stream, callback, fb)
		local event = fb:byte()
		local length, vlqLength = readVLQ(stream)
		local data = read(stream, length)
		local handler = metaEvents[event]
		if handler then
			handler(data, callback)
		end
		return 1 + vlqLength + length
	end

	---Reads the four magic bytes and length of a midi chunk.
	---@param stream file* A stream, pointing to the start of a midi chunk.
	---@return string type The four magic bytes the chunk type (usually `MThd` or `MTrk`).
	---@return integer length The length of the chunk in bytes.
	local function readChunkInfo(stream)
		local chunkInfo = stream:read(8)
		if not chunkInfo then
			return false
		end
		assert(#chunkInfo == 8, "incomplete chunk info")
		return (">c4I4"):unpack(chunkInfo)
	end

	---Reads the content in a header chunk of a midi file.
	---@param stream file* A stream, pointing to the data part of a header chunk.
	---@param callback function The feedback providing callback function.
	---@param chunkLength integer The length of the chunk in bytes.
	---@return integer format The format of the midi file (0, 1 or 2).
	---@return integer tracks The total number of tracks in the midi file.
	local function readHeader(stream, callback, chunkLength)
		local header = read(stream, chunkLength)
		assert(header and #header == 6, "incomplete or missing header")
		local format, tracks, division = (">I2I2I2"):unpack(header)
		callback("header", format, tracks, division)
		return format, tracks
	end

	---Reads only a single event from the midi stream.
	---@param stream file* A stream, pointing to a midi event.
	---@param callback function The callback function, reporting the midi event.
	---@param runningStatus? integer A running status of a previous midi event.
	---@return integer length, integer runningStatus Returns both read length and the updated running status.
	local function processEvent(stream, callback, runningStatus)
		local firstByte = assert(stream:read(1), "missing event")
		local status = firstByte:byte()

		local length = 0

		if status < 0x80 then
			status = assert(runningStatus, "no running status")
		else
			firstByte = stream:read(1)
			length = 1
			runningStatus = status
		end


		if status >= 0x80 and status < 0xF0 then
			length = length + midiEvent[status & 0xF0](stream, callback, (status & 0x0F) + 1, firstByte)
		elseif status == 0xF0 then
			length = length + sysexEvent(stream, callback, firstByte)
		elseif status == 0xF2 then
			length = length + 2
		elseif status == 0xF3 then
			length = length + 1
		elseif status == 0xFF then
			length = length + metaEvent(stream, callback, firstByte)
		else
			callback("ignore", status)
		end

		return length, runningStatus
	end

	---Reads the content of a track chunk of a midi file.
	---@param stream file* A stream, pointing to the data part of a track chunk.
	---@param callback function The feedback providing callback function.
	---@param chunkLength number The length of the chunk in bytes.
	---@param track integer The one-based index of the track, used in the "track" callback.
	local function readTrack(stream, callback, chunkLength, track)
		callback("track", track)

		local runningStatus

		while chunkLength > 0 do
			local ticks, vlqLength = readVLQ(stream)

			if ticks > 0 then
				callback("deltatime", ticks)
			end

			local readChunkLength
			readChunkLength, runningStatus = processEvent(stream, callback, runningStatus)
			chunkLength = chunkLength - readChunkLength - vlqLength
		end
	end

	---Processes a midi file by calling the provided callback for midi events.
	---@param stream file* A stream, pointing to the start of a midi file.
	---@param callback? function The callback function, reporting the midi events.
	---@param onlyHeader? boolean Wether processing should stop after the header chunk.
	---@param onlyTrack? integer If specified, only this single track (one-based) will be processed.
	---@return integer tracks Returns the total number of tracks in the midi file.
	local function process(stream, callback, onlyHeader, onlyTrack)
		callback = callback or function() end

		local format, tracks
		local track = 0
		while true do
			local chunkType, chunkLength = readChunkInfo(stream)

			if not chunkType then
				break
			end

			if chunkType == "MThd" then
				assert(not format, "only a single header chunk is allowed")
				format, tracks = readHeader(stream, callback, chunkLength)
				assert(tracks == 1 or format ~= 0, "midi format 0 can only contain a single track")
				assert(not onlyTrack or onlyTrack >= 1 and onlyTrack <= tracks, "track out of range")
				if onlyHeader then
					break
				end
			elseif chunkType == "MTrk" then
				track = track + 1
				assert(format, "no header chunk before the first track chunk")
				assert(track <= tracks, "found more tracks than specified in the header")
				assert(track == 1 or format ~= 0, "midi format 0 can only contain a single track")

				if not onlyTrack or track == onlyTrack then
					readTrack(stream, callback, chunkLength, track)
					if onlyTrack then
						break
					end
					else
					stream:seek("cur", chunkLength)
				end
			else
				local data = read(chunkLength)
				callback("unknownChunk", chunkType, data)
			end
		end

		if not onlyHeader and not onlyTrack then
			assert(track == tracks, "found less tracks than specified in the header")
		end

		return tracks
	end

	---Processes only the header chunk.
	---@param stream file* A stream, pointing to the start of a midi file.
	---@param callback function The callback function, reporting the midi events.
	---@return integer tracks Returns the total number of tracks in the midi file.
	local function processHeader(stream, callback)
		return process(stream, callback, true)
	end

	---Processes only the header chunk and a single, specified track.
	---@param stream file* A stream, pointing to the start of a midi file.
	---@param callback function The callback function, reporting the midi events.
	---@param track integer The one-based track index to read.
	---@return integer tracks Returns the total number of tracks in the midi file.
	local function processTrack(stream, callback, track)
		return process(stream, callback, false, track)
	end

	return {
		process = process,
		processHeader = processHeader,
		processTrack = processTrack,
		processEvent = processEvent
	}

end