Sunday, May 31, 2015

How to add sound using BASS.dll






.
Written by Tal Alon,

CVI has no innate sound capabilities, so if you wish to add sounds to your software you need some outside help. The windows libraries can let you run WAV files and ActiveX controls can let you play anything, but they are complicated (ish) to use. In this post I will present the BASS audio library provided by Un4Seen -another option which in my opinion is the easiest to use and most powerful.

Keep reading to learn the basics of using BASS and make some sound!

Un4seen provides a library of functions which is at this point completely free for non commercial use (check the licensing agreement on their page), and they provide several affordable licensing options for commercial use.

Here is how you integrate it with your project:

Step 1: First you need to download the bass library files from the Un4Seen page. Choose the BASS link on the right and click on the relevant download link on the top of the page. 

Step 2: Unzip the downloaded file and from it copy the next three files into your CVI project's folder:
  • bass.dll (found directly inside the zip file)
  • bass.lib (found inside the 'C' folder)
  • bass.h (found inside the 'C' folder)
You do not need the rest of the files so feel free to delete them.

Step 3: Link the library to your project using these 3 files:
  • add #include "bass.h" to the upper part of your code so the precompiler will add all the bass function prototypes to your project.
  • in CVI's upper menubar click 'Edit' --> 'Add Files to Project', select the bass.lib file and confirm. this will add the file to your project and you will see it on your project tree.
  • The bass.dll file should be present in the same folder your exe is in (no need to add it to the project or do anything else). If you create a distribution kit make sure to add this file to it, as it contains the actual sound making code.
That is it, you are all set up to start using the BASS library. Here is how you do it:

At the top of your file create a variable from the type HSTREAM (defined in the header file) for each sound you wish to add (music, sound effect etc.), for example:

HSTREAM Shot_SND;   
HSTREAM Music_SND;

At the beginning of your program (the 'main' function is a good place) write:

BASS_Init( -1,44100, 0,0,NULL); 

and also load the various sound files into memory and associate them with the HSTREAM variables you prepared beforehand:

Shot_SND = BASS_StreamCreateFile(FALSE,"shot.mp3",0,0,0);
Music_SND = BASS_StreamCreateFile(FALSE,"fotv2.mp3",0,0,0); 

(an explanation about all the function arguments will be given in another post...maybe).

now, to play a sound, call the following function at the right moment:

BASS_ChannelPlay(Shot_SND,TRUE);

you can play several channels simultaneously.

to stop a sound use:

BASS_ChannelStop(Shot_SND); 

and  to change the volume of a sound use:

BASS_ChannelSetAttribute(Shot_SND, BASS_ATTRIB_VOL, vol);

when 'vol' is a float between 0.0 (Min) and 1.0 (Max).

After you are done using the sound, at the end of the program (just before 'main' exits), or at any other point you can release the memory allocated for the sound using:

BASS_StreamFree(Shot_SND);
BASS_StreamFree(Music_SND);

You can learn about BASS and more of the function in here.

Enjoy.

2 comments:

  1. Perfect explanation!

    ReplyDelete
  2. Thank you for nice article! I downloaded bass.dll from this library http://fix4dll.com/ and want you to ask one thing. Are all this files the same? Can i download it from other sources and it will work correctly ?

    ReplyDelete