%sucht die datei (1)  "coelebs_aufn1_ch1"unter sourcedir  bei datei (2)
%   coelebs_aufn1_ch1_01 im segmentsdir.
%   daraufhin wird die genaue stelle des vorkommens von (2) in (1) gesucht,
%   und in sekunden an den dateinamen angehängt; 

clear
global globalvars;
addpath('..\vogelst\k-band250905')
sourcedir='F:\Aufnahmen\2006-Juni-3-Remscheid-Daniel';
%sourcedir='F:\Ample';
%sourcedir='M:\PrograMathe\matlab\work\hiwi\vogelst\testmaterial\online2';

segmentsdir='F:\viele_einzelne_strophen\fringila_coelebs_buchfink\typ1';

%segmentsdir='F:\viele_einzelne_strophen\fringila_coelebs_buchfink\tst';
%segmentsdir='M:\PrograMathe\matlab\work\hiwi\vogelst\testmaterial\online';



%---------------------------------------------z-------------zugehörige dateien suchen 
allpaths=genpath(sourcedir);
data_names=dir(strcat(segmentsdir,'\*.wav'));
psourcedirs = explode(';',allpaths);
for i = 1:length(data_names)
    ldata_names{i} = data_names(i).name;
end

names_done = [];
relsource_files = {};
for i = 1:length(psourcedirs)
    actdir = dir(strcat(psourcedirs{i},'\*.wav'));
    ldata_names = ldata_names(setdiff(1:length(ldata_names),names_done));
    for j = 1:length(ldata_names)
        for k = 1:length(actdir)
            if(strncmp(actdir(k).name,ldata_names{j},length(ldata_names{j})-7))
                names_done = [names_done j];
                relsource_files= cat(1,relsource_files,{[strcat(psourcedirs{i},'\',actdir(k).name)] [strcat(segmentsdir,'\',ldata_names{j})]});
            end
        end
    end
end



oldsource='';
oldsamplesource=0;
k=1;
position=zeros(size(relsource_files,1),2);

i=1;
while i <= size(relsource_files,1)
    aktsource =char(relsource_files(i,1));
    aktsamplesource=i;
%--------------------------------------------------features generieren und overhead

    if ~strcmp(aktsource,oldsource) || (aktsamplesource~=oldsamplesource)   %neue quelldatei
        aktmajstart=1;
        siz=WAVREAD(aktsource,'size');
        siz=siz(1,1);
        aktmajpointer = min(siz,globalvars.maxaudiodatalen);
        [aktdata,sr,nbits]=wavread(aktsource,[aktmajstart aktmajpointer]);
        oldsource=aktsource;	
        if length(aktdata(1,:)) > 1
            aktdata = aktdata(:,1);
        end
        windowsz=floor(sr/5);
         fprintf('generating features for fast search \n');
        aktfeat =resample(abs(specgram(aktdata,windowsz,sr,windowsz,floor(windowsz/2))),1,40);  
    elseif (aktmajpointer < siz) && (aktsamplesource==oldsamplesource)  %alte quelldatei zu lang
        aktmajstart=aktmajstart+floor(globalvars.maxaudiodatalen/2);
        aktmajpointer=min(aktmajpointer+floor(globalvars.maxaudiodatalen/2),siz);
        if (aktmajstart-aktmajpointer) < windowsz
            aktmajstart=max(aktmajstart-windowsz,1);
        end
         [aktdata,sr,nbits]=wavread(aktsource,[aktmajstart aktmajpointer]);
        if length(aktdata(1,:)) > 1
            aktdata = aktdata(:,1);
        end
         fprintf('generating features for fast search \n');
        aktfeat =resample(abs(specgram(aktdata,windowsz,sr,windowsz,floor(windowsz/2))),1,40);  
    end
    
    
    if (aktsamplesource~=oldsamplesource)%neue suchdatei
	    [aktsample,sr,nbits]=wavread(char(relsource_files(i,2)));
        oldsamplesource=aktsamplesource;
	    if length(aktsample(1,:)) > 1
	        aktsample = aktsample(:,1);
	    end
	    
	    if(length(aktsample)>=windowsz)
	        aktsamplefeat =resample(abs(specgram(aktsample(1:windowsz),windowsz,sr,windowsz,floor(windowsz/2))),1,40);
	        for j=1:size(aktfeat,2)
	            di(j)=dist(aktfeat(:,j),aktsamplefeat(:,1));
	        end
	        bdi = di>mean(di);
	    else 
	        fprintf('fast search not possible \n');
	        bdi=ones(1,size(aktfeat,2))
	    end   
	    fprintf(strcat('searching for \n', escape(aktsource ),' in \n' ,escape(char(relsource_files(i,2))),'\n' ));
    end
    
    
%---------------------- bestandteil lokalisieren (k mismatches), ausreichende minmatches        
    minmatches=length(aktsample);
    found=0;
    aktpointer=1;
    aktstart=1;
    len = length(aktdata);
    len2=length(aktsample);  
    while ((aktstart+len2)<=len) &&~found
        mismatches=0;
        aktstart=aktstart+1;
        aktpointer=aktstart;
        while (aktpointer <= len)&&(mismatches <k)&&((aktpointer-(aktstart)+1)<=len2)
            if(aktdata(aktpointer) ~= aktsample(aktpointer-(aktstart)+1))
                mismatches = mismatches+1;
                if(~bdi(floor(aktpointer/(windowsz/2))+1))
                    aktpointer=aktpointer+(windowsz/2);
                end
            end
            aktpointer=aktpointer+1;
            if (aktpointer-aktstart) >= minmatches
                fprintf('found');
                position(i,:)=[floor((aktmajstart-1+aktstart)/sr) floor((aktmajstart-1+aktpointer)/sr)]
                found=1;
                break
            end
        end
    end
    if(position(i,2)~=0)
        
%---------------------------------------------zugehörige infos in dateien schreiben 
        newfilename= strcat(substr(char(relsource_files(i,2)),0,length(relsource_files(i,2))-5),'__',...
            s_to_hhmmss(position(i,1)),'-',s_to_hhmmss(position(i,2)),'.wav');
        movefile(char(relsource_files(i,2)),newfilename);
        
        filetoopen=strcat(newfilename,'.txt');
        ftoex=dir(filetoopen);
        if(length(ftoex)==0)
           FID = fopen(filetoopen,'w');
           fprintf(FID,'%s','annotations for ');
           fprintf(FID,'%s',newfilename);
           fprintf(FID,'\n');
           fprintf(FID,'%s','sourcefile available at');
           fprintf(FID,'\n');
           fprintf(FID,'%s',char(relsource_files(i,1)));
           fprintf(FID,'\n');
           fprintf(FID,'%s','time of occurrence in hhmmss format:');
           fprintf(FID,'\n');
           fprintf(FID,'%s',s_to_hhmmss(position(i,1)));
           fprintf(FID,'%s','-');
           fprintf(FID,'%s',s_to_hhmmss(position(i,2)));
           fprintf(FID,'\n');
           fprintf(FID,'%s','in seconds:');
           fprintf(FID,'\n');
           fprintf(FID,'%s',sprintf('%d',position(i,1)));
           fprintf(FID,'\n');
           fprintf(FID,'%s',sprintf('%d',position(i,2)));
           fprintf(FID,'\n');
           fprintf(FID,'%s','comments:');

           fclose(FID);
        end

       i=i+1; 
    elseif(aktmajpointer==siz)
       i=i+1; 
    end
end


    
    
    
    
