How do I control which channel sound is played through using MATLAB? - 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?

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:
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.
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.

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!

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?

Related

MATLAB: Combine all videos in directory

I have directories that each have several short (~10 second) .avi videos. Does anybody know how I can concatenate all of the videos in a specific directory in alphabetical order to form one single video?
I would try to use VLC, but I have to do this for over a thousand different directories. I didn't realize this would be so difficult, but not able to find anything on Google.
More specifics:
For each directory I want to perform this action on, all videos are guaranteed to be:
.avi,MJPG,20fps,640x480 resolution,no audio,between less than 1 second to 15 seconds long
I'd like the single video file to play just as if I played the individuals back-to-back.
If there's any other specifics I missed please let me know.
The combined videos are intended to all be put into the same directory and given to another person to perform their own video processing on with Matlab. They'll be doing something with either crosscorrelation or machine learning to try and identify a particular object in the videos.
You can use a combination of the VideoReader and VideoWriter (see doc for more examples). Iterate through your video files in alphabetical order and "stream" them into a new file.
I threw together some (untested) code. I have no idea how fast this is, though:
cd(VIDEO_DIRECTORY);
tmp = dir('*.avi'); % all .avi video clips
videoList = {tmp.name}'; % sort this list if necessary! sort(videoList) might work
% create output in seperate folder (to avoid accidentally using it as input)
mkdir('output');
outputVideo = VideoWriter(fullfile(workingDir,'output/mergedVideo.avi'));
% if all clips are from the same source/have the same specifications
% just initialize with the settings of the first video in videoList
inputVideo_init = VideoReader(videoList{1}); % first video
outputVideo.FrameRate = inputVideo_init.FrameRate;
open(outputVideo) % >> open stream
% iterate over all videos you want to merge (e.g. in videoList)
for i = 1:length(videoList)
% select i-th clip (assumes they are in order in this list!)
inputVideo = VideoReader(videoList{i});
% -- stream your inputVideo into an outputVideo
while hasFrame(inputVideo)
writeVideo(outputVideo, readFrame(inputVideo));
end
end
close(outputVideo) % << close after having iterated through all videos

fft on samples of an audio file in matlab

I'm trying to extract information from a sound file in order to use it in a video classification algorithm I'm working on.
My problem is that I don't know how to work exactly with audio files in Matlab.
Below is what I need to accomplish:
open the audio file and get the sampling rate/frequency
I need to work on a window of 2 seconds so I have to loop over the file and get each 2 seconds as a window and then do the ftt (Fast-Fourier-Transform) on each window.
After that it is my turn to use these values to do what I want
any help would be appreciated
Thanks.
Following code may only give you some idea. You may need to determine another fft size, a windowing function like hamming etc.
To read a wav file:
[data, Fs] = wavread('path.wav');
Splitting to 2 sec windows and getting fft:
frameFFT = [];
timeStep = Fs*2;
for i=1:timeStep:length(data)-timeStep
frameFFT = [frameFFT; fft(data(i:i+timeStep-1),1024)];
end

Simultaneous playback of multiple videos with MATLAB

