BASS.NET API for the Un4seen BASS Audio LibraryBaseEncoder ClassBASS.NET API for the Un4seen BASS Audio Library
Base class for all encoder implementation classes (e.g. EncoderLAME, EncoderWMA or EncoderOGG).
Inheritance Hierarchy

SystemObject
  Un4seen.Bass.MiscBaseEncoder
    More...

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

[SerializableAttribute]
public abstract class BaseEncoder : IBaseEncoder

The BaseEncoder type exposes the following members.

Constructors

  NameDescription
Public methodBaseEncoder
Default constructor, which already evaluates the channel handle and evaluates the ChannelBitwidth.
Top
Properties

  NameDescription
Public propertyChannelBitwidth
This property returns the actual bitwidth of the sample data of the channel (e.g. 8, 16, 32).
Public propertyChannelHandle
Gets or Sets the channel handle which is used to create the broadcast encoder class.
Public propertyChannelInfo
Returns the general channel info.
Public propertyChannelNumChans
This property returns the actual number of channles of the sample data BASS is using with the channel (e.g. 1=mono, 2=stereo, etc.).
Public propertyChannelSampleRate
This property returns the actual sample rate in Hz of the sample data BASS is using with the channel (e.g. 44100).
Public propertyDefaultOutputExtension
This property should return the default extension of an output file for this encoder (e.g. ".mp3" or ".ogg" etc.).
Public propertyEffectiveBitrate
This property should return the target effective bitrate of the encoder output (in kbps, e.g. 128 kbps).
Public propertyEncoderCommandLine
This property should return an encoder command-line string, which might be used with the BASS_Encode_Start(Int32, String, BASSEncode, ENCODEPROC, IntPtr) method in order to create and start the encoder.
Public propertyEncoderDirectory
Gets or Sets the encoder's base directory.
Public propertyEncoderExists
This property should return , if the encoder exists and is available - else should be returned.
Public propertyEncoderHandle
Gets or sets the current HENCODE encoder handle.
Public propertyEncoderType
This property should return a value indicating which type of content the broadcast encoder implements.
Public propertyForce16Bit
Gets or Sets if a encoding in 16-bit sample data should be forced (default is ).
Public propertyInputFile
Gets or Sets the input file name to be processed by the encoder (null = STDIN, default).
Public propertyIsActive
Gets a value indicating if the encoder has been started (see Start(ENCODEPROC, IntPtr, Boolean)) and is active - else will be returned.
Public propertyIsPaused
Gets a value indicating if the encoder has been paused (see Pause(Boolean)) - else must be returned.
Public propertyNoLimit
Gets or Sets if encoding should use the CAST_NOLIMIT flag (which is only needed, if the encoder is used for streaming).
Public propertyOutputFile
Gets or Sets the output file name to be processed by the encoder (null = STDOUT, default).
Public propertySupportsSTDOUT
This property should return , if the base encoder implementation (resp. the underlying command-line tool being used) support STDOUT for output of the encoded data - else must be returned.
Public propertyTAGs
Gets or Sets the TAG_INFO structure associated with the encoder.
Public propertyUseAsyncQueue
Gets or Sets if encoding should use an async queue (i.e. the BASS_ENCODE_QUEUE flag).
Top
Methods

  NameDescription
Public methodDispose
Implement IDisposable.
Public methodStatic memberCode exampleEncodeFile(BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean, Boolean)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(String, String, BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(String, String, BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean, Boolean)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean, Boolean, Double, Double)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean, Boolean, Int64, Int64)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(String, String, BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean, Boolean, Double, Double)
Encodes a given input file to a given output file using the specified encoder.
Public methodStatic memberCode exampleEncodeFile(String, String, BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean, Boolean, Int64, Int64)
Encodes a given input file to a given output file using the specified encoder.
Protected methodFinalize
Finalization code.
(Overrides ObjectFinalize.)
Public methodPause
Pauses or resumes an encoder.
Public methodSettingsString
Returns the string representation of the current encoder settings.
Public methodStart
This method must implement the actual start of the encoder. After the encoder was started successfully the EncoderHandle must return a valid HENCODE handle.
Public methodStop
Stops the encoder (if started).
Public methodStop(Boolean)
Stops the encoder (if started).
Top
Remarks

This base class is not intended for direct use, but defines all abstract properties and methods which needs to be implemented by an actual broadcast encoder class.

