TGC Codebase Backup



BassMod DLL System by Peace

18th Nov 2004 12:22
Summary

Play modules and decode to waveformat



Description

The bassmod.dll includes great module playback functions, but even there is a possibility to convert any module to a raw soundfile. This source included of coz the playback functions, and a decode function with a made off a "RIFF" header, the Microsoft standart waveformat. So all decoded modules could be playback and worked with as *.wav files. You can download the added Example_BassMod.zip file included the bassmod.dll for further informations.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Remstart
   ------------------------------------------------------------------
   Author:        Volker Stepprath of Testaware
   Project:       BassMod DLL System
   Version:       0.00.002 ;D
   Language:      DarkBASIC Professional v1.057
   Description:   Play/Decode MOD,S3M,IT,XM,MTM,UMX using bassmod.dll
   Date:          18. November 2004 / 20:54:08
   ------------------------------------------------------------------
   Hint 1:        I used constants for better codestructure
   Hint 2:        Use functions as include in your own sources
   Hint 3:        See BASSMOD.dll manual for more details
   Hint 4:        Decode may take lot of time, press key to break
   Hint 5:        Try to add a volume level control ;D
   ------------------------------------------------------------------
Remend

`* Number of dll
#CONSTANT   BASS_DLL 1

`* Music init flags
#CONSTANT   BASS_MUSIC_RAMP             1
#CONSTANT   BASS_MUSIC_RAMPS            2
#CONSTANT   BASS_MUSIC_LOOP             4
#CONSTANT   BASS_MUSIC_FT2MOD          16
#CONSTANT   BASS_MUSIC_PT1MOD          32
#CONSTANT   BASS_MUSIC_POSRESET       256
#CONSTANT   BASS_MUSIC_SURROUND       512
#CONSTANT   BASS_MUSIC_SURROUND2     1024
#CONSTANT   BASS_MUSIC_STOPBACK      2048
#CONSTANT   BASS_MUSIC_CALCLEN       8192
#CONSTANT   BASS_MUSIC_NONINTER     16384
#CONSTANT   BASS_MUSIC_NOSAMPLE  0x400000

`* Is active return values
#CONSTANT   BASS_ACTIVE_STOPPED 0
#CONSTANT   BASS_ACTIVE_PLAYING 1
#CONSTANT   BASS_ACTIVE_PAUSED  3

`* Error codes
#CONSTANT   BASS_OK              0
#CONSTANT   BASS_ERROR_MEM       1
#CONSTANT   BASS_ERROR_FILEOPEN  2
#CONSTANT   BASS_ERROR_DRIVER    3
#CONSTANT   BASS_ERROR_HANDLE    5
#CONSTANT   BASS_ERROR_FORMAT    6
#CONSTANT   BASS_ERROR_POSITION  7
#CONSTANT   BASS_ERROR_INIT      8
#CONSTANT   BASS_ERROR_ALREADY  14
#CONSTANT   BASS_ERROR_ILLTYPE  19
#CONSTANT   BASS_ERROR_ILLPARAM 20
#CONSTANT   BASS_ERROR_DEVICE   23
#CONSTANT   BASS_ERROR_NOPLAY   24
#CONSTANT   BASS_ERROR_NOMUSIC  28
#CONSTANT   BASS_ERROR_NOSYNC   30
#CONSTANT   BASS_ERROR_NOTAVAIL 37
#CONSTANT   BASS_ERROR_DECODE   38
#CONSTANT   BASS_ERROR_FILEFORM 41
#CONSTANT   BASS_ERROR_UNKNOWN  -1

#CONSTANT   BASS_ERROR_NODLL    99  : `* Not an offizial constant!

#CONSTANT   BASS_DECODEBUF       1  : `* Number of MemBlk for decoding
#CONSTANT   BASS_DECODESIZE 100000  : `* Size in bytes of decoded MemBlk

mod$="Aryx.S3M"                     : `* Modulename *.mod *.s3m *.it *.xm *.mtm *.umx (<-2:21)
wav$="Decoded.Wav"                  : `* Wavefile to output decoded sound

