cycles_per_frame = 19656 rate_period = 31251 env=0 rc=0 for frame in range(120): gate_on_ticks = 0 for i in range(cycles_per_frame*2): rc+=1 if rc==rate_period: rc=0 gate_on_ticks+=1 env+=1 gate_off_ticks = 0 for i in range(cycles_per_frame*2): rc+=1 if rc==rate_period: rc=0 gate_off_ticks+=1 env-=1 if env<0: env=0 print("frame {:3d}: {} attack ticks, {} decay ticks, {} env".format (frame*4, gate_on_ticks, gate_off_ticks, env))