%% Specify Nondefault Channel Mapping
% The |audioPlayerRecorder| System object(TM) enables you to specify a
% nondefault mapping between the channels of your audio device and the data
% sent to and received from your audio device. To run this example, your
% audio device must have at least two channels and be capable of full-duplex
% mode.
%

%% Using Default Settings
%
% Create an |audioPlayerRecorder| object with default settings.
% The |audioPlayerRecorder| is automatically configured to a compatible
% device and driver.
%
aPR = audioPlayerRecorder;

%%
% The |audioPlayerRecorder| combines reading from your device and writing
% to your device in a single call: |audioFromDevice = aPR(audioToDevice)|.
% Calling the |audioPlayerRecorder| with default settings:
%
% * Maps columns of |audioToDevice| to output channels of your device
% * Maps input channels of your device to columns of |audioFromDevice|
%
% By default, |audioFromDevice| is a one-column matrix corresponding to
% channel 1 of your audio device. To view the maximum number of input and
% output channels of your device, use the |info| method.
%
aPRInfo = info(aPR);

%%
% |aPRInfo| is returned as a structure with fields containing information
% about your selected driver, audio device, and the maximum number of input
% and output channels in your configuration.

%%
% Call the |audioPlayerRecorder| with a two-column matrix. By default,
% column 1 is mapped to output channel 1, and column 2 is mapped to output
% channel 2.  The |audioPlayerRecorder| returns a one-column matrix with
% the same number of rows as the |audioToDevice| matrix.
%
highToneGenerator = audioOscillator('Frequency',600,'SamplesPerFrame',256);
lowToneGenerator = audioOscillator('Frequency',200,'SamplesPerFrame',256);

for i = 1:250
    C = highToneGenerator();
    D = lowToneGenerator();
    audioToDevice = [C,D];
    audioFromDevice = aPR(audioToDevice);
end

%%
%
% <<../channelMapping1.png>>
%

%% Nondefault Channel Mapping for Audio Output
% Specify a nondefault channel mapping for your audio output.  Specify that
% column 1 of |audioToDevice| maps to channel 2, and that column 2
% of |audioToDevice| maps to channel 1.  To modify the channel mapping, the |audioPlayerRecorder| object
% must be unlocked.
%
% Run the |audioPlayerRecorder| object. If you are using headphones or
% stereo speakers, notice that the high frequency and low frequency tones
% have switched speakers.
%

release(aPR)
aPR.PlayerChannelMapping = [2,1];

for i = 1:250
    C = highToneGenerator();
    D = lowToneGenerator();
    audioToDevice = [C,D];
    audioFromDevice = aPR(audioToDevice);
end

%%
%
% <<../channelMapping2.png>>
%

%% Nondefault Channel Mapping for Audio Input
% Specify a nondefault channel mapping for your audio input. Record data from
% only channel two of your device. In this case, channel 2 is mapped to a
% one-column matrix. Use |size| to verify that |audioFromDevice| is a
% 256-by-1 matrix.
%

release(aPR)
aPR.RecorderChannelMapping = 2;

audioFromDevice = aPR(audioToDevice);

[rows,col] = size(audioFromDevice)

%%
%
% <<../channelMapping3.png>>
%

%%
% As a best practice, release your audio device once complete.
%
release(aPR)


%% 
% Copyright 2012 The MathWorks, Inc.