0
$\begingroup$

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);
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – tobassist May 15 '14 at 12:57
  • $\begingroup$ 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. $\endgroup$ – Asau7610 May 15 '14 at 12:59
  • $\begingroup$ @Asau7610: I wonder how are you going to synchronise your playback? $\endgroup$ – jojek May 15 '14 at 13:12
  • $\begingroup$ @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. $\endgroup$ – Asau7610 May 15 '14 at 13:13
1
$\begingroup$

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;

So your code becomes:

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);
|improve this answer|||||
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Asau7610 May 15 '14 at 13:17
  • $\begingroup$ @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. $\endgroup$ – jojek May 15 '14 at 13:22
  • $\begingroup$ 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. $\endgroup$ – Asau7610 May 15 '14 at 13:24
  • $\begingroup$ @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. $\endgroup$ – jojek May 15 '14 at 13:28
  • $\begingroup$ oh ok I didn't see the new post. I will try now @jojek $\endgroup$ – Asau7610 May 15 '14 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.