function [par, created] = open_library_sound(name, lib, create)

% open_library_sound - open library sound
% ---------------------------------------
%
% [par, created] = open_library_sound(name, lib, create)
%
% Input:
% ------
%  name - sound name
%  lib - parent library
%  create - create new browser (def: 0)
%
% Output:
% -------
%  par - browser handle
%  created - new browser created indicator

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

%--
% set new browser option
%--

if (nargin < 3) || isempty(create)
	create = 0;
end

%--
% get library if needed
%--

if (nargin < 2) || isempty(lib)
	lib = get_active_library;
end

%----------------------------------
% SETUP
%----------------------------------

%--
% load sound file content
%--

content = sound_load(lib, name);

% NOTE: get browser state and sound from sound

field = fieldnames(content);

if (length(field) > 1) & ~isempty(find(strcmp('state', field)))
	state = content.state;
else
	state = [];
end

sound = content.sound;

%----------------------------------
% OPEN SOUND
%----------------------------------

%--
% check for open sound
%--

[opened, par] = sound_is_open(sound, lib);

if ~isempty(par) && ~create 

	figure(par(1)); created = 0; return;
	
end

%--
% open sound in browser
%--
	
% TODO: there is something minor not done here

if ~opened
	par = browser(sound);
end

created = 1;

%--
% update browser display state
%--

if ~isempty(state)
	
	try
				
		set_browser_state(par, state);
		
	catch
		
		% NOTE: develop a framework for warnings and errors with logging
		
		str = 'Unable to set browser state.';
		
		disp(' ')
		str_line(str); disp(str); str_line(str);
		disp(' ');
		
		xml_disp(lasterror);
				
	end
	
end