I searched the internet and stack overflow but could not find a solution or even helpful hints to my problem.
I need to write a specialised video annotation software in MATLAB which has to be capable to play multiple videos (at least 2) simultaneously on a GUI. The video files are XVID-encoded. Up to now, I basically just adjusted the mathworks.com example for video playback (xylophon.avi, see movie() description).
I am familiar with the mmreader, VideoReader, movie and implay functions but still I am facing two issues:
Even if I read in only a small number of frames (like in the xylophon.avi example), my progam soon exceeds available memory. Also, it takes quite long to read in even relatively few frames (say 100).
The movie() function is sycnhronous, so the second video does not start until the first video completed. How can I call two movie()-functions concurrently? Or is there another way to show two (or more) videos simultaneously?
Any suggestions? Thanks!
First of all MATLAB is not multithreaded. Doing two things in parallel will be difficult. Try to breakout to Java. Matlab uses JIDE as their graphical front-end which is built on Swing. Use MATLAB Builder JA in order to compile your MATLAB code to Java, or add your own 'Panels' to the IDE as shown in this question.
You could display the videos in two different windows and start the playback simultaneously by giving the videos a handle and calling its undocumented play function. This removes any struggle you might have with videos of unequal length as well.
handle1 = implay('file1.mp4');
handle2 = implay('file2.mp4');
handle1.Parent.Position = [100 100 640 480];
handle2.Parent.Position = [740 100 640 480];
play(handle1.DataSource.Controls)
play(handle2.DataSource.Controls)
In principle, you can display each video frame as an image and alternate updating each video, but getting it to play at exactly the right frame rate might be difficult.
Try something like the following. This probably won't work as-is, but you should be able to update it.
v1 = VideoReader(firstVideo)
v2 = VideoReader(secondVideo)
i1 = 0;
i2 = 0;
while i1 < v1.NumberOfFrames && i2 < v2.NumberOfFrames
if i1 < v1.NumberOfFrames
i1 = i1+1;
subplot(1,2,1)
image(v1.read(i1))
end
if i2 < v2.NumberOfFrames
i2 = i2+1;
subplot(1,2,2)
image(v2.read(i2))
end
drawnow
end

How to make an animation from multiple results?

I have some calculation with matrices and have set my loop to run for (let's say) 50 times.
I also assigned a color to each value so I can get a picture in the end of these matrices based on their values.
What I don't know is - how to make an animation from this multiple images I get in each turn.
Is it possible?!
The following code is what I have used previously to produce an .avi file
n = 15;
p = randperm(n);
figure('Color','white');
fcount = 0;
for k = 1:n-1
% produce the plot
[idx,idx] = min(p(k:n));
p(idx+k-1) = p(k);
p(k) = k;
plot(p,'*')
% Make sure plot updates before we capture the contents
pause(0.1)
F(k) = getframe(gcf); %#ok
end
movie2avi(F,'so1.avi','fps',2,'quality',100);
However, there seems to be some issues with the avi codec now for use with Windows XP, for example see this thread.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/271172
I had the same problem; the avi file produced with the default Indeo codec would not run in Windows Media Player. Using a different codec, such as
movie2avi(F,'so1.avi','fps',2,'quality',100,'compression','Cinepak');
solved the problem. You may need to experiment to find a working combination.
Hth, Darren
I'm not sure what you're trying to do. One option is to use the MS-GIF animator, although 50 images is a bit much. See http://en.wikipedia.org/wiki/Microsoft_GIF_Animator for info. Considering the number of images, you might want to create a powerpoint document.

a problem with wavread function

I wrote a program that record my voice write it and then play it
Fs=11025;
y= wavrecord(2*Fs,Fs,'int16');
wavwrite(y,Fs,'t1.wav');
wavplay(y);
I heard my voice clear
then I want to write another program that reads that file and play it
y=wavread('t1.wav');
wavplay(y);
when I run the second program I didn't hear my voice clear
so how can I read the wav file and then play it without a problem?
The wavwrite you used may assume that the incoming y is of range [-1 +1]. Try to normalize y before calling wavwrite
y_float = double(y)/2^15; %% Normalize int16(y) by 2^15
wavwrite(y_float,Fs, 't1.wav');
In the case that something goes wrong with wavplay reading the floating point y_float, you can convert the read-back signal from floating point to int16 again:
y_float_readback = wavread('t1.wav');
y_int16_readback = int16(y_float_readback*2^15);
wavplay(y_int16_readback);
You may compare y_int16_readback with the original y. They should be identical.
This script works absolutely fine for me using the latest version of matlab. I heard clear audio using both your 1st and 2nd code excerpts.
One thing I noticed is that you record y as 'int16' type and then save it to the wav file. When you load the wav file using wavread, y is stored in the matlab workspace as double precision floating point data by default. If you want y to be loaded in the same format in which you saved it try the following.
y=wavread('t1.wav','native');
wavplay(y);