Skip to content

Music System (music.nvgt)

Music System

This is an include that allows you to easily control music in your game. It can play anything from a stinger to a full music track, control looping, automatic playing, etc.





Updates the state of the music manager. This has to be called in your main loop, probably with the value of ticks() passed to it.

void music_manager::loop(uint64 t);


Play a music track with particular parameters.

bool music_manager::play(string track);


bool: true if the track was able to start playing, false otherwise.


Music tracks are specified using a very simple string format ("filename; flag; flag; option1=value; option2=value; flag; option3=value...").

Options are delimited by "; " excluding quotes. The only required setting is the track's main filename, which must be the first option provided. It does not matter in what order any other options or flags are set, the only rule is that the track's configuration string must start with it's main filename.

The difference between a flag and an option is that a flag is usually just a simple switch E. "stinger; ", while an option usually consists of a key/value pair e. "startpos=2.9"

When dealing with fades, values are usually in milliseconds, while when dealing with track durations they are usually specified in float seconds E.G. 4.555 for 4555 milliseconds.

####### List of possible options:


This system was originally made for Survive the Wild which needs to read sound data from strings not packs most of the time, so this class implements something more complicated than a music.pack variable being set. Someone feel free to add this functionality though or I may do later. Instead, we set a callback which receives a sound object and a filename, and calls the appropriate load method on that sound for your situation. Not needed if your sounds are simply on disk. A short example of a load callback is below.

void music_manager::set_load_callback(load_music_sound@ cb);


This is a basic example of how to write and set up a sound loading callback for use with the music manager.

sound@ music_load(sound@ sound_to_load, string filename_to_load) {
	// Usually a sound object will be provided to this function from the music manager. Encase not,
	if (@sound_to_load is null) @sound_to_load = sound();
	if( !sound_to_load.load(filename_to_load, pack_file)) return null; // indicates error.
	return s; // Return a handle to the loaded sound.

// ...


Stops any currently playing music.

void music_manager::stop(int fade = 0);




Determine if the music manager is currently playing a track or not.

bool music_manager::playing;


Controls the volume of any currently playing music in this music manager.

float music_manager::volume;