Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:48:32+00:00 2026-06-05T22:48:32+00:00

My iOS app sends MIDI bank and program changes to other devices using PGMidi

  • 0

My iOS app sends MIDI bank and program changes to other devices using PGMidi, a Core MIDI library. Some users have reported that the messages sometimes arrive in the wrong order — the program change followed by the bank change.

For each bank/program change, I’m assembling an NSArray of numeric values, then passing that array to a sendMidiDataInBackground method in a background thread:

int MSBStatus = 0xB0;
int MSBController = 0;
int MSBValue = 1;
NSArray *MSBValues = [NSArray arrayWithObjects:[NSNumber numberWithInt:MSBStatus], [NSNumber numberWithInt:MSBController], [NSNumber numberWithInt:MSBValue], nil];
[self performSelectorInBackground:@selector(sendMidiDataInBackground:) withObject:MSBValues];

int LSBStatus = 0xB0;
int LSBController = 32;
int LSBValue = 2;
NSArray *LSBValues = [NSArray arrayWithObjects:[NSNumber numberWithInt:LSBStatus], [NSNumber numberWithInt:LSBController], [NSNumber numberWithInt:LSBValue], nil];
[self performSelectorInBackground:@selector(sendMidiDataInBackground:) withObject:LSBValues];

int programStatus = 0xC0;
int programValue = 3
NSArray *programValues = [NSArray arrayWithObjects:[NSNumber numberWithInt:programStatus], [NSNumber numberWithInt:programValue], nil];
[self performSelectorInBackground:@selector(sendMidiDataInBackground:) withObject:programValues];

The sendMidiDataInBackground method changes the values to a C array and passes them on to PGMidi’s sendBytes method, which assembles them into a packet list and sends them out via MIDISend. I noticed the timestamps are set to 0, which means “now”:

- (void) sendBytes:(const UInt8*)bytes size:(UInt32)size {
    Byte packetBuffer[size+100];
    MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer;
    MIDIPacket     *packet     = MIDIPacketListInit(packetList);
    packet = MIDIPacketListAdd(packetList, sizeof(packetBuffer), packet, 0, size, bytes);
    OSStatus s = MIDISend(midi.outputPort, endpoint, packetList);
}

But at some point apparently some messages are getting delayed so they’re ending up in the wrong order. Is this because the background threads don’t finish in the order they were started? If so, I could combine the MSB, LSB and program arrays and send the combined array to sendMidiDataInBackground rather than starting three separate threads, and that should solve it.

Or could this be happening after the MIDISend function is called? If so, I’d need another solution.

I can’t repeat the problem in my own testing, so I’d like to know for sure what the problem is so I have a better chance of actually fixing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:48:36+00:00Added an answer on June 5, 2026 at 10:48 pm

    According to the documentation, -performSelectorInBackground:withObject: creates a new background thread. You should not make any assumptions about how the OS will schedule those threads. You cannot count on them being run in the order you created them.

    What makes you think you need to do this in the background? Sending a few short MIDI messages is unlikely to block your main thread for a noticeable amount of time. Creating three separate threads is an incredible amount of overhead for a tiny amount of work.

    If you really need to do it in the background, consider using something like GCD’s dispatch_async or an NSOperation with a serial queue, and/or do all your work in one method call rather than three.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ARC in an iOS app i'm building. I've created some objects, in
I'm making an iOS app using AIR 3.1 and Flex 4.5.1, and if I
I'm developing an iOS app. Its working properly on simulator. I'm using phonegap and
I am developing an iOS app that allows users to download documents off a
I am writing an ios app that talks to the asp.net server by using
Simple question: I am creating an iOS app and the view controller calculates some
So I'm making an iOS app that constantly sends and array of points through
I'm designing an iOS static library that will be used by other developers. This
I am using the StringEncryption.m library in iOS to encrypt messages, and then decode
Overview I have an iOS app which sends local notifications at specific dates. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.