How can i read wave files to a column vector?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
Ken Atwell
2013년 7월 8일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!