Could Someone Translate This Code To VB? 
Author Message
 Could Someone Translate This Code To VB?

Hello
I have been looking at some code to read MP3 files but I can't figure out how
to read the bitrate and a few other things.  Here is the original source code
in C.  I don't do C and am pretty new at VB so any help would be appreciated.

/************ Layer I, Layer II & Layer III ******************/
void decode_info(bs, fr_ps)
Bit_stream_struc *bs;
frame_params *fr_ps;
{    
    layer *hdr = fr_ps->header;
    hdr->version = get1bit(bs);    
    hdr->lay = 4-getbits(bs,2);
    hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */    
    hdr->bitrate_index = getbits(bs,4);
    hdr->sampling_frequency = getbits(bs,2);
    hdr->padding = get1bit(bs);
    hdr->extension = get1bit(bs);
    hdr->mode = getbits(bs,2);
    hdr->mode_ext = getbits(bs,2);
    hdr->copyright = get1bit(bs);
    hdr->original = get1bit(bs);
    hdr->emphasis = getbits(bs,2);

Quote:
}

If you need more code samples, let me know.  I have more but since I have no
idea what I'm looking at, I'm not sure if this is enough or not.  If not, I
will post all the code.
Thanks again.


Wed, 10 Jan 2001 03:00:00 GMT  
 Could Someone Translate This Code To VB?


Quote:
>Hello
>I have been looking at some code to read MP3 files but I can't figure out how
>to read the bitrate and a few other things.  Here is the original source code
>in C.  I don't do C and am pretty new at VB so any help would be appreciated.

>/************ Layer I, Layer II & Layer III ******************/
>void decode_info(bs, fr_ps)
>Bit_stream_struc *bs;
>frame_params *fr_ps;
>{    
>    layer *hdr = fr_ps->header;
>    hdr->version = get1bit(bs);    
>    hdr->lay = 4-getbits(bs,2);
>    hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */    
>    hdr->bitrate_index = getbits(bs,4);
>    hdr->sampling_frequency = getbits(bs,2);
>    hdr->padding = get1bit(bs);
>    hdr->extension = get1bit(bs);
>    hdr->mode = getbits(bs,2);
>    hdr->mode_ext = getbits(bs,2);
>    hdr->copyright = get1bit(bs);
>    hdr->original = get1bit(bs);
>    hdr->emphasis = getbits(bs,2);
>}

Marc, We can't do the specifics without the C header (.h) file
defining the structures, but if you can find this, you can work it out
for yourself with the following basic information:

This code is pulling out values of between 1 and 4 bits from a
structure held (probably) in a Long equivalent.  Once you've got hold
of the Long, you can fetch out individual bits in VB by masking the
word with a literal mask. Bits are traditionally number 0-31 with 0
being at the left of the word.  For instance, for the least
significant bit, bit 31, has the mask &H1, whereas bit 25's mask is
&H40

So, to determine whether bit 25 is set, do
Public Const bit25SensibleName As Long = &H40&

If (bs And bit25SensibleName) = 0 Then
        ' bit is clear
Else
        ' bit is set
End If

For values held in multiple bits, the simplest way to convert these
into Int or Long values is to shift them to the right-hand end of the
word and mask off any other bits in the word.  There are no bit
shifting operators in VB, but you can do it easily by dividing the
original value by the approproiate power of 2 - one power of 2 for
each bit position you need to shift.  For example, the
sampling_frequency above is 2 bits long. Let's assume these bits are
the next ones to bit 25 used in the previous example, that is, bits 23
and 24.  To move these to the end of the word means dividing by 7
powers of 2, ie, 128.  Once they've been shifted, any other
significant bits to their left need to be cleared.  Since this is a
2-bit field, we can achieve this by masking with 3 (binary 11). So the
whole thing can be achieved with:

Dim iSamplingFrequency as Integer
iSamplingFrequency = (bs \ 128) And 3

