Attribute VB_Name = "lay_ctSSLayer"
' Copyright (C) 2002-2006 Jan Vorel
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues

Option Explicit
Option Base 0

Public Type QPSongInfoStruct
  FormatOK As Long ' Boolean
  Format_Frequency As Long
  Format_BitRate As Long
  Format_Channels As Long
  Format_VBR As Long ' Boolean
  Format_QualityMulti As Single
  TagOK As Long 'Boolean
  Tag_Year As Long
  Tag_TrackID As Long
  Tag_Artist As String
  Tag_Title As String
  Tag_Album As String
  End Type

Private Type ShortType
  Tag_Artist As Long
  Tag_Title As Long
  Tag_Album As Long
  End Type

#Const ctSSLayer_FlagA = 2
  '0=ansi
  '1=partialoem (filename only cause not displayed)...,
  '2=tags (for display only)
  'F = alloem

Private Declare Sub RtlMoveMemory Lib "kernel32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private hModul As Long
Private ctSSLayer_CanUseItemInt As Long
Private ctSSLayer_GetDeviceNameInt As Long
Private ctSSLayer_GetInfoStringInt As Long
Private ctSSLayer_GetPositionInt As Long
Private ctSSLayer_GetSpectrumInt As Long
Private ctSSLayer_LoadSongInt As Long
Private ctSSLayer_LoadSongAbortInt As Long
Private ctSSLayer_LoadSongPlayInt As Long
Private ctSSLayer_MusicGetPatternInt As Long
Private ctSSLayer_MusicGetPatternsInt As Long
Private ctSSLayer_MusicHasFinishedInt As Long
Private ctSSLayer_MusicTimeInt As Long
Private ctSSLayer_ReadSongInfoInt As Long
Private ctSSLayer_SeekSongInt As Long
Private ctSSLayer_SetPauseInt As Long
Private ctSSLayer_SetVolumeInt As Long
Private ctSSLayer_SongLengthInt As Long
Private ctSSLayer_UnloadSongInt As Long

'private h

Public Function ctSSLayer_LoadVB(ByVal OptionalLayerVersionID As Long, ParamArray CustomDLLFilenames()) As Long
  Dim xLong1 As Long
  '1=ok/versionid, -1=no dll, 0=incompatible
  'Stop
  'ctSSLayer_LoadVB = 0&
  'Exit Function
  
  
  For xLong1 = 0& To UBound(CustomDLLFilenames())
    'If Not Len(CustomDLLName) = 0& Then
      hModul = LoadLibraryA(CStr(CustomDLLFilenames(xLong1)))
      If Not hModul = 0& Then Exit For
    '  End If
    Next
  'If hModul = 0& Then
  '  hModul = LoadLibraryA("ctSSLay.dll")
  '  End If
