Author Topic: BASS Audio Library  (Read 1204 times)

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
BASS Audio Library
« on: September 27, 2014 »
Here's a library/extension for the BASS Audio library. It's not complete (recording, for example has not been included), but I think it's enough for most types of games :)  The example covers the basic stuff, look at the documentation for more information.

The zip contains an mp3 file, that's why it's so large.

Edit 2014-09-29  Added missing function BASS_ChannelSetAttribute.

Code: [Select]
rem BASS example, by John Master.
rem
rem Requires bass_next.dll and bass.dll while running and bass.lib
rem when compiling.
rem
rem This is just the basics, please look at the documentation for more
rem information.

import "bass.lib"

if not BASS_Init() then end

rem The first parameter after the sample filename is the max number of
rem instances of the sample that can be played at the same time. The
rem second parameter can contain several flags OR:ed together. The
rem only flag used here is BASS_SAMPLE_LOOP, that makes one of the
rem samples loop.
sample1 = BASS_SampleLoad("assets/sound1.wav", 1, 0)
sample2 = BASS_SampleLoad("assets/sound2.wav", 4, 0)
sample3 = BASS_SampleLoad("assets/sound3.wav", 1, BASS_SAMPLE_LOOP)

rem Load music (XM, MOD, IT ...) and let it loop when played.
pieceOfMusic = BASS_MusicLoad("assets/music.mod", BASS_MUSIC_LOOP OR BASS_SAMPLE_FX)
echoFX = 0
reverbFX = 0

rem Create stream (MP3, OGG ...) from file. Could have added
rem BASS_SAMPLE_LOOP to make it loop when playing.
fileStream = BASS_StreamCreateFile("assets/stream.mp3", 0)

wln "Instructions"
wln
wln "1 - Play first sample"
wln "2 - Play second sample"
wln "3 - Play third sample, that loops"
wln "4 - Stop playing third sample"
wln
wln "5 - Play music"
wln "6 - Stop music"
wln
wln "7 - Play file stream"
wln "8 - Pause file stream"
wln
wln "9 - Turn on/off echo effect on music"
wln "0 - Turn on/off reverb effect on music"

do
rem Play sounds. Second parameter is the sound volume [0..1].
if keydown("1", true) then _BASS_SamplePlay sample1, 1.0
if keydown("2", true) then _BASS_SamplePlay sample2, 1.0
if keydown("3", true) then _BASS_SamplePlay sample3, 0.5
rem Stop looping of third sample.
if keydown("4", true) then _BASS_SampleStop sample3
rem Play music, restart if second parameter is true.
if keydown("5", true) then _BASS_ChannelPlay pieceOfMusic, true
if keydown("6", true) then _BASS_ChannelStop pieceOfMusic
rem Play file stream.
if keydown("7", true) then _BASS_ChannelPlay fileStream, false
if keydown("8", true) then _BASS_ChannelPause fileStream
rem Effects.
if keydown("9", true)
if echoFX
_BASS_ChannelRemoveFX pieceOfMusic, echoFX
echoFX = 0
else
echoFX = BASS_ChannelSetFX(pieceOfMusic, BASS_FX_DX8_ECHO, 1)
rem You can use BASS_FXSetEcho to change properties of this effect.
endif
endif
if keydown("0", true)
if reverbFX
_BASS_ChannelRemoveFX pieceOfMusic, reverbFX
reverbFX = 0
else
reverbFX = BASS_ChannelSetFX(pieceOfMusic, BASS_FX_DX8_ECHO, 1)
rem You can use BASS_FXSetReverb to change properties of this effect.
endif
endif
wait 16
until keydown(27, true)

rem You can free the handles to save some memory, but all memory is
rem automaticly released when your program exits.
_BASS_SampleFree sample1
_BASS_SampleFree sample2
_BASS_SampleFree sample3
_BASS_MusicFree pieceOfMusic
_BASS_StreamFree fileStream
« Last Edit: September 29, 2014 by John »

