src/main/java/ca/phon/plugins/praat/TextGridManager.java
/**
	 * Merge text grids for a session.
	 * 
	 * The returned {@link TextGrid} must be deleted
	 * using the {@link Thing#forget()} method when
	 * the object is no longer needed.
	 *
	 * @param session
	 *
	 * @return single TextGrid for merged session
	 */
Merge text grids for a session.

The returned  TextGrid must be deleted
using the  Thing#forget() method when
the object is no longer needed.

@param session

@return single TextGrid for merged session
Merge text grids for a session . The returned TextGrid must be deleted using the Thing#forget () method when the object is no longer needed . @param session @return single TextGrid for merged session
@Deprecated
	public TextGrid mergeTextGrids(Session session)
		throws IOException {
		double xmin = 0;
		double xmax = 0;
		List<TextGrid> textGrids = new ArrayList<>();
		for(Record record:session.getRecords()) {
			final String id = record.getUuid().toString();
			final String textGridPath = textGridPath(id);
			final File textGridFile = new File(textGridPath);
			if(textGridFile.exists()) {
				try {
					TextGrid tg = loadTextGrid(textGridFile);
					xmin = Math.min(xmin, tg.getXmin());
					xmax = Math.max(xmax, tg.getXmax());
					textGrids.add(tg);
				} catch (IOException e) {
					LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
				}
			}
		}
		try {
			TextGrid retVal = TextGrid.createWithoutTiers(xmin, xmax);
			Map<String, Long> intervalTiers = new LinkedHashMap<>();
			Map<String, Long> pointTiers = new LinkedHashMap<>();
			for(TextGrid tg:textGrids) {
				for(long i = 1; i <= tg.numberOfTiers(); i++) {
					try {
						IntervalTier intervalTier = tg.checkSpecifiedTierIsIntervalTier(i);
						IntervalTier fullIntervalTier = null;
						if(!intervalTiers.keySet().contains(intervalTier.getName().toString())) {
							fullIntervalTier = IntervalTier.create(xmin, xmax);
							fullIntervalTier.setName(intervalTier.getName());
							fullIntervalTier.removeInterval(1);
							retVal.addTier(fullIntervalTier);
							intervalTiers.put(intervalTier.getName().toString(), retVal.numberOfTiers());
						}
						Long tierNum = intervalTiers.get(intervalTier.getName().toString());
						if(tierNum != null && tierNum > 0 && tierNum <= retVal.numberOfTiers()) {
							try {
								fullIntervalTier = retVal.checkSpecifiedTierIsIntervalTier(tierNum);
							} catch (PraatException e) {
								LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
							}
						}
						if(fullIntervalTier != null) {
							for(long j = 1; j <= intervalTier.numberOfIntervals(); j++) {
								TextInterval interval = intervalTier.interval(j);
								fullIntervalTier.addInterval(interval.getXmin(), interval.getXmax(), interval.getText());
							}
						}
					} catch (PraatException e) {
						try {
							TextTier pointTier = tg.checkSpecifiedTierIsPointTier(i);
							TextTier fullPointTier = null;
							if(!pointTiers.keySet().contains(pointTier.getName().toString())) {
								fullPointTier = TextTier.create(xmin, xmax);
								fullPointTier.setName(pointTier.getName());
								fullPointTier.removePoint(1);
								retVal.addTier(fullPointTier);
								pointTiers.put(pointTier.getName().toString(), retVal.numberOfTiers());
							}
							Long tierNum = pointTiers.get(pointTier.getName().toString());
							if(tierNum != null && tierNum > 0 && tierNum <= retVal.numberOfTiers()) {
								try {
									fullPointTier = retVal.checkSpecifiedTierIsPointTier(tierNum);
								} catch (PraatException ex) {
									LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
								}
							}
							if(fullPointTier != null) {
								for(long j = 1; j < pointTier.numberOfPoints(); j++) {
									TextPoint tp = pointTier.point(j);
									fullPointTier.addPoint(tp.getNumber(), tp.getText());
								}
							}
						} catch (PraatException e1) {
							LOGGER.log(Level.SEVERE, e1.getLocalizedMessage(), e1);
						}
					}
				}
			}
			return retVal;
		} catch (PraatException e) {
			throw new IOException(e);
		}
	}
