Ocul
Ocul

Reputation: 21

How to make MATLAB play an array faster?

I'm working on a audio processor, which is supposed to change the pitch and add a vibrato to a song while it is playing. Note that, although the sound isn't live (e.g. from a microphone) the effects that I want to add are to be made in real time.

I found that the best way to approach it would be dividing the soundfile into small elements and apply the effects on each one by order.

So I wrote this to divide the audio file:

%Load Sound File
[fsample Fs] = wavread ('C:\Users\Ogulcan\Desktop\Bitirme Projesi\Codes\kravitz.wav'); 


%length of the sample
t=length(fsample);
%number of samples
ns=10;
%Defining the array:
A=[];


%Create the vectors and place them into the array 'A':

for i=1:ns-1
   v=fsample(i*t/ns:(i+1)*t/ns);
   A=[A;v];
end

This code works and divides the sound into 10 samples, however when I try to play them in a loop there is a small but noticable delay. Now I plan on doing this for a lot bigger sample numbers.

Can anyone help me with this speed issue? I really don't know any other language than MATLAB or have the required software, so I would appreciate if you could show me a way to do this in MATLAB.

Upvotes: 1

Views: 896

Answers (1)

Yair Altman
Yair Altman

Reputation: 5722

You can replace the entire loop with the following simpler snippet:

A = fsample(1:end-rem(length(fsample),ns));  % ensure data has ns full samples
A = reshape(A,[],ns)';

Upvotes: 1

Related Questions