How to convert keyframe (in NLA) to scene_frame#?

Question: How to get a keyframe's frame location from an offset nla strip?

When you’re using an action, the keyframe_points in the fcurves have a point.co property containing a frame number and a value. The frame number is generally the same number you could set the scene’s frame to, to see the same value from the curve.
If you put the action into an NLA strip, then move the strip around, scale it, cut it, etc, the frame number inside the strips still don’t change, and if you enter tweak mode, the keyframes will behave as though they were moved with the strip.

I’ve spent hours and hours for days and days trying to find the math to convert keyframe.co to the strips offset position.
Each time, I either give up or get a solution that I think works until I put the strip in another scenario which makes it not work anymore.

Here is a demo file containing several NLA strips with various actions performed on them.

What I’m looking for is a way to read the data from an nla strip and action, to find where the key will be when you start tweaking the strip.

Modified NLA Strips.blend (742.9 KB)

edit: did some research, found a formula for resizing a [range], and I believe I got it working correctly in Blender:

def scale_range(OldValue, OldMin, OldMax, NewMin, NewMax):
	OldRange = (OldMax - OldMin)  
	NewRange = (NewMax - NewMin)  
	if (OldRange == 0):
		NewValue = NewMin
	else:
		NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
	return NewValue

def strip_co_frame(strip, co=None, frame=None):
	sfs = strip.frame_start
	sfe = strip.frame_end
	afs = strip.action_frame_start
	afe = strip.action_frame_end
	if co:
		return scale_range(co, afs, afe, sfs, sfe)
	elif frame:
		return scale_range(frame, sfs, sfe, afs, afe)
	else:
		return None

ex:

frame = strip_co_frame(strip, co=key.co[0])
key.co[0] = strip_co_frame(strip, frame=frame)