Xeno-rat
Loading...
Searching...
No Matches
NAudio.Wave.MediaFoundationEncoder Class Reference

Media Foundation Encoder class allows you to use Media Foundation to encode an IWaveProvider to any supported encoding format. More...

Inheritance diagram for NAudio.Wave.MediaFoundationEncoder:
Collaboration diagram for NAudio.Wave.MediaFoundationEncoder:

Public Member Functions

 MediaFoundationEncoder (MediaType outputMediaType)
 Creates a new encoder that encodes to the specified output media type.
 
void Encode (string outputFile, IWaveProvider inputProvider)
 Encodes the input audio data from inputProvider into the specified transcodeContainerType and writes the result to the outputStream .
 
void Encode (Stream outputStream, IWaveProvider inputProvider, Guid transcodeContainerType)
 Encodes a file.
 
void Dispose ()
 Disposes this instance.
 

Static Public Member Functions

static int[] GetEncodeBitrates (Guid audioSubtype, int sampleRate, int channels)
 Retrieves the array of encoded bitrates for the specified audio subtype, sample rate, and channel count.
 
static MediaType[] GetOutputMediaTypes (Guid audioSubtype)
 Retrieves the available output media types for the specified audio subtype.
 
static void EncodeToWma (IWaveProvider inputProvider, string outputFile, int desiredBitRate=192000)
 Encodes the input audio data to WMA format and writes it to the output stream.
 
static void EncodeToWma (IWaveProvider inputProvider, Stream outputStream, int desiredBitRate=192000)
 Helper function to simplify encoding Window Media Audio Should be supported on Vista and above (not tested)
 
static void EncodeToMp3 (IWaveProvider inputProvider, string outputFile, int desiredBitRate=192000)
 Encodes the input audio data to MP3 format and writes it to the specified output stream using the specified bit rate.
 
static void EncodeToMp3 (IWaveProvider inputProvider, Stream outputStream, int desiredBitRate=192000)
 Helper function to simplify encoding to MP3 By default, will only be available on Windows 8 and above.
 
static void EncodeToAac (IWaveProvider inputProvider, string outputFile, int desiredBitRate=192000)
 Encodes the input audio data to AAC format and writes the result to the specified output stream.
 
static void EncodeToAac (IWaveProvider inputProvider, Stream outputStream, int desiredBitRate=192000)
 Helper function to simplify encoding to AAC By default, will only be available on Windows 7 and above.
 
static MediaType SelectMediaType (Guid audioSubtype, WaveFormat inputFormat, int desiredBitRate)
 Selects the media type that best matches the input audio subtype, format, and desired bit rate.
 

Protected Member Functions

void Dispose (bool disposing)
 Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 

Private Member Functions

void PerformEncode (IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider)
 Performs encoding of the input audio data using the specified sink writer and wave provider.
 
long ConvertOneBuffer (IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer)
 Converts data from the inputProvider to a memory buffer and writes it to the sink writer at the specified position.
 
 ~MediaFoundationEncoder ()
 Finalizer.
 

Static Private Member Functions

static IMFSinkWriter CreateSinkWriter (string outputFile)
 Creates a sink writer for writing media data to the specified output stream with the given transcode container type.
 
static IMFSinkWriter CreateSinkWriter (IStream outputStream, Guid TranscodeContainerType)
 
static long BytesToNsPosition (int bytes, WaveFormat waveFormat)
 Converts the specified number of bytes to the corresponding position in nanoseconds based on the provided WaveFormat.
 

Private Attributes

readonly MediaType outputMediaType
 
bool disposed
 

Detailed Description

Media Foundation Encoder class allows you to use Media Foundation to encode an IWaveProvider to any supported encoding format.

Constructor & Destructor Documentation

◆ MediaFoundationEncoder()

NAudio.Wave.MediaFoundationEncoder.MediaFoundationEncoder ( MediaType outputMediaType)
inline

Creates a new encoder that encodes to the specified output media type.

Parameters
outputMediaTypeDesired output media type
Here is the caller graph for this function:

◆ ~MediaFoundationEncoder()

NAudio.Wave.MediaFoundationEncoder.~MediaFoundationEncoder ( )
inlineprivate

Finalizer.

Here is the call graph for this function:

Member Function Documentation

◆ BytesToNsPosition()

static long NAudio.Wave.MediaFoundationEncoder.BytesToNsPosition ( int bytes,
WaveFormat waveFormat )
inlinestaticprivate

Converts the specified number of bytes to the corresponding position in nanoseconds based on the provided WaveFormat.

Parameters
bytesThe number of bytes to be converted.
waveFormatThe WaveFormat object containing information about the audio format.
Returns
The position in nanoseconds corresponding to the specified number of bytes based on the provided WaveFormat.
Here is the caller graph for this function:

◆ ConvertOneBuffer()

long NAudio.Wave.MediaFoundationEncoder.ConvertOneBuffer ( IMFSinkWriter writer,
int streamIndex,
IWaveProvider inputProvider,
long position,
byte[] managedBuffer )
inlineprivate

