BASS.NET API for the Un4seen BASS Audio Library

BassBASS_StreamCreate Method (Int32, Int32, BASSFlag, STREAMPROC, IntPtr)

BASS.NET API for the Un4seen BASS Audio Library
Creates a user sample stream.

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

[DllImportAttribute("bass")]
public static int BASS_StreamCreate(
	int freq,
	int chans,
	BASSFlag flags,
	STREAMPROC proc,
	IntPtr user
)

Parameters

freq
Type: SystemInt32
The default sample rate. The sample rate can be changed using BASS_ChannelSetAttribute(Int32, BASSAttribute, Single).
chans
Type: SystemInt32
The number of channels... 1 = mono, 2 = stereo, 4 = quadraphonic, 6 = 5.1, 8 = 7.1. More than stereo requires WDM drivers, and the SPEAKER flags are ignored.
flags
Type: Un4seen.BassBASSFlag
Any 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 stream is 16-bit.
BASS_SAMPLE_FLOATUse 32-bit floating-point sample data. WDM drivers or the BASS_STREAM_DECODE flag are required to use this flag. See Floating-point channels for more info.
BASS_SAMPLE_3DUse 3D functionality. This is ignored if BASS_DEVICE_3D wasn't specified when calling BASS_Init(Int32, Int32, BASSInit, IntPtr, IntPtr). 3D streams must be mono (chans=1). The SPEAKER flags can not be used together with this flag.
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.
proc
Type: Un4seen.BassSTREAMPROC
The user defined stream writing function (see STREAMPROC).

To create a DUMMY stream use the BASS_StreamCreateDummy(Int32, Int32, BASSFlag, IntPtr) method.

To create a PUSH stream use the BASS_StreamCreatePush(Int32, Int32, BASSFlag, IntPtr) method.

To create a DEVICE stream use the BASS_StreamCreateDevice(Int32, Int32, BASSFlag, IntPtr) method.

user
Type: SystemIntPtr
User instance data to pass to the callback function.

Return Value

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

Sample streams allow any sample data to be played through BASS, and are particularly useful for playing a large amount of sample data without requiring a large amount of memory. If you wish to play a sample format that BASS does not support, then you can create a stream and decode the sample data into it.

BASS can automatically stream MP3, MP2, MP1, OGG, WAV and AIFF files, using BASS_StreamCreateFile(String, Int64, Int64, BASSFlag), and also from HTTP and FTP servers, using BASS_StreamCreateURL(String, Int32, BASSFlag, DOWNLOADPROC, IntPtr), BASS_StreamCreateFileUser(BASSStreamSystem, BASSFlag, BASS_FILEPROCS, IntPtr) allows streaming from other sources too.

However, the callback method must deliver PCM sample data as specified, so opening an MP3 file and just passing that file data will not work here.

ERROR CODEDescription
BASS_ERROR_INITBASS_Init(Int32, Int32, BASSInit, IntPtr, IntPtr) has not been successfully called.
BASS_ERROR_NOTAVAILOnly decoding channels (BASS_STREAM_DECODE) are allowed when using the "no sound" device. The BASS_STREAM_AUTOFREE flag is also unavailable to decoding channels.
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.

Examples

A callback function to stream a file, in 44100hz 16-bit stereo:
private STREAMPROC _myStreamCreate;  // make it global, so that the GC can not remove it
private byte[] _data = null; // our local buffer
...
_myStreamCreate = new STREAMPROC(MyFileProc);
FileStream fs = File.OpenRead("test.raw");
int channel = Bass.BASS_StreamCreate(44100, 2, 
                   BASSFlag.BASS_DEFAULT, _myStreamCreate, IntPtr.Zero);
Bass.BASS_ChannelPlay(channel, false);
...
private int MyFileProc(int handle, IntPtr buffer, int length, IntPtr user)
{
  // implementing the callback for BASS_StreamCreate...
  // here we need to deliver PCM sample data

  // increase the data buffer as needed
  if (_data == null || _data.Length < length)
    _data = new byte[length];

  int bytesread = _fs.Read( _data, 0, length ); 
  Marshal.Copy( _data, 0, buffer, bytesread );
  if ( bytesread < length )
  {
    // set indicator flag
    bytesread |= (int)BASSStreamProc.BASS_STREAMPROC_END;
    _fs.Close();
  }
  return bytesread;
}
See Also

Reference