function Tapes = load_tapes(DB, tape_name)
%load_tapes Loads all or specific tapes and returns them as vector of Tape

if ~isempty(tape_name)
    % load only tapes in cell array tape_name
    
     % read training info
    FID= fopen([DB 'REFERENCE.csv']); 
    info=textscan(FID,'%5s , %d'); 
    Truth.Tape=cell2mat(info{1}); 
    Truth.label=info{2};
    fclose(FID);
    
    if ~iscell(tape_name)
        tape_name = {tape_name};
    end
    
    if strcmp(tape_name{1}(end-3:end), '.wav')
        wav_files(length(tape_name)) = dir([DB tape_name{1}]);
    else
        wav_files(length(tape_name)) = dir([DB tape_name{1} '.wav']);
    end
        
    for i = 1:length(tape_name)
        if strcmp(tape_name{i}(end-3:end), '.wav')
            wav_files(i) = dir([DB tape_name{i}]);
        else
            wav_files(i) = dir([DB tape_name{i} '.wav']);
        end
    end
    
    hwait = waitbar(0,'Loading Tapes ...','CreateCancelBtn','delete(gcf);return;');
    
    Tapes(numel(wav_files)) = Tape;
    for l = 1:numel(wav_files)
        file_name = wav_files(l).name(1:end-4);
        % search for file in training info
        ind = strfind(info{1},file_name); 

        % load signal
        [tm,signal] = rdsamp([DB file_name]); 

        Tapes(l) = Tape(file_name, Truth.label(~cellfun(@isempty,ind)), tm, signal);

        waitbar(l/length(Tapes), hwait, ['Loading Tapes ... ' num2str(floor(l*100/length(Tapes))) '%']);
    end
else
    % load all tapes (first try to find tapes.mat with all tapes already
    % stored
    
    tape_file = dir('./tapes.mat');
    if ~isempty(tape_file)
        load(tape_file.name);
    else
        % read training info
        FID= fopen([DB 'REFERENCE.csv']); 
        info = textscan(FID,'%s%d','delimiter',',');
        
        Truth.Tape = info{1};
        Truth.label= info{2};
        fclose(FID);
        
        % try to find real files
        wav_files = dir([DB '*.wav']);

        % assign Tapes
        hwait = waitbar(0,'Loading Tapes ...','CreateCancelBtn','delete(gcf);return;');

        Tapes(numel(wav_files)) = Tape;
        for l = 1:numel(wav_files)
            file_name = wav_files(l).name(1:end-4);
            % search for file in training info
            ind = strfind(info{1},file_name); 

            % load signal
            try
                [tm,signal] = rdsamp([DB file_name]); 
            catch
                [signal, fs] = audioread([DB file_name '.wav']);
                tm = 1/fs*length(signal);
            end
            truth = Truth.label(~cellfun(@isempty,ind));
            Tapes(l) = Tape(file_name, truth(1), tm, signal);

            waitbar(l/length(Tapes), hwait, ['Loading Tapes ... ' num2str(floor(l*100/length(Tapes))) '%']);
        end

        save('tapes.mat','Tapes');
    end
end