Internally, the sending of sample data to the encoder is implemented via a DSP callback on the ChannelHandle. That means when you play the ChannelHandle (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. It also means that if the BASS_CONFIG_FLOATDSP option is enabled, the sample data will be 32-bit floating-point.

A derived class must implement: EncoderType, SupportsSTDOUT, EncoderCommandLine, EffectiveBitrate and Stop.

The properties ChannelHandle, ChannelBitwidth, ChannelSampleRate, ChannelNumChans, EncoderHandle, EncoderDirectory, IsActive, IsPaused, InputFile and OutputFile as well as the methods Stop and Pause(Boolean) have been already implemented.

You might use this base class to derive your own Encoder implementations. In this case a derived class must only implement the following members: Start(ENCODEPROC, IntPtr, Boolean), EncoderCommandLine, EncoderType and SupportsSTDOUT.

If an instance of a derived class is being disposed, an already assigned Encoder will automatically be stopped.

How to use an encoder implementation:

Scenario:How-To:
"Recording" To "Streaming":Create a recording handle with BASS_RecordStart(Int32, Int32, BASSFlag, RECORDPROC, IntPtr).

Set InputFile and OutputFile to (STDIN, STDOUT).

Use the encoder instance to create a StreamingServer, which is then used by the BroadCast class.

"Recording" To "File":Create a recording handle with BASS_RecordStart(Int32, Int32, BASSFlag, RECORDPROC, IntPtr).

Set InputFile to and the OutputFile to a file name (STDIN, file).

Call Start(ENCODEPROC, IntPtr, Boolean) to start encoding and Stop to stop the live recording encoding.

VB
Dim n As New EncoderNeroAAC(_recHandle)
n.InputFile = Nothing
n.OutputFile = "test.m4a"
n.NERO_Bitrate = 48
n.Start(Nothing, IntPtr.Zero, False)
' your recording will be encoded until you call...
n.Stop()
EncoderNeroAAC n = new EncoderNeroAAC(_recHandle);
n.InputFile = null;
n.OutputFile = "test.m4a";
n.NERO_Bitrate = 48;
n.Start(null, IntPtr.Zero, false);
// your recording will be encoded until you call...
n.Stop();
"Stream" To "Streaming":Create a stream handle e.g. with BASS_StreamCreateFile(String, Int64, Int64, BASSFlag) (either normal or decoding).

Set InputFile to and the OutputFile to a file name (STDIN, file).

Use the encoder instance to create a StreamingServer, which is then used by the BroadCast class.

Call BASS_ChannelPlay(Int32, Boolean) resp. BASS_ChannelGetData(Int32, IntPtr, Int32) (if using a decoding stream) to play resp. decode the stream and actually feed the encoder and therewith the broadcasting server.

Hint: You might use BASS_Mixer_StreamCreate(Int32, Int32, BASSFlag) to create a continues mixer output stream channel for streaming.

"Stream" To "File":Create a stream handle e.g. with BASS_StreamCreateFile(String, Int64, Int64, BASSFlag) (either normal or decoding).

Set InputFile to and the OutputFile to a file name (STDIN, file).

Call BASS_ChannelPlay(Int32, Boolean) resp. BASS_ChannelGetData(Int32, IntPtr, Int32) (if using a decoding stream) to play resp. decode the stream and actually feed the encoder.

VB
Dim stream As Integer = Bass.BASS_StreamCreateFile("test.wav", 0, 0, BASSFlag.BASS_STREAM_DECODE)
Dim l As New EncoderLAME(stream)
l.InputFile = Nothing 'STDIN
l.OutputFile = "test.mp3"
l.LAME_Bitrate = CInt(EncoderLAME.BITRATE.kbps_64)
l.LAME_Mode = EncoderLAME.LAMEMode.Default
l.LAME_Quality = EncoderLAME.LAMEQuality.Quality
l.Start(Nothing, IntPtr.Zero, False)
' decode the stream (if not using a decoding channel, simply call "Bass.BASS_ChannelPlay" here)
Dim encBuffer(65536) As Byte ' our dummy encoder buffer
While Bass.BASS_ChannelIsActive(stream) = BASSActive.BASS_ACTIVE_PLAYING
  ' getting sample data will automatically feed the encoder
  Dim len As Integer = Bass.BASS_ChannelGetData(_stream, encBuffer, encBuffer.Length)
End While
l.Stop() ' finish
Bass.BASS_StreamFree(stream)
int stream = Bass.BASS_StreamCreateFile("test.wav", 0, 0, BASSFlag.BASS_STREAM_DECODE);
EncoderLAME l = new EncoderLAME(stream);
l.InputFile = null;    //STDIN
l.OutputFile = "test.mp3";
l.LAME_Bitrate = (int)EncoderLAME.BITRATE.kbps_64;
l.LAME_Mode = EncoderLAME.LAMEMode.Default;
l.LAME_Quality = EncoderLAME.LAMEQuality.Quality;
l.Start(null, IntPtr.Zero, false);
// decode the stream (if not using a decoding channel, simply call "Bass.BASS_ChannelPlay" here)
byte[] encBuffer = new byte[65536]; // our dummy encoder buffer
while (Bass.BASS_ChannelIsActive(stream) == BASSActive.BASS_ACTIVE_PLAYING)
{
    // getting sample data will automatically feed the encoder
    int len = Bass.BASS_ChannelGetData(_stream, encBuffer, encBuffer.Length);
}
l.Stop();  // finish
Bass.BASS_StreamFree(stream);
"File" To "Streaming":Create a stream handle e.g. with BASS_StreamCreateFile(String, Int64, Int64, BASSFlag) (either normal or decoding). Just like the above example. And use the stream handle to create the encoder instance.

Set InputFile to and OutputFile to (STDIN, STDOUT).

Use the encoder instance to create a StreamingServer, which is then used by the BroadCast class.

"File" To "File":No stream handle is actually needed.

Set InputFile and OutputFile to a file name (file, file).

Call Start(ENCODEPROC, IntPtr, Boolean) to start encoding and Stop to stop encoding.

Or simply use the EncodeFile(String, String, BaseEncoder, BaseEncoderENCODEFILEPROC, Boolean, Boolean) method instead.

VB
Dim o As New EncoderOGG(0)
o.InputFile = "test.wav"
o.OutputFile = "test.ogg"
o.OGG_UseQualityMode = True
o.OGG_Quality = 4
BaseEncoder.EncodeFile(o, Nothing, True, False, True)
EncoderOGG o = new EncoderOGG(0);
o.InputFile = "test.wav";
o.OutputFile = "test.ogg";
o.OGG_UseQualityMode = true;
o.OGG_Quality = 4;
BaseEncoder.EncodeFile(o, null, true, false, true);

Examples

This example shows a basic implementation of an own Encoder class:
VB
Public Class MyOwnEncoder Inherits BaseEncoder

    ' Constructor
    Public Sub New(channel As Integer) MyBase.New(channel)
    End Sub

    Public Overrides Function ToString() As String
      Return "My Encoder (BASS_CTYPE_STREAM_MP3)"
    End Function

    Public Overrides ReadOnly Property DefaultOutputExtension() As String
      Get
        Return ".mp3"
      End Get
    End Property 

    Public Overrides ReadOnly Property EncoderType() As BASSChannelType
      Get
        Return BASSChannelType.BASS_CTYPE_STREAM_MP3
      End Get
    End Property

    Public Overrides ReadOnly Property SupportsSTDOUT() As Boolean
      Get
        Return True
      End Get
    End Property 

    Public Overrides ReadOnly Property EncoderCommandLine() As String
      Get
        Return BuildEncoderCommandLine()
      End Get
    End Property 

    Public Overrides ReadOnly Property EffectiveBitrate() As Integer
      Get
        Return My_Bitrate
      End Get
    End Property

    Public Overrides Function Start(proc As ENCODEPROC, user As IntPtr, paused As Boolean) As Boolean
      If EncoderHandle <> 0 OrElse (Not (proc Is Nothing) AndAlso Not SupportsSTDOUT) Then
        Return False
      End If 
      ' start the encoder
      Dim flag As BASSEncode = BASSEncode.BASS_ENCODE_NOHEAD
      If Force16Bit Then
        flag = flag Or BASSEncode.BASS_ENCODE_FP_16BIT
      End If
      If paused Then
        flag = flag Or BASSEncode.BASS_ENCODE_PAUSE
      End If
      EncoderHandle = BassEnc.BASS_Encode_Start(ChannelHandle, EncoderCommandLine, 
                                                flag, proc, user)
      End If
      If EncoderHandle = 0 Then
        Return False
      Else
        Return True
      End If
    End Function

    ' local members for setting individual parameters
    Public My_Bitrate As Integer = 128

    ' just an example here!
    Private Function BuildEncoderCommandLine() As String
      Dim enCI As New CultureInfo("en-US", False)
      Dim sb As New StringBuilder()
      ' the file name and path
      sb.Append(Path.Combine(EncoderDirectory, "myencoder.exe"))
      ' raw input?
      If InputFile Is Nothing Then ' STDIN: add the raw pcm header data
        sb.Append([String].Format(enCI, " -r -x -s {0:##0.0##} --bitwidth {1}", 
                  ChannelSampleRate / 1000F,(If ChannelBitwidth > 16 Then 16 Else ChannelBitwidth))) 'ToDo: Unsupported feature: conditional (?) operator.
      End If ' ...more options here...
      sb.Append([String].Format(enCI, " -b {0} -h", My_Bitrate))
      ' STDIN or filename
      If Not (InputFile Is Nothing) Then
        sb.Append((" """ + InputFile + """"))
      Else
        sb.Append(" -")
      End If ' STDOUT or filename
      If Not (OutputFile Is Nothing) Then
        sb.Append((" """ + OutputFile + """"))
      Else
        sb.Append(" -")
      End If
      Return sb.ToString()
    End Function

End Class
public class MyOwnEncoder : BaseEncoder
{
    // Constructor
    public MyOwnEncoder(int channel) : base(channel)
    {
    }

    public override string ToString()
    {
      return "My Encoder (BASS_CTYPE_STREAM_MP3)";
    }

    public override string DefaultOutputExtension
    {
      get { return ".mp3"; }
    }

    public override BASSChannelType EncoderType
    {
      get { return BASSChannelType.BASS_CTYPE_STREAM_MP3; }
    }

    public override bool SupportsSTDOUT
    {
      get { return true; }
    }

    public override string EncoderCommandLine
    {
      get { return BuildEncoderCommandLine(); }
    }

    public override int EffectiveBitrate
    {
      get { return My_Bitrate; }    
    }

    public override bool Start(ENCODEPROC proc, IntPtr user, bool paused)
    {
      if (EncoderHandle != 0 || (proc != null && !SupportsSTDOUT))
        return false;
      // start the encoder
      BASSEncode flag = BASSEncode.BASS_ENCODE_NOHEAD;
      if (Force16Bit)
        flag |= BASSEncode.BASS_ENCODE_FP_16BIT;
      if (paused)
        flag |= BASSEncode.BASS_ENCODE_PAUSE;
      EncoderHandle = BassEnc.BASS_Encode_Start(ChannelHandle, EncoderCommandLine, 
                                                flag, proc, user);
      if (EncoderHandle == 0)
        return false;
      else
        return true;
    }

    // local members for setting individual parameters
    public int My_Bitrate = 128; 

    // just an example here!
    private string BuildEncoderCommandLine()
    {
      CultureInfo enCI = new CultureInfo("en-US", false);
      StringBuilder sb = new StringBuilder();
      // the file name and path
      sb.Append( Path.Combine( EncoderDirectory, "myencoder.exe" ) );
      // raw input?
      if (InputFile == null) // STDIN: add the raw pcm header data
        sb.Append( String.Format( enCI, " -r -x -s {0:##0.0##} --bitwidth {1}", 
                   ChannelSampleRate/1000f, ChannelBitwidth > 16 ? 16 : ChannelBitwidth ) );
      // ...more options here...
      sb.Append( String.Format( enCI, " -b {0} -h", My_Bitrate ) );
      // STDIN or filename
      if (InputFile != null)
        sb.Append( " \""+InputFile+"\"" );
      else
        sb.Append( " -" );
      // STDOUT or filename
      if (OutputFile != null)
        sb.Append( " \""+OutputFile+"\"" );
      else
        sb.Append( " -" );
      return sb.ToString();    
    }
}
See Also

Reference

Inheritance Hierarchy

SystemObject
  Un4seen.Bass.MiscBaseEncoder
    Un4seen.Bass.MiscEncoderACM
    Un4seen.Bass.MiscEncoderAIFF
    Un4seen.Bass.MiscEncoderCMDLN
    Un4seen.Bass.MiscEncoderFAAC
    Un4seen.Bass.MiscEncoderFHGAACplus
    Un4seen.Bass.MiscEncoderFLAC
    Un4seen.Bass.MiscEncoderLAME
    Un4seen.Bass.MiscEncoderMP3S
    Un4seen.Bass.MiscEncoderMPC
    Un4seen.Bass.MiscEncoderNeroAAC
    Un4seen.Bass.MiscEncoderOGG
    Un4seen.Bass.MiscEncoderOPUS
    Un4seen.Bass.MiscEncoderQTAAC
    Un4seen.Bass.MiscEncoderTooLAME
    Un4seen.Bass.MiscEncoderTwoLAME
    Un4seen.Bass.MiscEncoderWAV
    Un4seen.Bass.MiscEncoderWavPack
    Un4seen.Bass.MiscEncoderWinampAACplus
    Un4seen.Bass.MiscEncoderWMA