How can i read wave files to a column vector?
3 views (last 30 days)
Show older comments
Hi All!
I would like to read many wave files to a column vector, where i can reach each of them anytime. Pls help me to do this.
Here is my source code:
clear; clc;
myFolder = 'C:\Users\Aron\Samples\proba';
if exist(myFolder, 'dir') ~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav');
wavFiles = dir(filePattern);
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[sampleArray, Fs] = wavread(fullFileName);
end
%sound(sampleArray, Fs);
In this case i can only reach the last sample, because of the loop. How can i play the chosen sample???If for example there are 80 samples that folder and i just want to play the 50th sample.
Thx in advance
0 Comments
Accepted Answer
Ken Atwell
on 8 Jul 2013
Rather than having sampleArray hold only the most recently read WAV file, make sampleArray a cell array of all of them. You would also need make Fs a vector of all sample rates. Your code would look something like this (written from memory, untested):
sampleArray = cell(length(wavFiles),1);
Fs = zeros(size(sampleArray));
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[sampleArray{k}, Fs(k)] = wavread(fullFileName);
end
To play the 50th sample:
sound(sampleArray{50}, Fs(50));
Keep your eye on your application's memory usage. If you have lots of long WAV files in memory, your application will potentially use a lot of memory.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!