How to use the audio convert utility

Overview

Audio convert utility is used to convert audio clips contained in files or descriptors to other formats using the default codecs provided by MMF. This functionality is implemented by the CMdaAudioConvertUtility class.

Description

Using a audio converter utility involves the following tasks:

Construct and open converter utility object

The MMF provides a utility class called CMdaAudioConverterUtility. Using this class, you can convert audio clips from one format to another. The CMdaAudioConverterUtility::NewL() function need to be used to construct an object of this utility class, with the following arguments:

  • An object of the class, which inherits the MMdaObjectStateChangeObserver class to receive state change notifications from the converter object.

  • Client's relative priority, which can be EMdaPriorityMin, EMdaPriorityNormal or EMdaPriorityMax.

  • An object of TMdaPriorityPreference specifying the behaviour, if a higher priority client takes over the device.

The following code shows how the client application CClientApp inherits the MMdaObjectStateChangeObserver and creates the audio converter utility object:

class CClientApp : public CBase, MMdaObjectStateChangeObserver
    {
public:
   void CClientApp();
protected:

    // from MMdaObjectStateChangeObserver
    virtual void MoscoStateChangeEvent(CBase *aObject, TInt aPreviousState, TInt aCurrentState, TInt aErrorCode);
    
protected:
    CMdaAudioConverterUtility*    iConverter;   
    };

void CClientApp()
{    
    iConverter = CMdaAudioConverterUtility::NewL(*this, EMdaPriorityNormal,
                   EMdaPriorityPreferenceTimeAndQuality);
}

Once the audio converter utility object is constructed, you can open an audio clip for conversion using the function CMdaAudioConverterUtility::OpenL(). You can use the following versions of this function:

  • To open an existing audio file and append the converted audio data to another file, use the following version:

    OpenL(const TDesC& aPrimaryFile, const TdesC& aSecondaryFile);

    Where, aPrimaryFile and aSecondaryFile are used to specify the source and destination audio file names.

  • To open existing audio file and store the converted audio data to either a file or a descriptor, use the following version:

    OpenL(const TdesC& aPrimaryFile
            TMdaClipLocation* aLocation,
            TMdaClipFormat* aFormat,
            TMdaPackage* aArg1 = NULL,
            TMdaPackage* aArg2 = NULL);
    

    Where, aPrimaryFile specifies the source audio file names. The aLocation, aFormat, aArg1 and aArg2 are used to specify the target location for the converted audio data, target audio format, codec for the target audio and target audio settings.

  • To open audio data either from a file or a descriptor source and store the resulting data to either a file or descriptor, use the following version:

    OpenL(TmdaClipLocation* aPriLocation
            TMdaClipLocation* aSecLocation,
            TMdaClipFormat* aPriFormat,
            TMdaClipFormat* aSecFormat,
            TMdaPackage* aPriArg1 = NULL,
            TMdaPackage* aPriArg2 = NULL,
            TMdaPackage* aSecArg1 = NULL,
            TMdaPackage* aSecArg2 = NULL);
    

    Where, aPriLocation and aPriLocation specifies the source and destination audio data locations. The aPriFormat and aSecFormat specifies the audio format of the source and target audio data. The aPriArg1 and aPriArg2 specifies codec and settings for the source audio data. Whereas, the aSecArg1 and aSecArg2 specifies the codec and settings for the target audio data.

Note: It is recommended to use the first version of OpenL to specify source and destination audio file location. The arguments for the other versions are deprecated.

The following code uses the first version of OpenL to specify source and destination audio location:

TFileName fromFilename = _L("C:\\rectest2.wav");
TFileName toFilename = _L("C:\\rectest2.wav");

iConverter->OpenL(&fromFilename, &toFilename);

Once the converter calls the CMdaAudioConverterUtility::OpenL() function, the callback function MMdaObjectStateChangeObserver::MoscoStateChangeEvent() will notify the client application about the change in state of the object.

Configure conversion parameters

The configuration parameters can be broadly classified into four categories as shown below:

Set recorded file length

You can control the size settings of a recorded file. This is handled by the SetMaxWriteLength() function that sets the maximum number of bytes that can be written to a recorded audio clip. The SetMaxWriteLength() function is provided so that converters can limit the amount of file storage or memory that should be allocated. If the maximum limit is reached, MMF stops recording and notifies the client application.

The following code sets the maximum number of bytes to 800:

iConverter->SetMaxWriteLength(800);

Formats and Codecs

You can report and set formats and codecs for audio data that is already open. The four methods related to DataTypes (codecs) enable you to retrieve a list of supported codecs for the current data format, set a new codec to use or determine which codec is currently in use. A further three methods are provided to query and set the audio format to use.

Codec related actions:

Data format related actions:

Note: It is only possible to report the format and codec used by the source audio and cannot be changed.

Bit and sample rates

You can report and set bit and sample rates as required by specific codecs.

Methods to control the bit rate of audio data:

Note: None of the default codecs provided in the MMF support the use of bit rates. These methods are provided to enable the creation of additional codecs that might require bit rate information, such as the audio format MP3.

Methods for controlling the sampling rate of audio data:

Note: It is only possible to report the bit rate or sample rate used by the source audio and cannot be changed.

Balance

You can report and set the audio mode (mono or stereo).

Note: It is only possible to report the number of supported channels for the source audio and cannot be changed.

Convert audio clip

Several methods are provided in CMdaAudioConvertUtility to enable the conversion of audio data. The specific functions to convert and perform related tasks are as follows:

  • To start converting audio data from the current position within the audio source and writing the converted data to a play window within the destination file, use the ConvertL() function.

  • To stop audio conversion, use the Stop() function. The positions within the audio source and destination are maintained to handle subsequent ConvertL() statements.

  • To stop audio conversion and close all related controllers, use the Close() function.

Change conversion position and crop audio data

It is possible to crop the audio clip from the current position to the end of the clip or from the begging to the current position as illustrated below:

  • To crop the current clip from the current position, use the CropL() function. The remainder of the clip is discarded. The effects of the function cannot be undone. The function is synchronous and can leave if there is a problem. The leave code depends on the configuration settings.

  • To crop the audio clip from the start of the file to the current position, use the CropFromBeginningL() function. The audio data prior to the current position is discarded. The effects of the function cannot be undone. The function is synchronous and can leave if there is a problem. The leave code depends on the configuration settings.

To retrieve the current position of the audio clip or move the position to the desired location use the following functions:

  • To get the current position in the audio clip, use the Position() function. The position is defined in terms of a time interval measured from the beginning of the audio sample data.

  • To set the current position in the audio clip, use the SetPosition() function. A subsequent call to the ConvertL() function starts conversion from this new position.

Custom commands

  • To send a raw custom command synchronously to the controller or to allow data to be returned from the controller, use the CustomCommandSync() function.

  • To send a raw custom command asynchronously to the controller or to allows data to be returned from the controller, use the CustomCommandAsync() function.