BASS.NET API for the Un4seen BASS Audio LibraryBassEncBASS_Encode_StartACMFile Method (Int32, IntPtr, BASSEncode, String)BASS.NET API for the Un4seen BASS Audio Library
Sets up an encoder on a channel, using an ACM codec and writing the output to a file.

This overload represents the Unicode version for the file name. The BASS_UNICODE flag will automatically be added if missing.

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

public static int BASS_Encode_StartACMFile(
	int handle,
	IntPtr form,
	BASSEncode flags,
	string filename


Type: SystemInt32
The channel handle... a HSTREAM, HMUSIC, or HRECORD.
Type: SystemIntPtr
ACM codec output format (buffer as returned by BASS_Encode_GetACMFormat(Int32, IntPtr, Int32, String, BASSACMFormat)).
Type: Un4seen.Bass.AddOn.EncBASSEncode
A combination of these flags BASSEncode:
BASS_ENCODE_NOHEADDon't write a WAVE header to the file.
BASS_ENCODE_RF64Write an RF64 header instead of a standard RIFF header, allowing the file to go beyond 4GB in size. This flag is ignored if the BASS_ENCODE_NOHEAD flag is used.
BASS_ENCODE_QUEUEQueue data to feed the encoder asynchronously. This prevents the data source (DSP system or BASS_Encode_Write(Int32, IntPtr, Int32) call) getting blocked by the encoder, but if data is queud more quickly than the encoder can process it, that could result in lost data.
BASS_ENCODE_PAUSEStart the encoder paused.
BASS_ENCODE_AUTOFREEAutomatically free the encoder when the source channel is freed.
Type: SystemString
The filename to write.

Return Value

Type: Int32
The encoder handle is returned if the encoder is successfully started, else 0 is returned. Use BASS_ErrorGetCode to get the error code.

The ACM encoder allows installed ACM (Audio Compression Manager) codecs to be used for encoding. The codec used is determined by the contents of the form parameter. The BASS_Encode_GetACMFormat(Int32, IntPtr, Int32, String, BASSACMFormat) function can be used to initialize that.

Unless the BASS_ENCODE_NOHEAD flag is specified, a WAVE header and the form contents will be written to the file. This is generally required for the file to be playable, but in some cases (eg. MP3) it's not. Standard RIFF WAV files are limited to a little over 4GB in size, so BASSenc will automatically stop encoding at that point. That size limit can be overcome with an RF64 file. When writing an RF64 WAV file, a standard RIFF header will still be written initially, which will only be replaced by an RF64 header at the end if the file size has exceeded the standard limit.

Internally, the sending of sample data to the encoder is implemented via a DSP callback on the channel. That means when you play the channel (or call BASS_ChannelGetData(Int32, IntPtr, Int32) if it's a decoding channel), the sample data will be sent to the encoder at the same time. The encoding is performed in the DSP callback. There isn't a separate process doing the encoding, as when using an external encoder via BASS_Encode_Start(Int32, String, BASSEncode, ENCODEPROC, IntPtr).

By default, the encoder DSP has a priority setting of -1000, which determines where in the DSP chain the encoding is performed. That can be changed using the BASS_CONFIG_ENCODE_PRIORITY config option (see BASS_SetConfig(BASSConfig, Int32)).

Besides the automatic DSP system, data can also be manually fed to the encoder via the BASS_Encode_Write(Int32, IntPtr, Int32) function. Both methods can be used together, but in general, the "automatic" system ought be paused when using the "manual" system, by use of the BASS_ENCODE_PAUSE flag or the BASS_Encode_SetPaused(Int32, Boolean) function.

When done encoding, use BASS_Encode_Stop(Int32) to close the encoder.

Multiple encoders can be set on a channel. For simplicity, the encoder functions will accept either an encoder handle or a channel handle. When using a channel handle, the function is applied to all encoders that are set on that channel.

ERROR CODEDescription
BASS_ERROR_HANDLEhandle is not valid.
BASS_ERROR_NOTAVAILThe codec specified in form couldn't be initialized.
BASS_ERROR_CREATEThe file couldn't be created.
BASS_ERROR_UNKNOWNSome other mystery problem!


Setup an ACM encoder with a callback to process the encoded data:
' get suggested (maximum) format buffer size
Dim formlen As Integer = BassEnc.BASS_Encode_GetACMFormat(0, IntPtr.Zero, 0, Nothing, BASSACMFormat.BASS_ACM_NONE)
' create a buffer for the codec
Dim buffer(formlen - 1) As Byte
' now create a pinned handle, so that the Garbage Collector will not move this object
Dim hGC As GCHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned)
' get the pointer to that pinned object
Dim codec As IntPtr = hGC.AddrOfPinnedObject()
' automatically suggest an MP3 codec
If BassEnc.BASS_Encode_GetACMFormat(handle, codec, formlen, Nothing, 
                                CInt(WAVEFormatTag.MPEGLAYER3)), BASSACMFormat)) > 0 Then
  ' begin encoding
  Dim encHandle As Integer = BassEnc.BASS_Encode_StartACMFile(channel, codec, 
                                     BASSEncode.BASS_ENCODE_NOHEAD, "output.mp3")
End If
' free the codec format buffer (you might free it even if encoding is still running)
// get suggested (maximum) format buffer size
int formlen = BassEnc.BASS_Encode_GetACMFormat(0, IntPtr.Zero, 0, null, BASSACMFormat.BASS_ACM_NONE);
// create a buffer for the codec
byte[] form = new byte[formlen];
  fixed (byte* p = form)
    // automatically suggest an MP3 codec
    if (BassEnc.BASS_Encode_GetACMFormat(handle, (IntPtr)p, formlen, null, 
                (BASSACMFormat)Utils.MakeLong((int)(BASSACMFormat.BASS_ACM_SUGGEST | BASSACMFormat.BASS_ACM_RATE | BASSACMFormat.BASS_ACM_CHANS), 
                                              (int)WAVEFormatTag.MPEGLAYER3)) > 0)
      // begin encoding
      int encHandle = BassEnc.BASS_Encode_StartACMFile(channel, (IntPtr)p, 
                              BASSEncode.BASS_ENCODE_NOHEAD, "output.mp3" );
See Also