function out = sound_load(varargin)

% sound_load - load contents of library sound file
% ------------------------------------------------
%
% out = sound_load(file)
%
%     = sound_load(lib, name)
%
% Input:
% ------
%  file - sound file
%  lib - library to load sound from (def: active library)
%  name - name of sound
%
% Output:
% -------
%  out - sound file contents struct

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 6184 $
% $Date: 2006-08-16 19:53:20 -0400 (Wed, 16 Aug 2006) $
%--------------------------------

%-----------------------------------
% HANDLE INPUT
%-----------------------------------

%--
% handle variable input
%--

switch length(varargin)

	% file input
	
	case 1, lib = []; name = []; file = varargin{1};
	
	% library and sound name input
	
	case 2, 
		lib = varargin{1}; name = varargin{2}; file = get_library_sound_file(lib, name);
		
	% pass all on recursive call
	
	case 3,
		lib = varargin{1}; name = varargin{2}; file = varargin{3};
		
	% error
	
	otherwise
		error('Input must be either sound file location, or library and name.');
		
end

%--
% handle multiple sounds recursively
%--

if iscellstr(file)
	
	for k = 1:length(file)
		out(k) = sound_load(lib, name, file{k});
	end
	
	return; 
	
end

%--
% check for file existence
%--

if ~exist(file, 'file')
	
	[ignore, name, ignore] = fileparts(file);
	
	error(['Unable to find ''', name, ''' sound file in library.']);

end

%-----------------------------------
% LOAD SOUND
%-----------------------------------

%--
% load sound file variables
%--

% TODO: rename 'snd' to 'sound', this requires a change in 'sound_save' as well

[out, fail] = mat_load(file, 'sound', 'state');

if isempty(fail)
	out.sound = update_sound(out.sound, lib, file); return;
end

%--
% handle missing sound variables
%--

if ismember('sound', fail)
		
	% NOTE: this loads old sound files and does not update them
	
	%---------------------------------
	% BC CODE
	%---------------------------------
	
	[old, fail] = mat_load(file, 'snd', 'state');
	
	if ~ismember('snd', fail)
		out.sound = update_sound(old.snd, lib, file);
	else
		error('File does not contain sound variable.');
	end	
	
end

% NOTE: return empty state when state is not available

if ismember('state', fail)
	out.state = [];
end


%---------------------------------
% UPDATE_SOUND
%---------------------------------

% NOTE: this will work while updates to the sound structure remain simple

function sound = update_sound(sound, lib, file)

%--
% update struct using current constructor
%--

opt = struct_update; opt.flatten = 0;

sound = struct_update(sound_create(''), sound, opt);

%--
% hack
%--

% NOTE: this code required the change in the signature of this function

% NOTE: this code was pulled from XBAT palette it is not very good code

sound = hack_sound(sound, lib, file);


%---------------------------------
% HACK_SOUND
%---------------------------------

function sound = hack_sound(sound, lib, file)

%----------------------------------------

%--
% update sound with attributes
%--

sound = sound_attribute_update(sound);

%----------------------------------------

%--
% check that sound files exist in the same location
%--

if iscell(sound.file)
	
	%--
	% check all files in sound
	%--
	
	for k = 1:length(sound.file)		
		test(k) = exist([sound.path, sound.file{k}], 'file');
	end
	
else
	
	%--
	% check single sound file
	%--
		
	test = exist([sound.path, sound.file], 'file');
	
end

%--
% find the sound files and reassign if needed
%--

if any(test ~= 2)
	
	%--
	% ask to repair sound
	%--
	
	str = { ...
		'Unable to find sound files.', ...
		' Would you like to find the sound files?' ...
	};
	
	out = quest_dialog(double_space(str), ' Missing Sound Files', 'Cancel');

	% return on cancel
	
	if ~strcmp(out, 'Yes')
		return;
	end

	%--
	% repair sound
	%--

	repaired = sound_repair(sound);

	if isempty(repaired)

		% NOTE: sound files were not found or the operation was cancelled

		return;

	else 

		% NOTE: update sound file with new file locations

		sound = repaired; 

		if isempty(lib)

			% TODO: get library from file

			libs = get_unique_libraries;

			candidates = strcat({libs.path}, sound_name(sound));

			ix = find(strcmp(fileparts(file), candidates));

			% NOTE: the empty should produce a warning

			if ~isempty(ix)
				lib = libs(ix);
			else
				lib = get_active_library;
			end

		end

		sound_save(lib, sound); 

	end
	
end

%--
% check for improper time and page in sound
%--

% FIXME: this is a temporary fix. write a general sound repair function

% NOTE: this exception handles new sounds

try
	
	if ((sound.view.time + sound.view.page.duration) > get_sound_duration(sound))

		%--
		% display warning
		%--

		warning('There was a problem with the saved sound, part of the state may be lost.');

		%--
		% rename fields for convenience
		%--

		t = sound.view.time;

		dt = sound.view.page.duration;

		dur = sound.duration;

		%--
		% update time and if needed page duration
		%--

		% NOTE: this part is very ugly, this needs to be revised

		t = dur - (1.1 * dt);

		if (t < 0)
			t = 0;
			dt = dur - 0.1;
		end

		%--
		% update sound fields
		%--

		sound.view.time = t; 

		sound.view.page.duration = dt;

	end
	
end















