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");
The + operator adds the selected channels without automatically averaging their levels. This applies to channel mixing in both MPlatform and MFormats. For example, the following MPlatform configuration sums input channels 0 through 7 into output channel 0 and makes output channels 1 through 7 silent:
(myPlaylist as IMProps).PropsSet("object::audio_channels", "0+1+2+3+4+5+6+7,-1,-1,-1,-1,-1,-1,-1");
If all eight inputs contain the same tone at the same amplitude and phase, their sum has eight times the amplitude of one input: 20 * log10(8), or approximately 18.06 dB more, provided the signal does not clip. With other material, the resulting level depends on the content and phase of the channels. A higher audio-meter reading does not mean that the audio_gain property has changed.
To preserve the level of that identical-tone example, attenuate each contributing input by approximately 18.06 dB before summing. Choose the attenuation for your actual material and leave enough headroom: reducing gain after clipping cannot restore the original waveform. For automatic loudness control, see Audio loudness normalization.
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 reduces the level of all audio channels by 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.
At this property-processing stage, channel remapping and summing happen before audio_gain. A list of gains therefore addresses the resulting output channels. Setting audio_gain before audio_channels in your code does not reverse that processing order or attenuate the individual contributions before they are summed.
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 attenuation before object::audio_channels mixing, use IMAudioTrack.TrackGainSet on the contributing track channels, with IMAudio processing enabled. Channel indexes belong to the original track. For example, once track 0 is available, the following prepares its first eight channels for the identical-tone mix above. This example assumes that those channels remain in their original order and are not combined by another track operation:
(myPlaylist as IMAudio).AudioTrackGetByIndex(0, out string description, out IMAudioTrack track);
try
{
for (int channel = 0; channel < 8; ++channel)
track.TrackGainSet(channel, -18.0618, 0.0);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(track);
}
Apply this input attenuation before processing the mix. If the contributing channels belong to several tracks, configure the corresponding channels on each track separately.
If later object properties change the signal, the track loudness returned by TrackLoudnessGet can differ from the audio-meter values in the resulting frame.
What about MFormats SDK?
For an MFReader object, use audio_channels and audio_gain without the MPlatform object:: or file:: prefixes. For example, after opening the reader and before pulling frames:
reader.PropsSet("audio_channels", "0+1+2+3+4+5+6+7,-1,-1,-1,-1,-1,-1,-1");
The reader applies channel remapping before its audio_gain setting. Your application pulls the resulting frames and sends them to a preview, writer or another receiver. To control gain before summing on individual frames instead, leave the original channels separate at the reader and perform the frame operations below before sending each frame onward.
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().
For this example, clear reader remapping with reader.PropsSet("audio_channels", "") before pulling frames, so the frame still contains its 16 original audio channels. The code attenuates channels 0 through 7 before summing them into the first output channel. It mutes the second through eighth output channels, leaves the ninth through twelfth channels unchanged, and reverses the order of the thirteenth through sixteenth channels:
for (int channel = 0; channel < 8; ++channel)
frame.MFAudioGain(channel.ToString(), -18.0618, -18.0618);
frame.MFAVPropsGet(out M_AV_PROPS avProps, out int audioSamples);
avProps.audProps.nChannels = 16;
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");
With the identical in-phase tone described above and no additional gain changes, this compensation keeps the first output channel approximately at the level of one original input. Apply the sequence to each original frame in your processing loop, then pass that frame to its receiver and release it after use. For more remapping examples, see Mixing of audio channels with updating frame properties.
Still have questions?
Tell us what didn’t work or what you’d like clarified.