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.