Converts data from the inputProvider to a memory buffer and writes it to the sink writer at the specified position.

Parameters
writerThe sink writer to which the data will be written.
streamIndexThe index of the stream in the sink writer.
inputProviderThe input wave provider from which data will be read.
positionThe position in nanoseconds at which the data will be written.
managedBufferThe managed buffer used for data conversion.
Returns
The duration of the converted data in nanoseconds.

This method converts data from the inputProvider to a memory buffer, sets the sample time and duration, and writes it to the sink writer at the specified position. If the read operation from the inputProvider is successful, the method returns the duration of the converted data in nanoseconds. If the read operation fails, no data is written to the sink writer, and the method returns 0.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateSinkWriter() [1/2]

static IMFSinkWriter NAudio.Wave.MediaFoundationEncoder.CreateSinkWriter ( IStream outputStream,
Guid TranscodeContainerType )
inlinestaticprivate
Here is the call graph for this function:

◆ CreateSinkWriter() [2/2]

static IMFSinkWriter NAudio.Wave.MediaFoundationEncoder.CreateSinkWriter ( string outputFile)
inlinestaticprivate

Creates a sink writer for writing media data to the specified output stream with the given transcode container type.

Parameters
outputStreamThe output stream to write the media data to.
TranscodeContainerTypeThe GUID representing the transcode container type.
Returns
The sink writer for writing media data to the specified output stream.
Exceptions
System.Runtime.InteropServices.COMExceptionThrown if there is an error creating the sink writer.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Dispose() [1/2]

void NAudio.Wave.MediaFoundationEncoder.Dispose ( )
inline

Disposes this instance.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Dispose() [2/2]

void NAudio.Wave.MediaFoundationEncoder.Dispose ( bool disposing)
inlineprotected

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

This method disposes of unmanaged resources and suppresses the finalization of the object.

◆ Encode() [1/2]

void NAudio.Wave.MediaFoundationEncoder.Encode ( Stream outputStream,
IWaveProvider inputProvider,
Guid transcodeContainerType )
inline

Encodes a file.

Parameters
outputStreamOutput stream
inputProviderInput provider (should be PCM, some encoders will also allow IEEE float)
transcodeContainerTypeOne of TranscodeContainerTypes
Here is the call graph for this function:

◆ Encode() [2/2]

void NAudio.Wave.MediaFoundationEncoder.Encode ( string outputFile,
IWaveProvider inputProvider )
inline

Encodes the input audio data from inputProvider into the specified transcodeContainerType and writes the result to the outputStream .

Parameters
outputStreamThe stream to which the encoded audio data will be written.
inputProviderThe input audio data provider.
transcodeContainerTypeThe GUID specifying the container type for the transcoded audio data.
Exceptions
ArgumentExceptionThrown when the input format of inputProvider is not PCM or IEEE float.

This method encodes the input audio data using the specified transcodeContainerType and writes the result to the outputStream . It first checks if the input format is PCM or IEEE float, and then creates a media type based on the input format. It then creates a sink writer and adds a stream with the output media type, sets the input media type, and performs the encoding. Finally, it releases the allocated COM objects.

Here is the call graph for this function:

◆ EncodeToAac() [1/2]

static void NAudio.Wave.MediaFoundationEncoder.EncodeToAac ( IWaveProvider inputProvider,
Stream outputStream,
int desiredBitRate = 192000 )
inlinestatic

Helper function to simplify encoding to AAC By default, will only be available on Windows 7 and above.

Parameters
inputProviderInput provider, must be PCM
outputStreamOutput stream
desiredBitRateDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
Here is the call graph for this function:

◆ EncodeToAac() [2/2]

static void NAudio.Wave.MediaFoundationEncoder.EncodeToAac ( IWaveProvider inputProvider,
string outputFile,
int desiredBitRate = 192000 )
inlinestatic

Encodes the input audio data to AAC format and writes the result to the specified output stream.

Parameters
inputProviderThe input audio data provider.
outputStreamThe stream to which the encoded AAC data will be written.
desiredBitRateThe desired bit rate for the AAC encoding (default is 192000).
Exceptions
InvalidOperationExceptionThrown when no suitable AAC encoders are available.

This method selects the appropriate media type for AAC encoding based on the input audio format and desired bit rate. It then uses the selected media type to create a MediaFoundationEncoder, which is used to encode the input audio data to AAC format. The encoded AAC data is written to the specified output stream.

Here is the call graph for this function:

◆ EncodeToMp3() [1/2]

static void NAudio.Wave.MediaFoundationEncoder.EncodeToMp3 ( IWaveProvider inputProvider,
Stream outputStream,
int desiredBitRate = 192000 )
inlinestatic

Helper function to simplify encoding to MP3 By default, will only be available on Windows 8 and above.

Parameters
inputProviderInput provider, must be PCM
outputStreamOutput stream
desiredBitRateDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
Here is the call graph for this function:

