Author Topic: uFmod + Freebasic  (Read 5689 times)

0 Members and 1 Guest are viewing this topic.

Offline rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2325
  • Karma: 392
    • View Profile
    • http://www.rbraz.com/
uFmod + Freebasic
« on: August 06, 2006 »


Well, to make things easy, I have updated the uFmod include by MichaelW (freebasic forum)‚  to the latest version. (uFmod 1.18)

The best thing about this one is that it use Bin2Bas to include your music into your executable, Ok I know, it's the Holy Grall for democoders‚  ;)

Enjoy!


Test.bas
Code: [Select]
'--------------------------------------------------
'   uFmod example for Freebasic
'
'   uFmod.bi:
'       Version 0.1 by MichaelW
'       Version 0.2 by Rbraz - 05 Aug 2006
'
'   FreeBasic + Bin2Bas version by Rbraz - 2006
'
'--------------------------------------------------

ChDir ExePath
Option Explicit

'Includes
#include "windows.bi"
#include "ufmod.bi"

'Include our music
#include "Music.bas"

'Variables
Dim hWave As HWAVEOUT
Dim title_ptr as zstring ptr

'Allocate 50 bytes for music title
title_ptr = allocate(50)


'----------------Main

    'Play our music  (50601 = music length in bytes)
    hWave = uFMOD_PlaySong(@Music(0),50601,XM_MEMORY)
   
    'Get music title
    title_ptr = uFMOD_GetTitle()

    'Set music volume
    uFMOD_SetVolume(60)
   
    Cls
   
    print "Playing ... "; *title_ptr

    While Inkey$() <> Chr$(27)
       
        Locate 3,1
        Print "Time: "; uFMOD_GetTime()/1000
        Sleep 100
       
    Wend
   
    uFMOD_StopSong()

'----------------End
 


uFmod.bi
Code: [Select]
'---------------------------------------------------
' uFmod 1.18 - http://ufmod.sourceforge.net/
'
' uFmod.bi
' Version 0.1 by MichaelW
' Version 0.2 by Rbraz - 05 Aug 2006
'
'----------------------------------------------------

#include "win/mmsystem.bi"

#define XM_RESOURCE 0
#define XM_MEMORY   1
#define XM_FILE     2

' The uFMOD_PlaySong function starts playing an XM song.
'   --------------
'   Parameters:
'     lpXM
'        Specifies the song to play. If this parameter is NULL,
'        any currently playing song is stopped. In such a case, function
'        does not return a meaningful value.
'        fdwSong parameter determines whether this value is interpreted
'        as a filename, as a resource identifier or a pointer to an image
'        of the song in memory.
'     param
'        Handle to the executable file that contains the resource to be
'        loaded or size of the image of the song in memory. This parameter
'        is ignored unless XM_RESOURCE or XM_MEMORY is specified in fdwSong.
'     fdwSong
'        Flags for playing the song. The following values are defined
'        Value        Meaning
'        XM_FILE      lpXM points to filename.
'                     param is ignored.
'        XM_MEMORY    lpXM points to an image of a song in memory.
'                     param is the image size. Once, uFMOD_PlaySong
'                     returns, it's safe to free/discard the memory buffer.
'        XM_RESOURCE  lpXM Specifies the name of the resource.
'                     param identifies the module whose executable file
'                     contains the resource.
'                     The resource type must be RT_RCDATA.
'  Return Values:
'     Returns a pointer to HWAVEOUT on success or NULL otherwise.

declare function uFMOD_PlaySong stdcall alias "uFMOD_PlaySong" (byval as LPVOID , byval as DWORD, byval as DWORD) as HWAVEOUT

' The uFMOD_StopSong function stops the currently playing song, if any.
#define uFMOD_StopSong() uFMOD_PlaySong(0, 0, 0)

' The uFMOD_Pause function pauses the currently playing song, if any.
declare sub uFMOD_Pause stdcall alias "uFMOD_Pause" ()

' The uFMOD_Resume function resumes the currently paused song, if any.
declare sub uFMOD_Resume stdcall alias "uFMOD_Resume" ()

' The uFMOD_GetStats function returns the current RMS volume coefficients
'   in L and R channels.
'   --------------
'   Return Values:
'      low-order word : RMS volume in R channel
'      hi-order  word : RMS volume in L channel
'
declare function uFMOD_GetStats stdcall alias "uFMOD_GetStats" () as UINTEGER

' The uFMOD_GetTime function returns the time in milliseconds since the
'   song was started. This is useful for synchronizing purposes.
'   --------------
'   Return Values:
'      Returns the time in milliseconds since the song was started.
'
declare function uFMOD_GetTime stdcall alias "uFMOD_GetTime" () as UINTEGER

' The uFMOD_GetTitle function returns the current track's title, if any.
'   --------------
'   Return Values:
'      Returns the track's title in ASCIIZ format.
'
declare function uFMOD_GetTitle stdcall alias "uFMOD_GetTitle" () as ZSTRING ptr

