BASS.NET API for the Un4seen BASS Audio LibraryBassWmaBASS_WMA_EncodeOpen Method BASS.NET API for the Un4seen BASS Audio Library
Initializes WMA encoding to a user defined function.

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

public static int BASS_WMA_EncodeOpen(
	int freq,
	int chans,
	BASSWMAEncode flags,
	int bitrate,
	IntPtr user


Type: SystemInt32
The sample rate in Hz, or a BASS channel handle if the BASS_WMA_ENCODE_SOURCE flag is specified.
Type: SystemInt32
The number of channels (1=mono, 2=stereo, etc.).
Type: Un4seen.Bass.AddOn.WmaBASSWMAEncode
Any combination of these flags (see BASSWMAEncode):
BASS_SAMPLE_8BITS8-bit sample data. If neither this or the BASS_SAMPLE_FLOAT flags are specified, then 16-bit data is expected.
BASS_SAMPLE_FLOAT32-bit floating-point sample data.
BASS_WMA_ENCODE_STANDARDUse standard WMA encoding. If neither this or the BASS_WMA_ENCODE_PRO flag is specified, then either codec could be used (whichever supports the requested sample format and bitrate).
BASS_WMA_ENCODE_PROUse WMA Professional encoding.
BASS_WMA_ENCODE_PCMWrite uncompressed PCM data in an ASF container. bitrate is ignored except that it should be non-0.
BASS_WMA_ENCODE_24BITEncode in 24-bit, else 16-bit. 24-bit encoding requires WMA Pro.
BASS_WMA_ENCODE_SCRIPTEnable the specification of tags mid-stream (after encoding has begun).
BASS_WMA_ENCODE_SOURCEUse the BASS channel with the handle in freq as the encoder's source. The chansparameter is ignored, as are the BASS_SAMPLE_8BITS and BASS_SAMPLE_FLOAT flags. If the BASSenc add-on is loaded, then the BASS_CONFIG_ENCODE_PRIORITY setting is used to determine where in the channel's DSP chain the encoding is performed, otherwise priority -1000 is used.
BASS_WMA_ENCODE_QUEUEQueue data to feed the encoder asynchronously. This prevents BASS_WMA_EncodeWrite(Int32, IntPtr, Int32) getting blocked by the encoder, but the application should control the rate at which data is encoded, as it is possible to queue too much data for the encoder to handle.
Type: SystemInt32
The encoding bitrate (in bits per second, e.g. 128000), or VBR quality (100 or less).
Type: Un4seen.Bass.AddOn.WmaWMENCODEPROC
The user defined function to receive the encoded data (see WMENCODEPROC).
Type: SystemIntPtr
User instance data to pass to the callback function.

Return Value

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

Encoding to a user defined function allows any storage or delivery method to be used for the encoded WMA data. For example, encoding to memory.

The WMA codec expects 16-bit or 24-bit sample data depending on the BASS_WMA_ENCODE_24BIT flag, but BASSWMA will accept 8-bit, 16-bit or floating-point data, and convert it to the appropriate format. Use BASS_WMA_EncodeGetRates(Int32, Int32, BASSWMAEncode) to retrieve a list of the encoding bitrates available for a specific sample format.

Use BASS_WMA_EncodeSetTag(Int32, IntPtr, IntPtr, BASSWMATag) for each tag you wish to set.

Use BASS_WMA_EncodeWrite(Int32, IntPtr, Int32) to encode sample data, and BASS_WMA_EncodeClose(Int32) to finish encoding.

ERROR CODEDescription
BASS_ERROR_WMAThe Windows Media modules (v9 or above) are not installed.
BASS_ERROR_NOTAVAILNo codec could be found to support the specified sample format and bitrate.
BASS_ERROR_UNKNOWNSome other mystery problem!


Private _myEndoderProc As WMENCODEPROC
Private _fs As FileStream
Private _encbuffer(1048510) As Byte ' 1MB buffer, should be enough
' open a wma file
_fs = File.OpenRead("test.wma")
' create the encoder (44kHz, stereo at 64kbps, no tags)
_myEndoderProc = New WMENCODEPROC(AddressOf MyEncodingWriter)
Dim encoder As Integer = BassWma.BASS_WMA_EncodeOpen(44100, 2, BASSWMAEncode.BASS_WMA_ENCODE_DEFAULT, 
                                 64000, _myEndoderProc, IntPtr.Zero)
Private Function MyEncodingWriter(handle As Integer, type As BASSWMAEncodeCallback, 
                                  buffer As IntPtr, length As Integer, user As IntPtr) As Boolean
  If type = BASSWMAEncodeCallback.BASS_WMA_ENCODE_HEAD Then
    ' rewind to start of file to write the header    
    _fs.Position = 0L.ToUInt32()
  End If

  If type = BASSWMAEncodeCallback.BASS_WMA_ENCODE_DONE Then
    ' done encoding - close the file
    ' copy the unmanaged buffer content to out managed buffer
    Marshal.Copy(buffer, _encbuffer, 0, length)
    ' process the data in _encbuffer
    _fs.Write(_encbuffer, 0, length)
  End If
End Function
// the encoding callback
private WMENCODEPROC _myEndoderProc;
private FileStream _fs;
private byte[] _encbuffer = new byte[1048510]; // 1MB buffer, should be enough
// open a wma file
_fs = File.OpenRead("test.wma");
// create the encoder (44kHz, stereo at 64kbps, no tags)
_myEndoderProc = new WMENCODEPROC(MyEncodingWriter);
int encoder = BassWma.BASS_WMA_EncodeOpen(44100, 2, BASSWMAEncode.BASS_WMA_ENCODE_DEFAULT, 
                      64000, _myEndoderProc, IntPtr.Zero); 
private bool MyEncodingWriter(int handle, BASSWMAEncodeCallback type, IntPtr buffer, int length, IntPtr user)
  if (type == BASSWMAEncodeCallback.BASS_WMA_ENCODE_HEAD)
    // rewind to start of file to write the header    
    _fs.Position = 0L;

  if (type == BASSWMAEncodeCallback.BASS_WMA_ENCODE_DONE)
    // done encoding - close the file
    // copy the unmanaged buffer content to out managed buffer
    Marshal.Copy(buffer, _encbuffer, 0, length);
    // process the data in _encbuffer
    _fs.Write(_encbuffer, 0, length);
NOTE: This is just an example. It's obviously simpler to use BASS_WMA_EncodeOpenFile(Int32, Int32, BASSWMAEncode, Int32, String) to encode to a file.
See Also