Playing sound using 4 channels or converting 4 channels to two

20 views (last 30 days)
I am trying to mix two wav sound files (lets say two different songs) that have two chanels each. I then want to make a single sound file that has two chanels and has both those songs on it playing at the same time. I managed to achive this however my output mix file has 4 chanels which doesnt let me play the sound using audioplayer. I also tried using 1 chanel from each original file which lets me play the song as it has two chanels however i can only hear 1 song on each side of my headphones. My goal is to either find a method to play the 4 chanels song or make it so i can convert the 4 chanels in to two and still hear the mixed songs in both sides of my headset.
I have the following code:
This generates 4 chanels ( i can play the output of this on other software i.e Grove Music )
audioMix1 = [app.y1(:,1),app.y1(:,2)]; % get signals from first song
audioMix2 = [app.y2(:,1),app.y2(:,2)]; % get signals from second song
maxlength = max(length(audioMix1), length(audioMix2)); %find the longest song
audioMix1(end+1:maxlength,:) = 0;
audioMix2(end+1:maxlength,:) = 0;
app.outputMixedTrack = [audioMix1, audioMix2]; %mix the songs together
length(app.outputMixedTrack)
plot(app.UIAxes3,app.outputMixedTrack); %plot spectogram
%save new file
[file, path] = uiputfile({'.wav';},'Save Trim As...', 'C:\Users\Alex\Desktop\audio');
if(file)
p = fullfile(path,file);
audiowrite(p, app.outputMixedTrack, 44000);
end
This generates only two chanels but i can only hear each of them on one side of my headset
track1Left = app.y1(:,1); %get only 1 signal from first song
track2Left = app.y2(:,1); %get only 1 signal from second song
maxlength = max(length(track1Left), length(track2Left)); %find the longest song
track1Left(end+1:maxlength,:) = 0;
track2Left(end+1:maxlength,:) = 0;
app.outputMixedTrack = [track1Left, track2Left]; %mix the songs together
size(app.outputMixedTrack)
%save new file
[file, path] = uiputfile({'.wav';},'Save Trim As...', 'C:\Users\Alex\Desktop\audio');
if(file)
p = fullfile(path,file);
audiowrite(p, app.outputMixedTrack, 44000);
end

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Jan 2022
Hello Alex
seems to me that none of your codes does actually mix the two songs,
here you simply create a 4 channels audio from two 2 channels files (a concatenation)
app.outputMixedTrack = [audioMix1, audioMix2]; %mix the songs together
to actually mix the audio you have to do a (weighted) sum of the signals
assuming you want to have 50% / 50% volume ratio between the two files (this ratio can of course be changed , or you can even create a amplitude modulation profile vs time)
app.outputMixedTrack = 0.5*audioMix1 + 0.5*audioMix2; %mix the songs together
so now whe a have summed two stereo files and generated a stereo mix of them
if you export to wav format, always make sure that you have normalized the ouput to the +/-1 amplitude range of wav files

More Answers (0)

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!