Qbasicnews.com

Full Version: can anybody help with sound
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i need sound in my games but i cant figure out how to do it can anyone help? actually write a code that works in freebasic and let me use it i will give credit in my game when posted on here
Copied from the examples included in fb:
FMOD
Code:
'Simple FMOD test for FB
'by Plasma  [11-16-2004]

DefInt A-Z
'$Include: 'fmod.bi'

Declare Sub ErrorQuit (Message$)

Const FALSE = 0
Const MusicFile = "dne_trtn.mod"
    Dim Shared Handle As Long


    If FSOUND_GetVersion <= FMOD_VERSION Then
          ErrorQuit "FMOD version " + STR$(FMOD_VERSION) + " or greater required"
    End If

    If FSOUND_Init(44100, 32, 0) = FALSE Then
          ErrorQuit "Can't initialize FMOD"
    End If

    Handle = FMUSIC_LoadSong(MusicFile)
    If Handle = FALSE Then
          ErrorQuit "Can't load music file " + CHR$(34) + MusicFile + CHR$(34)
    End if

    FMUSIC_PlaySong(Handle)

    Print "FMOD test for freeBASIC"
    Print
    Print "(press any key to quit)"

    Sleep

    FMUSIC_FreeSong(Handle)
    FSOUND_Close

    End


Sub ErrorQuit (Message$)

  print "ERROR: "; Message$
  FMUSIC_FreeSong(Handle)
  FSOUND_Close
  End 1

End Sub

Play MP3
Code:
''
'' play all mp3 files at the path given in command-line (current dir if none)
''
'' uses the fmod library to do the hard work
''

option explicit
option escape

'$include: 'fmod.bi'

declare function     listmp3            ( path as string, mp3table() as string ) as integer
declare function     getmp3name        ( byval stream as integer ) as string
declare function     getmp3artist    ( byval stream as integer ) as string
declare function     getmp3album        ( byval stream as integer ) as string
declare sub         printmp3tags    ( byval stream as integer )

    dim stream as integer
    dim mp3table() as string
    dim songs as integer, currsong as integer
    dim doexit as integer
    dim shared streamended as integer
    
    ''
    songs = listmp3( command$, mp3table() )
  
    if( songs = 0 ) then
        print "No mp3 files found, usage: playmp3.exe path"
        sleep
        end 1
    end if
  
       ''
       if( FSOUND_GetVersion <= FMOD_VERSION ) then
          print "FMOD version " + str$(FMOD_VERSION) + " or greater required"
          end 1
       end If

       ''
       if( FSOUND_Init( 44100, 4, 0 ) = 0 ) then
          print "Can't initialize FMOD"
          end 1
       end if

       ''
       FSOUND_Stream_SetBufferSize( 50 )
          
       ''       
       currsong = 0
       doexit = 0
       do
           ''
           stream = FSOUND_Stream_Open( mp3table(currsong), FSOUND_MPEGACCURATE, 0, 0 )
           if( stream = 0 ) then
              print "Can't load music file \"" + mp3table(currsong) + "\""
            exit do
        end if
           
           ''
           print "Title:", getmp3name( stream )
           print "Album:", getmp3album( stream )
           print "Artist:", getmp3artist( stream )
      
           'printmp3tags stream

           ''
           streamended = 0
           
           FSOUND_Stream_Play( FSOUND_FREE, stream )
  
           ''
           print "(press P for previous, N for next or any other key to exit)"
           print
        
        dim key as string        
           do
              
              key = inkey$
              if( len( key ) > 0 ) then
                  select case ucase$( key )
                  case "P"
                      currsong = currsong - 1
                      if( currsong < 0 ) then currsong = songs - 1
                  case "N"
                      currsong = currsong + 1
                      if( currsong >= songs ) then currsong = 0
                  case else
                      doexit = -1
                  end select
                  exit do    
              
              else
                  
                  if( FSOUND_Stream_GetPosition( stream ) >= FSOUND_Stream_GetLength( stream ) ) then
                      currsong = currsong + 1
                      if( currsong >= songs ) then currsong = 0
                      exit do    
                  end if                      

              end if
              
              sleep 50
           loop
  
           FSOUND_Stream_Stop stream
           FSOUND_Stream_Close stream
    loop until( doexit )

    FSOUND_Close
    end
    
''::::
function byteptr2string( byval pbyte as byte ptr, byval lgt as integer ) as string
    dim text as string, ptext as byte ptr
    dim i as integer

    text = ""
    if( lgt > 0 ) then
        text = space$( lgt )
        ptext = strptr( text )
        for i = 1 to lgt
            *ptext = *pbyte
            ptext = ptext + 1
            pbyte = pbyte + 1
        next i
    end if
    
    byteptr2string = trim$( text )
    
end function


'':::::
sub printmp3tags( byval stream as integer )
    dim numtags as integer
       dim tagtype as integer, tagname as byte ptr, tagvalue as byte ptr, taglen as integer
       dim tag as integer

       FSOUND_Stream_GetNumTagFields( stream, @numtags )
  
       for tag = 0 to numtags-1
        FSOUND_Stream_GetTagField( stream, tag, @tagtype, @tagname, @tagvalue, @taglen )
        print byteptr2string( tagname, taglen )
    next tag