' The uFMOD_SetVolume function sets the global volume.
'   --------------
'   0:  muting
'   64: maximum volume
'   NOTE: Any value above 64 maps to maximum volume too.
'   The volume scale is linear. Maximum volume is set by default.
'
declare sub uFMOD_SetVolume stdcall alias "uFMOD_SetVolume" (byval as DWORD)

Compile.bat
..\..\fbc -s console ufmod.o Test.bas

Ps: Don't forget to use the correct path for fbc.  Eg:  C:\FreeBasic\fbc -s console ufmod.o Test.bas


Check out the attach below...


[ Edited: music length  ::) ]
« Last Edit: September 06, 2009 by rbz »
rbz ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #1 on: August 06, 2006 »
Neat!!!
Have lots of good Karma Rbraz, I'll make this topic sticky.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #2 on: August 06, 2006 »
I just packed the test exe with upx and got it down to only 32kb. Really nice work Rbraz.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2325
  • Karma: 392
    • View Profile
    • http://www.rbraz.com/
Re: uFmod + Freebasic
« Reply #3 on: August 06, 2006 »
Yeah, this is really cool   8)  , and I will share the good karma with MichaelW  ;)
rbz ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #4 on: August 06, 2006 »
What I love about this is that it will make synching to music really easy, also the function to give L+R volume is very cool.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2325
  • Karma: 392
    • View Profile
    • http://www.rbraz.com/
Re: uFmod + Freebasic
« Reply #5 on: August 07, 2006 »
I just packed the test exe with upx and got it down to only 32kb......

Looking forward to see your first 64Kb  ;D
rbz ^ Codigos
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 886
  • Karma: 63
    • View Profile
    • zac-interactive
Re: uFmod + Freebasic
« Reply #6 on: August 08, 2006 »
Quite useful, great work.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #7 on: August 08, 2006 »
I just packed the test exe with upx and got it down to only 32kb......

Looking forward to see your first 64Kb ;D

Now where did I leave Werkzeug?!?! :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline norki

  • ZX 81
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
Re: uFmod + Freebasic
« Reply #8 on: August 09, 2006 »
Lo guys, first post here. I was a member on the last forum too. Didn't post too much tho  :P

I like this uFMOD thing! Playback isn't perfect, but it's free, and doesn't have any silly license (I think).

Quote
What I love about this is that it will make synching to music really easy, also the function to give L+R volume is very cool.
I had a go on synching a counter to a 60bpm/120bpm module, but failed. I've attached the xm I made to test this. My code went a little something like this (it's rbraz' example, slightly modified):
Code: [Select]
ChDir ExePath
Option Explicit

#include "windows.bi"
#include "ufmod.bi"

Dim hWave As HWAVEOUT
Dim title_ptr as zstring ptr
Dim buffer As Zstring * 512
buffer="kim3.xm"
title_ptr = allocate(50)
dim ctr as double
dim ctr2 as double
dim test1 as double 'this variable is meant to make up for any delay (if any, I think there is) upon loading the module...

hWave = uFMOD_PlaySong(@buffer,0,XM_FILE)
ctr=uFMOD_GetTime()
test1=ctr
title_ptr = uFMOD_GetTitle()

uFMOD_SetVolume(64)
print test1
print "Playing ... "; *title_ptr
   
ctr2=1
While Inkey$() <> Chr$(27)
       
   if uFMOD_GetTime >= ctr + (500-test1) then
      ctr=uFMOD_GetTime
      ctr2=ctr2+1
      if ctr2>4 then ctr2=1       
   endif
   locate 5,2
   print ctr2; " " ; ctr ; " " ; uFMOD_GetTime()
Wend
   
uFMOD_StopSong()

Any ideas?
« Last Edit: August 09, 2006 by norki »

Offline rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2325
  • Karma: 392
    • View Profile
    • http://www.rbraz.com/
Re: uFmod + Freebasic
« Reply #9 on: August 12, 2006 »
Hello dude, you will need to convert the result of uFMOD_GetTime() (millisecs) into seconds, just use  -->  uFMOD_GetTime() / 1000
Check out the example below, hope this is what you looking for :)

Code: [Select]
ChDir ExePath
Option Explicit

#include "windows.bi"
#include "ufmod.bi"

Dim hWave As HWAVEOUT
Dim title_ptr as zstring ptr
Dim buffer As Zstring * 512
buffer="kim3.xm"
title_ptr = allocate(50)

'Kim3.xm lenght = 8 seconds
dim as double music_time = 8

'Test when time reach to 3 seconds
dim as double music_time_test = 3

'Music elapsed time in seconds
dim as double elapsed_time


hWave = uFMOD_PlaySong(@buffer,0,XM_FILE)
title_ptr = uFMOD_GetTitle()

cls

uFMOD_SetVolume(64)
Locate 1,1

if *title_ptr <> "" then
    print "Playing ... "; *title_ptr
else
    print "Playing ... No Title!"
end if

