How to change from audio-out to MIDI out from same headphone jack?
There is an offical standard for MIDI with TRS connectors; not using it would lead to confusion. (There is already enough confusion due to the two other unofficial standards.)
Many cheap USB/MIDI interfaces do not use an optocoupler but measure the voltage at pin 5 of the MIDI connector, so you should use an active-low driver.
The standard output circuit with to 220 Ω resistors is designed to protect against shorts between any pins, so you should use it.
If you have enough GPIOs, you can connect the audio and MIDI circuits in parallel, and disable the unused GPIOs. To save pins, use a two-channel mux to switch between the audio and MIDI signals (but this needs an additional GPIO for the switch).
If it is possible that the user connects some audio output to the TRS jack, consider implementing over/undervoltage protection.