QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Petr on July 06, 2020, 04:45:08 am
-
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???
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.
-
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
HeadCheck = 0 '// FALSE
HeadCheck = 0 '// FALSE
-
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.
-
FYI, here is a literal (not necessarily the most efficient or contextually correct) word for word translation into QB64
head_check = 0
head_check = 0
head_check = 0
head_check = 0
head_check = 0
head_check = -1
-
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. ;)
-
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
-
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:
head_check = -1
IF (head
AND &HFFE00000) <> &HFFE00000 THEN head_check
= 0 IF (head
AND &HFFFF0000) = &HFFFF0000 THEN head_check
= 0 IF ((head \
(2 ^ 12)) AND &HF) = &HF THEN head_check
= 0 IF ((head \
(2 ^ 10)) AND &H3) = &H3 THEN head_check
= 0
-
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) :
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
-
Thank you all for your help! :)
-
@Unseen Machine
Cool your synthesis!