While Inkey$() <> Chr$(27)
   
    'Elapsed time in seconds
    elapsed_time = uFMOD_GetTime() / 1000
   
    Locate 3,2
    Print "Elapsed Time: ";elapsed_time
   
    if (elapsed_time >= music_time) then
        uFMOD_StopSong()
        Sleep(2000)
        hWave = uFMOD_PlaySong(@buffer,0,XM_FILE)
    end if
       
    if (elapsed_time >= music_time_test) then
        Locate 5,2
        Print "Song Time >= 3 seconds"
    else
        Locate 5,2
        Print "                      "
    end if
   
Wend
   
uFMOD_StopSong()
rbz ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 562
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: uFmod + Freebasic
« Reply #10 on: August 12, 2006 »
Not sure if im doing something wrong, i keep on getting compilation errors . . .

Command executed:
"C:\Program Files\FreeBASIC\fbc.exe" "C:\Documents and Settings\DrewPee\Desktop\FreeBasic\FreeBasic+ufmod\Test.bas"

Compiler output:
C:\Documents and Settings\DrewPee\Desktop\FreeBasic\FreeBasic+ufmod\Test.o:fake:(.text+0x55): undefined reference to `uFMOD_PlaySong@12'
C:\Documents and Settings\DrewPee\Desktop\FreeBasic\FreeBasic+ufmod\Test.o:fake:(.text+0x5d): undefined reference to `uFMOD_GetTitle@0'
C:\Documents and Settings\DrewPee\Desktop\FreeBasic\FreeBasic+ufmod\Test.o:fake:(.text+0x67): undefined reference to `uFMOD_SetVolume@4'
C:\Documents and Settings\DrewPee\Desktop\FreeBasic\FreeBasic+ufmod\Test.o:fake:(.text+0xd0): undefined reference to `uFMOD_GetTime@0'
C:\Documents and Settings\DrewPee\Desktop\FreeBasic\FreeBasic+ufmod\Test.o:fake:(.text+0xef): undefined reference to `uFMOD_PlaySong@12'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 0.16 for win32 (target:win32)
OS:    Windows XP (build 2600, Service Pack 2)

Any ideas?

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2325
  • Karma: 392
    • View Profile
    • http://www.rbraz.com/
Re: uFmod + Freebasic
« Reply #11 on: August 12, 2006 »
You must compile your prog. with ufmod.o (library),  so ,use "Compile.bat" to make it (download my first example)

Check the correct path for fbc (inside of Compile.bat file). 
Example: 
C:\FreeBasic\fbc -s console ufmod.o Test.bas
rbz ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #12 on: August 13, 2006 »
That caught me out too Drew, I kludged my way around it to be honest with you. Rbraz has the most elegant solution there but I will post a step by step guide to getting your programs to compile and create an example zip later today for everyone who's stuck.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7145
  • Karma: 69
    • View Profile
Re: uFmod + Freebasic
« Reply #13 on: August 21, 2006 »
Alternatively If your using FBIDE.

Go into View -> Settings which will bring up the FBide Settings then choose the Freebasic tab.
And under Compiler Command type in:

Code: [Select]
"<$fbc>" "<$file>" ufmod.o  -s gui
« Last Edit: August 22, 2006 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #14 on: August 22, 2006 »
Thanks for the tip Clyde.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline .:] Druid [:.

  • freebasic n00b
  • Pentium
  • *****
  • Posts: 563
  • Karma: 47
    • View Profile
    • Intro-Inferno
Re: uFmod + Freebasic
« Reply #15 on: November 12, 2006 »
 :||  yes, it works :)  :||  thanks for sharing!
[sheep]: im sure he wants to goto prison.. they didnt get him last time.. he was promised a big cock up his arse.. and no doubt looking forward to it.. lets hope he gets his wish this year.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1304
  • Karma: 88
    • View Profile
Re: uFmod + Freebasic
« Reply #16 on: May 05, 2007 »
hey guys how exactly would i go about retriving left and right audio vol do i have to shl and shr to get the values?

also is there a way of getting to the audio data at whatever position the audio bufferis at?

cheers
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16787
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: uFmod + Freebasic
« Reply #17 on: May 05, 2007 »
Mmm. I am not sure if this is possible with Ufmod.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1304
  • Karma: 88
    • View Profile
Re: uFmod + Freebasic
« Reply #18 on: May 05, 2007 »
i know that getting to the left and right audio volums is possible via

Code: [Select]
' The uFMOD_GetStats function returns the current RMS volume coefficients
'   in L and R channels.
'   --------------
'   Return Values:
'      low-order word : RMS volume in R channel
'      hi-order  word : RMS volume in L channel
'
declare function uFMOD_GetStats stdcall alias "uFMOD_GetStats" () as UINTEGER

but im not sure exactly how to get to the high and low words ( l/r channels )

and as for the getting to the audio data i dont think it is possible but thought id ask.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: uFmod + Freebasic
« Reply #19 on: May 06, 2007 »
The high word and low word are just the top and bottom 16bits of the result - it's a shortcut to return two values in one parameter.
Something like this will split them up again
Code: [Select]
dim high, low as uinteger
dim value as uinteger

value=&H12345678
high=value shr 16
low=value and &H0000ffff

print hex$(high),hex$(low)

It's important to use uinteger, not integer.

Jim

Challenge Trophies Won: