;GetCDDB_CDFuncs.bb2

;-----------------------------------------------------------
; GetCDDB_CDFuncs V0.06b by Roger Light (c)1999,2000
; Calculates the CDDB discid of the current CD.
;-----------------------------------------------------------

XINCLUDE MyCDCode.bb2
XINCLUDE "GetCDDB_Consts.bb2"
XINCLUDE "GetCDDB_MiscFuncs.bb2"

Function.s ComputeCDDBDiscId{}

  SHARED TOClength
  SHARED TOCaddr()
  SHARED cdtoc()
  SHARED t.l

  Dim trackpos.l(100) ; position of tracks, in frame format
;  Dim cdtoc.toc(100)  ; position of tracks, in MSF format

  ;calculate position of tracks
  For i.l=0 To TOClength
    trackpos(i)=TOCaddr(i)+150
    cdtoc(i)\m=trackpos(i)/4500
    trackpos(i)-cdtoc(i)\m*4500

    cdtoc(i)\s=trackpos(i)/75
    trackpos(i)-cdtoc(i)\s*75

    cdtoc(i)\f=trackpos(i)
  Next


  ; below is the code taken from the CDDB site and converted to Blitz
  i.l = 0
  n.l = 0
  While i < TOClength

    ;// cddb_sum
    cddb_sum.l=0;
    j.l=(cdtoc(i)\m * 60) + cdtoc(i)\s;
    While j.l > 0
      cddb_sum = cddb_sum + (j MOD 10);
      j = j / 10;
    Wend

    n = n + cddb_sum;
    i+1
  Wend

;  // compute total CD Length in seconds
  t.l = ((cdtoc(TOClength)\m * 60) + cdtoc(TOClength)\s) - ((cdtoc(0)\m * 60) + cdtoc(0)\s);

;  // compute disc-id
  dwDiscId.l = ((n MOD 255) * 16777216 OR t*256 OR TOClength);

  Function Return Hex$(dwDiscId.l)
End Function

;------------------------------------------------------------
Function.s GetCurrentId{}
  SHARED scsidev$
  SHARED scsiid
  SHARED TOCCDID$

  scsiid=GTGetInteger(0,#_UNIT_INTEGER)
  scsidev$=GTGetString(0,#_DEVICE_STRING)
  If NOT Len(scsidev$)
	Function Return "0"
  EndIf

  If InitCDDA{}
    If CDDAReadContents{}
;      ErrorReq{"Unable to read CD contents"}
    Else
      ;TOCCDID$ now holds the current CDID
      currentid$=TOCCDID$
    EndIf
    EndingCDDA{}
  Else
;   ErrorReq{"Unable to open CD"}
  EndIf

  Function Return currentid$
End Function

