0

The midi norm for music allows to code delta time duration as integer values (representing ticks). For example I have a delta time of 960. The binary value of 960 is 1111000000. The thing is that the midi norm doesn't code the number on 16 bits. It codes it on 14 bits and then, adds 10 at the 2 first bits to create another 16 bits value, 1 meaning that there is a following byte, and 0 meaning that it is the last byte. My question is : how can I easily calculate 960 as a binary value coded on 14 bits? Cheers

DarkVapor
  • 61
  • 6
  • http://www.cprogramming.com/tutorial/bitwise_operators.html Have a look at this. It'll give you an idea on how to manipulate data at the bit level in C or C++. – Ben Oct 06 '14 at 19:58
  • Going off of @Ben, use the shift operators along with a mask equal to 1. Then you'll be able to bitwise AND, and then right shift 960 by 1. – Disco Globeulon Oct 06 '14 at 20:03
  • thank you. But I still need to start with the binary value of 960 coded on 16 bits right? – DarkVapor Oct 06 '14 at 20:08
  • You might want to look at this [SO Answer](http://stackoverflow.com/questions/13589979/calculating-coremidi-pitch-bend-values-for-ios/13599463#13599463) . In particular an implementation of `encode14BitValue` – Michael Petch Oct 06 '14 at 20:15

3 Answers3

1

You can specify any number of bits as length inside a struct like so:

struct customInt {
    unsigned int n:14; // 14-bit long unsigned integer type
};

Or you can make your own functions that take care of these kind of specific calculations/values.

notadam
  • 2,754
  • 2
  • 19
  • 35
1

In the bytes that make up a delta time, the most significant bit specifies whether another byte with more bits is following. This means that a 14-bit value like 00001111000000 is split into two parts, 0000111 and 1000000, and encoded as follows:

1 0000111   0 1000000
^ ^         ^  lower 7 bits
| |         \
| \          last byte
\  upper 7 bits
 more bytes follow

In C, a 14-bit value could be encoded like this:

int value = 960;
write_byte(0x80 | ((value >> 7) & 0x7f));
write_byte(0x00 | ((value >> 0) & 0x7f));

(Also see the function var_value() in arecordmidi.c.)

CL.
  • 173,858
  • 17
  • 217
  • 259
  • thank you very much, indeed I was wrong its 1+byte on 7 bits + 0+byte on 7 bits. Not 10+byte on 14 bits. – DarkVapor Oct 07 '14 at 09:45
0

If you are using unsigned integers, just do the calculations normally.

Start with

 value = 960 ;

To convert the final output to 14 bits, do

  value &= 0x3FFF ;

To add binary 10 to the front do

  value |= 0x8000 ;
user3344003
  • 20,574
  • 3
  • 26
  • 62