Save stereo audio file

10 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2024-2-6
audiowrite(filename, y, fs); should be okay provided that y has 2 columns.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2024-2-6
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 个评论
An
An 2024-2-6
编辑:An 2024-2-6
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by