bass_DLLInit()                      : `* >> First of all: install the BASSMOD.dll
my_Program(mod$,wav$)               : `* Your own code here!
bass_DLLClose()                     : `* >> At last: free the soundbuffer and close the BASSMOD.dll

FUNCTION my_Program(mod$,wav$)

   `* Very easy example how we can use the bassmod.dll (code own here)!
   `* ------------------------------------------------------------------

   gosub _setup
   repeat
      a=val(inkey$())
      if a<>0
         select a
            case 1 : bass_MusicPlayEx(mod$)      : endcase
            case 2 : bass_MusicStop()            : endcase
            case 3 : bass_MusicPause()           : endcase
            case 4 : bass_MusicPlay()            : endcase
            case 5 : bass_MusicDecode(mod$,wav$) : endcase
            case 6 : bass_MusicStop()            : endcase
         endselect
         gosub _setup
         print bass_ErrorGetCode()  : `* Don't worry about error when music decoded, it's almost OK
         repeat : sync : until inkey$()=""
      endif
   until a=6
   exitfunction
   _setup:
   cls 0
   set cursor 0,0
   set text opaque
   restore _scrnupd
   for i=1 to 6
      read a$
      print "["+str$(i)+"]:";a$
   next i
   return
   _scrnupd: data "Play Music","Stop Music","Pause Music","Resume Music","Decode Music","Exit"

ENDFUNCTION

FUNCTION bass_DLLinit

   if file exist("bassmod.dll")
      load dll "bassmod.dll",BASS_DLL
   endif

ENDFUNCTION
FUNCTION bass_DLLclose

   if dll exist(BASS_DLL)
      call dll BASS_DLL,"BASSMOD_Free"
      delete dll BASS_DLL
   endif

ENDFUNCTION

FUNCTION bass_MusicPlayEx(mod$)

   if dll exist(BASS_DLL)
      call dll BASS_DLL,"BASSMOD_Free"
      call dll BASS_DLL,"BASSMOD_Init",0 ,44100 ,0
      call dll BASS_DLL,"BASSMOD_SetVolume",100
      call dll BASS_DLL,"BASSMOD_MusicLoad",0 ,mod$ ,0 ,0 ,(BASS_MUSIC_RAMP||BASS_MUSIC_LOOP||BASS_MUSIC_SURROUND)
      call dll BASS_DLL,"BASSMOD_MusicPlayEx",0 ,4 ,1
   endif

ENDFUNCTION
FUNCTION bass_MusicStop

   if dll exist(BASS_DLL)
      call dll BASS_DLL,"BASSMOD_MusicStop"
      call dll BASS_DLL,"BASSMOD_MusicFree"
      call dll BASS_DLL,"BASSMOD_Free"
   endif

ENDFUNCTION

FUNCTION bass_MusicPause

   if dll exist(BASS_DLL)
      a=call dll(BASS_DLL,"BASSMOD_MusicIsActive")
      if a=BASS_ACTIVE_PLAYING then call dll BASS_DLL,"BASSMOD_MusicPause"
   endif

ENDFUNCTION
FUNCTION bass_MusicPlay

   if dll exist(BASS_DLL)
      a=call dll(BASS_DLL,"BASSMOD_MusicIsActive")
      if a=BASS_ACTIVE_PAUSED then call dll BASS_DLL,"BASSMOD_MusicPlay"
   endif

ENDFUNCTION

