Clear Filters
Clear Filters

Save stereo audio file

9 views (last 30 days)
An
An on 6 Feb 2024
Commented: Star Strider on 6 Feb 2024
When using
player = audioplayer(y, fs);
This sounds stereo, as intended:
play(player) % sound stereo OK
But after saving a file to disk, stereo seems to be lost and both channels mixed:
audiowrite(filename, y, fs);
Is there anyway to overcome time?
  1 Comment
Walter Roberson
Walter Roberson on 6 Feb 2024
audiowrite(filename, y, fs); should be okay provided that y has 2 columns.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Feb 2024
Testing those functions, they both appear to be working correctly —
Fs1 = 44100;
L1 = 2;
t1 = linspace(0, L1*Fs1, L1*Fs1+1).'/Fs1; % Column Vector
s = [sin(2*pi*t1*1E+3) sin(2*pi*t1*2E+3)]; % Sound
audiowrite('Test.wav', s, Fs1)
[y, Fs2] = audioread('Test.wav');
t2 = linspace(0, size(y,1)-1, size(y,1)).'/Fs2;
figure
tiledlayout(2,1)
nexttile
plot(t1, s(:,1), '-', 'LineWidth',2)
hold on
plot(t2, y(:,1), '--', 'LineWidth',2)
hold off
grid
xlim([1 1.1]*1E-2)
title('Left Channel')
nexttile
plot(t1, s(:,2), '-', 'LineWidth',2)
hold on
plot(t2, y(:,2), '--', 'LineWidth',2)
hold off
grid
xlim([1 1.1]*1E-2)
title('Right Channel')
It might be appropriate to check to be certain that your audiocard drivers are up-to-date and that your audiocard is working correctly. You can use this code to check it.
.
  2 Comments
An
An on 6 Feb 2024
Edited: An on 6 Feb 2024
You are right; I must have been doing something wrong. Worked fine.
Simplified your code a bit and Tested stereo sound with:
% testwav
Fs = 44100; % sample frequency (points per second)
duration = 5; %seconds
t = linspace(0, duration, duration*Fs)'; % Column Vector
f1 = 440; % frequency 1
f2 = 442; % frequency 2
s1 = sin(2*pi*t*f1);
s2 = sin(2*pi*t*f2);
nil = zeros(size(s1));
% build test: left ear, right ear, both ears
% generating a 2Hz binaural beat in the last 5s of audio
ba = [
s1 nil;
nil s2;
s1 s2;
];
audiowrite('Test.wav', ba, Fs)
Star Strider
Star Strider on 6 Feb 2024
Thank you!

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!