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

deviceWriter = audioDeviceWriter;

%
% By default, the audioDeviceWriter object writes the maximum number of channels available, corresponding to the columns of the input matrix. Use info to get the maximum number of channels of your device.

info(deviceWriter)

%
% If deviceWriter is called with one column of data, two channels are written to your audio output device. Both channels correspond to the one column of data.
% Use the audioOscillator object to output a tone to your audioDeviceWriter object. Your object, sineGenerator, returns a vector when called.

sineGenerator = audioOscillator;

%
% Write the sine tone to your audio device. If you are using headphones, you can hear the tone from both channels.

count = 0;
while count < 500
    sine = sineGenerator();
    deviceWriter(sine);
    count = count + 1;
end

%
% If your audioDeviceWriter object is called with two columns of data, two channels are written to your audio output device. The first column corresponds to channel 1 of your audio output device, and the second column corresponds to channel 2 of your audio output device.
% Write a two-column matrix to your audio output device. Column 1 corresponds to the sine tone, and column 2 corresponds to a static signal. If you are using headphones, you can hear the tone from one speaker and the static from the other speaker.

count = 0;
while count < 500
    sine = sineGenerator();
    static = randn(length(sine),1);
    deviceWriter([sine,static]);
    count = count + 1;
end

%
% Specify alternative mappings between channels of your device and columns of the output matrix by indicating the output channel number at an index corresponding to the input column. Set ChannelMappingSource to 'Property'. Indicate that the first column of your input data writes to channel 2 of your output device, and that the second column of your input data writes to channel 1 of your output device. To modify the channel mapping, you must first unlock the audioDeviceReader object.

release(deviceWriter)
deviceWriter.ChannelMappingSource = 'Property';
deviceWriter.ChannelMapping = [2,1];

%
% Play your audio signals with reversed mapping. If you are using headphones, notice that the tone and static have switched speakers.

count = 0;
while count < 500
    sine = sineGenerator();
    static = randn(length(sine),1);
    deviceWriter([sine,static]);
    count = count + 1;
end

%
% Copyright 2012 The MathWorks, Inc.