MidiOutput problem with SendBlockOfMessagesNow()

I’m working on a plugin that sends data from a midi file to the host. For the standalone app I use a Midi Output device but for some reason when I use sendBlockOfMessagesNow(), the output doesn’t use the specified midi patch. Everything else works but all the files sound like piano instead of the instrument that is specified by the program change. The code below is what I am using in my processBlock() function. Any help would be greatly appreciated, thanks!

const ScopedTryLock myScopedLock(processLock);
if (myScopedLock.isLocked())
{
	if (midiTransports.isPlaying || m_HostPlayerInfo.isPlaying)
	{
		double startTime = ((Time::getMillisecondCounterHiRes() - midiTransports.timer) / 1000) + midiTransports.startPosition;
		if (m_HostPlayerInfo.isPlaying && m_StateInfo.m_BBSyncPlayback)
			startTime = m_HostPlayerInfo.timeInSeconds;
		double endTime = startTime + getBlockSize() / getSampleRate();

	    if (m_HostPlayerInfo.isPlaying
		&&	std::abs(startTime - midiTransports.nextPosition) > (1.0 / getSampleRate())
		&&  midiTransports.nextPosition > 0.0)
			sendAllNotesOff(midiBuffer);
		midiTransports.nextPosition = endTime; 
        
        if (midiTransports.notesAreOn && startTime > midiManager.getEndTime())
		{
			sendAllNotesOff(midiBuffer);
			midiTransports.hasStreamFinished = true;
		}
		else
		{
			midiTransports.currentPosition = startTime;
			for (auto i = 0; i < midiManager.getNumEvents(); i++)
			{
				MidiMessage message = midiManager.getEventPointer(i)->message;
				if (message.getTimeStamp() >= startTime && message.getTimeStamp() < endTime)
				{
					auto samplePosition = roundToInt((message.getTimeStamp() - startTime) * getSampleRate());
					midiTransports.hasStreamFinished = false;
					midiTransports.notesAreOn = true;
					message.setVelocity((float)midiTransports.gain * message.getFloatVelocity());
					midiBuffer.addEvent(message, samplePosition);
				}
			}
		}
	}
	else if (midiTransports.notesAreOn)
		sendAllNotesOff(midiBuffer);
}
else if (midiTransports.notesAreOn)
	sendAllNotesOff(midiBuffer);

if (JUCEApplication::isStandaloneApp() && midiSource != nullptr)
	midiSource->sendBlockOfMessagesNow(midiBuffer);

Bump!

Does anyone have a clue as to how I can fix this? If I replace SendBlockOfMessagesNow() with SendMessageNow() then it works correctly but I would need to create my own thread which I’m hoping to avoid if possible.

I think there is an issue with your code, as SendBlockOfMessagesNow() contains this code:

    while (i.getNextEvent (message, samplePosition))
        sendMessageNow (message);
}

I suggest using the debugger to figure out the issue…

Thanks for the response. I did a bit more debugging and discovered that what was causing the issue was the fact that I was using Time::getMillisecondCounterHiRes() instead of Time::getMillisecondCounter() to determine my current time. However, when I initialize midiTransports.timer before playback begins, the opposite is true and it only works if I use Time::getMillisecondCounterHiRes() … Not sure what the exact problem is there, but I’m glad to have solved it! Thanks again.

Another issue that I’m having is that the MidiOutput device only plays through the output that is selected in Windows (not the output selected in the plugin). If I change my output in Audio/Midi settings to headphones, audio will play correctly through the headphones, but midi will still play through the speakers. The only way I can change this is by changing my playback device from the Windows taskbar.

How can I set my MidiOutput device to use the selected output in my plugin rather than the selected Windows output? Thanks in advance!