BASS.NET API for the Un4seen BASS Audio Library

BassBASS_SampleGetChannel Method

BASS.NET API for the Un4seen BASS Audio Library
Creates/initializes a playback channel for a sample.

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

[DllImportAttribute("bass", CharSet = CharSet.Unicode)]
public static int BASS_SampleGetChannel(
	int handle,
	BASSFlag flags


Type: SystemInt32
Handle of the sample to play.
Type: Un4seen.BassBASSFlag
A combination of these flags (see BASSFlag):
BASS_SAMCHAN_NEWDo not recycle/override one of the sample's existing channels.
BASS_SAMCHAN_STREAMCreate a stream (HSTREAM) rather than a sample channel (HCHANNEL).
BASS_SAMPLE_LOOPLooped? Note that only complete sample loops are allowed, you can't loop just a part of the sample. More fancy looping can be achieved by streaming the file.
BASS_SAMPLE_OVER_VOLOverride: the channel with the lowest volume is overriden.
BASS_SAMPLE_OVER_POSOverride: the longest playing channel is overriden.
BASS_SAMPLE_OVER_DISTOverride: the channel furthest away (from the listener) is overriden (3D samples only).
BASS_STREAM_AUTOFREEAutomatically free the stream's resources when it has reached the end, or when BASS_ChannelStop(Int32) (or BASS_Stop) is called.
BASS_STREAM_DECODEDecode the sample data, without outputting it. Use BASS_ChannelGetData(Int32, IntPtr, Int32) to retrieve decoded sample data. The BASS_SAMPLE_SOFTWARE, BASS_SAMPLE_3D, BASS_SAMPLE_FX, BASS_STREAM_AUTOFREE and SPEAKER flags can not be used together with this flag.
BASS_SPEAKER_xxxSpeaker assignment flags.

Return Value

Type: Int32
If successful, the handle of the new channel is returned, else 0 is returned. Use BASS_ErrorGetCode to get the error code.

A sample can be played in two ways: with a sample channel (HCHANNEL) or a stream (HSTREAM). A sample channel is a bit more CPU and memory efficient, while a stream has more features available, eg. DSP/FX and synchronization. In either case, they all share the same sample data, and just have their own individual state information (volume/position/etc).

Use BASS_SampleGetInfo(Int32, BASS_SAMPLE) and BASS_SampleSetInfo(Int32, BASS_SAMPLE) to set a sample's default attributes, which are used when creating a channel. After creation, a channel's attributes can be changed via BASS_ChannelSetAttribute(Int32, BASSAttribute, Single), BASS_ChannelSet3DAttributes(Int32, BASS3DMode, Single, Single, Int32, Int32, Int32) and BASS_ChannelSet3DPosition(Int32, BASS_3DVECTOR, BASS_3DVECTOR, BASS_3DVECTOR). BASS_Apply3D should be called before starting playback of a 3D sample, even if you just want to use the default settings.

The number of sample streams (HSTREAM) is unlimited. The number of sample channels (HCHANNEL) is limited to the sample's max value (set during creation or via BASS_SampleSetInfo(Int32, BASS_SAMPLE)), and existing channels can be overridden when needed for new ones. When channel overriding has been enabled (via a BASS_SAMPLE_OVER flag) and there are multiple candidates for overriding (eg. with identical volume), the oldest of them will be chosen to make way for the new channel.

If a sample has a maximum number of simultaneous playbacks of 1 (the max parameter was 1 when calling BASS_SampleLoad(String, Int64, Int32, Int32, BASSFlag) or BASS_SampleCreate(Int32, Int32, Int32, Int32, BASSFlag)), then the HCHANNEL handle returned will be identical to the HSAMPLE handle. That means you can use the HSAMPLE handle with functions that usually require a HCHANNEL handle, but you must still call this function first to initialize the channel.

A sample channel is automatically freed when it's overridden by a new channel, or when stopped manually via BASS_ChannelStop(Int32), BASS_SampleStop(Int32) or BASS_Stop. If you wish to stop a channel and re-use it, it should be paused (BASS_ChannelPause(Int32)) instead of stopped. Determining whether a channel still exists can be done by trying to use the handle in a function call, eg. BASS_ChannelGetAttribute(Int32, BASSAttribute, Single).

When channel overriding has been enabled via a BASS_SAMPLE_OVER flag and there are multiple candidates for overriding (eg. with identical volume), the oldest of them will be chosen to make way for the new channel.

A new sample channel (HCHANNEL) will have an initial state of being paused (BASS_ACTIVE_PAUSED). This prevents the channel being claimed by another call of this function before it has been played, unless it gets overridden due to a lack of free channels.

All of a sample's channels share the same sample data, and just have their own individual playback state information (volume/position/etc).

ERROR CODEDescription
BASS_ERROR_HANDLEhandle is not a valid sample handle.
BASS_ERROR_NOCHANThe sample has no free channels... the maximum number of simultaneous playbacks has been reached, and no BASS_SAMPLE_OVER flag was specified for the sample or onlynew = .
BASS_ERROR_TIMEOUTThe sample's minimum time gap (BASS_SAMPLE) has not yet passed since the last channel was created.
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.


Play a sample with it's default settings:
int sample = Bass.BASS_SampleLoad("test.wav", 0L, 0, 1, BASSFlag.BASS_DEFAULT);
int channel = Bass.BASS_SampleGetChannel(sample, false); // get a sample channel
Bass.BASS_ChannelPlay(channel, false); // play it
See Also