Help with signal conditioning

I wonder if somebody can point me to a software algorithm or an hardware solution to obtain nice square waves (sharp attack, sustain and, what's missing here, a sharp release).

Fundamentally, there is no good solution.

Alternatively, you can introduce some derivative gain, similar to a PID controller, for this.