Author Topic: Please can you translate this C code to normal language for me?  (Read 3559 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Hi.

This code is MP3 head validity control. MP3 head is 4 byte (32 bites) long. This C program is controling, if this 4 bytes long record is correct. I do not understandt very well to C language. Can you write here, which bite can be just 1 and which not???

Code: [Select]
int head_check(unsigned long head)
{
   if ((head & 0xffe00000) != 0xffe00000) return FALSE;
   if ((head & 0xffff0000) == 0xffff0000) return FALSE;
   if(!((head>>17)&3)) return FALSE;
   if( ((head>>12)&0xf) == 0xf) return FALSE;
   if( ((head>>10)&0x3) == 0x3 ) return FALSE;
   return TRUE;
}

Thank for help.

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #1 on: July 06, 2020, 05:19:55 am »
Hi petr

What is it youre trying to access in the .mp3? ID3 tags or something else?

You can just use the code you posted if you save it in a .h file or you can continue working on the little bit i have converted for you so you can see (how i think) it should be done, i might be wrong though!

Unseen

Code: QB64: [Select]
  1.  
  2.  
  3.  
  4.  FUNCTION head_check (Head AS _UNSIGNED LONG)
  5.  
  6.  
  7.  
  8.  
  9. FUNCTION HeadCheck (Head AS _UNSIGNED LONG)
  10.  
  11.  
  12.  IF ((Head AND VAL(HEX$(ffe00000)) <> VAL(HEX$((ffe00000))))) THEN
  13.   HeadCheck = 0 '// FALSE
  14.  ELSEIF ((Head AND VAL(HEX$(ffff0000)) = VAL(HEX$((ffff0000))))) THEN
  15.   HeadCheck = 0 '// FALSE
  16.  
  17.  
« Last Edit: July 06, 2020, 05:27:19 am by Unseen Machine »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #2 on: July 06, 2020, 05:47:17 am »
Hi,
thank you i will try it. What do I want to read from MP3? Music length, biterate, number of channels. I can already read ID3 (both the old version at the end of the file and the new version at the beginning). I want to go this direct way, because it takes an awful long time to load music just to get the length of the sound with SNDOPEN.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #3 on: July 06, 2020, 09:27:12 am »
FYI, here is a literal (not necessarily the most efficient or contextually correct) word for word translation into QB64

Code: QB64: [Select]
  1. FUNCTION head_check (head AS _UNSIGNED _INTEGER64)
  2.     IF (head AND &HFFE00000) <> &HFFE00000 THEN
  3.         head_check = 0
  4.         EXIT FUNCTION
  5.     END IF
  6.     IF (head AND &HFFFF0000) = &HFFFF0000 THEN
  7.         head_check = 0
  8.         EXIT FUNCTION
  9.     END IF
  10.     IF NOT ((head \ (2 ^ 17)) AND 3) THEN
  11.         head_check = 0
  12.         EXIT FUNCTION
  13.     END IF
  14.     IF ((head \ (2 ^ 12)) AND &HF) = &HF THEN
  15.         head_check = 0
  16.         EXIT FUNCTION
  17.     END IF
  18.     IF ((head \ (2 ^ 10)) AND &H3) = &H3 THEN
  19.         head_check = 0
  20.         EXIT FUNCTION
  21.     END IF
  22.  
  23.     head_check = -1
  24.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Please can you translate this C code to normal language for me?
« Reply #4 on: July 06, 2020, 10:18:56 am »
To break it down, piece by piece to help you understand what it's doing:

if ((head & 0xffe00000) != 0xffe00000) return FALSE;

In the above, "&" would translate to QB64 "AND".  The "0x" is basically the same as how we use "&H".   The "!" Basically translates to "NOT"...   FALSE is 0.

IF ((head AND &HFFE00000) <> &HFFE00000 THEN return 0.  (And in QB64, we return a value in a function by assigning that function name that value and then exiting the function.)


   if(!((head>>17)&3)) return FALSE;

Now, the above follows the same rules as before, with one new set of instructions to translate.  head>>17 means "bit shift the value of head to the right 17 places".    In QB64, we do these bit shifts with the new_SHL and _SHR keywords Cobalt added to the language for us.  https://www.qb64.org/wiki/SHR

IF ( NOT ((_SHR(head, 17) AND 3)) THEN return 0



Nothing too complex in these to translate, and they make an excellent tool to showcase how similar the languages can be, once you understand the basic differences in syntax.

& is shorthand for AND
! is shorthand for NOT, and != is the same as <> (not equal)
0x is equivalent to &H
>> is equivalent to _SHR
<< is equivalent to _SHL

With the above little guidelines, I don't think anyone would have much trouble translating those statements for you.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #5 on: July 06, 2020, 12:01:41 pm »
Edit : I forgot to say, i kinda messed up using Hex$ there eh! Show s that to much red wine can do things to a mans brain!

Nice one Steve! Thats a very thorough description, i think you should extend it to a proper tutorial, i never even new we could bit shift in QB64!

Unseen
« Last Edit: July 06, 2020, 12:22:53 pm by Unseen Machine »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #6 on: July 06, 2020, 01:44:32 pm »
Hey Guys
you're wonderful!!!
thanks to
Petr for the question
Unseen and Steve to translation in QB64
_Vince for literal translation that is great and he, with its translation, has opened a box of my dusted memory! _SHR (n) = \ 2^n (otherwise dividing by power of 2) and _SHL (n) = * 2^n (otherwise multipling by power of 2).
Thanks only to your apport you give energy to my mind!

PS @_vince
sorry to MOD your code  to beautify it:
I cancel EXIT FUNCTION because it remembers to me the BREAK of SWITCH
while if we put the true result at first line of Function as default we get this:
Code: QB64: [Select]
  1. FUNCTION head_check (head AS _UNSIGNED _INTEGER64)
  2.     head_check = -1
  3.     IF (head AND &HFFE00000) <> &HFFE00000 THEN   head_check = 0
  4.     IF (head AND &HFFFF0000) = &HFFFF0000 THEN     head_check = 0
  5.     IF NOT ((head \ (2 ^ 17)) AND 3) THEN   head_check = 0
  6.     IF ((head \ (2 ^ 12)) AND &HF) = &HF THEN   head_check = 0
  7.     IF ((head \ (2 ^ 10)) AND &H3) = &H3 THEN  head_check = 0
  8.  
Programming isn't difficult, only it's  consuming time and coffee

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #7 on: July 06, 2020, 01:50:51 pm »
Quote
sorry to MOD your code  to beautify it:

Heres your mod in a single if block (might be faster to check this way i dunno though) :

Code: QB64: [Select]
  1. FUNCTION head_check (head AS _UNSIGNED _INTEGER64)
  2.  IF ((head AND &HFFE00000) <> &HFFE00000) OR ((head AND &HFFFF0000) = &HFFFF0000) OR NOT ((head \ (2 ^ 17)) AND 3) OR (((head \ (2 ^ 12)) AND &HF) = &HF) OR (((head \ (2 ^ 10)) AND &H3) = &H3) THEN head_check = 0 ELSE head_check = -1

Unseen

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #8 on: July 06, 2020, 04:29:28 pm »
Thank you all for your help! :)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Please can you translate this C code to normal language for me?
« Reply #9 on: July 07, 2020, 05:19:42 am »
@Unseen Machine
Cool your synthesis!
Programming isn't difficult, only it's  consuming time and coffee