BASS.dll own DSP (karaoke) example

Share your advanced PureBasic knowledge/code with the community.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

BASS.dll own DSP (karaoke) example

Post by ricardo »

Just a small example of some DSP using BASS dll.

Code: Select all

IncludeFile "bassinclude.pb"

uBasDll = BASS_OpenDLL()

Global Handle, Karaoke


Enumeration
   ;PLAY GADGETS
   #MP3Play
   #MP3Pause
   #MP3Stop
   #MP3Load
   ;EFFECTS GADGETS
   #FX_KARAOKE
EndEnumeration


Procedure MyDSP(Handle,Channel,Buffer,Lenght,User)
   ;Just an example of DSP
   
   ;This karaoke is the method used by some hardware
   ;Just find the sound in the middle of the channels pan
   ;and take it out
   ;Makes poor the sound, yes and only works in tracks with not much effects on the main voice
   ;and also takes out bass and some other sounds (snaredrum usually) that was setted on the middle
   ;As i said, its only an example of some DSP, not a very accurate karaoke.
   
   ;Please close the windos and door of your house if you plan to sing... don't be rude to your neighbors peace of mind :)
   
   
   If Karaoke = 1
      For i = 0 To Lenght-1 Step 4
         Channel1.w = PeekW(Buffer+i)
         Channel2.w = PeekW(Buffer+i+2)
         nChannel1.w = Channel1 * -1
         nChannel2.w = Channel2 * -1
         Channel1 + nChannel2
         Channel2 + nChannel1
         PokeW(Buffer+i,Channel1)
         PokeW(Buffer+i+2,Channel2)
      Next
   EndIf
EndProcedure