end sub

'':::::
function getmp3name( byval stream as integer ) as string
    dim tagname as byte ptr, taglen as integer
  
    FSOUND_Stream_FindTagField( stream, FSOUND_TAGFIELD_ID3V1, "TITLE", @tagname, @taglen )
    if( taglen = 0 ) then
        FSOUND_Stream_FindTagField( stream, FSOUND_TAGFIELD_ID3V2, "TIT2", @tagname, @taglen )
    end if
  
    getmp3name = byteptr2string( tagname, taglen )
    
end function

'':::::
function getmp3artist( byval stream as integer ) as string
    dim tagname as byte ptr, taglen as integer
  
    FSOUND_Stream_FindTagField( stream, FSOUND_TAGFIELD_ID3V1, "ARTIST", @tagname, @taglen )
    if( taglen = 0 ) then
        FSOUND_Stream_FindTagField( stream, FSOUND_TAGFIELD_ID3V2, "TPE1", @tagname, @taglen )
    end if
  
    getmp3artist = byteptr2string( tagname, taglen )
    
end function

'':::::
function getmp3album( byval stream as integer ) as string
    dim tagname as byte ptr, taglen as integer
  
    FSOUND_Stream_FindTagField( stream, FSOUND_TAGFIELD_ID3V1, "ALBUM", @tagname, @taglen )
    if( taglen = 0 ) then
        FSOUND_Stream_FindTagField( stream, FSOUND_TAGFIELD_ID3V2, "TALB", @tagname, @taglen )
    end if
  
    getmp3album = byteptr2string( tagname, taglen )
    
end function

'':::::
function listmp3( path as string, mp3table() as string ) as integer
    dim fname as string  
    dim maxsongs as integer, song as integer
    
       ''
       maxsongs = 20
       redim mp3table(0 to maxsongs-1) as string

    ''
#ifdef FB__WIN32
const pathdiv = "\\"    
#else
const pathdiv = "/"
#endif
    
    if( len( path ) > 0 ) then
        if( left$( path, 1 ) = "\"" ) then
            path = mid$( path, 2 )
        end if

        if( right$( path, 1 ) = "\"" ) then
            path = left$( path, len( path ) - 1 )
        end if
        
        if( right$( path, 1 ) <> pathdiv ) then
            path = path + pathdiv
        end if
    end if

    ''
    song = 0
    
    fname = dir$( path + "*.mp3" )
    do while( len( fname ) > 0 )
           
           if( song >= maxsongs ) then
               maxsongs = maxsongs + (maxsongs \ 2)
               redim mp3table(0 to maxsongs-1) as string
           end if
           
           mp3table(song) = path + fname
           song = song + 1
           
           fname = dir$( "" )
       loop

    listmp3 = song
    
end function

winmmhlp
Code:
''
'' MCI helper
''

option explicit
'$include: 'win\mmsystem.bi'

'':::::
function winmmCreate as integer

    winmmCreate = -1

end function

'':::::
function winmmRelease as integer

    winmmRelease = -1

end function

''
'' MIDI
''

'':::::
function winmmInitMidi as integer

    winmmInitMidi = mciSendString( "open sequencer", byval NULL, 0, 0 ) = 0

end function

'':::::    
function winmmEndMidi as integer
    
    winmmEndMidi = mciSendString( "close sequencer", byval NULL, 0, 0 ) = 0
    
end function

'':::::
function winmmPlayMidi( filename as string ) as integer
    
    if( mciSendString( "open " + filename + " alias mymidifile", byval NULL, 0, 0 ) <> 0 ) then
        winmmPlayMidi = 0
        exit function
    end if
    
    winmmPlayMidi = mciSendString( "play mymidifile", byval NULL, 0, 0 ) = 0
    
end function

'':::::
function winmmStopMidi( ) as integer
    
    mciSendString( "stop mymidifile", byval NULL, 0, 0 )

    winmmStopMidi = mciSendString( "close mymidifile", byval NULL, 0, 0 ) = 0
    
end function

''
'' WAV
''

'':::::
function winmmInitWave as integer

    winmmInitWave = -1

end function

'':::::
function winmmEndWave as integer

    winmmEndWave = -1

end function

'':::::
function winmmPlayWave( filename as string ) as integer

    winmmPlayWave = sndPlaySound( filename, SND_ASYNC )
    
end function

'':::::
function winmmStopWave as integer

    winmmStopWave = -1

end function

winmmhlp.bi (Is this even used?)
Code:
declare function winmmCreate as integer
declare function winmmRelease as integer
declare function winmmInitMidi as integer
declare function winmmEndMidi as integer
declare function winmmPlayMidi( filename as string ) as integer
declare function winmmStopMidi( ) as integer

declare function winmmInitWave as integer
declare function winmmEndWave as integer
declare function winmmPlayWave( filename as string ) as integer
declare function winmmStopWave as integer