function collect_vowels(remote_path)
%function collect_vowels(remote_path)
%
%  COLLECT_VOWELS   --  This function will collect several examples of the five
%                       vowels from a speaker, calculate features on each of these
%                       vowel signals, and save them to a directory on the CAEN network
%                       for later processing
%
%  This function requires the following functions:
%     calc_formants.m
%     calc_allpole.m
%     calc_fbank.m
%


frame_size = 1024;
samples = 10;
fs = 8192;

if nargin < 1
    remote_path = 'k:\perm\eecs206\lab10\';
end

if isempty(dir(remote_path))
    error(['Invalid path name: "' remote_path '".']);
end

vowels = {'"oo" as in the word "moon"';
          '"oh" as in the word "boat"';
          '"ah" as in the word "father"';
          '"ae" as in the word "wait"';
          '"ee" as in the word "feet'};

ohs = zeros(samples,frame_size);
ahs = zeros(samples,frame_size);
aes = zeros(samples,frame_size);
ees = zeros(samples,frame_size);

disp(['This program will record ' num2str(samples) ' examples of each of five vowels.  It will then'])
disp(' calculate features on these vowel signals and store them to a directory on')
disp(' the CAEN network.  Another program, process_vowels.m, will collect the')
disp(' data files recorded by the class and calculate statistics on them.');
disp(' ');

for i=1:length(vowels)
    disp(['We will now record examples of the vowel ' vowels{i} '.']);
    disp(' ');
    disp(' When the program indicates that it is ready, begin speaking this vowel');
    disp(' into the microphone and hit the <Enter> key.  The program will then record');
    disp(' a short segment of sound. You should try to keep your vowel at a constant');
    disp(' loudness and pitch while the program is recording.  When recording is complete,');
    disp(' the program will plot the signal and ask if you wish to re-record the sample');
    disp(' or not.  If the sample has an uneven amplitude, you should re-record it.');
    disp(' ');

    ct = 1;
    vowel_sigs{i} = zeros(samples,frame_size);
    while ct <= samples
    
        disp(['Trial #' num2str(ct) '/' num2str(samples) ...
                ':  Ready... begin speaking an ' vowels{i}(1:4) ...
                ' vowel, then hit any key to record.']);
        pause
        frame = wavrecord(frame_size*2,fs);
        frame = frame(frame_size/2 + (1:frame_size))';
        frame = frame/max(abs(frame));
        
        figure(1);
        plot((1:frame_size)/fs,frame);
        xlabel('Time (seconds)');
        ylabel('Amplitude')

        answer = input('Keep this sample (y/n)? ','s');
        if ~isempty(answer) & upper(answer(1)) == 'N'
            disp('Discarding sample');
        else
            disp('Keeping sample');
            vowel_sigs{i}(ct,:) = frame';
            ct = ct+1;
        end
        disp(' ');
    end
end

for i=1:length(vowels)
    allpole{i} = calc_allpole(vowel_sigs{i});
    formant{i} = calc_formants(vowel_sigs{i});
    fbank{i} = calc_fbank(vowel_sigs{i});
end

done = 0;
while ~done
    fname = sprintf('%0.8f',rand);
    fname = fname(3:end);
    if isempty(dir([remote_path fname '.mat']))
        save([remote_path fname '.mat'],'allpole','formant','fbank');
        done = 1;
    end
end
    