If OpenWindow(0,100,150,450,200,"Test",#PB_Window_SystemMenu)
   CreateGadgetList(WindowID(0))
   
   ButtonGadget(#MP3Load,150,50,50,25,"Load")
   ButtonGadget(#MP3Stop,200,50,50,25,"Stop")
   ButtonGadget(#MP3Play,250,50,75,25,"Play")
   ButtonGadget(#MP3Pause,325,50,50,25,"Pause")
   
  
   CheckBoxGadget(#FX_KARAOKE,10,100,120,25,"Karaoke")
   SetGadgetState(#FX_KARAOKE,1)
   
   
   
   ;- INICIA BASS
   BASS_Init(-1,#WAVE_FORMAT_4S16,0,WindowID(0),#Null)
   FileName.s = OpenFileRequester("","","MP3|*.MP3|;",0)
   BASS_SetVolume(100)
   Handle=BASS_StreamCreateFile(#False, FileName, 0, 0, #BASS_SAMPLE_3D) 
   MyDSPHandle = BASS_ChannelSetDSP(Handle,@MyDSP(),0,0)
   BASS_ChannelPlay(Handle,0) 
   
   Dim FFT.f(512)
   Karaoke = 1
   Repeat
      EventID=WindowEvent()
      Delay(10)
      Select EventID
         Case 0
            If Handle And BASS_ChannelIsActive(Handle) = #BASS_ACTIVE_PLAYING
               BASS_ChannelGetData(Handle,@FFT(),#BASS_DATA_FFT1024);Here we get the FFT info to make later a metter
               uPos = BASS_StreamGetFilePosition(Handle,#BASS_FILEPOS_CURRENT)
               SetWindowTitle(0,StrF(FFT(1)) + "  " + Str(uPos))
            EndIf
         Case #PB_Event_Gadget
            Select EventGadget()
               ;- PLAY
               Case #MP3Load
                  BASS_StreamFree(Handle)
                  FileName.s = OpenFileRequester("","","MP3|*.MP3|;",0)
                  BASS_SetVolume(100)
                  Handle=BASS_StreamCreateFile(#False, FileName, 0, 0, #BASS_SAMPLE_3D) 
                  MyDSPHandle = BASS_ChannelSetDSP(Handle,@MyDSP(),0,0)
               Case #MP3Play
                  BASS_ChannelPlay(Handle,0) 
               Case #MP3Pause
                  BASS_ChannelPause(Handle)
                  BASS_Start()
               Case #MP3Stop
                  BASS_ChannelStop(Handle)
                  BASS_ChannelSetPosition(Handle,0)
                  ;- EFFECTS
               Case #FX_KARAOKE
                  If GetGadgetState(#FX_KARAOKE) = 1
                     Karaoke = 1
                  Else
                     Karaoke = 0
                  EndIf
            EndSelect
      EndSelect
   Until EventID=#PB_Event_CloseWindow
EndIf
BASS_ChannelRemoveDSP(Handle,MyDSPHandle)
BASS_CloseDLL()
End
The included file:

Code: Select all

;- Error codes returned by BASS_GetErrorCode 
#BASS_OK   =         0   ; all is OK 
#BASS_ERROR_MEM   =   1   ; memory error 
#BASS_ERROR_FILEOPEN   =2   ; can't open the file 
#BASS_ERROR_DRIVER   =3   ; can't find a free/valid driver 
#BASS_ERROR_BUFLOST   =4   ; the sample buffer was lost 
#BASS_ERROR_HANDLE   =5   ; invalid handle 
#BASS_ERROR_FORMAT   =6   ; unsupported sample format 
#BASS_ERROR_POSITION   =7   ; invalid playback position 
#BASS_ERROR_INIT      =8   ; BASS_Init has not been successfully called 
#BASS_ERROR_START   =9   ; BASS_Start has not been successfully called 
#BASS_ERROR_ALREADY   =14   ; already initialized 
#BASS_ERROR_NOPAUSE   =16   ; not paused 
#BASS_ERROR_NOCHAN   =18   ; can't get a free channel 
#BASS_ERROR_ILLTYPE   =19   ;an illegal type was specified 
#BASS_ERROR_ILLPARAM   =20   ; an illegal parameter was specified 
#BASS_ERROR_NO3D      =21   ; no 3D support 
#BASS_ERROR_NOEAX   =22   ; no EAX support 
#BASS_ERROR_DEVICE   =23   ; illegal device number 
#BASS_ERROR_NOPLAY   =24   ; not playing 
#BASS_ERROR_FREQ      =25   ; illegal sample rate 
#BASS_ERROR_NOTFILE   =27   ; the stream is not a file stream 
#BASS_ERROR_NOHW      =29   ; no hardware voices available 
#BASS_ERROR_EMPTY   =31   ; the MOD music has no sequence Data 
#BASS_ERROR_NONET   =32   ; no internet connection could be opened 
#BASS_ERROR_CREATE   =33   ; couldn't create the file 
#BASS_ERROR_NOFX      =34   ;effects are not available 
#BASS_ERROR_PLAYING   =35   ; the channel is playing 
#BASS_ERROR_NOTAVAIL   =37   ; requested Data is not available 
#BASS_ERROR_DECODE   =38   ; the channel is a "decoding channel" 
#BASS_ERROR_DX      =39   ; a sufficient DirectX version is not installed 
#BASS_ERROR_TIMEOUT   =40   ; connection timedout 
#BASS_ERROR_FILEFORM   =41   ; unsupported file format 
#BASS_ERROR_SPEAKER   =42   ; unavailable speaker 
#BASS_ERROR_UNKNOWN   =-1   ; some other mystery error 
#BASS_ERROR_VERSION = 43
#BASS_ERROR_CODEC = 44

;- BASS Constants 
#BASS_MP3_SETPOS=$20000 ; enable pin-point seeking on the MP3/MP2/MP1 

#BASS_STREAM_AUTOFREE=$40000   ; automatically free the stream when it stop/ends 
#BASS_STREAM_RESTRATE=$80000   ; restrict the download rate of internet file streams 
#BASS_STREAM_BLOCK=$100000 ; download/play internet file stream in small blocks 
#BASS_STREAM_DECODE=$200000 ; don't play the stream, only decode (BASS_ChannelGetData) 
#BASS_STREAM_META=$400000 ; request metadata from a Shoutcast stream 
#BASS_STREAM_STATUS=$800000 ; give server status info (HTTP/ICY tags) in DOWNLOADPROC 

#BASS_SAMPLE_8BITS=1   ; 8 bit 
#BASS_SAMPLE_FLOAT=256   ; 32-bit floating-point 
#BASS_SAMPLE_MONO=2   ; mono, Else stereo 
#BASS_SAMPLE_LOOP=4   ; looped 
#BASS_SAMPLE_3D=8   ; 3D functionality enabled 
#BASS_SAMPLE_SOFTWARE=16   ; it's NOT using hardware mixing 
#BASS_SAMPLE_MUTEMAX=32   ; muted at max distance (3D only) 
#BASS_SAMPLE_VAM=64   ; uses the DX7 voice allocation & management 
#BASS_SAMPLE_FX=128   ; old implementation of DX8 effects are enabled 
#BASS_SAMPLE_OVER_VOL=$10000   ; override lowest volume 
#BASS_SAMPLE_OVER_POS=$20000   ; override longest playing 
#BASS_SAMPLE_OVER_DIST=$30000 ; override furthest from listener (3D only) 

#BASS_SPEAKER_FRONT=$1000000   ; front speakers 
#BASS_SPEAKER_REAR=$2000000   ; rear/side speakers 
#BASS_SPEAKER_CENLFE=$3000000   ; center & LFE speakers (5.1) 
#BASS_SPEAKER_REAR2=$4000000   ; rear center speakers (7.1) 
#BASS_SPEAKER_LEFT=$10000000   ; modifier: left 
#BASS_SPEAKER_RIGHT=$20000000   ; modifier: right 
#BASS_SPEAKER_FRONTLEFT=#BASS_SPEAKER_FRONT|#BASS_SPEAKER_LEFT 
#BASS_SPEAKER_FRONTRIGHT=#BASS_SPEAKER_FRONT|#BASS_SPEAKER_RIGHT 
#BASS_SPEAKER_REARLEFT=#BASS_SPEAKER_REAR|#BASS_SPEAKER_LEFT 
#BASS_SPEAKER_REARRIGHT=#BASS_SPEAKER_REAR|#BASS_SPEAKER_RIGHT 
#BASS_SPEAKER_CENTER   =#BASS_SPEAKER_CENLFE|#BASS_SPEAKER_LEFT 
#BASS_SPEAKER_LFE=#BASS_SPEAKER_CENLFE|#BASS_SPEAKER_RIGHT 
#BASS_SPEAKER_REAR2LEFT=#BASS_SPEAKER_REAR2|#BASS_SPEAKER_LEFT 
#BASS_SPEAKER_REAR2RIGHT=#BASS_SPEAKER_REAR2|#BASS_SPEAKER_RIGHT 

#BASS_UNICODE=$80000000 

#BASS_SYNC_MUSICPOS=0 
#BASS_SYNC_POS=0 
#BASS_SYNC_MUSICINST=1 
#BASS_SYNC_END=2 
#BASS_SYNC_MUSICFX=3 
#BASS_SYNC_META=4 
#BASS_SYNC_SLIDE=5 
#BASS_SYNC_STALL=6 
#BASS_SYNC_DOWNLOAD=7 
#BASS_SYNC_FREE=8 
#BASS_SYNC_MESSAGE=$20000000 
#BASS_SYNC_MIXTIME=$40000000 
#BASS_SYNC_ONETIME=$80000000 

#BASS_CONFIG_BUFFER=0 
#BASS_CONFIG_UPDATEPERIOD=1 
#BASS_CONFIG_MAXVOL=3 
#BASS_CONFIG_GVOL_SAMPLE=4 
#BASS_CONFIG_GVOL_STREAM=5 
#BASS_CONFIG_GVOL_MUSIC=6 
#BASS_CONFIG_CURVE_VOL=7 
#BASS_CONFIG_CURVE_PAN=8 
#BASS_CONFIG_FLOATDSP=9 
#BASS_CONFIG_3DALGORITHM=10 
#BASS_CONFIG_NET_TIMEOUT=11 
#BASS_CONFIG_NET_BUFFER=12 
#BASS_CONFIG_PAUSE_NOPLAY=13 
#BASS_CONFIG_NET_NOPROXY=14 

#BASS_ACTIVE_STOPPED=0 
#BASS_ACTIVE_PLAYING=1 
#BASS_ACTIVE_STALLED=2 
#BASS_ACTIVE_PAUSED=3 

#BASS_FILEPOS_DECODE=0 
#BASS_FILEPOS_DOWNLOAD=1 
#BASS_FILEPOS_END=2 
#BASS_FILEPOS_START=3 

; DX8 effect types, use with BASS_ChannelSetFX
#BASS_FX_CHORUS = 0         ;  GUID_DSFX_STANDARD_CHORUS
#BASS_FX_COMPRESSOR = 1     ;  GUID_DSFX_STANDARD_COMPRESSOR
#BASS_FX_DISTORTION = 2     ;  GUID_DSFX_STANDARD_DISTORTION
#BASS_FX_ECHO = 3           ;  GUID_DSFX_STANDARD_ECHO
#BASS_FX_FLANGER = 4        ;  GUID_DSFX_STANDARD_FLANGER
#BASS_FX_GARGLE = 5         ;  GUID_DSFX_STANDARD_GARGLE
#BASS_FX_I3DL2REVERB = 6    ;  GUID_DSFX_STANDARD_I3DL2REVERB
#BASS_FX_PARAMEQ = 7        ;  GUID_DSFX_STANDARD_PARAMEQ
#BASS_FX_REVERB = 8         ;  GUID_DSFX_WAVES_REVERB

#ASS_DATA_FLOAT = $40000000    ; flag: return floating-point sample data
#BASS_DATA_FFT512 = $80000000   ; 512 sample FFT
#BASS_DATA_FFT1024 = $80000001  ; 1024 FFT
#BASS_DATA_FFT2048 = $80000002  ; 2048 FFT
#BASS_DATA_FFT4096 = $80000003 

; BASS_StreamGetFilePosition modes
#BASS_FILEPOS_CURRENT = 0
#BASS_FILEPOS_DECODE = #BASS_FILEPOS_CURRENT
#BASS_FILEPOS_DOWNLOAD = 1
#BASS_FILEPOS_END = 2
#BASS_FILEPOS_START = 3

#BASSFALSE = 0
#BASSTRUE = 1
;- Bass Globals 
Structure BASS_CHANNELINFO 
   freq.l 
   chans.l 
   flags.l 
   ctype.l 
   origres.l 
EndStructure 

Structure BASS_VERSION 
   ver.w 
   rev.w 
EndStructure 

Structure BASS_FXPARAMEQ
   fCenter.l
   fBandwidth.l
   fGain.l
EndStructure

Global bassdll.l 

;- Procedures 

Procedure.l BASS_OpenDLL() 
   bassdll=OpenLibrary(#PB_Any,"bass.dll") 
   ProcedureReturn bassdll 
EndProcedure 

Procedure.l BASS_CloseDLL() 
   If bassdll 
      CallFunction(bassdll,"BASS_Free") 
      CloseLibrary(bassdll) 
      bassdll=0 
   EndIf 
EndProcedure 

Procedure.l BASS_GetVersion() 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_GetVersion") 
   EndIf 
EndProcedure 

Procedure.l BASS_CheckVersion(ver.l,rev.l) 
   Version.BASS_VERSION 
   PokeL(@Version,BASS_GetVersion()) 
   If Version/ver=ver ; if version match 
      If Version/rev>=rev ; if revision match or higher 
         result.l=#True 
      Else ; revision was lower 
         result.l=#False 
      EndIf 
   Else ; version was lower 
      result.l=#False 
   EndIf 
   ProcedureReturn result 
EndProcedure 

Procedure.l BASS_SetConfig(option.l,Value.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_SetConfig",option,Value) 
   EndIf 
EndProcedure 

Procedure.l BASS_GetConfig(option.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_GetConfig",option) 
   EndIf 
EndProcedure 

ProcedureDLL.l BASS_ChannelSetFX(Handle.l,uType.l,Priority.l)
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelSetFX",Handle.l,uType.l,Priority.l) 
EndProcedure 

ProcedureDLL.l BASS_FXSetParameters(Handle.l,par.l)
   ProcedureReturn CallFunction(bassdll,"BASS_FXSetParameters",Handle.l,par.l) 
EndProcedure 

Procedure.l BASS_Init(device.l,freq.l,flags.l,window.l,guid.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_Init",device,freq,flags,window,guid) 
   EndIf 
EndProcedure 

Procedure.l BASS_ChannelGetInfo(Handle.l,*chaninfo) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_ChannelGetInfo",Handle,*chaninfo) 
   EndIf 
EndProcedure 

Procedure.l BASS_StreamGetFilePosition(Handle.l,Mode.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_StreamGetFilePosition",Handle,Mode) 
   EndIf 
EndProcedure 

Procedure.l BASS_ChannelSetPosition(Handle.l,Position.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_ChannelSetPosition",Handle,Position,0) 
   EndIf 
EndProcedure 

Procedure.l BASS_ChannelIsActive(Handle.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_ChannelIsActive",Handle) 
   EndIf 
EndProcedure 

Procedure.l BASS_ChannelGetData(Handle.l,Buffer.l,length.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_ChannelGetData",Handle,Buffer.l,length.l) 
   EndIf 
EndProcedure 

Procedure.l BASS_StreamCreateFile(mem.b,File$,offset.l,length.l,flags.l) 
   If bassdll 
      ProcedureReturn CallFunction(bassdll,"BASS_StreamCreateFile",mem,File$,offset,length,flags) 
   EndIf 
EndProcedure 
  
Procedure.l BASS_StreamFree(Handle.l) 
   If bassdll 
      CallFunction(bassdll,"BASS_StreamFree",Handle) 
   EndIf 
EndProcedure 

ProcedureDLL.l BASS_ChannelPlay(a.l,b.l) 
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelPlay",a,b) 
EndProcedure 

ProcedureDLL.l BASS_ChannelPause(a.l) 
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelPause",a) 
EndProcedure 

ProcedureDLL.l BASS_Start()
   ProcedureReturn CallFunction(bassdll,"BASS_Start") 
EndProcedure 

ProcedureDLL.l BASS_ChannelStop(a.l)
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelStop",a) 
EndProcedure 

ProcedureDLL.l BASS_SampleStop(a.l)
   ProcedureReturn CallFunction(bassdll,"BASS_SampleStop",a) 
EndProcedure 

ProcedureDLL.l BASS_ChannelSetDSP(a.l,b.l,c.l,d.l)
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelSetDSP",a,b,c,d) 
EndProcedure 

ProcedureDLL.l BASS_ChannelRemoveDSP(a.l,b.l)
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelRemoveDSP",a,b) 
EndProcedure 

ProcedureDLL.l BASS_SetVolume(a.l) 
   ProcedureReturn CallFunction(bassdll,"BASS_SetVolume",a) 
EndProcedure 

ProcedureDLL.l BASS_PluginLoad(Dll$,b.l)
   ProcedureReturn CallFunction(bassdll,"BASS_PluginLoad",Dll$,b.l) 
EndProcedure 

ProcedureDLL.l BASS_ChannelGetAttributes(a.l,b.l,c.l,d.l)
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelGetAttributes",a.l,b.l,c.l,d.l) 
EndProcedure

ProcedureDLL.l BASS_ChannelSetAttributes(Handle.l,Volume.l,Frequency.l,Pan.l)
   ProcedureReturn CallFunction(bassdll,"BASS_ChannelSetAttributes",Handle.l,Volume.l,Frequency.l,Pan.l) 
EndProcedure

Procedure.s BASS_ErrorGetText() 
   Protected errortxt.s 
   If bassdll 
      errorcode.l=CallFunction(bassdll,"BASS_ErrorGetCode")
      Debug "Error code: " + Str(errorcode)
      Select errorcode 
         Case #BASS_OK : errortxt="OK" 
         Case #BASS_ERROR_MEM :   errortxt="Memory error" 
         Case #BASS_ERROR_FILEOPEN    : errortxt="Can't open the file" 
         Case #BASS_ERROR_DRIVER    : errortxt="Can't find a free/valid driver" 
         Case #BASS_ERROR_BUFLOST : errortxt="The sample buffer was lost" 
         Case #BASS_ERROR_HANDLE : errortxt="Invalid handle" 
         Case #BASS_ERROR_FORMAT : errortxt="Unsupported sample format" 
         Case #BASS_ERROR_POSITION : errortxt="Invalid playback position" 
         Case #BASS_ERROR_INIT : errortxt="BASS_Init has not been successfully called" 
         Case #BASS_ERROR_START : errortxt="BASS_Start has not been successfully called" 
         Case #BASS_ERROR_ALREADY : errortxt="Already initialized" 
         Case #BASS_ERROR_NOPAUSE : errortxt="Not paused" 
         Case #BASS_ERROR_NOCHAN : errortxt="Can't get a free channel" 
         Case #BASS_ERROR_ILLTYPE : errortxt="An illegal type was specified" 
         Case #BASS_ERROR_ILLPARAM : errortxt="An illegal parameter was specified" 
         Case #BASS_ERROR_NO3D : errortxt="No 3D support" 
         Case #BASS_ERROR_NOEAX : errortxt="No EAX support" 
         Case #BASS_ERROR_DEVICE : errortxt="Illegal device number" 
         Case #BASS_ERROR_NOPLAY : errortxt="Not playing" 
         Case #BASS_ERROR_FREQ : errortxt="Illegal sample rate" 
         Case #BASS_ERROR_NOTFILE : errortxt="The stream is not a file stream" 
         Case #BASS_ERROR_NOHW : errortxt="No hardware voices available" 
         Case #BASS_ERROR_EMPTY    : errortxt="The MOD music has no sequence Data" 
         Case #BASS_ERROR_NONET : errortxt="No internet connection could be opened" 
         Case #BASS_ERROR_CREATE : errortxt="Couldn't create the file" 
         Case #BASS_ERROR_NOFX : errortxt="Effects are not available" 
         Case #BASS_ERROR_PLAYING : errortxt="The channel is playing" 
         Case #BASS_ERROR_NOTAVAIL : errortxt="Requested Data is not available" 
         Case #BASS_ERROR_DECODE : errortxt="The channel is a decoding channel" 
         Case #BASS_ERROR_DX : errortxt="A sufficient DirectX version is not installed" 
         Case #BASS_ERROR_TIMEOUT : errortxt="Connection timedout" 
         Case #BASS_ERROR_FILEFORM : errortxt="Unsupported file format" 
         Case #BASS_ERROR_SPEAKER : errortxt="Unavailable speaker" 
         Case #BASS_ERROR_UNKNOWN : errortxt="Some other mystery error" 
         Case #BASS_ERROR_VERSION : errortxt = "Wrong version"
         Case #BASS_ERROR_CODEC :errortxt = "Codec problem"
      EndSelect 
   EndIf 
   ProcedureReturn errortxt 
EndProcedure 
dige
Addict
Addict
Posts: 1405
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Where can I download the bass.dll?

-EDIT-
Found it at http://www.un4seen.com
Post Reply