You can absolutely use JUCE to build a plugin that will create a MIDI file during runtime and save it to your user’s computer.
The sticky point is this part:
the user can easily drag your generated MIDI file into the DAW. But as far as I know, your plugin cannot directly write to the DAW’s timeline, or force it to load a MIDI file.
I think what’s happening in Melodyne (never really used it myself) is that they have an internal editor – there’s another timeline within Melodyne itself, right?
So you could absolutely write your own piano roll & timeline inside your plugin and keep track of your midi, so that that’s what you’re outputting to the host each processBlock() call, depending on the transport position. But that midi data would only live inside your plugin, you can’t force the host to record your output. What would really be happening here is that your plugin would have its own internal concept of a “timeline” – as far as I know, directly interfacing with the DAW’s timeline through any plugin API is not possible.