'  Stop
  
  
  If hModul = 0& Then
    ctSSLayer_LoadVB = -1&
    Else
    xLong1 = GetProcAddress(hModul, "ctSSLayer_Version")
    If xLong1 = 0& Then
      Call FreeLibrary(hModul)
      ctSSLayer_LoadVB = 0&
      Else
      ctSSLayer_LoadVB = CallFunctionFast(xLong1, OptionalLayerVersionID)
      'ctSSLayer_Version = CallFunction(hModul, "ctSSLayer_Version", ByVal VarPtr(OptionalLayerVersionID))
      If ctSSLayer_LoadVB = 0& Then
        Call FreeLibrary(hModul)
        Else
        'ctSSLayer_InitForVB = 0&
        ctSSLayer_CanUseItemInt = GetProcAddress(hModul, "ctSSLayer_CanUseItem")
        #If (ctSSLayer_FlagA And &HF&) < &HF& Then
          ctSSLayer_GetDeviceNameInt = GetProcAddress(hModul, "ctSSLayer_GetDeviceNameA")
          #Else
          ctSSLayer_GetDeviceNameInt = GetProcAddress(hModul, "ctSSLayer_GetDeviceNameW")
          #End If
        #If (ctSSLayer_FlagA And &HF&) < &HF& Then
          ctSSLayer_GetInfoStringInt = GetProcAddress(hModul, "ctSSLayer_GetInfoStringA")
          #Else
          ctSSLayer_GetInfoStringInt = GetProcAddress(hModul, "ctSSLayer_GetInfoStringW")
          #End If
        ctSSLayer_GetPositionInt = GetProcAddress(hModul, "ctSSLayer_GetPosition")
        ctSSLayer_GetSpectrumInt = GetProcAddress(hModul, "ctSSLayer_GetSpectrum")
        #If (ctSSLayer_FlagA And &HF&) < &HF& Then
           ctSSLayer_LoadSongInt = GetProcAddress(hModul, "ctSSLayer_LoadSongA")
          #Else
           ctSSLayer_LoadSongInt = GetProcAddress(hModul, "ctSSLayer_LoadSongW")
          #End If
        ctSSLayer_LoadSongAbortInt = GetProcAddress(hModul, "ctSSLayer_LoadSongAbort")
        ctSSLayer_LoadSongPlayInt = GetProcAddress(hModul, "ctSSLayer_LoadSongPlay")
        ctSSLayer_MusicGetPatternInt = GetProcAddress(hModul, "ctSSLayer_MusicGetPattern")
        ctSSLayer_MusicGetPatternsInt = GetProcAddress(hModul, "ctSSLayer_MusicGetPatterns")
        ctSSLayer_MusicHasFinishedInt = GetProcAddress(hModul, "ctSSLayer_MusicHasFinished")
        ctSSLayer_MusicTimeInt = GetProcAddress(hModul, "ctSSLayer_MusicTime")
        #If (ctSSLayer_FlagA And &H2&) = &H0& Then
          ctSSLayer_ReadSongInfoInt = GetProcAddress(hModul, "ctSSLayer_ReadSongInfoA")
          #Else
          ctSSLayer_ReadSongInfoInt = GetProcAddress(hModul, "ctSSLayer_ReadSongInfoW")
          #End If
        ctSSLayer_SeekSongInt = GetProcAddress(hModul, "ctSSLayer_SeekSong")
        ctSSLayer_SetPauseInt = GetProcAddress(hModul, "ctSSLayer_SetPause")
        ctSSLayer_SetVolumeInt = GetProcAddress(hModul, "ctSSLayer_SetVolume")
        ctSSLayer_SongLengthInt = GetProcAddress(hModul, "ctSSLayer_SongLength")
        ctSSLayer_UnloadSongInt = GetProcAddress(hModul, "ctSSLayer_UnloadSong")
        End If
      End If
    End If
  
  End Function

Public Sub ctSSLayer_UnLoadVB()
  If Not hModul = 0& Then FreeLibrary (hModul)
  End Sub





Public Function ctSSLayer_CanUseItem(ByVal Index As Long) As Long
  ctSSLayer_CanUseItem = CallFunctionFast(ctSSLayer_CanUseItemInt, Index)
  End Function

Public Sub ctSSLayer_Close(ByVal Fast As Long)
  Call CallFunction(hModul, "ctSSLayer_Close", Fast)
  End Sub

#If (ctSSLayer_FlagA And &HF&) < &HF& Then
  Public Function ctSSLayer_GetDeviceNameA(ByVal DeviceID As Long, ByRef Flags As Long) As String
    ctSSLayer_GetDeviceNameA = GetApiCharA(CallFunctionFast(ctSSLayer_GetDeviceNameInt, DeviceID, VarPtr(Flags)))
    End Function
  #Else
  Public Function ctSSLayer_GetDeviceNameW(ByVal DeviceID As Long, ByRef Flags As Long) As String
    ctSSLayer_GetDeviceNameW = GetApiCharA(CallFunctionFast(ctSSLayer_GetDeviceNameInt, DeviceID, VarPtr(Flags)))
    End Function
  #End If

#If (ctSSLayer_FlagA And &HF&) < &HF& Then
  Public Function ctSSLayer_GetInfoStringA(ByVal Index As Long) As String
    ctSSLayer_GetInfoStringA = GetApiCharA(CallFunctionFast(ctSSLayer_GetInfoStringInt, Index))
    End Function
  #Else
  Public Function ctSSLayer_GetInfoStringW(ByVal Index As Long) As String
    ctSSLayer_GetInfoStringW = GetApiCharW(CallFunctionFast(ctSSLayer_GetInfoStringInt, Index))
    End Function
  #End If

