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 8906005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:32:25+00:00 2026-06-15T02:32:25+00:00

I need to hand assemble 14bit MIDI Pitch Bend values from raw UInt16 values

  • 0

I need to hand assemble 14bit MIDI Pitch Bend values from raw UInt16 values in iOS. I’m wondering if anybody out there has had a chance to come up with an elegant solution? Here’s where I’m at – I’ll get a chance to test this probably later today, but if I hear back before then, great:

First, some MIDI preliminaries for anybody curious.

MIDI Pitch Bend is broken up into one Status Byte followed by two Data Bytes (it’s a 14bit controller), these two Data Bytes are associated with their Status Byte by both leading with a Zero status bit, MIDI Spec has them appearing in the order of MSB -> LSB

(Edit: Update, it’s actually Status -> LSB -> MSB )

( ie 1110 0000, 0111 1111, 0111 1111 )

The challenge is how to break up an ARM/Intel 16bit UInt16 into two 7 bit segments on iOS, and have it make sense for MIDI?

Please keep in mind that, because we’re dealing with an unsigned integer, a 0 value is NOT neutral pitch bend, but rather full pitch down – where as neutral pitch bend is defined as 8192 – and 16,383 is full pitch up.

So here’s my best guess as to how to do this:

UInt16 msbAnd = base10ValueUInt16 & 16256; //clearing out LSB 
UInt16 msbAndShift = msbAnd << 1; //shift into leading Byte, with 0 status bit

UInt16 lsbAnd = base10ValueUInt16 & 127; //isolating LSB
UInt16 finalTwoBytePitchWord = msbFinalAndShift | lsbAnd; //make UInt16 word

UInt16 finalTwoBytePitchWordFlipped = CFSwapInt16HostToBig(finalTwoBytePitchWord); //Endian tweak

This code runs fine and seems to create the two Data Bytes with the required zero status bits and flips them around from little endian Intel/ARM which seems to be necessary for MIDI (MIDI is STATUS -> MSB -> LSB ): I can slap on the leading Status Byte with the appropriate MIDI channel later.

So, does this make sense? Has anybody come up with a more elegant solution? ( is there a Library I’m overlooking? ) … I’ll check back in later and also let folks know if this actually worked on the sampler I have to target it at.

Thanks

  • 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-15T02:32:26+00:00Added an answer on June 15, 2026 at 2:32 am

    I think your code is close to right, but it’s overly complicated. This question has nothing to do with iOS or endianness or ARM or Intel; it’s just plain old C bit-twiddling. If you write the code correctly, it will work on any reasonable platform without modification. You don’t need a library; it’s only a couple lines of code.

    It’s best to work with MIDI on a byte-by-byte basis. You want a function that takes a 16-bit unsigned integer (which we’ll trust has at most 14 bits worth of value) and returns two single-byte values, one with the most significant bits, one with the least significant bits.

    Later on, when you send the message, you assemble the bytes in the appropriate order. According to the specification, pitch wheel messages are three bytes: STATUS, then LSB, then MSB. You have them backwards in your question!

    The least-significant 7 bits are easy: just mask off those bits from the original value. The most-significant 7 bits are similar: mask off the next higher 7 bits from the original value, then shift them down.

    It doesn’t matter whether the 16-bit integers are little-endian or big-endian in memory on your machine; the compiler takes care of that.

    Here’s a function and a test tool.

    #include <stdio.h>
    #include <stdint.h>  // for C standard uint8_t and uint16_t
    // or, if you prefer, use unsigned char and unsigned short, or Byte and UInt16;
    // they'll all work, although some are more portable than others
    
    void encode14BitValue(uint16_t value, uint8_t *out_msb, uint8_t *out_lsb)
    {
        uint16_t mask = 0x007F;  // low 7 bits on
                                 // "(1 << 7) - 1" is arguably clearer
        *out_lsb = value & mask;
        *out_msb = (value & (mask << 7)) >> 7;
    }
    
    int main(int argc, const char * argv[])
    {
        typedef struct {
            uint16_t in;
            uint8_t expected_msb;
            uint8_t expected_lsb;
        } test_case;
    
        test_case cases[] = {
            { 0x0000, 0x00, 0x00 },
            { 0x0001, 0x00, 0x01 },
            { 0x0002, 0x00, 0x02 },
            { 0x0004, 0x00, 0x04 },
            { 0x0008, 0x00, 0x08 },
            { 0x0009, 0x00, 0x09 },
            { 0x000F, 0x00, 0x0F },
            { 0x0010, 0x00, 0x10 },
            { 0x0011, 0x00, 0x11 },
            { 0x001F, 0x00, 0x1F },
            { 0x0020, 0x00, 0x20 },
            { 0x0040, 0x00, 0x40 },
            { 0x0070, 0x00, 0x70 },
            { 0x007F, 0x00, 0x7F },
            { 0x0080, 0x01, 0x00 },
            { 0x0081, 0x01, 0x01 },
            { 0x008F, 0x01, 0x0F },
            { 0x0090, 0x01, 0x10 },
            { 0x00FF, 0x01, 0x7F },
            { 0x0100, 0x02, 0x00 },
            { 0x0200, 0x04, 0x00 },
            { 0x0400, 0x08, 0x00 },
            { 0x0800, 0x10, 0x00 },
            { 0x1000, 0x20, 0x00 },
            { 0x1FFF, 0x3F, 0x7F },
            { 0x2000, 0x40, 0x00 },
            { 0x2001, 0x40, 0x01 },
            { 0x3FFF, 0x7F, 0x7F },
        };
    
        int passed = 1;
        for (int i = 0, c = sizeof(cases) / sizeof(cases[0]); i < c; i++) {
            uint8_t msb, lsb;
            encode14BitValue(cases[i].in, &msb, &lsb);
    
            if (cases[i].expected_msb != msb || cases[i].expected_lsb != lsb) {
                printf("failed: 0x%04hX expected 0x%02hhX 0x%02hhX got 0x%02hhX 0x%02hhX\n", cases[i].in, cases[i].expected_msb, cases[i].expected_lsb, msb, lsb);
                passed = 0;
            }
        }
    
        return passed ? 0 : 1;
    }
    

    In your code, trying to pack the two bytes of result into one 16-bit integer just adds confusion. I don’t know why you’re doing that, since you’re going to have to extract individual bytes again, whenever you send the MIDI anywhere else. That’s where any worries about endianness come up, since your packing and unpacking code have to agree. You might as well not bother. I bet your code was incorrect, but your error in swapping MSB and LSB compensated for it.

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

Sidebar

Related Questions

I need a quick hand figuring out what this code is doing, and how
I need to get the left hand side integer value from a decimal or
I've been away from inheritance for a while and need a little helping hand.
I'm new to php and I need a hand. I have width by height
I am stuck and in need of a hand. Hope someone can help? Anyone
I have following array, that I need to operate by hand on bitmaps. const
I need a hand. I'm using android-maven-plugin 3.0.0 and maven-external-dependency-plugin 0.4 in an android
Hay i need to hand implemeneting a voting system into a model. I've had
Having a mental block today, need a hand verifying my logic isn't fubar'ed. Traditionally
Morning, I am new to this so just need a quick hand on how

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.