Page 1 of 1

Using csound dll in PB

Posted: Sat Mar 28, 2020 5:56 pm
by soren
I am using ProcedureC to access functions of an external DLL (the Csound API) - it's going well, but one function doesn't seem to be called correctly - can anyone see what I'm doing wrong?

This is the documentation for the function: https://csound.com/docs/api/group___r_t ... 7c037ffd74

Code: Select all

Structure CsoundAudioDev
  device_name.c[64]
  device_id.c[64]
  rt_module.c[64]
  max_nchnls.i
  isOutput.i
EndStructure

OpenLibrary(0, "csound64.dll")

PrototypeC.l ftype1()
PrototypeC.i ftype2(*p1, *p2, p3.i)

Global csoundCreate.ftype1          = GetFunction(0, "csoundCreate")
Global csoundGetAudioDevList.ftype2 = GetFunction(0, "csoundGetAudioDevList")

csound = csoundCreate()                     ; works fine

Debug csoundGetAudioDevList(csound, 0, 1)   ; should return number of audio output devices, always shows 0

Dim devlist.CsoundAudioDev(20)

csoundGetAudioDevList(csound, @devlist, 1)

Debug devlist(0)\device_name                ; should return name of first output devise, always returns 0

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sat Mar 28, 2020 6:38 pm
by HeX0R
Shouldn't it be:

Code: Select all

