BASS.NET API for the Un4seen BASS Audio Library

BassBASS_MusicLoad Method (Byte, Int64, Int32, BASSFlag, Int32)

BASS.NET API for the Un4seen BASS Audio Library
Loads a MOD music file - MO3 / IT / XM / S3M / MTM / MOD / UMX formats.

This overload implements loading from memory.

Namespace:  Un4seen.Bass
Assembly:  Bass.Net (in Bass.Net.dll) Version: 2.4.17.5
Syntax

public static int BASS_MusicLoad(
	byte[] memory,
	long offset,
	int length,
	BASSFlag flags,
	int freq
)

Parameters

memory
Type: SystemByte
A managed memory location as a byte[].
offset
Type: SystemInt64
File offset to load the MOD music from.
length
Type: SystemInt32
Data length... 0 = use all data up to the end of file. If length over-runs the end of the file, it'll automatically be lowered to the end of the file.
flags
Type: Un4seen.BassBASSFlag
A combination of these flags (see BASSFlag):
BASS_SAMPLE_8BITSUse 8-bit resolution. If neither this or the BASS_SAMPLE_FLOAT flags are specified, then the sample data will be 16-bit.
BASS_SAMPLE_FLOATUse 32-bit floating-point sample data. WDM drivers or the BASS_MUSIC_DECODE flag are required to use this flag in Windows. See Floating-point channels for more info.
BASS_SAMPLE_MONODecode/play the MOD music in mono (uses less CPU than stereo). This flag is automatically applied if BASS_DEVICE_MONO was specified when calling BASS_Init(Int32, Int32, BASSInit, IntPtr, IntPtr).
BASS_SAMPLE_SOFTWAREForce the MOD music to not use hardware mixing.
BASS_SAMPLE_3DEnable 3D functionality. This is ignored if BASS_DEVICE_3D wasn't specified when calling BASS_Init(Int32, Int32, BASSInit, IntPtr, IntPtr). 3D channels are mono, so BASS_SAMPLE_MONO is automatically applied. The SPEAKER flags can not be used together with this flag.
BASS_SAMPLE_FXRequires DirectX 8 or above: Enable the old implementation of DirectX 8 effects. See the DX8 effect implementations section for details. Use BASS_ChannelSetFX(Int32, BASSFXType, Int32) to add effects to the music.
BASS_SAMPLE_LOOPLoop the music.
BASS_MUSIC_RAMPUse "normal" ramping (as used in FastTracker 2).
BASS_MUSIC_RAMPSUse "sensitive" ramping".
BASS_MUSIC_NONINTERUse non-interpolated mixing. This generally reduces the sound quality, but can be good for chip-tunes.
BASS_MUSIC_SINCINTERUse sinc interpolated sample mixing. This increases the sound quality, but also requires quite a bit more processing. If neither this or the BASS_MUSIC_NONINTER flag is specified, linear interpolation is used.
BASS_MUSIC_FT2MODPlay .MOD file as FastTracker 2 would.
BASS_MUSIC_PT1MODPlay .MOD file as ProTracker 1 would.
BASS_MUSIC_POSRESETStop all notes when seeking (using BASS_ChannelSetPosition(Int32, Int64, BASSMode)).
BASS_MUSIC_POSRESETEXStop all notes and reset bpm/etc when seeking.
BASS_MUSIC_SURROUNDApply XMPlay's surround sound to the music (ignored in mono).
BASS_MUSIC_SURROUND2Apply XMPlay's surround sound mode 2 to the music (ignored in mono).
BASS_MUSIC_FT2PANApply FastTracker 2 panning to XM files.
BASS_MUSIC_STOPBACKStop the music when a backward jump effect is played. This stops musics that never reach the end from going into endless loops. Some MOD musics are designed to jump all over the place, so this flag would cause those to be stopped prematurely. If this flag is used together with the BASS_SAMPLE_LOOP flag, then the music would not be stopped but any BASS_SYNC_END sync would be triggered.
BASS_MUSIC_PRESCANCalculate the playback length of the music, and enable seeking in bytes. This slightly increases the time taken to load the music, depending on how long it is. In the case of musics that loop, the length until the loop occurs is calculated. Use BASS_ChannelGetLength to retrieve the length.
BASS_MUSIC_NOSAMPLEDon't load the samples. This reduces the time taken to load the music, notably with MO3 files, which is useful if you just want to get the name and length of the music without playing it.
BASS_MUSIC_AUTOFREEAutomatically free the music when it ends. This allows you to play a music and forget about it, as BASS will automatically free the music's resources when it has reached the end or when BASS_ChannelStop(Int32) (or BASS_Stop) is called. Note that some musics never actually end on their own (ie. without you stopping them).
BASS_MUSIC_DECODEDecode the music into sample data, without outputting it. Use BASS_ChannelGetData(Int32, IntPtr, Int32) to retrieve decoded sample data. BASS_SAMPLE_SOFTWARE/3D/FX/AUTOFREE are ignored when using this flag, as are the SPEAKER flags.
BASS_SPEAKER_xxxSpeaker assignment flags. The BASS_SAMPLE_MONO flag is automatically applied when using a mono speaker assignment flag.
freq
Type: SystemInt32
Sample rate to render/play the MOD music at... 0 = the rate specified in the BASS_Init(Int32, Int32, BASSInit, IntPtr, IntPtr) call.

