EnableExplicit
EnableASM

Procedure.l LineSpec_Startup(Flags.l)
  ProcedureReturn CallFunctionFast(?LineSpec_Startup_ASM, Flags)
LineSpec_Startup_ASM:
  !cmp byte [HasPreCalced], 0
  !jne NoPreCalculation
  !inc byte [HasPreCalced]
  !call PreCalcCode

!NoPreCalculation:

FASM LineSpec_Startup.fasm
  EndProcedure
  
Procedure.l LineSpec_GetSpectrum(*xSingleArray1, ArraySize.l, Stereo.l, Flags.l, *Boost)
ProcedureReturn CallFunctionFast(?LineSpec_GetSpectrum_ASM, *xSingleArray1, ArraySize.l, Stereo.l, Flags.l, *Boost)
LineSpec_GetSpectrum_ASM:
FASM LineSpec_GetSpectrum.fasm
FASM LineSpec_StagingL.fasm
FASM LineSpec_StagingR.fasm
FASM LineSpec_GrabThread_DS.fasm
FASM LineSpec_GrabThread_WI.fasm
EndProcedure
  
Procedure LineSpec_Shutdown()
ProcedureReturn CallFunctionFast(?LineSpec_Shutdown_ASM)
LineSpec_Shutdown_ASM:
FASM LineSpec_Shutdown.fasm
EndProcedure

Procedure.l LineSpec_PBLoader()
  Define.l l1 = ?BeginLineSpecAPI
  Define.l l2 = ?EndLineSpecAPI
  Define.l l3
  Define.l l4
  Define.s c1
  Define.s c2
  While l1 < l2
    If Not PeekB(l1) = 0
      c1 = PeekS(l1)
      l3 = Len(c1)
      l4 = l3
      If l3 > 3
        If Left(c1, 4) = "wave"
          c2 = "winmm"
          c1 = "#" + c1
          Else
          Select Left(c1, 1)
            Case "k": c2 = "kernel32"
            Case "g": c2 = "gdi32"
;            Case "u": c2 = "user32"
;            Case "s": c2 = "shell32"
            Default:
              l3 = 0
            EndSelect
          EndIf
        If l3
          c1 = Mid(c1, 2, Len(c1) - 1)
          c2 + ".dll"
          l3 = GetModuleHandle_(c2)
          If l3 = 0
            l3 = LoadLibrary_(c2)
            EndIf
          If l3
            l3 = GetProcAddress_(l3, c1)
            If l3
              PokeL(l1, l3)
              Debug "OK: " + c1
              Else
              Debug "Cannot patch (function not found in dll: : " + c1 + ")"
              ProcedureReturn 0
              EndIf
            Else
            Debug "Cannot patch (dll loading failure: " + c1 + ")"
            ProcedureReturn 0
            EndIf
          Else
          Debug "Cannot patch (dll unknown: " + c1 + ")"
          ProcedureReturn 0
          EndIf
        EndIf
      l1 + l4
      EndIf
    l1 + 1
    Wend
  ProcedureReturn 1
  EndProcedure
  
DataSection
FASM LineSpec_Data.fasm
BeginLineSpecAPI:
!kCloseHandle db 'kCloseHandle', 0
!kCreateThread db 'kCreateThread', 0
!kDeleteCriticalSection db 'kDeleteCriticalSection', 0
!kEnterCriticalSection db 'kEnterCriticalSection', 0
!kFreeLibrary db 'kFreeLibrary', 0
!kGetProcAddress db 'kGetProcAddress', 0
!kGetTickCount db 'kGetTickCount', 0
!kInitializeCriticalSection db 'kInitializeCriticalSection', 0
!kLeaveCriticalSection db 'kLeaveCriticalSection', 0
!kLoadLibraryA db 'kLoadLibraryA', 0
!kSleep db 'kSleep', 0
!kWaitForSingleObject db 'kWaitForSingleObject', 0
!waveInAddBuffer db 'waveInAddBuffer', 0
!waveInClose db 'waveInClose', 0
!waveInOpen db 'waveInOpen', 0
!waveInPrepareHeader db 'waveInPrepareHeader', 0
!waveInReset db 'waveInReset', 0
!waveInStart db 'waveInStart', 0
!waveInStop db 'waveInStop', 0
!waveInUnprepareHeader db 'waveInUnprepareHeader', 0
EndLineSpecAPI:
EndDataSection
DisableASM

; IDE Options = PureBasic 4.51 (Windows - x86)
; CursorPosition = 6
; Folding = -
; EnableXP