Blog Archive

Wednesday, November 24, 2010

Matlab foder and file read in loop & Struct

function speakerData=DirFileRead(dirName, sentenceNumPerSpeaker);
% speakerDataRead: Read wave file info for speaker identification from a given directory.
% Usage: speakerData=DirFileRead(dirName, sentenceNumPerSpeaker);
% dirName: root directory that contains subdirectory of each speaker, in which the wave files reside.
% senenceNumPerSpeaker: How many sentences to read for each speaker
% speakerData: the retrieved data structure
% Adapted from Roger Jang


if nargin<1
fprintf('We will make a demo here');
fprintf('First we will create fake folder with one file saved under temp_demo/dir1/1a1 ');
mkdir('temp_demo/dir1/1a1');
mkdir('temp_demo/dir1/1a2');
mkdir('temp_demo/dir2/2b1');
mkdir('temp_demo/dir2/2b2');
save temp_demo/dir1/1a1/space.txt;
dirName='temp_demo'; sentenceNumPerSpeaker=1;
fprintf('read dir and file');
speakerData=DirFileRead(dirName, sentenceNumPerSpeaker)

fprintf('Display result:');
fprintf('Total %d files/folders are under %s', length(speakerData), dirName);
fprintf('File/Folder 1 is: %s', speakerData(1).name );


end
if (dirName(end)~='/') | (dirName(end)~='\'); dirName=[dirName, '/']; end

% dirName='E:\SI_Ref\data_digit\waveFile';
% ====== Collect feature from all wave files
speakerData = dir(dirName);
speakerData(1:2) = []; % Get rid of '.' and '..'
speakerData=speakerData([speakerData.isdir]); % Take directories only
speakerNum=length(speakerData); % speakerNum=28
for i=1:speakerNum
waveFiles = dir([dirName, speakerData(i).name, '/*.wav']); % this is euqal to waveFiles = dir('dirName\speakerData(i).name/*.wav');
% eg; [dirName, speakerData(i).name, '/*.wav']=E:\SI_Ref\data_digit\waveFile\10028/*.wav
% This line works the same as the next line.
% waveFiles = dir([dirName, '\',speakerData(i).name, '/*.wav']);
waveNum=length(waveFiles);
% fprintf('%d/%d: Reading %d wave files recorded by %s\n', i, speakerNum, waveNum, speakerData(i).name);
for j=1:waveNum, % wavNum=20
if j>sentenceNumPerSpeaker, break; end % sentenceNumPerSpeaker=2
speakerData(i).sentence(j).path = [dirName, speakerData(i).name, '/', waveFiles(j).name];
% fprintf('\t%d/%d: Processing %s...\n', j, waveNum, speakerData(i).sentence(j).path);
speakerData(i).sentence(j).text=waveFiles(j).name(1:end-4); % "end-4" means truncate the part of ".wav"
% =10028-0b, ie, the name of the wave file without the extention name
end
end

speakerData=rmfield(speakerData, 'isdir'); % get rid of 'isdir' field
speakerData=rmfield(speakerData, 'bytes'); % get rid of 'bytes' field


%self_demo
%wav_dir='E:\SI_Ref\Data_Demo';speakerDataRead(wav_dir,1);

No comments:

Post a Comment