How to mix two sound of different channel?

7 次查看(过去 30 天)
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 个评论
Birdman
Birdman 2018-1-8
What is your code that causes this error?
Sushil Pun
Sushil Pun 2018-1-8
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.

请先登录,再进行评论。

回答(2 个)

John Harris
John Harris 2018-1-8
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 2018-1-8
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.

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by