#! /usr/bin/python import sys, os, mido #************************************** def GetMaxVelocity(midFile): rtn = 0 rMid = MidiFile(midFile) for i, track in enumerate(rMid.tracks): for ev in track: if ev.type == "note_on": if ev.velocity > rtn: rtn = ev.velocity return rtn #************************************** def NormalizeVelocity(velo, mxVelo): rtn = 0 # Set compression ratio eg: 2:1, 3:1, 4:1, 6:1, 8:1 etc compress = 8 # Pick up bumped or hidden notes if velo < 16: velo = 16 # Calc gain target = 95 net = mxVelo / compress gain = target - net # Normalize x = (velo / compress) + gain y = round(x) rtn = int(y) return rtn #************************************** # Run process trckNmbr = 0 mxVelo = GetMaxVelocity(midFile) # Open Midi file rMid = MidiFile(midFile) for i, track in enumerate(rMid.tracks): if i == trckNmbr: for ev in track: if ev.type == "note_on": newVelocity = NormalizeVelocity(ev.velocity, mxVelo) print('New Velocity:',newVelocity)