@ Deprecated public TextGrid mergeTextGrids ( Session session ) throws IOException { double xmin = 0 ; double xmax = 0 ; List < TextGrid > textGrids = new ArrayList < > ( ) ; for ( Record record : session . getRecords ( ) ) { final String id = record . getUuid ( ) . toString ( ) ; final String textGridPath = textGridPath ( id ) ; final File textGridFile = new File ( textGridPath ) ; if ( textGridFile . exists ( ) ) { try { TextGrid tg = loadTextGrid ( textGridFile ) ; xmin = Math . min ( xmin , tg . getXmin ( ) ) ; xmax = Math . max ( xmax , tg . getXmax ( ) ) ; textGrids . add ( tg ) ; } catch ( IOException e ) { LOGGER . log ( Level . SEVERE , e . getLocalizedMessage ( ) , e ) ; } } } try { TextGrid retVal = TextGrid . createWithoutTiers ( xmin , xmax ) ; Map < String , Long > intervalTiers = new LinkedHashMap < > ( ) ; Map < String , Long > pointTiers = new LinkedHashMap < > ( ) ; for ( TextGrid tg : textGrids ) { for ( long i = 1 ; i <= tg . numberOfTiers ( ) ; i ++ ) { try { IntervalTier intervalTier = tg . checkSpecifiedTierIsIntervalTier ( i ) ; IntervalTier fullIntervalTier = null ; if ( ! intervalTiers . keySet ( ) . contains ( intervalTier . getName ( ) . toString ( ) ) ) { fullIntervalTier = IntervalTier . create ( xmin , xmax ) ; fullIntervalTier . setName ( intervalTier . getName ( ) ) ; fullIntervalTier . removeInterval ( 1 ) ; retVal . addTier ( fullIntervalTier ) ; intervalTiers . put ( intervalTier . getName ( ) . toString ( ) , retVal . numberOfTiers ( ) ) ; } Long tierNum = intervalTiers . get ( intervalTier . getName ( ) . toString ( ) ) ; if ( tierNum != null && tierNum > 0 && tierNum <= retVal . numberOfTiers ( ) ) { try { fullIntervalTier = retVal . checkSpecifiedTierIsIntervalTier ( tierNum ) ; } catch ( PraatException e ) { LOGGER . log ( Level . SEVERE , e . getLocalizedMessage ( ) , e ) ; } } if ( fullIntervalTier != null ) { for ( long j = 1 ; j <= intervalTier . numberOfIntervals ( ) ; j ++ ) { TextInterval interval = intervalTier . interval ( j ) ; fullIntervalTier . addInterval ( interval . getXmin ( ) , interval . getXmax ( ) , interval . getText ( ) ) ; } } } catch ( PraatException e ) { try { TextTier pointTier = tg . checkSpecifiedTierIsPointTier ( i ) ; TextTier fullPointTier = null ; if ( ! pointTiers . keySet ( ) . contains ( pointTier . getName ( ) . toString ( ) ) ) { fullPointTier = TextTier . create ( xmin , xmax ) ; fullPointTier . setName ( pointTier . getName ( ) ) ; fullPointTier . removePoint ( 1 ) ; retVal . addTier ( fullPointTier ) ; pointTiers . put ( pointTier . getName ( ) . toString ( ) , retVal . numberOfTiers ( ) ) ; } Long tierNum = pointTiers . get ( pointTier . getName ( ) . toString ( ) ) ; if ( tierNum != null && tierNum > 0 && tierNum <= retVal . numberOfTiers ( ) ) { try { fullPointTier = retVal . checkSpecifiedTierIsPointTier ( tierNum ) ; } catch ( PraatException ex ) { LOGGER . log ( Level . SEVERE , ex . getLocalizedMessage ( ) , ex ) ; } } if ( fullPointTier != null ) { for ( long j = 1 ; j < pointTier . numberOfPoints ( ) ; j ++ ) { TextPoint tp = pointTier . point ( j ) ; fullPointTier . addPoint ( tp . getNumber ( ) , tp . getText ( ) ) ; } } } catch ( PraatException e1 ) { LOGGER . log ( Level . SEVERE , e1 . getLocalizedMessage ( ) , e1 ) ; } } } } return retVal ; } catch ( PraatException e ) { throw new IOException ( e ) ; } }
Merge text grids for a session.
Merge text grids for a session .
// get xmin and xmax // TODO - show warning to user