kcfb

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: BASS Audio Library
« Reply #1 on: September 27, 2014 »
Hi John.....I have no knowledge of BASS but you mentioned in another post that "none of them are really free to use, like audiere."....are there any implications in that? Thanks - Kevin.

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
Re: BASS Audio Library
« Reply #2 on: September 27, 2014 »
Hi John.....I have no knowledge of BASS but you mentioned in another post that "none of them are really free to use, like audiere."....are there any implications in that? Thanks - Kevin.

As long as you're not making money from your product you can use use BASS without worries:

Quote
BASS is free for non-commercial use. If you are a non-commercial entity (eg. an individual) and you are not making any money from your product (through sales, advertising, etc), then you can use BASS in it for free. Otherwise, one of the following licences will be required.

I'm not sure how far this stretches. Let's say you make a free game, create a video about it and put it on YouTube. Are you then not allowed to make money on ads?

The other licenses (http://www.un4seen.com/), even for shareware, are quite costy, same goes for FMOD. This makes BASS an ok deal for me, because I'm not in it for the money. But I don't think, after all, that it's suitable for it to be a "part" of NaaLaa (put in the core).

I will continue adding features to the library, because you can do quite interesting things with BASS :)


« Last Edit: September 27, 2014 by John »

kcfb

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: BASS Audio Library
« Reply #3 on: September 28, 2014 »
Thanks for the explanation John - for what it's worth, I think that your idea of developing it as a library is the best way forward too. Kevin.

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
Re: BASS Audio Library
« Reply #4 on: September 28, 2014 »
I've updated the original post and zip.

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: BASS Audio Library
« Reply #5 on: September 28, 2014 »
 Looking good. Should we rewrite all of our old programs?

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
Re: BASS Audio Library
« Reply #6 on: September 28, 2014 »
Should we rewrite all of our old programs?

Absolutely not. I will replace audiere with something, just don't know what yet.

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: BASS Audio Library
« Reply #7 on: September 28, 2014 »
 Years ago, when I was playing with C#, I used the DLL-- WinMM.dll for sound effects. Is there some way to do that with NaaLaa?

 
Code: [Select]
    public class Sounds
    {
        [DllImport("WinMM.dll")]

        public static extern bool PlaySound(string fname, int Mod, int flag);

        // these are the SoundFlags we are using here, check mmsystem.h for more
        public static int SND_PURGE = 0x0040; // purge non-static events
        public static int SND_ASYNC = 0x0001; // play asynchronously
        public static int SND_SYNC = 0x0000; // play synchronously (default)
        public static int SND_NODEFAULT = 0x0002; // silence (!default) if sound not found
        public static int SND_MEMORY = 0x0004; // pszSound points to a memory file
        public static int SND_LOOP = 0x0008; // loop the sound until next sndPlaySound
        public static int SND_NOSTOP = 0x0010; // don't stop any currently playing sound
        public static int SND_NOWAIT = 0x00002000; // don't wait if the driver is busy
        public static int SND_ALIAS = 0x00010000; // name is a registry alias
        public static int SND_ALIAS_ID = 0x00110000; // alias is a predefined ID
        public static int SND_FILENAME = 0x00020000; // name is file name
        public static int SND_RESOURCE = 0x00040004; // name is resource name or atom

        public static void Play(string fname)
        {
            //StopPlay();
            PlaySound(fname, 0, SND_ASYNC | SND_NODEFAULT | SND_FILENAME | SND_NOWAIT);
        }

        public static void Stop()
        {
            PlaySound(null, 0, SND_PURGE);
        }
    }

 

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
Re: BASS Audio Library
« Reply #8 on: September 28, 2014 »
Only if you wrap it up in an extension, I'm afraid. How advanced is that command? The sound commands in naalaa are very simple, so maybe it's possible to use your idea for sound effects and something else for music (there are atleast simple mod/xm player libraries out there). Then one can use this for the standard stuff (put in the core) and have the BASS extension when a little more is needed. Then we'll get rid of audiere and its dll.


Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 541
    • View Profile
