Playing an audio file in matlab.

Hello
I found it very interesting that an audio file could be played using matlab. But I am unable to figure out the problem with the code that I am using to do so. I used the following code to play the file and get the samples:
hfile = 'w1.wav';
clear y Fs
% Read the data back into MATLAB, and listen to audio.
[y, Fs, nbits, readinfo] = wavread(hfile);
y,Fs,nbits,readinfo;
sound (y, Fs);
??? Error using ==> wavread at 166 Out of memory. Type HELP MEMORY for your options.
Error in ==> project at 7 [y, Fs, nbits, readinfo] = wavread(hfile);
Could anyone please tell me where I am going wrong or provide an alternative. Also, I am using the awvread function, should I use some other function?
Thanks

Answers (1)

Walter Roberson
Walter Roberson on 4 Feb 2012
You do not have enough memory available to store the data for the entire .wav file in memory at the same time.
Audio that was recorded at 44.1 kHz requires more than 340 kilobytes per second, per channel. 2 channels (stereo) would be about 690 kilobytes per second. If you are using 32 bit MATLAB on an MS Windows machine, you probably do not have more than about 690 megabytes continuous available to you -- enough to store roughly 1000 seconds if I did my calculations correctly.
What rate was your file recorded at, how many channels, and how long is it (in seconds) ?

3 Comments

Thank you very much for the detailed explanation. It was recorded at 128 kbps and the audio length is 5sec.
Please try
[y, Fs, nbits, readinfo] = wavread(hfile,1);
and then tell us Fs and nbits.
128 Kbps indicates how much _compressed_ data was recorded per second. When wavread() is used, the data is decompressed and we cannot tell from the "128 Kbps" figure what it will expand to. The Fs and nbits should tell us.
Oh yes, also please size(y) and class(y) . But we will not need the value of y, just the size and datatype information.

Sign in to comment.

Asked:

on 4 Feb 2012

Edited:

Jan
on 30 Sep 2013

Community Treasure Hunt

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

Start Hunting!