function sound = sound_repair(sound)

% sound_repair - reassign files to sound
% --------------------------------------
%
% sound = sound_repair(sound)
%
% Input:
% ------
%  sound - sound to repair
%
% Output:
% -------
%  sound - repaired sound or empty

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 6176 $
% $Date: 2006-08-15 19:41:57 -0400 (Tue, 15 Aug 2006) $
%--------------------------------

%----------------------------------------------------------
% GET SOUND FILES TO ASSIGN
%----------------------------------------------------------

%--
% get location of file or files
%--

[f, p] = uiget_sound_files(sound);

% NOTE: return empty on cancel

if ~p
	sound = []; return;
end

%--
% get file stream files
%--

if ~strcmp(sound.type, 'File')
	
	%--
	% try to get format handler 
	%--

	format = get_file_format(f);

	%--
	% get same format files
	%--
	
	files = get_format_files(p, format); 
	
	%--
	% put files sound sorted order
	%--
	
	[ignore, ext] = file_ext(f);
	
	files = flipud(files.(lower(ext)));
	
end

%----------------------------------------------------------
% COMPARE OLD AND NEW FILES
%----------------------------------------------------------

switch (sound.type)
	
	%-------------------------------
	% FILE
	%-------------------------------
	
	case ('file')
		
		%--
		% get and check selected file info
		%--
				
		info = sound_file_info([p,f]);
				
		% NOTE: return empty if we don't match
		
		if ( ...
			(info.samplerate ~= sound.samplerate) | ...
			(info.samples ~= sound.samples) | ...
			(info.channels ~= sound.channels) | ...
			(info.samplesize ~= sound.samplesize) ...
		)
			sound = []; return;
		end
			
		%--
		% update file location
		%--

		sound.path = p; sound.file = f;
			
	%-------------------------------
	% FILE STREAM
	%-------------------------------
	
	case ('file stream')
		
		%--
		% check the number of files
		%--
		
		if (length(sound.file) ~= length(files))
			sound = []; return;
		end
		
		%--
		% check the info of the files
		%--
		
		for k = 1:length(files)
		
			%--
			% get and check selected sound file info
			%--
						
			info = sound_file_info([p, files{k}]);
			
			% NOTE: return empty if we don't match
			
			if ( ...
				(info.samplerate ~= sound.samplerate) | ...
				(info.samples ~= sound.samples(k)) | ...
				(info.channels ~= sound.channels) | ...
				(info.samplesize ~= sound.samplesize) ...
			)
				sound = []; return;
			end
			
		end
				
		%--
		% update file location
		%--
				
		sound.path = p; sound.file = files;		
		
end