Public Function ctSSLayer_GetPosition(ByVal SongPointer As Long) As Long
  ctSSLayer_GetPosition = CallFunctionFast(ctSSLayer_GetPositionInt, SongPointer)
  End Function

'public Sub ctSSLayer_GetSpectrum (xSingleA1Array As Any, ByVal SongPointer As Long)
Public Sub ctSSLayer_GetSpectrum(ByRef xSingleA1Array As Single, ByVal SongPointer As Long)
  'Stop
  Call CallFunctionFast(ctSSLayer_GetSpectrumInt, VarPtr(xSingleA1Array), SongPointer)
  'Stop
  End Sub
'  Public Sub ctSSLayer_GetSpectrum(ByRef xSingleA1Array As Single, ByVal SongPointer As Long)

Public Function ctSSLayer_Init(ByVal OutputDevice As Long, ByVal MainHWnd As Long, ByVal UseCDDA As Long, ByVal UseJitterCorrection As Long) As Long
  ctSSLayer_Init = CallFunction(hModul, "ctSSLayer_Init", OutputDevice, MainHWnd, UseCDDA, UseJitterCorrection)
  End Function

#If (ctSSLayer_FlagA And &HF&) < &HF& Then
  Public Function ctSSLayer_LoadSongA(ByVal mode As Long, ByVal FileName As String, ByVal CheckAccurate As Long) As Long
'    Stop
    'Dim xByteA1() As Byte
    ReDim xByteA1(Len(FileName)) As Byte
    Call RtlMoveMemory(xByteA1(0), ByVal FileName, Len(FileName))
    ctSSLayer_LoadSongA = CallFunctionFast(ctSSLayer_LoadSongInt, mode, VarPtr(xByteA1(0)), CheckAccurate)
'    Stop
    End Function
  #Else
  Public Function ctSSLayer_LoadSongW(ByVal mode As Long, ByVal FileName As String, ByVal CheckAccurate As Long) As Long
    ctSSLayer_LoadSongW = CallFunctionFast(ctSSLayer_LoadSongInt, mode, StrPtr(FileName), CheckAccurate)
    End Function
  #End If

Public Sub ctSSLayer_LoadSongAbort(ByVal SongPointer As Long)
  Call CallFunctionFast(ctSSLayer_LoadSongAbortInt, SongPointer)
  End Sub

Public Function ctSSLayer_LoadSongPlay(ByVal SongPointer As Long, ByVal Volume As Long, ByVal LoopSong As Long, ByVal Panning As Long, ByVal Paused As Long) As Long
  ctSSLayer_LoadSongPlay = CallFunctionFast(ctSSLayer_LoadSongPlayInt, SongPointer, Volume, LoopSong, Panning, Paused)
  End Function

Public Function ctSSLayer_MusicGetPattern(ByVal SongPointer As Long) As Long
ctSSLayer_MusicGetPattern = CallFunctionFast(ctSSLayer_MusicGetPatternInt, SongPointer)
  End Function

Public Function ctSSLayer_MusicGetPatterns(ByVal SongPointer As Long) As Long
ctSSLayer_MusicGetPatterns = CallFunctionFast(ctSSLayer_MusicGetPatternsInt, SongPointer)
  End Function

Public Function ctSSLayer_MusicHasFinished(ByVal SongPointer As Long) As Long
ctSSLayer_MusicHasFinished = CallFunctionFast(ctSSLayer_MusicHasFinishedInt, SongPointer)
  End Function

Public Function ctSSLayer_MusicTime(ByVal SongPointer As Long) As Long
ctSSLayer_MusicTime = CallFunctionFast(ctSSLayer_MusicTimeInt, SongPointer)
  End Function

Public Function ctSSLayer_PreInit(ByVal EnforceDevice As Long, ByVal MainHWnd As Long) As Long
  ctSSLayer_PreInit = CallFunction(hModul, "ctSSLayer_PreInit", EnforceDevice, MainHWnd)
  End Function

