function seqObject = seq_av163( seqName, basedir )
  
% FUNCTION SEQOBJECT = SEQ_AV163( SEQNAME, BASEDIR )
% 
% For a given sequence of the AV16.3 corpus,
% returns various constants such as waveform filenames, microphone array geometry, etc.
% Each constant is a field of the returned structure "seqObject".
%
% Note that filename strings are always absolute (from '/' downward)
% so you can use them as is.
%
% You need to have the "CONTENTS_DETAILED" file sitting in your
% "BASEDIR", because "seq_av163.m" extracts the timecode of the first
% video frame from "CONTENTS_DETAILED".
%
% By Guillaume Lathoud, 2004 - lathoud@idiap.ch

  dbstop error
  
  if nargin < 2
    error( 'seq needs two input arguments.' );
  end
  
  
  AVCORPUS_DIR = basedir;
  SYNTHMULTISOURCE_DIR = fullfile( basedir, 'synthmultisource' );
  
  % Microphone array geometry:
  % two uniform circular arrays, 8 mic each.
  
  a = (4:11)*pi/4; 
  g = 0.1* [cos(a);sin(a);0*a];
  
  g1 = g; g1( 2,: ) = g1( 2,: ) + 0.4;
  g2 = g; g2( 2,: ) = g2( 2,: ) - 0.4;
  
  % Cartesian 3D coordinates
  AVCORPUS_MA_GEOMETRY = [ g1 g2 ];
  SYNTHMULTISOURCE_MA_GEOMETRY = g;
  
  % Default: we did not find this sequence in the corpus
  o = [];
  found_it = 0;
  
  if ~found_it & strcmp( seqName(1:3), 'seq' )
    
    % First look for a directory bearing this name
    
    d_session = dir( fullfile( AVCORPUS_DIR, 'session*' ) );
    for a = 1:length( d_session )
      
      if d_session( a ).isdir
	
	if exist( fullfile( AVCORPUS_DIR, d_session( a ).name, seqName ) , 'dir' )
	  found_it = 1;
	  session_name = d_session( a ).name;
	  break;
	end
	
      end
      
    end
    
    
    % Second, if we found it, look for WAV files and such
    % Mark their location in the "o" structure
    
    if found_it
      
      o.NAME = seqName;
      
      o.DIR = fullfile( AVCORPUS_DIR, session_name, seqName );
      
      o.MA_DIR = fullfile( o.DIR, '16kHz' );
      
      o.MA_GEOMETRY = AVCORPUS_MA_GEOMETRY;
      o.MA_FILE_LIST = {};
      
      % Microphone array 1
      
      for a = 1:8
	a_name = sprintf( '%s_array1_mic%d.wav', seqName, a );
	o.MA_FILE_LIST{ a } = fullfile( o.MA_DIR, a_name );
      end
      
      % Microphone array 2
      
      for a = 1:8
	a_name = sprintf( '%s_array2_mic%d.wav', seqName, a );
	o.MA_FILE_LIST{ a + 8 } = fullfile( o.MA_DIR, a_name );
      end
      
      % Lapels
      
      o.LAPEL_DIR = o.MA_DIR;
      o.LAPEL_FILE_LIST = {};
      for a = 1:1
	a_name = sprintf( '%s_lapel%d.wav', seqName, a );
	o.LAPEL_FILE_LIST{ a } = fullfile( o.LAPEL_DIR, a_name );
      end
      
      % Ground-Truth annotation
      
      o.GT_DIR = o.DIR;
      o.GT_FILE = fullfile( o.GT_DIR, [ seqName '_gt.mat' ] );

      % Get time stamp of frame #1, for each camera
      for a = 1:3
	fid = fopen( fullfile( AVCORPUS_DIR, 'CONTENTS_DETAILED' ), 'rt' );
	if fid < 0
	  warning( sprintf( 'seq_av163 could not access "CONTENTS_DETAILED". Camera %d timecode unknowns for this recording.', a ) );
	else
	  s = [ seqName sprintf( '_cam%d_divx.avi', a ) ];
	  while 1
	    aline = fgetl( fid );
	    if ~ischar( aline), break, end;

	    ind = strfind( aline, s );
	    if ~isempty( ind )
	      [t,r] = strtok( aline( ind(1)+length(s):end ) );
	      atime = timecode_to_sec( t, 25 );
	      o.cam( a ).frame1_timestamp = atime;
	    end
	  end
	  fclose( fid );
	end
      end
      
      % Camera calibration parameters for session08
      
      cal_dir = fullfile( AVCORPUS_DIR, 'CAL_session08', 'MANUAL_CENTER_R2_TG' );
      
      % Euclidian projection matrix
      for a_cam = 1:3
	Pmat = load( fullfile( cal_dir, sprintf( 'camera%d.Pmat.cal', a_cam ) ), '-ASCII' );
	o.calib.cam( a_cam ).session08_Pmat = Pmat;
      end

      % Radial distorsion parameters
      for a_cam = 1:3
	[ K, kc, alpha_c ] = readradfile( fullfile( cal_dir, sprintf( 'cam%d.rad', a_cam ) ) );
	o.calib.cam( a_cam ).session08_K  = K;
	o.calib.cam( a_cam ).session08_kc = kc;
	o.calib.cam( a_cam ).session08_alpha_c = alpha_c;
      end
      
      % Additional small rigid alignment
      % Note that we do not use the combination [cam 2, cam3] because it is not so precise
      
      load( fullfile( cal_dir, 'rigid010203.mat' ) );
      o.calib.rigid010203_Pmat = rigid.Pmat;
      
      load( fullfile( cal_dir, 'rigid0102.mat' ) );
      o.calib.rigid0102_Pmat = rigid.Pmat;
      
      load( fullfile( cal_dir, 'rigid0103.mat' ) );
      o.calib.rigid0103_Pmat = rigid.Pmat;
      
      % Determine the shift in image plane from session08 to this session
      if strcmp( session_name, 'session08' )
	for a_cam = 1:3
	  o.calib.cam( a_cam ).delta_x_from_session08 = 0;
	  o.calib.cam( a_cam ).delta_y_from_session08 = 0;
	end
      else
	shift_dir = fullfile( AVCORPUS_DIR, [ 'CAL_' session_name ], 'SHIFT' );
	s = [ 'session08_to_' session_name(end-1:end) ];
	load( fullfile( shift_dir, [ session_name '_shift.mat' ] ), s );
	for a_cam = 1:3
	  o.calib.cam( a_cam ).delta_x_from_session08 = eval( sprintf( [ s '.cam(%d).delta_x' ], a_cam ) );
	  o.calib.cam( a_cam ).delta_y_from_session08 = eval( sprintf( [ s '.cam(%d).delta_y' ], a_cam ) );
	end
      end
      
    end
    
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  if ~found_it 
    
    if strcmp( seqName, 'synthmultisource-setup1' )
      
      found_it = 1;
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      % Recording with 3 loudspeakers playing synthetic voyels
      % One 8-mic microphone array
      
      o.NAME = seqName;
      
      o.DIR = SYNTHMULTISOURCE_DIR;
      
      % Recorded microphone array waveforms
      
      o.MA_DIR = o.DIR;
      o.MA_GEOMETRY = SYNTHMULTISOURCE_MA_GEOMETRY;
      o.MA_FILE_LIST = {};
      d = dir( fullfile( o.MA_DIR, [ seqName '*.wav' ] ) );
      for a = 1:length( d )
	o.MA_FILE_LIST{ a } = fullfile( o.MA_DIR, d( a ).name );
      end
      
      % Loudspeaker waveforms that were played (synthesized vowels)

      o.SYNTHMULTISOURCE_DIR = o.DIR;
      o.SYNTHMULTISOURCE_FILE_LIST = {};
      d = dir( fullfile( o.SYNTHMULTISOURCE_DIR, 'synthmultisource-synth-channel-*.wav' ) );
      for a = 1:length( d )
	o.SYNTHMULTISOURCE_FILE_LIST{ a } = fullfile( o.SYNTHMULTISOURCE_DIR, d( a ).name );
      end

      % Truth
      
      o.GT_DIR  = o.DIR;
      o.GT_FILE = fullfile( o.GT_DIR, [ seqName '_gt.mat' ] );
      
    elseif strcmp( seqName, 'synthmultisource-setup2' )

      found_it = 1;
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      % Recording with 3 loudspeakers playing synthetic voyels
      % One 8-mic microphone array
      
      o.NAME = seqName;
      
      o.DIR = SYNTHMULTISOURCE_DIR;
      
      % Recorded microphone array waveforms
      
      o.MA_DIR = o.DIR;
      o.MA_GEOMETRY = SYNTHMULTISOURCE_MA_GEOMETRY;
      o.MA_FILE_LIST = {};
      d = dir( fullfile( o.MA_DIR, [ seqName '*.wav' ] ) );
      for a = 1:length( d )
	o.MA_FILE_LIST{ a } = fullfile( o.MA_DIR, d( a ).name );
      end
      
      % Loudspeaker waveforms that were played (synthesized vowels)

      o.SYNTHMULTISOURCE_DIR = o.DIR;
      o.SYNTHMULTISOURCE_FILE_LIST = {};
      d = dir( fullfile( o.SYNTHMULTISOURCE_DIR, 'synthmultisource-synth-channel-*.wav' ) );
      for a = 1:length( d )
	o.SYNTHMULTISOURCE_FILE_LIST{ a } = fullfile( o.SYNTHMULTISOURCE_DIR, d( a ).name );
      end
      
      % Truth
      
      o.GT_DIR  = o.DIR;
      o.GT_FILE = fullfile( o.GT_DIR, [ seqName '_gt.mat' ] );

    elseif strcmp( seqName, 'synthmultisource-setup3' )

      found_it = 1;
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      % Recording with 3 loudspeakers playing synthetic voyels
      % One 8-mic microphone array
      
      o.NAME = seqName;
      
      o.DIR = SYNTHMULTISOURCE_DIR;
      
      % Recorded microphone array waveforms
      
      o.MA_DIR = o.DIR;
      o.MA_GEOMETRY = SYNTHMULTISOURCE_MA_GEOMETRY;
      o.MA_FILE_LIST = {};
      d = dir( fullfile( o.MA_DIR, [ seqName '*.wav' ] ) );
      for a = 1:length( d )
	o.MA_FILE_LIST{ a } = fullfile( o.MA_DIR, d( a ).name );
      end

      % Loudspeaker waveforms that were played (synthesized vowels)
      
      o.SYNTHMULTISOURCE_DIR = o.DIR;
      o.SYNTHMULTISOURCE_FILE_LIST = {};
      d = dir( fullfile( o.SYNTHMULTISOURCE_DIR, 'synthmultisource-synth-channel-*.wav' ) );
      for a = 1:length( d )
	o.SYNTHMULTISOURCE_FILE_LIST{ a } = fullfile( o.SYNTHMULTISOURCE_DIR, d( a ).name );
      end

      % Truth
      
      o.GT_DIR  = o.DIR;
      o.GT_FILE = fullfile( o.GT_DIR, [ seqName '_gt.mat' ] );
      
    end
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Return the structure: [] if sequence not found
  seqObject = o;
  