Re: BASS Audio Library
« Reply #9 on: September 28, 2014 »
I used the Windows sound stuff for all of my old (pre-naalaa) games. Works very fine, but I can't remember if you can even change the volume of the sound you're playing?

I've seen the audiere bug too, in Robowack 2, so we really should get rid of it. I say, go with Rick's suggestion and find an additional player lib for music.

Nice work with BASS :)
.\\\opz

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: BASS Audio Library
« Reply #10 on: September 28, 2014 »
 The sound commands in WinMM are too complicated for me to use, and there's a lot of them. I only used a couple of them.

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: BASS Audio Library
« Reply #11 on: February 16, 2015 »
 Today I gave the Winmm thing another try and just goofed it up.
What am I doing wrong?
 I'm trying do what I can do with Code Blocks Mingw, which looks like this:
      PlaySound ( "\beep1.wav " ,NULL, SND_FILENAME | SND_ASYNC ) ;

Code: [Select]
visible:
rem DLL handle.
myDLL
  s1$
hidden:

rem Load DLL and check if the returned handle is valid.
 rem myDLL = DLL_LOAD("MyExtension.dll")
    myDLL = DLL_LOAD("winmm.dll")

if myDLL >= 0
wln " winmm.dll was successfully loaded!"
  s1$ = "Sound1.wav"
   proc PlaySound
else
wln "Could not load DLL. Press any key to exit ..."
wait keydown
end
endif

rem wln AddTwoNumbers(10, 20)
rem wln MultiplyTwoFloats(2.5, 3.7)
rem wln ReverseString("Hello there, chum!")
wait keydown

procedure PlaySound()
 
params?
params.string$ = s1
  params.p1 = 0
  params.p2 = 0
wln params.string$
 rem  PlaySoundA "c:/windows/media/tada.wav",0,SND_ASYNC
DLL_CALL myDLL, "PlaySoundA",params

endproc

function AddTwoNumbers(a, b)
params?
params.first = a
params.second = b
DLL_CALL myDLL, "AddTwoNumbers", params
return params.added
endfunc

function MultiplyTwoFloats#(a#, b#)
params?
params.first# = a
params.second# = b
DLL_CALL myDLL, "MultiplyTwoFloats", params
return params.result#
endfunc

function ReverseString$(s$)
params?
params.string$ = s
DLL_CALL myDLL, "ReverseString", params
return params.string$
endfunc
« Last Edit: February 17, 2015 by Rick3137 »

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
Re: BASS Audio Library
« Reply #12 on: February 18, 2015 »
Sorry, you need to create a "wrapper" dll (extension) for naalaa to access that functionailty.

I've created one and attached it to this post. I gave the extension the name ksink, which is kind of classic for something that contains random stuff that really doesn't fit elsewhere. Marcus taught me. So we can fill it up with other small and arbitrary functions later on, not just PlaySound.

I did not remove the visual studio workspace, but you only need to look at the Example folder. Import ksink.lib and put ksink.dll in the same folder as your program. ksink.txt is just there for your reference.

Code: [Select]
rem PlaySound example.

import "ksink.lib"

do
if keydown(" ", true)
_PlaySound "assets/boom.wav"
endif
wait 16
until keydown(27, true)

Are there any requests for other functions we can put in this extension?

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: BASS Audio Library
« Reply #13 on: February 18, 2015 »
  Thank you,  Works good. I was just trying to figure out how to avoid large Sound Dlls.
  I still might try Bass in the future, but I do not want to be stuck with it for everything.

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 541
    • View Profile
Re: BASS Audio Library
« Reply #14 on: February 20, 2015 »
I think you should add generic Windows related stuff that people have been asking about throughout the years, like changing the title of the program window and showing simple popup dialogs (without having to go through IUP). Maybe create a "wishlist" thread?
.\\\opz