How to mix two sound of different channel?

4 views (last 30 days)
Sushil Pun
Sushil Pun on 8 Jan 2018
Answered: Jan on 8 Jan 2018
Here is my code
handles.y = (handles.y2)'; lenY = size(handles.y1, 1); lenZ = size(handles.y, 1);
handles
len = max(lenY, lenZ);
S = zeros(len, size(handles.y1, 2));
S(1:lenY, :) = handles.y1;
S(1:lenZ, :) = S(1:lenZ, :) + handles.y;
maxValue = max(abs(S(:)));
S = S / maxValue;
handles.mPlayer = audioplayer(S, 44100);
handles.y3 = S;
handles.Fs3 = 44100;
% save the updated handles object
guidata(hObject,handles);
msgbox('Sound Mixed');
  2 Comments
Birdman
Birdman on 8 Jan 2018
What is your code that causes this error?
Sushil Pun
Sushil Pun on 8 Jan 2018
y2 has number of channel 1 and y has number of channel 2, i think thats causing a problem i don't know how to fix it, i have provided the code above.

Sign in to comment.

Answers (2)

John Harris
John Harris on 8 Jan 2018
It's that you're trying to add 1-dimensional and 2-dimensional matrix, but look at your dimensions for y1 and y. y1 (and thus, S) is tall, while handles.y is wide.
Try transposing handles.y:
S(1:lenZ, :) = S(1:lenZ, :) + handles.y';
Check the results; are you trying to add handles.y to one column of S, or both?

Jan
Jan on 8 Jan 2018
Insert some meaningful comments in your code, such that it gets clear, what you want to achieve. I assume, you want to mix the 2 channel handles.y1 and the 1 channel handles.y - by the way: there might be more useful names for the fields.
I omit the "handles" for clarity:
y1 = rand(220500, 2) - 0.5;
y = rand(1, 220501) - 0.5;
s1 = y1;
s2 = y.'; % Transpose to have the same orientation
[len1, w1] = size(s1);
[len2, w2] = size(s2, 1);
s = zeros(max(len1, len2), max(w1,w2));
s(1:len1, 1:w1) = s1;
s(1:len2, 1:w2) = s(1:len2, 1:w2) + s2;
s = s / max(s(:)); % Normalize to [-1.0, 1.0]
This adds the two signals independent from their number of channels and lengths.

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!