Apply the same principles to all other fields.  Obviously, 3-bit
fields must be masked with 7 and 4-bit with 15.

Have fun,

John Beresford, MCSD



Fri, 12 Jan 2001 03:00:00 GMT  
 Could Someone Translate This Code To VB?
Marc,
This is a tough one.
This code is not only in C, but in old-style C, with parameter
declarations inside the function!
Strange.

My comments throughout the code..

Quote:

> Hello
> I have been looking at some code to read MP3 files but I can't figure out how
> to read the bitrate and a few other things.  Here is the original source code
> in C.  I don't do C and am pretty new at VB so any help would be appreciated.

> /************ Layer I, Layer II & Layer III ******************/

The following 3 lines translate to the VB code:

Sub decode_info(ByRef bs as Bit_stream_struc, ByRef fr_ps as
frame_params)

Quote:
> void decode_info(bs, fr_ps)
> Bit_stream_struc *bs;
> frame_params *fr_ps;
> {

The following shows a "C" feature which VB does not have.
The variable "hdr" is a pointer to a "layer". VB does not really support
pointers in this way.
What this is doing is getting a reference to the "header" structure
within the fr_ps structure.
In VB, you couldn't do it like this, so you would set the members of the
structure directly (see below)

Quote:
>     layer *hdr = fr_ps->header;

The rest of the code sets members of the "header" structure.

I would guess that the code is getting one or two bits at a time from
the bitstream "bs".
The bitstream probably points to the MP3 file. Now, in that MP3 file,
there is a header
which describes some characteristics of the data to follow. This code is
reading that header
one or more bits at a time and filling the hdr structure with the
information. I would guess
that the get1bit and getbits functions return the (1 or 2 byte) value of
the specified number
of bits from the stream. So getbits(bs,4) would return the value of the
next 4 bits in the
stream.

In VB you would reference the structure directly like this:

fr_ps.header.version = get1bit(bs)

Quote:
>     hdr->version = get1bit(bs);
>     hdr->lay = 4-getbits(bs,2);
>     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
>     hdr->bitrate_index = getbits(bs,4);
>     hdr->sampling_frequency = getbits(bs,2);
>     hdr->padding = get1bit(bs);
>     hdr->extension = get1bit(bs);
>     hdr->mode = getbits(bs,2);
>     hdr->mode_ext = getbits(bs,2);
>     hdr->copyright = get1bit(bs);
>     hdr->original = get1bit(bs);
>     hdr->emphasis = getbits(bs,2);
> }

<snip>

In all honesty, this is probably a bit too much to tackle if you are a
novice programmer.
Do you know any other languages? Pascal, Java, QuickBasic?
You may not want to hear this, but C is better for this application. It
CAN be done in VB,
but you would have to take a completely different approach to tasks such
as this.
C just requires a little bit different approach than VB. There are a few
core concepts which
are difficult at first (pointers in particular), but all in all, it's
not too {*filter*}ce you
get past the initial difficulties.

Feel free to email me with questions or comments. I am busy, so I cannot
guarantee
a reply. I'll do my best, though.

Good Luck,
Dave

(remove x's from email address)

--
I am a resident of Washington state. In Washington, Spam
is subject to a $500 or greater penalty. You are warned.



Sat, 13 Jan 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Could someone translate this C++ code in VB code ?

2. how to translate c# code to VB.net?

3. Translating PLAY in QuickBasic to VB-code

4. Anybody can translate this code from C to Basic or Vb

5. Translate VC code to vb

6. Translating VC++ code to VB.

7. Translating FORTRAN code into VBA code

8. Someone convert code to VB.net?

9. Someone to write VB 4.0 wav code

10. Looking for some Visual Basic code if it already exist, or someone to write this code

11. Translating Code from 2000 to 2002

12. Translate WordBasic AppShow in a real VBA-Code

 

 
Powered by phpBB® Forum Software