function loctimeseg = construct_raw_loctimeseg( frame_index, tag, fl, fs, cluster_list, startTime, endTime )

% function loctimeseg = construct_raw_loctimeseg( frame_index, tag, fl, fs, cluster_list, startTime, endTime )
%
% Location-based speaker segmentation using the result of multispeaker
% detection-localization, which itself includes short-term clustering.
%
% Segmentation is unsupervised: locations were determined automatically
% by "static_bic.m" or "static_sigma.m". Speaker ids are unknown.
%
%
% INPUTS:
%
% frame_index:  vector of nframes integers: time frame indices (frames defined by fl and fs).
%               Need not be unique, e.g. multiple location estimates within a frame.
% tag:          vector of nframes integers: to which short-term cluster the location estimate belongs.
% fl:           time frame length in seconds.
% fs:           time frame shift in seconds.
% cluster_list: array of objects, as returned by "static_bic.m" or "static_sigma.m".
%
%
% OUTPUT:
%
% loctimeseg: an object defining segmentation of concurrent speakers, IN TERMS OF LOCATIONS.
%  Fields are:
% - loctimeseg.nChannels:  an integer defining the number of LOCATIONS.
% - loctimeseg.azList:     vector of nChannels azimuth locations (float, radians).
% - loctimeseg.startTime:  a float, in seconds, beginning of the segmentation.
% - loctimeseg.endTime:    a float, in seconds, end of the segmentation.
% - loctimeseg.seg:        a list of nChannels matrices. Each matrix defines
% the speech/non-speech segmentation for one speaker: each column is a segment.
%   - row 1 is start of the segment in seconds.
%   - row 2 is end of the segment in seconds.
%   - row 3 is 0 or 1. 0 means non-speech, 1 means speech.
%
%
% G. Lathoud 2006 - lathoud@idiap.ch
  
  if nargin < 7
    error( [ mfilename ' needs 7 input arguments.' ] );
  end 
  
  [ tmp, ind ] = sort( mod( [ cluster_list.mu ], 2 * pi ) );
  cluster_list = cluster_list( ind );
  
  loctimeseg.nChannels = numel( cluster_list );
  loctimeseg.azList    = [ cluster_list.mu ];
  loctimeseg.startTime = startTime;
  loctimeseg.endTime   = endTime;
  loctimeseg.seg       = cell( loctimeseg.nChannels );
  
  for a = 1:loctimeseg.nChannels
    
    a_seg = [];

    for b = 1:numel( cluster_list( a ).tag )
      
      ind = find( tag == cluster_list( a ).tag( b ) );
      
      % Create a speech segment for location #a,
      % from the middle of the first time frame 
      % to the middle of the last time frame.
      
      a_seg = [ a_seg,  [ fl / 2 + fs * ( min( frame_index( ind ) ) - 1 );
			  fl / 2 + fs * ( max( frame_index( ind ) ) - 1 ) ] ];
      
    end

    % Sort them

    a_seg = sortrows( a_seg.' ).';
    
    % Each frame should be occupied from beginning to end
    
    a_seg = dilate_seg( a_seg, fl / 2 );

    % Merge consecutive segments

    a_seg = simplify_seg( a_seg );
    
    % Explicit silences and store

    tmp           = define_silence_in_seg( a_seg - loctimeseg.startTime, loctimeseg.endTime - loctimeseg.startTime );
    tmp( 1:2, : ) = tmp( 1:2, : ) + loctimeseg.startTime;
    
    loctimeseg.seg{ a } = tmp;
    
  end
  