realbasic-nug
[Top] [All Lists]

Re: Porting MIDI variable-length quantity C code to REALbasic

To: realbasic-nug at lists dot realsoftware dot com
Subject: Re: Porting MIDI variable-length quantity C code to REALbasic
From: Ron Benditt <rbenditt at alienapparatus dot com>
Date: Sun, 30 Jan 2005 19:24:52 -0500
Cc:
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <20050130180036 dot 083EB6D9CF9 at lists dot realsoftware dot com>
Philip,

I have to admit, the variable-length quantities in MIDI are very confusing. Let me see if I can explain what's going on by way of example.

Suppose we have the number 8192 decimal that we want to convert to a MIDI variable-length quantity. In hex, this is 20 00.

Usually, the bits in a two-byte binary numbers are counted this way in decimal:

(32768 16384 8192 4096 2048 1024 512 256) (128 64 32 16 8 4 2 1)

So, normally you would convert 8192 to binary by setting the bit representing 8192 like this:
00100000 00000000

Now, in the MIDI variable-length system, binary bits are represented this way:

(skip 8192 4096 2048 1024 512 256 128) (skip 64 32 16 8 4 2 1)

where the skipped bits are flags that tell you when you're at the last byte.

In our example, we would set the bit representing 8192, and also set the bit labeled "skip" in the leftmost byte, signifying that this is the last byte.

This gives, in binary:

11000000 00000000

In hex this becomes C0 00

From the web page that you've taken you're examples from, you'll see that they say that 2000h converts to C000h in the variable-length system. Hopefully what I've written here shows why that is the case.


Ron Benditt
Alien Apparatus Company, Inc.
http://www.alienapparatus.com

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>