How can I control which channel sound is played using MATLAB?

I am a beginner MATLAB user, so I apologize if the question is very simple. I need a .wav sound file to play on one specific channel - let's say the left channel. Anyway, my code is being read in a sound file, and I add zeros to the column to invalidate the channel I don't want, like this:

currentDir = pwd;
soundFile = [currentDir '\sound1.wav']; % load the file

[y, Fs] = audioread(soundFile); % read the file in

soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = 0; % nullifies the right channel

sound = audioplayer(soundData,Fs); 
play(sound);

      

The code currently plays audio at full volume in the left speaker and half the volume (but still very audible) in the right speaker. I've tried this with at least 20.wav files with the same result.

In case it matters, it even happens when I write in code that explicitly matches the length of the sound variable in 0s, for example:

[y, Fs] = audioread(soundFile);
silentChannel = zeros(size(y));

soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = silentChannel(:,2); % nullifies the right channel

      

Does anyone know what I am doing wrong or have any thoughts?

+3


source to share


3 answers


Your code is definitely correct and it should only play audio in the left channel. I suspect the problem is caused by sound card / driver issues. Let me suggest the following troubleshooting steps:

  • Save the output as a wav file using audiowrite('output.wav', soundData, Fs)

    . Play this using another audio player like Audacity . If you can still hear the output on both channels, it must be a sound card / driver issue.
  • Assuming you are using a Windows PC (by following the syntax in the file path), make sure all audio enhancements are turned off. How to do this depends on the PC. If third party apps control playback options, you will have to use that. Otherwise, find the settings shown in the figure below on the control panel.


enter image description here

+1


source


In MatLab, the expected method for playing sound is the method sound(data,Fs)

To control the channel, the sound starts to sound, you need to know how it sound()

reads data

.

data

is a matrix with columns representing channels and rows containing waveform samples for a given sample rate Fs



here's a simple implementation.

function treismanwolfe()
    close all
    clear all
    clc
    Fs = 40000;
    tau = 2*pi();
    t = 0:tau/(Fs-1):tau/2;
    left = sin(t).*(sin(t*200)+sin(t*1600));
    left= left/max(abs(left));
    left = left'; %turn column vector into row
    right = sin(t).*(sin(t*800)+sin(t*400));
    right= right/max(abs(right));
    right = right'; %turn column vector into row
    data = [left,right*0]; %multiply either by 0 to nullify
    sound(data,Fs); %so you can hear it.
end

      

Hope this works for you. Enjoy!

+1


source


When I run your code, the audio output is only heard on the left channel as you pointed out.

Version

@Austin Kootz with the sound () function is just as good and also produces what you are looking for, but with the audio player object you have the option to stop playing in the middle of playback (as you probably know)

Have you tried converting your .wav to a different format to see if it changed?

0


source







All Articles