treismanwolfe
treismanwolfe

Reputation: 39

How do I control which channel sound is played through using MATLAB?

I'm a novice MATLAB user, so apologies if the question is very basic. I need a .wav sound file to be played in a single, specific channel -- let's say the left channel. As it is, my code reads in the sound file, and I add in a column of zeros to nullify the channel I don't want, like so:

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);

As it stands, the code currently produces a sound that is full volume in the left speaker, and half volume (but still very much audible) in the right speaker. I have tried this with at least 20 .wav files, with the same result.

In case it's relevant, this happens even when I write in code that explicitly matches the length of the sound variable in 0s, like so:

[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 anybody know what I'm doing wrong, or have any thoughts?

Upvotes: 3

Views: 641

Answers (3)

aksadv
aksadv

Reputation: 806

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

  1. Save your output as a wav file using audiowrite('output.wav', soundData, Fs). Play this using a different audio player, such as Audacity. If you still hear output in both channels, it must be a sound-card/driver issue.
  2. Assuming that you are using a Windows PC (going by the syntax in your file path), make sure all sound enhancements are disabled. How to do this depends on the PC. If there is a third-party app controlling the playback settings, you'd have to use that. Otherwise find the settings shown in the picture below in the Control Panel.

enter image description here

Upvotes: 1

matiastofteby
matiastofteby

Reputation: 431

When I run your code the audio output is only audible in the left channel as you have specified.

@Austin Kootz' version with the sound()-function is just as good and also produces what you're looking for, but with audioplayer-object you have the ability to stop the playback in the middle of the playback (as you probably know)

Have you tried converting your .wav to another format to see if that makes a change?

Upvotes: 0

Alea Kootz
Alea Kootz

Reputation: 919

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

To control the channel the sound emits on, you'll want to know how sound() reads data.

data is a matrix with the columns representing channels, and with the rows holding the samples of the waveform for a given sampling fequency Fs

here is 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

I hope this works for you. Enjoy!

Upvotes: 1

Related Questions