The simplest way to work with audio is to use object properties with the IMProps interface. You can change the order of audio channels, modify a number of channels and set the audio gain for each of them.
Audio channels selecting
The "object::audio_channels" property modifies a number of audio channels and channels order. For example, this code:
(myFile as IMProps).PropsSet("object::audio_channels", "0,1,2,3");
sets only 4 audio channels in strict order from 1st channel to 4th for your myFile object. If myFile has more than 4 audio channels, other channels are cut. You can use any source object as myFile - MMixer, MFile, MPlaylist, MLive.
To change the order of audio channels, you should vary channel indexes in the property value:
(myFile as IMProps).PropsSet("object::audio_channels", "3,1,2,0");
You can copy audio channels by using the same channel index several times:
(myFile as IMProps).PropsSet("object::audio_channels", "0,1,1,0");
To use just a single channel you should set its index as value for this property:
(myFile as IMProps).PropsSet("object::audio_channels", "2");
To make a channel muted, you should set "-1" as the index of required channel. For example, to mute 3rd result channel you should call:
(myFile as IMProps).PropsSet("object::audio_channels", "0,1,-1,3");
In case of several audio tracks
For example, you have a file with 10 audio tracks and each track is of 2 audio channels. To merge them, you can use "file::audio_track" property:
myFile.PropsSet("file::audio_track", "all");
As a result, you will have 20 audio channels in total.
If you need to use only the last 2 channels, you should set the "audio_channels" property to "18,19". But please note that if you use an audio conversion to 16 channels with the FormatAudioSet method, you can't access the channels with indexes higher than 15 using "object::audio_channels" - because after the conversion you have only 16 audio channels.
In this case, you should use "file::audio_channels" property. This property is used right after the audio is decoded and before any conversion, unlike the "object::audio_channels" case.
Audio channels mixing
You can mix several original audio channels into a single new channel. For example, to create a 2 channels audio from original 4 channels audio the way the 1st new channel contains audio from 1st, 2nd, and 4th original channels, and the 2nd new channel is a sum of 2nd, 3rd and 4th original channels you should call:
myLive.PropsSet("object::audio_channels", "0+1+3,1+2+3");
Audio track splitting
To split audio channels into separated tracks, use '|' symbol in "audio_channels" property value. For example, to split 4 channels audio track into 4 mono tracks:
myLive.PropsSet("object::audio_channels", "0|1|2|3");
For splitting on the Writer level, you can use "split_channels" encoding attribute.
Audio gain
The "object::audio_gain" property modifies the audio gain of audio channels. You can modify audio gain for all audio channels at once, or set the audio gain for each channel separately.
To set the audio gain for all the channels, you should call:
(myFile as IMProps).PropsSet("object::audio_gain", "-10.0");
This decrease the audio gain of all audio channels to 10.0 dB.
To specify audio gain for each channel, you should call
(myFile as IMProps).PropsSet("object::audio_gain", "-10.0,5.2,-7.5,2.1");
In this case, 4 channels have a new audio gain. If there are more than 4 audio channels in the object, all the other channels gain are unchanged.
The difference between working with audio through properties and IMAudio interface in MPlatform SDK
With IMAudio and IMAudioTrack interfaces, you can work with audio in a more flexible way, but most common actions are much easier to do through properties. The main difference between these approaches is that when MPlatform calculates audio in your stream it firstly uses setting made through IMAudio and IMAudioTrack interfaces and in the last step it modifies audio according to properties:
For example, if you get audio VU meter value of an audio channel with TrackLoudnessGet method and compare its value with VU meter data that contains in the result frame, you get different values.
What about MFormats SDK?
The same things work, but you don't need to use "object::" or "file::"prefixes for properties of MFormats SDK objects
Audio channels mapping and mixing
MFormats SDK provides an opportunity to work with audio on a frame level. This way you can map, mix or mute audio channels not only through the source object, but also on the frames themselves received from the source. There is a method for this - MFAVPropsSet().
The following example mixes 1st - 8th audio channels and puts them into the first audio channels, mutes 2nd - 8th audio channels, leaves 8th - 11th channels unchanged, and reverses the order of the 12th - 16th audio channels:
frame.MFAVPropsSet(ref avProps, "0 + 1 + 2 + 3 + 4 + 5 + 6 + 7, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11, 15, 14, 13, 12");