You can compensate for this in software!
As you can see from the shape of the signal (the curve that is slowly pulling your signal to ground), there is a time component involved. You can offset it in software by emulating the time constant and compensating for it.
This ability will be slightly limited:
- It will not be perfect at all frequencies
- Long pulses will require more "headroom"
If you lower the base amplitude of the signal, the available headroom can be used for a longer time, giving you the ability to compensate for longer pulses. But you have to keep in mind that your time constant compensator will have to recenter itself on 0V eventually, otherwise the DC bias will eventually creep to maximum positive or negative amplitude and then clip and distort.
So the idea is to come up with a filter that will boost what the headphone amplifier is limiting.
Here is an example in which I superimposed a triangle wave on top of the square wave. Note that this example is a simplification because the voltage of the triangle wave normally depends on the voltage of the square wave. This circuit will only work with that specific square wave.
The values were obtained empirically. The square wave at the left represents your signal. The two identical clusters on the right represent the capacitive coupling of your headphone amplifier. In the upper part of the circuit, the signal is sent through unaltered, resulting in the waveform in the first graph.
The lower part of the circuit adds a triangle wave on top of the signal. The phase is matched with the square wave. The op-amp part is simply a follower amplifier that attenuates the signal a bit so that the output is not too loud for the headphone amplifier.
By superimposing the triangle wave on the original signal, the output will look more like a square wave, like in the second graph.
The third graph shows the output before it is filtered by the capacitive coupling.
So what you should try to do is reimplement the triangle wave in software. Beware of the pitfalls!
Edit: and as Dwayne Reid pointed out, this capacitive coupling is specific to your phone. Other devices might have different characteristics, or not have coupling at all. So this is a solution for your device only.