0
votes

I have a left channel and a right channel on my pc. I was hoping to get some advice or a place to start in order to vary the audio balance between the two speakers. I want to write Matlab code that will play a sound and output through my PC speakers which will be able to accomplish:

1) No sound in 1 of the channels, full sound in 2nd channel 2) Lower volume in 1 channel, higher volume in 2nd channel 3) No sound in either channel.

Thank you, Any help would be sweet.

1

1 Answers

2
votes

Sounds like a job for bsxfun:

% Data
left_channel = rand(1e5,1); % for example. Column vector
right_channel = rand(1e5,1); % for example. Another column vector
signal = [left_channel right_channel]; % stereo signal. Two columns
fm = 48e3; % sample frequency
balance = [1 1]; % for example. Row vector

% Construct balanced signal
balanced_signal = bsxfun(@times,signal,balance);

% Play it
sound(balanced_signal,fm) % or use the scaled version, "soundsc"

You can control balance with the vector balance. For the cases you want, set it to something like 1) [0 1], 2) [.5 1], 3) [0 0].