fichier -> string
Publié : ven. 06/mars/2009 17:14
Bonjour à tous !
J'essaye de récupérer le contenu d'un fichier mp3 dans une chaîne de caractères, mais il se trouve que dès le Xème caractère, le caractère 0 (nul) est là, et la fonction PeekS() ne veut pas récupérer plus loin. Voici le code :
Pour info, ce code est traduit de Liberty Basic, et je suis sûr qu'il y a même moyen de bien simplifier sans avoir à récupérer tout le contenu du fichier ! Mais je ne m'y connais pas encore assez en PB... (je compte bien apprendre à gérer ce genre de code ^^)
Il permet de récupérer les ID3Tag d'un fichier MP3 (titre, artiste, etc) et d'autres infos, comme l'échantillonage, etc.
Merci de votre aide,
Kris
J'essaye de récupérer le contenu d'un fichier mp3 dans une chaîne de caractères, mais il se trouve que dès le Xème caractère, le caractère 0 (nul) est là, et la fonction PeekS() ne veut pas récupérer plus loin. Voici le code :
Code : Tout sélectionner
Declare.l GetID3Tag(fileName$, drawConsole = 0)
GetID3Tag(OpenFileRequester("", "", "MPEG Layer|*.mp3", 1), 1)
Repeat : Until Inkey()
CloseConsole()
End
Procedure.s dec2bin8(decNum)
While decNum > 0
If decNum And 1
bin$ = "1" + bin$
Else
bin$ = "0" + bin$
EndIf
decNum = Int(decNum / 2)
Wend
bin$ = Right("00000000" + bin$, 8)
ProcedureReturn bin$
EndProcedure
Procedure.s mciSendString(string$)
buffer$=Space(1024)+Chr(0)
r = mciSendString_(string$,buffer$,1028,0)
buffer$=Trim(buffer$)
If r > 0
buffer2$=Space(129)
mciGetErrorString_(r, buffer2$,128)
mciSendString$=buffer2$
Else
mciSendString$=buffer$
EndIf
ProcedureReturn mciSendString$
EndProcedure
Procedure Mod(dividende, diviseur)
ProcedureReturn dividende - Int(dividende / diviseur) * diviseur
EndProcedure
Procedure.s msTohr(DECt.l)
h$=Str(Int(DECt/3600000))
m$=Str(Int((Mod(DECt, 3600000))/60000))
s$=Str(Int(Mod((Mod(DECt, 3600000)),60000)/1000))
If Len(h$)=1 : h$="0"+h$ : EndIf
If Len(m$)=1 : m$="0"+m$ : EndIf
If Len(s$)=1 : s$="0"+s$ : EndIf
msTohr$=h$+":"+m$+"."+s$
ProcedureReturn msTohr$
EndProcedure
Procedure.s getduration(f$)
r$ = mciSendString("open "+Chr(34)+ f$ +Chr(34)+" alias media")
r$ = mciSendString("set media time format milliseconds")
TempsMs = Val(mciSendString("status media length")) ;la varibale TempsMs récupère le nombre de millisecondes écoulées
getduration$ = msTohr(TempsMs) ;Permet de formater (grâce à la fonction msTohr$) le temps en hh:mm:ss
r$ = mciSendString("close media")
ProcedureReturn getduration$
EndProcedure
Procedure.l GetID3Tag(fileName$, drawConsole = 0)
;ProcedureDLL.l GetID3Tag(fileName$, drawConsole = 0)
If drawConsole
OpenConsole()
EndIf
Print("- Infos MP3 v2 -"+Chr(13)+Chr(10))
Print("Je récupère le nom du ficher..."+Chr(13)+Chr(10))
If fileName$=""
Print("Aucun ficher n'a été envoyé !"+Chr(13)+Chr(10))
ProcedureReturn -1
EndIf
;Open file
;========================================
;C'est là !!!
;========================================
Print("J'ouvre le fichier..."+Chr(13)+Chr(10))
OpenFile(0, fileName$)
FileSeek(0, 0)
lenght = Lof(0);length of file
Print("Je lis le fichier..."+Chr(13)+Chr(10))
bin$ = ""
*buffer = AllocateMemory(Lof(0))
ReadData(0, *buffer, Lof(0))
adress = *buffer
bin$ = PeekS(adress, Lof(0), #PB_Unicode)
bin2$ = bin$ ;backup
CloseFile(0)
;============================================
;C'est la fin du code qui me pose problème
;============================================
;Begin search for header
RepeatL:
Print("Je recherche les ID3Tag..."+Chr(13)+Chr(10))
;Search for frame sync - first character should always be ASCII 255
Pos=FindString(bin$,Chr(255), 0)
If Pos=0
temoinu=1
Goto Quit
EndIf
Header$=dec2bin8(Asc(Mid(bin$,Pos,1)));Convert from ASCII to binary
Header$=Header$+dec2bin8(Asc(Mid(bin$,Pos+1,1)))
Header$=Header$+dec2bin8(Asc(Mid(bin$,Pos+2,1)))
Header$=Header$+dec2bin8(Asc(Mid(bin$,Pos+3,1)))
String$=Mid(bin$,Pos,4)
If Left(Header$,11)<>"11111111111"
Goto NotFound
EndIf
;Possible header found!
;Retrieve MPEG Audio Version ID
Print("Je regarde la version du MPEG..."+Chr(13)+Chr(10))
If Mid(Header$,12,2)="00"
Ver$="MPEG Version 2.5"
Ver=2
Goto Skip1
EndIf
If Mid(Header$,12,2)="10"
Ver$="MPEG Version 2"
Ver=2
Goto Skip1
EndIf
If Mid(Header$,12,2)="11"
Ver$="MPEG Version 1"
Ver=1
Goto Skip1
EndIf
Goto NotFound
Skip1:
;Retrieve Layer description
Print("Je regarde la version du Layer..."+Chr(13)+Chr(10))
If Mid(Header$,14,2)="01"
Layer$="Layer III"
Layer=3
Goto Skip2
EndIf
If Mid(Header$,14,2)="10"
Layer$="Layer II"
Layer=2
Goto Skip2
EndIf
If Mid(Header$,14,2)="11"
Layer$="Layer I"
Layer=1
Goto Skip2
EndIf
Goto NotFound
Skip2:
;Protection bit
Print("Je regarde si le fichier est protégé..."+Chr(13)+Chr(10))
If Mid(Header$,16,1)="0"
Protection$="Yes"
EndIf
If Mid(Header$,16,1)="1"
Protection$="No"
EndIf
;BitRate
Print("Je recherche le taux du flux d'information par secondes..."+Chr(13)+Chr(10))
If Mid(Header$,17,4)="0001"
If Ver=1 And Layer=1 : BitRate=32:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=32:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=32:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=32:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=8:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=8:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="0010"
If Ver=1 And Layer=1 : BitRate=64:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=48:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=40:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=48:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=16:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=16:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="0011"
If Ver=1 And Layer=1 : BitRate=96:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=56:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=48:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=56:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=24:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=24:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="0100"
If Ver=1 And Layer=1 : BitRate=128:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=64:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=56:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=64:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=32:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=32:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="0101"
If Ver=1 And Layer=1 : BitRate=160:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=80:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=64:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=80:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=40:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=40:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="0110"
If Ver=1 And Layer=1 : BitRate=192:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=96:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=80:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=96:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=48:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=48:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="0111"
If Ver=1 And Layer=1 : BitRate=224:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=112:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=96:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=112:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=56:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=56:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1000"
If Ver=1 And Layer=1 : BitRate=256:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=128:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=112:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=128:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=64:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=64:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1001"
If Ver=1 And Layer=1 : BitRate=288:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=160:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=128:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=144:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=80:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=80:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1010"
If Ver=1 And Layer=1 : BitRate=320:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=192:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=160:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=160:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=96:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=96:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1011"
If Ver=1 And Layer=1 : BitRate=352:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=224:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=192:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=176:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=112:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=112:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1100"
If Ver=1 And Layer=1 : BitRate=384:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=256:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=224:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=192:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=128:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=128:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1101"
If Ver=1 And Layer=1 : BitRate=416:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=320:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=256:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=224:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=144:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=144:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1110"
If Ver=1 And Layer=1 : BitRate=448:Goto Skip3 : EndIf
If Ver=1 And Layer=2 : BitRate=384:Goto Skip3 : EndIf
If Ver=1 And Layer=3 : BitRate=320:Goto Skip3 : EndIf
If Ver=2 And Layer=1 : BitRate=256:Goto Skip3 : EndIf
If Ver=2 And Layer=2 : BitRate=160:Goto Skip3 : EndIf
If Ver=2 And Layer=3 : BitRate=160:Goto Skip3 : EndIf
Goto Skip3
EndIf
If Mid(Header$,17,4)="1111" : BitRate=0:Goto Skip3:EndIf;Bad BitRate
Goto NotFound
Skip3:
;Sampling rate
Print("Je recherche le taux d'échantillonnage..."+Chr(13)+Chr(10))
If Mid(Header$,21,2)="00"
If Ver$="MPEG Version 1" : SampleRate=44100:Goto Skip4 : EndIf
If Ver$="MPEG Version 2" : SampleRate=22050:Goto Skip4 : EndIf
If Ver$="MPEG Version 2.5" : SampleRate=11025:Goto Skip4 : EndIf
Goto Skip4
EndIf
If Mid(Header$,21,2)="01"
If Ver$="MPEG Version 1" : SampleRate=48000:Goto Skip4 : EndIf
If Ver$="MPEG Version 2" : SampleRate=24000:Goto Skip4 : EndIf
If Ver$="MPEG Version 2.5" : SampleRate=12000:Goto Skip4 : EndIf
SampleRate$="N/A"
Goto Skip4
EndIf
If Mid(Header$,21,2)="10"
If Ver$="MPEG Version 1" : SampleRate=32000:Goto Skip4 : EndIf
If Ver$="MPEG Version 2" : SampleRate=16000:Goto Skip4 : EndIf
If Ver$="MPEG Version 2.5" : SampleRate=8000:Goto Skip4 : EndIf
SampleRate$="N/A"
Goto Skip4
EndIf
Goto NotFound
Skip4:
Print("Je recherche les autres informations audio..."+Chr(13)+Chr(10))
;Padding bit
If Mid(Header$,23,1)="0" : Padding=0 : EndIf
If Mid(Header$,23,1)="1" : Padding=1 : EndIf
;Calculate frame length
If Layer=1 : FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4 : EndIf
If Layer=2 Or Layer=3 : FrameLengthInBytes = (144 * BitRate / SampleRate + Padding) : EndIf
;Channel Mode
Print("Je regarde le nombre de canaux..."+Chr(13)+Chr(10))
If Mid(Header$,25,2)="00" : ChannelMode$="Stereo":Goto Skip5 : EndIf
If Mid(Header$,25,2)="01" : ChannelMode$="Joint stereo":Goto Skip5 : EndIf
If Mid(Header$,25,2)="10" : ChannelMode$="Dual channel":Goto Skip5 : EndIf
If Mid(Header$,25,2)="11" : ChannelMode$="Single channel":Goto Skip5 : EndIf
Goto NotFound
Skip5:
;Copyright
Print("Je regarde s'il y a un Copyright..."+Chr(13)+Chr(10))
If Mid(Header$,29,1)="0" : Copyright$="No" : EndIf
If Mid(Header$,29,1)="1" : Copyright$="Yes" : EndIf
;Orginal
Print("Je regarde si le fichier est l'original..."+Chr(13)+Chr(10))
If Mid(Header$,30,1)="0" : Orginal$="No" : EndIf
If Mid(Header$,30,1)="1" : Orginal$="Yes" : EndIf
Print(fileName$+Chr(13)+Chr(10))
Print("-=-=-=-=-=-=-=-=-=-=-=-"+Chr(13)+Chr(10))
Print(Ver$+" "+Layer$+Chr(13)+Chr(10))
Print("CRC's: "+Protection$+Chr(13)+Chr(10))
Print("KBPS: "+Str(BitRate)+" / KHZ: "+Str(SampleRate)+Chr(13)+Chr(10))
Print("Frame Length: "+Str(FrameLengthInBytes)+Chr(13)+Chr(10))
Print("Channel Mode: "+ChannelMode$+Chr(13)+Chr(10))
Print("Copyright: "+Copyright$+Chr(13)+Chr(10))
Print("Orginal: "+Orginal$+Chr(13)+Chr(10))
Print("Header found at "+Str(FindString(bin2$,String$, 0)-1)+" bytes"+Chr(13)+Chr(10))
Print("Length of music : "+getduration(fileName$)+Chr(13)+Chr(10))
Print(""+Chr(13)+Chr(10))
Print("MPEG Audio Tag ID3v1 info"+Chr(13)+Chr(10))
Print("Title: "+Trim(Mid(String$,4,30))+Chr(13)+Chr(10))
Print("Artist: "+Trim(Mid(String$,34,30))+Chr(13)+Chr(10))
Print("Album: "+Trim(Mid(String$,64,30))+Chr(13)+Chr(10))
Print("Year: "+Trim(Mid(String$,94,4))+Chr(13)+Chr(10))
Print("Comment: "+Trim(Mid(String$,98,30))+Chr(13)+Chr(10))
Quit:
Print("Finalisation..."+Chr(13)+Chr(10))
bin$="":Header$="":Pos=0
If temoinu=1
Print("Erreur dans le fichier : Unable to find frame header !"+Chr(13)+Chr(10))
Else
Print("Fini !"+Chr(13)+Chr(10))
EndIf
ProcedureReturn 0
NotFound:
Print("En-tête du fichier non trouvé !"+Chr(13)+Chr(10))
;Header not found, try again
bin$=Mid(bin$,Pos+1,l)
Header$=""
Goto RepeatL
EndProcedure
; IDE Options = PureBasic 4.30 (Windows - x86)
; ExecutableFormat = Shared Dll
; CursorPosition = 89
; FirstLine = 66
; Folding = --
; EnableAsm
; EnableUnicode
; EnableThread
; CompileSourceDirectory
Il permet de récupérer les ID3Tag d'un fichier MP3 (titre, artiste, etc) et d'autres infos, comme l'échantillonage, etc.
Merci de votre aide,
Kris