Attribute VB_Name = "lay_ctSSLayer"
' Copyright (C) 2002-2007 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

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
  For xLong1 = 0& To UBound(CustomDLLFilenames())
    hModul = LoadLibrary(CStr(CustomDLLFilenames(xLong1)))
    If Not hModul = 0& Then Exit For
    Next
  
  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)
      If ctSSLayer_LoadVB = 0& Then
        Call FreeLibrary(hModul)
        Else
        ctSSLayer_CanUseItemInt = GetProcAddress(hModul, "ctSSLayer_CanUseItem")
        If IsUnicode Then
          ctSSLayer_GetDeviceNameInt = GetProcAddress(hModul, "ctSSLayer_GetDeviceNameW")
          Else
          ctSSLayer_GetDeviceNameInt = GetProcAddress(hModul, "ctSSLayer_GetDeviceNameA")
          End If
        If IsUnicode Then
          ctSSLayer_GetInfoStringInt = GetProcAddress(hModul, "ctSSLayer_GetInfoStringW")
          Else
          ctSSLayer_GetInfoStringInt = GetProcAddress(hModul, "ctSSLayer_GetInfoStringA")
          End If
        ctSSLayer_GetPositionInt = GetProcAddress(hModul, "ctSSLayer_GetPosition")
        ctSSLayer_GetSpectrumInt = GetProcAddress(hModul, "ctSSLayer_GetSpectrum")
        If IsUnicode Then
           ctSSLayer_LoadSongInt = GetProcAddress(hModul, "ctSSLayer_LoadSongW")
          Else
           ctSSLayer_LoadSongInt = GetProcAddress(hModul, "ctSSLayer_LoadSongA")
          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 IsUnicode Then
          ctSSLayer_ReadSongInfoInt = GetProcAddress(hModul, "ctSSLayer_ReadSongInfoW")
          Else
          ctSSLayer_ReadSongInfoInt = GetProcAddress(hModul, "ctSSLayer_ReadSongInfoA")
          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


Public Function ctSSLayer_GetDeviceName(ByVal DeviceID As Long, ByRef Flags As Long) As String
  ctSSLayer_GetDeviceName = PeekS(CallFunctionFast(ctSSLayer_GetDeviceNameInt, DeviceID, VarPtr(Flags)))
  End Function

Public Function ctSSLayer_GetInfoString(ByVal Index As Long) As String
  ctSSLayer_GetInfoString = PeekS(CallFunctionFast(ctSSLayer_GetInfoStringInt, Index))
  End Function

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

Public Sub ctSSLayer_GetSpectrum(ByRef xSingleA1Array As Single, ByVal ArraySize As Long, ByVal SongPointer As Long)
  Call CallFunctionFast(ctSSLayer_GetSpectrumInt, VarPtr(xSingleA1Array), ArraySize, SongPointer)
  End Sub

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
    ReDim xByteA1(Len(Filename)) As Byte
    Call RtlMoveMemory(xByteA1(0), ByVal Filename, Len(Filename))
    ctSSLayer_LoadSongA = CallFunctionFast(ctSSLayer_LoadSongInt, mode, VarPtr(xByteA1(0)), CheckAccurate)
    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

Public Function ctSSLayer_ReadSongInfo(ByVal SongPointer As Long, xSongInfo As QPSongInfoStruct) As Boolean
  'Dim xType1 As ShortType
  'Dim xByteA1(47) As Byte
  Dim xLongA1(11&) As Long
  
  ctSSLayer_ReadSongInfo = Not (CallFunctionFast(ctSSLayer_ReadSongInfoInt, SongPointer, VarPtr(xLongA1(0))) = 0&)
  Call RtlMoveMemory(xSongInfo, ByVal VarPtr(xLongA1(0)), 36&)
  'Call RtlMoveMemory(xType1, xByteA1(36), 12&)
  xSongInfo.Tag_Artist = PeekS(xLongA1(9&)) 'xType1.Tag_Artist)
  xSongInfo.Tag_Title = PeekS(xLongA1(10&)) 'xType1.Tag_Title)
  xSongInfo.Tag_Album = PeekS(xLongA1(11&)) 'xType1.Tag_Album)
  End Function

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)
  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
