# Playing sound in left and right speaker alternatively and storing output in array in matlab

I have a play function that is called twice, each plays a channel (a column of the two dimensional array) in both speakers. What I am trying to do is to play both channel at the same time. How do I silence a channel and set the the elements in the corresponding column to 0. I also need an output array that im trying to make look like this when the data is output into it. The code is in the link below. 1 0 2 0 1 0 0 4 0 1 0 1

Fs = 44100;
N = 400
t = (1:N)*(1/Fs);
freq = 600;

sound_array = zeros(N,2);
%starts N x 2 matrix
sound_array(:,1) = sin(2*pi*freq*t);
sound_array(:,2) = sin(2*pi*freq*t);

%Sound 1
sound(sound_array(:,1), Fs);
%Sound 2
sound(sound_array(:,2), Fs);
%Play Consecutive
sound(sound_array, Fs);

• I don't understand. Line 16 of your file plays both at the same time and lines 12 and 14 each play one of the two channels. – tobassist May 15 '14 at 12:57
• I meant to comment those parts out @tobassist but I am trying to have one sound play in the left speaker while the right channel is set to 0 then have them switch and the right channel that was set to 0 plays and left is set to 0 but store the output samples in an array. – Asau7610 May 15 '14 at 12:59
• @Asau7610: I wonder how are you going to synchronise your playback? – jojek May 15 '14 at 13:12
• @jojek im going to a mic to record the sound i just need the output array for the 2 channels for live channel and silent. – Asau7610 May 15 '14 at 13:13

If you want to play only one channel then you must set zeros at one of your matrix columns, i.e.:

sound_L = sound_array;
sound_L(:,2) = 0;

sound_R = sound_array;
sound_R(:,1) = 0;


Fs = 44100;
N = 400
t = (1:N)*(1/Fs);
freq = 600;

sound_array = zeros(N,2);
%starts N x 2 matrix
sound_array(:,1) = sin(2*pi*freq*t);
sound_array(:,2) = sin(2*pi*freq*t);

%Left and Right channels

sound_L = sound_array;
sound_L(:,2) = 0;
sound_R = sound_array;
sound_R(:,1) = 0;

%Sound 1
sound(sound_L, Fs);
%Sound 2
sound(sound_R, Fs);
%Play Consecutive
sound(sound_array, Fs);

• oh ok @jojek but how would i put them in an output array so that say i have 400 samples that are active for the left then switch to the right doing the same making the left the silent channel. – Asau7610 May 15 '14 at 13:17
• @Asau7610: You must provide array with one column full of zeros in order to play it back in one speaker only. Otherwise mono recording is played on both channels. Create such variables and do your sound(sound_L, Fs); which will play on left speaker. You have three variables then. If you want that in one only, then I suggest to use some cell array. – jojek May 15 '14 at 13:22
• oh ok I semi understand what your saying...this is my first real time using matlab and just doing it for a grad project. So stil kind of confused on how I would do the variables in the code. – Asau7610 May 15 '14 at 13:24
• @Asau7610: Have you even tried to apply the code I posted? Please see the updated link. You have three variables. One with both channels at the same time and two with signal played at left and right channel - that's what you want. – jojek May 15 '14 at 13:28
• oh ok I didn't see the new post. I will try now @jojek – Asau7610 May 15 '14 at 13:32