function [audiosignal,audiomodsignal]=align(filename,filepath)

%   align

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      

cd (filepath);
[audio.signal,audio.fs,audio.bits]=wavread(filename);

global anzahlsegmente
segmentedit=findobj('tag','segmentedit');
anzahlsegmente=str2num(get(segmentedit,'string'));

segmentreihenfolge=randperm(anzahlsegmente);
segmentreihenfolge=segmentreihenfolge-1;
segmentgroessenvec=size(audio.signal)/anzahlsegmente;
segmentgroesse=floor(segmentgroessenvec(1));
for i=1:anzahlsegmente
   segment(i).signal=zeros(segmentgroesse,1);
   for j=1:segmentgroesse
      segment(i).signal(j,1)=audio.signal(j+segmentreihenfolge(i)*segmentgroesse);
   end
end

audio.modsignal=segment(1).signal;
for k=2:anzahlsegmente
   audio.modsignal=[audio.modsignal;segment(k).signal];
end

%abspielen
sound(audio.modsignal,audio.fs)


targetfilename=strrep(filename, '.wav','ral.wav');
wavwrite(audio.modsignal,audio.fs,audio.bits,targetfilename);

%origfftsizevec=size(audio.signal);
%origfftsize=origfftsizevec(1);
%modfftsizevec=size(audio.modsignal);
%modfftsize=modfftsizevec(1);

%FFT
%audio.spektrum=fft(audio.signal,origfftsize);
%audio.modspektrum=fft(audio.modsignal,modfftsize);


% Komplexe Zahlen in real und imag-Teil aufsplitten
%audio.realspektrum=real(audio.spektrum);
%audio.imagspektrum=imag(audio.spektrum);
%audio.modrealspektrum=real(audio.modspektrum);
%audio.modimagspektrum=imag(audio.modspektrum);

% Magnitude 

%for l=1:origfftsize
%    z1=(audio.realspektrum(l))^2;
%    z2=(audio.imagspektrum(l))^2;
%    audio.mag(l,1)=sqrt(z1+z2);
%end
%for l=1:modfftsize
%    z1=(audio.modrealspektrum(l))^2;
%    z2=(audio.modimagspektrum(l))^2;
%    audio.modmag(l,1)=sqrt(z1+z2);
%end


audiosignal=audio.signal;
audiomodsignal=audio.modsignal;
return