%% Specify Channel Mapping for audioDeviceReader
% Specify nondefault channel mapping for an audioDeviceReader object. This example is hardware specific. It assumes that your computer has a default audio input device with two available channels.
% Create an audioDeviceReader object with default settings.

deviceReader = audioDeviceReader;

%
% The default number of channels is 1. Call your audioDeviceReader object like a function with no arguments to read one frame of data from your audio device. Verify that the output data matrix has one column.

x = deviceReader();
[frameLength,numChannels] = size(x)

%
% Use info to determine the maximum number of input channels available with your specified Driver and Device configuration.

info(deviceReader)

%
% Set ChannelMappingSource to 'Property'. The audioDeviceReader object must be unlocked to change this property.

release(deviceReader)
deviceReader.ChannelMappingSource = 'Property'

%
% By default, if ChannelMappingSource is set to 'Property', all available channels are mapped to the output. Call your audioDeviceReader object to read one frame of data from your audio device. Verify that the output data matrix has two columns.

x = deviceReader();
[frameLength,numChannels] = size(x)

%
% Use the ChannelMapping property to specify an alternative mapping between channels of your device and columns of the output matrix. Indicate the input channel number at an index corresponding to the output column. To change this property, first unlock the audioDeviceReader object.

release(deviceReader)
deviceReader.ChannelMapping = [2,1];

%
% Now when you call your audioDeviceReader:
% Input channel 1 of your device maps to the second column of your output matrix.
% Input channel 2 of your device maps to the first column of your output matrix.
% Acquire a specific channel from your input device.

deviceReader.ChannelMapping = 2;

%
% If you call your audioDeviceReader, input channel 2 of your device maps to an output vector.
%
% Copyright 2012 The MathWorks, Inc.