Midi playback is delayed for some notes

Hey everyone!
I am currently working on my first midi plugin, and I ran into some issues that I can’t seem to get rid of. I want to write a basic transposer, where the transposition follows certain rules. For that purpose, with the user setting the song’s key, an array (NoteArray) is generated, which contains the target notes. So for example if C gets mapped onto G then NoteArray[60] will equal 67. The array generation takes only place on key change and should enable fast midi processing.
I believed this implementation would be sufficiently fast, but for some notes (even when they are played just by themselves) I hear a clear delay in my DAW (Ableton). For other notes it works just fine. The problematic notes depend on the key. But for a given key, it’s always the same notes. During the issue, the instrument that plays the midi spits out the note delayed. If I record the midi however, the recording is perfectly in time. I tried to increase the buffer size in Ableton but this didn’t help.
I can only guess as to why it doesn’t work, but I think that maybe my way of handling the midi is deprecated / incorrect. I followed along a video on YouTube, and made some minor changes to the functionality. Here is the relevant part of the code:

void MIditestAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
    // clear anything that might intererfere (current buffer and left over audio)
    buffer.clear();

    midiProcessor.process(midiMessages);
}

void MidiProcessor::process(juce::MidiBuffer& midiMessages)
{
	processedBuffer.clear();
	
	processMidiInput(midiMessages);
	midiMessages.swapWith(processedBuffer);
	
}

void MidiProcessor::processMidiInput(const juce::MidiBuffer& midiMessages)
{
	for (const auto& metadata : midiMessages)
	{
		auto msg = metadata.getMessage();
		auto sampleNumber = metadata.samplePosition;

		if (msg.isNoteOnOrOff())
		{
			addTransposedNote(msg, sampleNumber);
		}
	}
}

void MidiProcessor::addTransposedNote(juce::MidiMessage& MessagetoTranspose, int samplePos)
{
	// inside here the actual transformation takes place 
	auto oldNoteNum = MessagetoTranspose.getNoteNumber();
	auto newNoteNum = NoteArray[oldNoteNum];

	if (MessagetoTranspose.isNoteOn())
	{
		activeNotes.insert(newNoteNum);  // Track active notes
	}
	else if (MessagetoTranspose.isNoteOff())
	{
		activeNotes.erase(newNoteNum);  // Erase notes when key is lifted
	}

	MessagetoTranspose.setNoteNumber(newNoteNum);
	processedBuffer.addEvent(MessagetoTranspose, samplePos);
}

Note that I keep track of active notes in case a key change happens while a note is on, but the problem is still there if I remove that functionality.
I would be very thankful if you could tell me how to improve this, and I am very curious as to whether there is a better way to handle the midi, because I am still quite new to C++ and Juce! Many thanks in advance!