#If (ctSSLayer_FlagA And &H2&) = &H0& Then
  Public Function ctSSLayer_ReadSongInfoA(ByVal SongPointer As Long, xSongInfo As QPSongInfoStruct)
    'Dim xChar1 As String
    Dim xType1 As ShortType
    Dim xByteA1(47) As Byte  '35) '9 * 4 - 1)
    
    ctSSLayer_ReadSongInfoA = CallFunctionFast(ctSSLayer_ReadSongInfoInt, SongPointer, xByteA1(0))
    Call RtlMoveMemory(xSongInfo, xByteA1(0), 36)
    Call RtlMoveMemory(xType1, xByteA1(36), 12)
    'Stop
    xSongInfo.Tag_Artist = GetApiCharA(xType1.Tag_Artist)
    xSongInfo.Tag_Title = GetApiCharA(xType1.Tag_Title)
    xSongInfo.Tag_Album = GetApiCharA(xType1.Tag_Album)
    End Function
  #Else
  Public Function ctSSLayer_ReadSongInfoW(ByVal SongPointer As Long, xSongInfo As QPSongInfoStruct)
    'Dim xChar1 As String
    Dim xType1 As ShortType
    Dim xByteA1(47) As Byte  '35) '9 * 4 - 1)
    
    'xChar1 = String(Len(xSongInfo), vbNullChar)
 '   Stop
    ctSSLayer_ReadSongInfoW = CallFunctionFast(ctSSLayer_ReadSongInfoInt, SongPointer, VarPtr(xByteA1(0)))
'    Stop
    Call RtlMoveMemory(xSongInfo, xByteA1(0), 36)
    Call RtlMoveMemory(xType1, xByteA1(36), 12)
    'Stop
    xSongInfo.Tag_Artist = GetApiCharW(xType1.Tag_Artist)
    xSongInfo.Tag_Title = GetApiCharW(xType1.Tag_Title)
    xSongInfo.Tag_Album = GetApiCharW(xType1.Tag_Album)
    End Function
  #End If

Public Function ctSSLayer_SeekSong(ByVal SongPointer As Long, ByVal SeekPosition As Long) As Long
  ctSSLayer_SeekSong = CallFunctionFast(ctSSLayer_SeekSongInt, SongPointer, SeekPosition)
  End Function

Public Sub ctSSLayer_SetPause(ByVal SongPointer As Long, ByVal Pause As Long)
  Call CallFunctionFast(ctSSLayer_SetPauseInt, SongPointer, Pause)
  End Sub

Public Sub ctSSLayer_SetSpectrum(ByVal Status As Long)
  Call CallFunction(hModul, "ctSSLayer_SetSpectrum", Status)
  End Sub

Public Sub ctSSLayer_SetVolume(ByVal SongPointer As Long, ByVal Volume As Long)
  'Debug.Print "Vol: ", Volume
  Call CallFunctionFast(ctSSLayer_SetVolumeInt, SongPointer, Volume)
  End Sub

Public Function ctSSLayer_SongLength(ByVal SongPointer As Long) As Long
  ctSSLayer_SongLength = CallFunctionFast(ctSSLayer_SongLengthInt, SongPointer)
  End Function

Public Sub ctSSLayer_UnloadSong(ByVal SongPointer As Long)
  Call CallFunctionFast(ctSSLayer_UnloadSongInt, SongPointer)
  End Sub

Public Sub ctSSLayer_UpdateCDDA(ByVal UseCDDA As Long, ByVal UseJitterCorrection As Long)
  Call CallFunction(hModul, "ctSSLayer_UpdateCDDA", UseCDDA, UseJitterCorrection)
  End Sub

Public Sub ctSSLayer_UpdateLooping(ByVal SongPointer As Long, ByVal LoopSong As Long)
  Call CallFunction(hModul, "ctSSLayer_UpdateLooping", SongPointer, LoopSong)
  End Sub

Public Sub ctSSLayer_UpdatePanning(ByVal SongPointer As Long, ByVal Panning As Long)
  Call CallFunction(hModul, "ctSSLayer_UpdatePanning", SongPointer, Panning)
  End Sub

'Public Function ctSSLayer_Version(ByVal OptionalLayerVersionID As Long) As Long
'  ctSSLayer_Version = CallFunction(hModul, "ctSSLayer_Version", OptionalLayerVersionID)
'  End Function