Return Value

Type: Int32
If successful, the loaded music's handle is returned, else 0 is returned. Use BASS_ErrorGetCode to get the error code.
Remarks

BASS uses the same code as XMPlay for it's MOD music support, giving the most accurate reproduction of MO3 / IT / XM / S3M / MTM / MOD / UMX files available from any sound system.

MO3s are treated and used in exactly the same way as normal MOD musics. The advantage of MO3s is that they can be a lot smaller with virtually identical quality. Playing a MO3 does not use any more CPU power than playing the original MOD version does. The only difference is a slightly longer load time as the samples are being decoded. MO3 files are created using the MO3 encoder available at the BASS website.

DMO effects (the same as available with BASS_ChannelSetFX(Int32, BASSFXType, Int32)) can be used in IT and XM files (and MO3 versions of them) created with Modplug Tracker. This allows effects to be added to a track without having to resort to an MP3 or OGG version, so it can remain small and still sound fancy. Of course, the effects require some CPU, so should not be used carelessly if performance is key. DirectX 8 (or above) is required for the effects to be heard - without that, the music can still be played, but the effects are disabled.

Ramping doesn't take a lot of extra processing and improves the sound quality by removing "clicks". Sensitive ramping leaves sharp attacked samples, while normal ramping can cause them to lose a-bit of their impact. Generally, normal ramping is recommended for XMs, and sensitive ramping for the other formats. But, some XMs may also sound better using sensitive ramping.

When loading a MOD music from memory, BASS does not use the memory after it's loaded the MOD music. So you can do whatever you want with the memory after calling this function. This means there is no need to pin the memory buffer for this method.

ERROR CODEDescription
BASS_ERROR_INITBASS_Init(Int32, Int32, BASSInit, IntPtr, IntPtr) has not been successfully called.
BASS_ERROR_NOTAVAILThe BASS_MUSIC_AUTOFREE flag is unavailable to decoding channels.
BASS_ERROR_FILEOPENThe file could not be opened.
BASS_ERROR_FILEFORMThe file's format is not recognised/supported.
BASS_ERROR_FORMATThe sample format is not supported by the device/drivers. If the stream is more than stereo or the BASS_SAMPLE_FLOAT flag is used, it could be that they are not supported.
BASS_ERROR_SPEAKERThe specified SPEAKER flags are invalid. The device/drivers do not support them, they are attempting to assign a stereo stream to a mono speaker or 3D functionality is enabled.
BASS_ERROR_MEMThere is insufficient memory.
BASS_ERROR_NO3DCould not initialize 3D support.
BASS_ERROR_UNKNOWNSome other mystery problem!

Platform-specific

Away from Windows, all mixing is done in software (by BASS), so the BASS_SAMPLE_SOFTWARE flag is unnecessary. The BASS_SAMPLE_FX flag is also ignored.

DMO effects are not supported in MOD music on Windows CE, and DirectX 8 (or above) is required on Windows. They are always available on other platforms, except for the following: Compressor, Gargle, and I3DL2Reverb. When a DMO effect is unavailable, the MOD music can still be played, but the effect will be disabled.

Examples

The following example demontrates how to load music from memory:
// Init Bass
Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
// open file
FileStream fs = File.OpenRead( "test.mod" );
// get the length of the file
int length = (int)fs.Length;
// create the buffer which will keep the file in memory
byte[] buffer = new byte[length];
// read the file into the buffer
fs.Read(buffer, 0, length);
// buffer is filled, file can be closed
fs.Close();
// load music from memory buffer
int music = Bass.BASS_MusicLoad(buffer, 0, length, BASSFlag.BASS_DEFAULT, 0);
if (music != 0 && Bass.BASS_ChannelPlay(stream, false) )
{
    // playing...
}
else
{
    Console.WriteLine("Error={0}", Bass.BASS_ErrorGetCode());
}
See Also

Reference