PrototypeC.i ftype1(*hostData = #Null)

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sat Mar 28, 2020 8:03 pm
by infratec
Hm.... also no luck.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

#CSOUND_SUCCESS = 0
#CSOUND_ERROR = -1
#CSOUND_INITIALIZATION = -2
#CSOUND_PERFORMANCE = -3
#CSOUND_MEMORY = -4
#CSOUND_SIGNAL = -5


#CSOUNDINIT_NO_SIGNAL_HANDLER = 1
#CSOUNDINIT_NO_ATEXIT = 2 

Structure CS_AUDIODEVICE Align #PB_Structure_AlignC 
  device_name.a[64]
  device_id.a[64]
  rt_module.a[64]
  max_nchnls.i
  isOutput.i
EndStructure


Structure CS_MIDIDEVICE Align #PB_Structure_AlignC
  device_name.a[64]
  interface_name.a[64]
  device_id.a[64]
  midi_module.a[64]
  isOutput.i
EndStructure


PrototypeC.i Prototype_csoundCreate(*hostData)
PrototypeC.i Prototype_csoundGetAudioDevList(*csound, *list.CS_AUDIODEVICE, isOutput.i)
PrototypeC.i Prototype_csoundGetVersion()

Global csoundCreate.Prototype_csoundCreate
Global csoundGetAudioDevList.Prototype_csoundGetAudioDevList
Global csoundGetVersion.Prototype_csoundGetVersion

Global CSoundLib.i


Define *CSound, n.i, i.i

CSoundLib = OpenLibrary(#PB_Any, "csound64.dll")
If CSoundLib
  
  csoundCreate = GetFunction(CSoundLib, "csoundCreate")
  csoundGetAudioDevList = GetFunction(CSoundLib, "csoundGetAudioDevList")
  csoundGetVersion = GetFunction(CSoundLib, "csoundGetVersion")
  
  
  CompilerIf #PB_Compiler_IsMainFile
    
    Debug "CSound Version " + Str(csoundGetVersion())
    
    *csound = csoundCreate(#Null)
    If *csound
      
      n = csoundGetAudioDevList(*csound, #Null, 1)
      Debug "Output AudioDevices: " + Str(n)
      If n
        Dim devlist.CS_AUDIODEVICE(n)
        
        csoundGetAudioDevList(*csound, @devlist(), 1)
        For i = 0 To n - 1
          Debug PeekS(@devlist(0)\device_name[0])
        Next i
      EndIf
    EndIf
    
    CloseLibrary(CSoundLib)
    
  CompilerEndIf
EndIf

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sat Mar 28, 2020 8:16 pm
by infratec
Is it possible that it needs MIDI devices :?:

Because

csound --help

sounds like this
and

csound.exe --devices=out

shows also 0

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sat Mar 28, 2020 9:04 pm
by soren
@infratec - thanks for trying also - I'm beginning to think it might be a problem with Csound.. when I run "csound.exe --devices=out" on my computer I get a list of all my output devices.

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sat Mar 28, 2020 11:13 pm
by breeze4me
Try this one. It seems to work.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

#CSOUND_SUCCESS = 0
#CSOUND_ERROR = -1
#CSOUND_INITIALIZATION = -2
#CSOUND_PERFORMANCE = -3
#CSOUND_MEMORY = -4
#CSOUND_SIGNAL = -5


#CSOUNDINIT_NO_SIGNAL_HANDLER = 1
#CSOUNDINIT_NO_ATEXIT = 2 

Structure CS_AUDIODEVICE Align #PB_Structure_AlignC 
  device_name.a[64]
  device_id.a[64]
  rt_module.a[64]
  max_nchnls.l
  isOutput.l
EndStructure



PrototypeC.i Prototype_csoundCreate(*hostData)
PrototypeC.i Prototype_csoundDestroy(*hostData)
PrototypeC.i Prototype_csoundGetAudioDevList(*csound, *list.CS_AUDIODEVICE, isOutput.i)
PrototypeC.i Prototype_csoundGetVersion()
PrototypeC.i Prototype_csoundSetRTAudioModule(*csound, sModule.p-utf8)

Global csoundCreate.Prototype_csoundCreate
Global csoundDestroy.Prototype_csoundDestroy
Global csoundGetAudioDevList.Prototype_csoundGetAudioDevList
Global csoundGetVersion.Prototype_csoundGetVersion
Global csoundSetRTAudioModule.Prototype_csoundSetRTAudioModule

Global CSoundLib.i


Define *CSound, n.i, i.i

CSoundLib = OpenLibrary(#PB_Any, "csound64.dll")
If CSoundLib
  
  csoundCreate = GetFunction(CSoundLib, "csoundCreate")
  csoundDestroy = GetFunction(CSoundLib, "csoundDestroy")
  csoundGetAudioDevList = GetFunction(CSoundLib, "csoundGetAudioDevList")
  csoundGetVersion = GetFunction(CSoundLib, "csoundGetVersion")
  csoundSetRTAudioModule = GetFunction(CSoundLib, "csoundSetRTAudioModule")
  
  CompilerIf #PB_Compiler_IsMainFile
    
    Debug "CSound Version " + Str(csoundGetVersion())
    
    *csound = csoundCreate(#Null)
    If *csound
      
      csoundSetRTAudioModule(*csound, "PortAudio")
      
      n = csoundGetAudioDevList(*csound, 0, 1)
      Debug "Output AudioDevices: " + Str(n)
      If n
        Dim devlist.CS_AUDIODEVICE(n)
        
        csoundGetAudioDevList(*csound, @devlist(), 1)
        For i = 0 To n - 1
          Debug PeekS(@devlist(i)\device_name[0], -1, #PB_UTF8)
        Next i
      EndIf
      
      csoundDestroy(*csound)
    EndIf
    
    CloseLibrary(CSoundLib)
    
  CompilerEndIf
EndIf

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sun Mar 29, 2020 10:33 am
by soren
@breeze4me - amazing, thanks!! I had no idea it was necessary first to call csoundSetRTAudioModule - how did you figure that out..?

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sun Mar 29, 2020 12:40 pm
by breeze4me
soren wrote:@breeze4me - amazing, thanks!! I had no idea it was necessary first to call csoundSetRTAudioModule - how did you figure that out..?
Googling ! :mrgreen:

https://csound-devel.narkive.com/DhUQx8 ... s-from-api

BTW, the following code may be a proper way.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

#CSOUND_SUCCESS = 0
#CSOUND_ERROR = -1
#CSOUND_INITIALIZATION = -2
#CSOUND_PERFORMANCE = -3
#CSOUND_MEMORY = -4
#CSOUND_SIGNAL = -5


#CSOUNDINIT_NO_SIGNAL_HANDLER = 1
#CSOUNDINIT_NO_ATEXIT = 2 

Structure CS_AUDIODEVICE Align #PB_Structure_AlignC 
  device_name.a[64]
  device_id.a[64]
  rt_module.a[64]
  max_nchnls.l
  isOutput.l
EndStructure



PrototypeC.i Prototype_csoundCreate(*hostData)
PrototypeC.i Prototype_csoundDestroy(*hostData)
PrototypeC.i Prototype_csoundGetAudioDevList(*csound, *list.CS_AUDIODEVICE, isOutput.l)
PrototypeC.i Prototype_csoundGetVersion()
PrototypeC.i Prototype_csoundSetRTAudioModule(*csound, sModule.p-utf8)
PrototypeC.l Prototype_csoundGetModule(*csound, number.i, *name, *type)


Global csoundCreate.Prototype_csoundCreate
Global csoundDestroy.Prototype_csoundDestroy
Global csoundGetAudioDevList.Prototype_csoundGetAudioDevList
Global csoundGetVersion.Prototype_csoundGetVersion
Global csoundSetRTAudioModule.Prototype_csoundSetRTAudioModule
Global csoundGetModule.Prototype_csoundGetModule

Global CSoundLib.i


Define *CSound, n.i, i.i

CSoundLib = OpenLibrary(#PB_Any, "csound64.dll")
If CSoundLib
  
  csoundCreate = GetFunction(CSoundLib, "csoundCreate")
  csoundDestroy = GetFunction(CSoundLib, "csoundDestroy")
  csoundGetAudioDevList = GetFunction(CSoundLib, "csoundGetAudioDevList")
  csoundGetVersion = GetFunction(CSoundLib, "csoundGetVersion")
  csoundSetRTAudioModule = GetFunction(CSoundLib, "csoundSetRTAudioModule")
  csoundGetModule = GetFunction(CSoundLib, "csoundGetModule")
  
  
  CompilerIf #PB_Compiler_IsMainFile
    
    Debug "CSound Version " + Str(csoundGetVersion())
    
    *csound = csoundCreate(#Null)
    If *csound
      
      Define *name
      Define *type
      Define ModuleName.s
      
      While csoundGetModule(*csound, i, @*name, @*type) = #CSOUND_SUCCESS
        
        Debug PeekS(*name, -1, #PB_UTF8)
        Debug PeekS(*type, -1, #PB_UTF8)
        
        If PeekS(*type, -1, #PB_UTF8) = "audio"
          ModuleName = PeekS(*name, -1, #PB_UTF8)
          ; Break
        EndIf
        
        i + 1
        
      Wend
      
      Debug ""
      
      If ModuleName
        csoundSetRTAudioModule(*csound, ModuleName)   ; it seems to work with "portaudio", "pa_bl", "pa_cb"
        
        n = csoundGetAudioDevList(*csound, 0, 1)
        Debug "Output AudioDevices: " + Str(n)
        If n
          Dim devlist.CS_AUDIODEVICE(n)
          
          csoundGetAudioDevList(*csound, @devlist(), 1)
          For i = 0 To n - 1
            Debug PeekS(@devlist(i)\device_name[0], -1, #PB_UTF8)
          Next i
        EndIf
      EndIf
      
      csoundDestroy(*csound)
    EndIf
    
    CloseLibrary(CSoundLib)
    
  CompilerEndIf
EndIf

Re: Accessing 'special' DLL function with ProcedureC

Posted: Sun Mar 29, 2020 3:19 pm
by skywalk
@soren
If your code question is solved, can you rename this topic to:
"Using csound dll in PB"
or something like that for others to find in the future?

https://csound.com/download.html

Re: Using csound dll in PB

Posted: Mon Mar 30, 2020 8:13 am
by Aleks_Longard
There my old codes for CSound, i only update csound64.dll for version 6.14
https://www.dropbox.com/s/b0davjq79pan336/CS.zip?dl=0

Cheers!