◆ EncodeToMp3() [2/2]

static void NAudio.Wave.MediaFoundationEncoder.EncodeToMp3 ( IWaveProvider inputProvider,
string outputFile,
int desiredBitRate = 192000 )
inlinestatic

Encodes the input audio data to MP3 format and writes it to the specified output stream using the specified bit rate.

Parameters
inputProviderThe input audio data provider.
outputStreamThe output stream to write the encoded MP3 data to.
desiredBitRateThe desired bit rate for the MP3 encoding, defaults to 192000 bps if not specified.
Exceptions
InvalidOperationExceptionThrown when no suitable MP3 encoders are available.

This method selects the appropriate media type for MP3 encoding based on the input audio format and desired bit rate. It then uses the selected encoder to encode the input audio data and writes the encoded MP3 data to the output stream.

Here is the call graph for this function:

◆ EncodeToWma() [1/2]

static void NAudio.Wave.MediaFoundationEncoder.EncodeToWma ( IWaveProvider inputProvider,
Stream outputStream,
int desiredBitRate = 192000 )
inlinestatic

Helper function to simplify encoding Window Media Audio Should be supported on Vista and above (not tested)

Parameters
inputProviderInput provider, must be PCM
outputStreamOutput stream
desiredBitRateDesired bitrate. Use GetEncodeBitrates to find the possibilities for your input type
Here is the call graph for this function:

◆ EncodeToWma() [2/2]

static void NAudio.Wave.MediaFoundationEncoder.EncodeToWma ( IWaveProvider inputProvider,
string outputFile,
int desiredBitRate = 192000 )
inlinestatic

Encodes the input audio data to WMA format and writes it to the output stream.

Parameters
inputProviderThe input audio data provider.
outputStreamThe stream to which the encoded WMA data will be written.
desiredBitRateThe desired bit rate for the WMA encoding (default is 192000).
Exceptions
InvalidOperationExceptionThrown when no suitable WMA encoders are available.

This method selects the appropriate media type for WMA encoding based on the input audio format and desired bit rate. It then uses the selected media type to create a MediaFoundationEncoder, which is used to encode the input audio data to WMA format. The encoded data is written to the output stream in ASF container format.

Here is the call graph for this function:

◆ GetEncodeBitrates()

static int[] NAudio.Wave.MediaFoundationEncoder.GetEncodeBitrates ( Guid audioSubtype,
int sampleRate,
int channels )
inlinestatic

Retrieves the array of encoded bitrates for the specified audio subtype, sample rate, and channel count.

Parameters
audioSubtypeThe unique identifier for the audio subtype.
sampleRateThe sample rate of the audio.
channelsThe number of audio channels.
Returns
An array containing the encoded bitrates for the specified audio configuration.
Here is the call graph for this function:

◆ GetOutputMediaTypes()

static MediaType[] NAudio.Wave.MediaFoundationEncoder.GetOutputMediaTypes ( Guid audioSubtype)
inlinestatic

Retrieves the available output media types for the specified audio subtype.

Parameters
audioSubtypeThe GUID of the audio subtype for which to retrieve the output media types.
Returns
An array of MediaType objects representing the available output media types for the specified audio subtype.
Exceptions
COMExceptionThrown when an error occurs while retrieving the available output media types.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PerformEncode()

void NAudio.Wave.MediaFoundationEncoder.PerformEncode ( IMFSinkWriter writer,
int streamIndex,
IWaveProvider inputProvider )
inlineprivate

Performs encoding of the input audio data using the specified sink writer and wave provider.

Parameters
writerThe sink writer used for writing the encoded data.
streamIndexThe index of the stream to write the encoded data to.
inputProviderThe wave provider containing the input audio data to be encoded.

This method performs encoding of the input audio data using the specified sink writer and wave provider. It iterates through the input data, converts and writes each buffer to the specified stream index, and finalizes the writing process once all data has been processed.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SelectMediaType()

static MediaType NAudio.Wave.MediaFoundationEncoder.SelectMediaType ( Guid audioSubtype,
WaveFormat inputFormat,
int desiredBitRate )
inlinestatic

Selects the media type that best matches the input audio subtype, format, and desired bit rate.

Parameters
audioSubtypeThe audio subtype to be matched.
inputFormatThe input wave format.
desiredBitRateThe desired bit rate in bits per second.
Returns
The media type that best matches the input criteria, or null if no match is found.

This method selects the media type that best matches the input audio subtype, wave format, and desired bit rate from the available output media types. It filters the output media types based on sample rate and channel count, calculates the delta between the desired bit rate and the average bytes per second of each media type, orders the filtered media types by delta, and returns the first matching media type or null if no match is found.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ disposed

bool NAudio.Wave.MediaFoundationEncoder.disposed
private

◆ outputMediaType

readonly MediaType NAudio.Wave.MediaFoundationEncoder.outputMediaType
private

The documentation for this class was generated from the following file: