BASS.NET API for the Un4seen BASS Audio Library

ENCODEPROC Delegate

BASS.NET API for the Un4seen BASS Audio Library
User defined callback function to process (receive) encoded sample data.

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

public delegate void ENCODEPROC(
	int handle,
	int channel,
	IntPtr buffer,
	int length,
	IntPtr user
)

Parameters

handle
Type: SystemInt32
The encoder that the data is from (as returned by BASS_Encode_Start(Int32, String, BASSEncode, ENCODEPROC, IntPtr)).
channel
Type: SystemInt32
The channel that the data is from.
buffer
Type: SystemIntPtr
The pointer to the buffer containing the encoded data.
length
Type: SystemInt32
The number of bytes in the buffer.
user
Type: SystemIntPtr
The user instance data given when BASS_Encode_Start(Int32, String, BASSEncode, ENCODEPROC, IntPtr) was called.
Remarks

To have the encoded data received by this callback function, the encoder needs to be told to output to STDOUT (instead of a file).

It is clever to NOT alloc any buffer data (e.g. a byte[]) everytime within the callback method, since ALL callbacks should be really fast! And if you would do a 'byte[] data = new byte[]' every time here...the GarbageCollector would never really clean up that memory. Sideeffects might occure, due to the fact, that BASS will call this callback too fast and too often...

NOTE: When you pass an instance of a callback delegate to one of the BASS functions, this delegate object will not be reference counted. This means .NET would not know, that it might still being used by BASS. The Garbage Collector might (re)move the delegate instance, if the variable holding the delegate is not declared as global. So make sure to always keep your delegate instance in a variable which lives as long as BASS needs it, e.g. use a global variable or member.

Examples

// the encoding callback
private ENCODEPROC _myEndoderProc;
private byte[] _encbuffer = new byte[1048510]; // 1MB buffer
...
_myEndoderProc = new ENCODEPROC(MyEncodingWriter);
// create the encoder (with default setting for lame)
BassEnc.BASS_Encode_Start(channel, "lame --alt-preset standard", 
        BASSEncode.BASS_ENCODE_DEFAULT, _myEndoderProc, IntPtr.Zero);
Bass.BASS_ChannelPlay(channel, false); // start the channel playing & encoding 
...
private bool MyEncodingWriter(int handle, int channel, IntPtr buffer, int length, IntPtr user)
{
    // copy from managed to unmanaged memory
    Marshal.Copy(buffer, _encbuffer, 0, length);
    // process the data in _encbuffer, e.g. write to disk or whatever
    ...
}
If you are into C# you might also use native pointer access in an unsafe code block:
C#
// assuming you have created a: BinaryWriter bw = new BinaryWriter(_fs);
private unsafe bool MyEncodingWriter(int handle, int channel, IntPtr buffer, int length, IntPtr user)
{
    byte *data = (byte*)buffer;
    // process the data in 'data', e.g. write to disk or whatever
    for (int a=0; a<length; a++)
    {
      // write the received sample data to a local file
      bw.Write( data[a] );
    }
}
See Also

Reference