examples mixing Pb and C

Linux specific forum
User avatar
idle
Always Here
Always Here
Posts: 6023
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

examples mixing Pb and C

Post by idle »

We can now use c libs without to much effort.
note, pb variables in c are all lowercase and prefixed with v_ pointers with p_ and functions with f_
calling functions in c is straight forward and if you need to pass by address just use & which is the same as @ in pb
constants don't cross the boundary, so if you need to use a constant in pb from c declare it as a variable

importing is done like this

Code: Select all

 ImportC "-lsomelib2.0" : EndImport    ;tell linker which lib
 !#include "/usr/include/somelib.h";   ;tell c which headers 

Test hello world from Lua 5.2

Code: Select all

CompilerIf Not Defined(PB_Compiler_Backend,#PB_Constant)
  CompilerError "Please use PureBasic Version 6.00 Beta1" 
CompilerElseIf #PB_Compiler_Backend<>#PB_Backend_C
  CompilerError "Please use Compiler-Option C Backend."
CompilerEndIf 

CompilerIf #PB_Compiler_OS = #PB_OS_Linux 
  ImportC "-llua5.2" : EndImport  
  !#include "/usr/include/lua5.2/lua.h";
CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
   CompilerError "Please add windows imports"
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
   CompilerError "Please add osx imports"
CompilerEndIf  

Global lua

!v_lua = luaL_newstate();
!luaL_openlibs(v_lua);

*code = UTF8("print('Hello, World from Lua')")

!if (luaL_loadstring(v_lua, p_code) == LUA_OK) {
!   if (lua_pcall(v_lua, 0, 0, 0) == LUA_OK) {
!     lua_pop(v_lua, lua_gettop(v_lua));
!     }
!   }
FreeMemory(*code) 
!lua_close(v_lua);


Basic test of portaudio sawtooth wave

Code: Select all

CompilerIf Not Defined(PB_Compiler_Backend,#PB_Constant)
  CompilerError "Please use PureBasic Version 6.00 Beta1" 
CompilerElseIf #PB_Compiler_Backend<>#PB_Backend_C
  CompilerError "Please use Compiler-Option C Backend."
CompilerEndIf 

CompilerIf #PB_Compiler_OS = #PB_OS_Linux 
  ImportC "-lportaudio" : EndImport 
  !#include "/usr/include/portaudio.h" // Please adapt for OS. 
CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
  CompilerError "Please add windows imports"
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
  CompilerError "Please add osx imports"
CompilerEndIf  

Structure paTestData
  left_phase.f;
  right_phase.f;
EndStructure

Global *stream;
Global err 
Global num_seconds= 4;
Global sample_rate = 44100;
Global *output
Global mData.PaTestData 

;** This routine will be called by the PortAudio engine when audio is needed.
;** It may called at interrupt level on some machines so don't do anything
;** that could mess up the system like calling malloc() Or free().
Procedure patestCallback(*inputBuffer,*outputBuffer,framesPerBuffer,*timeInfo,statusFlags,*userData.paTestData)
  
  *out.float = *outputBuffer;
  
  Protected i
  
  For i=0 To framesPerBuffer
    *out\f = *userData\left_phase;  /* left */
    *out+4
    *out\f = *userData\right_phase;  /* right */
    *out+4
    ;/* Generate simple sawtooth phaser that ranges between -1.0 And 1.0. */
    *userData\left_phase + 0.01 
    ;/* When signal reaches top, drop back down. */
    If *userData\left_phase >= 1.0 
      *userData\left_phase - 1.0
    EndIf   
    ;higher pitch so we can distinguish left And right. */
    *userData\right_phase + 0.03
    If *userData\right_phase >= 1.0
      *userData\right_phase = -1.0;
    EndIf
    
  Next   
EndProcedure ;

Procedure Pa_Init()
  Protected ret 
  !v_ret = Pa_Initialize(); 
  ProcedureReturn ret 
EndProcedure

Procedure Pa_Terminate()
  !Pa_Terminate();
EndProcedure   

Global paCallback = @patestCallback()     

Debug "PortAudio Test: output sawtooth wave."

err = Pa_Init(); 
If err <> 0
  Goto error;
EndIf   

!v_err = Pa_OpenDefaultStream(&p_stream,0,2,paFloat32,v_sample_rate,256,v_pacallback,&v_mdata); 
If err <> 0 
  Goto error;
EndIf  

!v_err = Pa_StartStream(p_stream);
If err <> 0
  Goto error;
EndIf   

!Pa_Sleep(v_num_seconds*1000);
!v_err = Pa_StopStream(p_stream );
If err <> 0 
  Goto error;
EndIf   

!v_err = Pa_CloseStream(p_stream);
If err <> 0 
  Goto error;
EndIf   

Pa_Terminate();
Debug "Test finished"
End  

error:
Pa_Terminate();
Debug "An error occured while using the portaudio stream"
Debug "error number " + Str(err) 
!p_output = Pa_GetErrorText(v_err);
Debug PeekS(*output,-1,#PB_UTF8) 


User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: examples mixing Pb and C

Post by skywalk »

This looks so tempting. 8) Thanks for the example!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
IceSoft
Addict
Addict
Posts: 1698
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: examples mixing Pb and C

Post by IceSoft »

Hope there comes also an official description like:
"How to use C/C++ sources/headers on PB 6.xx"
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
punak
Enthusiast
Enthusiast
Posts: 113
Joined: Tue Sep 07, 2021 12:08 pm

Re: examples mixing Pb and C

Post by punak »

I couldn't do this trick for windows.
I get this error message when running :
error: no include path in which to search for stdarg.h #include <stdarg.h>
src\lua.h: no include path in which to search for stddef.h #include <stddef.h>
in file included from src\lua.h
can you help me ?
Post Reply