FUNCTION bass_MusicDecode(mod$,wav$)

   `* Decode any known module format to Microsoft wave format
   `* ------------------------------------------------------------------
   `* Functioncall:  mod$=modulename
   `*                wav$=wavename for output (add a .wav suffix)
   `* ------------------------------------------------------------------
   `* Here is one of the greatest functions of the bassmod.dll (I think so)
   `* Every module can be convert to the sound raw format, but with a little
   `* trick we add a header as conform wave format ;D This may take a bit
   `* of time coz the whole song will be converted. (=PlayTime)
   `* ------------------------------------------------------------------

   if dll exist(BASS_DLL)

      if file exist(wav$) then delete file wav$

      make memblock BASS_DECODEBUF,BASS_DECODESIZE

      call dll BASS_DLL,"BASSMOD_Free"
      call dll BASS_DLL,"BASSMOD_Init",-3 ,44100 ,0
      call dll BASS_DLL,"BASSMOD_MusicLoad",0 ,mod$ ,0 ,0 ,0

      open to write 1,wav$

      `* Make room for the wavehdr (11 Longs=44 bytes)
      for a=0 to 10
         write long 1,0
      next a

      `* Let's convert module to music rawformat, press any key to exit
      repeat

         ByteDecode=call dll(BASS_DLL,"BASSMOD_MusicDecode",get memblock ptr(BASS_DECODEBUF),BASS_DECODESIZE)

         if ByteDecode>0
            inc ByteSize,ByteDecode
            for b=0 to ByteDecode-1
               write byte 1,memblock byte(BASS_DECODEBUF,b)
            next b
         endif

         set cursor 0,90 : print bass_CurWaveTimePos(ByteSize) : `* Info about current position

      until ByteDecode<0 or inkey$()<>""

      close file 1

      `* Now we create our own tricky waveformat header (1411 kbit/s) works fine ;D
      fill memory get memblock ptr(BASS_DECODEBUF),0,44

      a$="RIFF"     : for a=1 to len(a$) : write memblock byte BASS_DECODEBUF, 0+a-1,asc(mid$(a$,a)) : next a
      a$="WAVEfmt " : for a=1 to len(a$) : write memblock byte BASS_DECODEBUF, 8+a-1,asc(mid$(a$,a)) : next a
      a$="data"     : for a=1 to len(a$) : write memblock byte BASS_DECODEBUF,36+a-1,asc(mid$(a$,a)) : next a
      write memblock dword BASS_DECODEBUF, 4,ByteSize+36
      write memblock byte  BASS_DECODEBUF,16,0x10
      write memblock byte  BASS_DECODEBUF,20,0x1
      write memblock byte  BASS_DECODEBUF,22,0x2
      write memblock word  BASS_DECODEBUF,24,0xac44
      write memblock word  BASS_DECODEBUF,28,0xb110
      write memblock byte  BASS_DECODEBUF,30,0x2
      write memblock byte  BASS_DECODEBUF,32,0x4
      write memblock byte  BASS_DECODEBUF,34,0x10
      write memblock dword BASS_DECODEBUF,40,ByteSize

      for a=0 to 43
         write byte to file wav$,a,memblock byte(BASS_DECODEBUF,a)
      next a

      delete memblock BASS_DECODEBUF

   endif

ENDFUNCTION

FUNCTION bass_ErrorGetCode

   if dll exist(BASS_DLL)
      a=call dll(BASS_DLL,"BASSMOD_ErrorGetCode")
      select a
         case BASS_OK             : a$="all is OK"                                  : endcase
         case BASS_ERROR_MEM      : a$="memory error"                               : endcase
         case BASS_ERROR_FILEOPEN : a$="can't open the file"                        : endcase
         case BASS_ERROR_DRIVER   : a$="can't find a free/valid driver"             : endcase
         case BASS_ERROR_HANDLE   : a$="invalid handle"                             : endcase
         case BASS_ERROR_FORMAT   : a$="unsupported format"                         : endcase
         case BASS_ERROR_POSITION : a$="invalid playback position"                  : endcase
         case BASS_ERROR_INIT     : a$="BASS_Init has not been successfully called" : endcase
         case BASS_ERROR_ALREADY  : a$="already initialized/loaded"                 : endcase
         case BASS_ERROR_ILLTYPE  : a$="an illegal type was specified"              : endcase
         case BASS_ERROR_ILLPARAM : a$="an illegal parameter was specified"         : endcase
         case BASS_ERROR_DEVICE   : a$="illegal device number"                      : endcase
         case BASS_ERROR_NOPLAY   : a$="not playing"                                : endcase
         case BASS_ERROR_NOMUSIC  : a$="no MOD music has been loaded"               : endcase
         case BASS_ERROR_NOSYNC   : a$="synchronizers have been disabled"           : endcase
         case BASS_ERROR_NOTAVAIL : a$="requested data is not available"            : endcase
         case BASS_ERROR_DECODE   : a$="the channel is a 'decoding channel'"        : endcase
         case BASS_ERROR_FILEFORM : a$="unsupported file format"                    : endcase
         case BASS_ERROR_UNKNOWN  : a$="some other mystery error"                   : endcase
      endselect
   else
      a=BASS_ERROR_NODLL          : a$="No bassmod.dll avail"
   endif

   if a$<>"" then a$="Error code: ("+str$(a)+") "+a$

ENDFUNCTION a$

FUNCTION bass_CurWaveTimePos(a)

   `* Calculate decoded playtime of wave ;D
   `* ------------------------------------------------------------------

   a$=str$(a/1024)

   a=a/((1411*1024)/8)

   s=a mod 60
   m=a/60

   s$=str$(s) : if s<10 then s$="0"+s$
   m$=str$(m) : if m<10 then m$="0"+m$

   a$="CurTime:"+m$+":"+s$+" CurPosition:"+a$+"kb"

ENDFUNCTION a$