1. Unknown Code
- Posted by timmy <tim781 at PACBELL.NET>
Feb 02, 2001
-
Last edited Feb 03, 2001
HI,
I'm writting to ask if someone will
translate these few lines of C code
to Euphoria. I'm studying MIDI
and understand everything except
how to convert "variable length
quantities". These are needed for
storing delta-time. I can't figure out
which bits are to be discarded and
don't know if << 7 shift equals an 8-bit
shift or not. ..thanks timmy :)
unsigned long ReadVarLen()
{
register unsigned long value;
register unsigned char c;
if ( (value = getc(infile)) & 0x80 )
{
value &= 0x7F;
do
{
value = (value << 7) + ((c = getc(infile)) & 0x7F);
} while (c & 0x80);
}
return(value);
}
2. Re: Unknown Code
timmy wrote:
>unsigned long ReadVarLen()
Untested, but it *should* work. Variable length data is stored as 7 bits of
data, with the 8th bit set high if there is more data following.
function ReadVarLen()
-- read variable length data from a midi file
atom value
integer c
-- clear accumulator
value = 0
-- read bytes from infile
while 1 do
-- read a byte
c = getc(infile)
-- add low 7 bits to accumulator
value += and_bits(c,#7F)
-- high 8th bit not set?
if not and_bits(c,#80) then
-- return accumulated value
return value
end if
-- right shift the 7 bits
c *= power(2,7)
end while
end function
3. Re: Unknown Code
Thanks :)
..timmy
euphoria_coder at HOTMAIL.COM wrote:
> timmy wrote:
>
> >unsigned long ReadVarLen()
>
> Untested, but it *should* work. Variable length data is stored as 7 bits of
> data, with the 8th bit set high if there is more data following.
>
> function ReadVarLen()
> -- read variable length data from a midi file
>
> atom value
> integer c
>
> -- clear accumulator
> value = 0
>
> -- read bytes from infile
> while 1 do
> -- read a byte
> c = getc(infile)
> -- add low 7 bits to accumulator
> value += and_bits(c,#7F)
> -- high 8th bit not set?
> if not and_bits(c,#80) then
> -- return accumulated value
> return value
> end if
> -- right shift the 7 bits
> c *= power(2,7)
> end while
> end function