Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: taj on January 18, 2007
-
Music Coding Tutorial II : Taj
More bass! stuff.
OK Bass is very subtley different from Bassmod. The code for bassmod is here:
http://dbfinteractive.com/index.php?topic=1209.0
Here is the code for Bass. Bass is about 100k, so only suitable for demos and large intros. However it is great for musicdisks and the like as it can read mo3 format which is far more compressed than say xm with the same quality.
ok so... the very basic playback in bass:
#include "bass.h"
Then to play the file:
void startMusic () {
HMUSIC hm;
BASS_Init (-1, 44100, 0, 0, NULL);
hm=BASS_MusicLoad(FALSE,"music.xm",0,0,BASS_SAMPLE_LOOP,0);
BASS_ChannelPlay(hm,FALSE);
}
Note the extra parameters to BASS_Init (the last two are ignored though ). Also the need to get the channel returned by BASS_MusicLoad to pass to BASS_ChannelPlay.
Lastly to stop and close down:
void endMusic() {
BASS_Stop();
BASS_Free();
}
Now its up to the musicians to waste megabytes on samples ;-)
-
Music Coding Tutorial III : Taj
How to use Bass to play more than one tune. Useful for musicdisks etc. Trivial stuff but it may save you some time.
#include "bass.h"
Next create a music handle for each track you wish to play, together with the name of the track:
#define NTRACKS 3
HMUSIC trackh[NTRACKS];
char *tracks[NTRACKS]={"track1.mo3", "track2.mo3", "track3.mo3"};
So next its necessary to intialise Bass and load each track. Here in loading the load command specifies Fast tracker 2 compatability and that samples ramp at the beginning and end (prevents clicking in some music). The sample will also loop. The frequency for all tracks is set to 44100 by the bass init, however this can be overridden in the MusicLoad if required.
void initMusic () {
HMUSIC hm;
int i;
// initialise Bass
BASS_Init (-1, 44100, 0, 0, NULL);
// load the tunes...
for ( i=0; i<NTRACKS; i++ )
trackh[i] = BASS_MusicLoad ( FALSE, tracks[i], 0, 0,
BASS_SAMPLE_LOOP | BASS_MUSIC_FT2MOD | BASS_MUSIC_RAMPS, 0);
}
So now you can start the first track like this:
BASS_ChannelPlay( trackh[0] , FALSE);
Next we need some simple code to move from one track to another. The actual details of the user input code are unimportant, what is important are the bass calls for this tutorial so here they are:
void nextTrack() {
static int currTrack=0;
// stop the current music
BASS_ChannelStop ( trackh[currTrack] );
currTrack++;
currTrack=currTrack%NTRACKS;
// play new track
BASS_ChannelPlay( trackh[currTrack] , FALSE);
}
Finally we need to kill the bass...
void endMusic() {
BASS_Stop();
BASS_Free();
}
Disclaimer: this code is adapted from existing code but is not identical, with variable names changed etc. for clarity and generality. It may have some syntax errors.
-
++ Karma.
Very useul stuff Taj. This is exactly the sort of thing that people need to get started in C++.
-
:clap: :clap: MEGABASS!! :clap: :clap: