src/main/java/ca/phon/plugins/praat/export/TextGridExporter.java
/**
	 * Export the specified tier from the given Phon record
	 * into the given text grid with specified name.
	 *
	 * @param record
	 * @param tier
	 * @param type
	 * @param textgrid
	 * @param tgName
	 */
Export the specified tier from the given Phon record
into the given text grid with specified name.


Export the specified tier from the given Phon record into the given text grid with specified name .
public void addTierToTextGrid(Record record, String tier, Segmentation type,
			TextGrid textgrid, String tgName) {
		IntervalTier tgTier = findIntervalTier(textgrid, tgName);
		if(tgTier == null) return;
		final MediaSegment mediaSeg = record.getSegment().getGroup(0);
		if(mediaSeg == null || (mediaSeg.getEndValue() - mediaSeg.getStartValue()) <= 0) return;
		double startTime = mediaSeg.getStartValue() / 1000.0;
		double endTime = mediaSeg.getEndValue() / 1000.0;
		final TextInterval lastInterval =
				(tgTier.numberOfIntervals() > 0 ? tgTier.interval(tgTier.numberOfIntervals()) : null);
		final double lastIntervalEnd =
				(lastInterval != null ? lastInterval.getXmax() : 0.0);
		if(lastIntervalEnd < startTime) {
			tgTier.addInterval(lastIntervalEnd, startTime, "");
		} else if(lastIntervalEnd > startTime) {
			startTime = lastIntervalEnd;
		}
		final SystemTierType systemTier = SystemTierType.tierFromString(tier);
		final double totalTime = endTime - startTime;
		final double groupLength = totalTime / record.numberOfGroups();
		if(type == Segmentation.TIER) {
			final Tier<String> t = record.getTier(tier, String.class);
			final String tierData = (t == null ? "" : t.toString());
			setupSingleIntervalTier(textgrid, tgTier, tierData, startTime, endTime);
		} else {
			double currentStart = startTime;
			double dataEnd = endTime;
			for(int i = 0; i < record.numberOfGroups(); i++) {
				final Group group = record.getGroup(i);
				final double groupStart = currentStart;
				final double groupEnd = (i == record.numberOfGroups() - 1 ? dataEnd : groupStart + groupLength);
				if(type == Segmentation.GROUP) {
					String data = "";
					if(systemTier != null) {
						if(systemTier == SystemTierType.Orthography) {
							data = (group.getOrthography() != null ? group.getOrthography().toString() : "");
						} else if(systemTier == SystemTierType.IPATarget) {
							data = (group.getIPATarget() != null ? group.getIPATarget().toString() : "");
						} else if(systemTier == SystemTierType.IPAActual) {
							data = (group.getIPAActual() != null ? group.getIPAActual().toString() : "");
						}
					} else {
						if(group.getTier(tier) != null)
							data = group.getTier(tier, String.class);
					}
					addGroup(tgTier, data, currentStart, groupEnd);
					currentStart = groupEnd;
				} else if(type == Segmentation.WORD) {
					String data = "";
					if(systemTier != null) {
						if(systemTier == SystemTierType.Orthography) {
							for(int wIdx = 0; wIdx < group.getAlignedWordCount(); wIdx++) {
								final Word alignedWord = group.getAlignedWord(wIdx);
								final String orthoWord = (alignedWord != null && alignedWord.getOrthography() != null ? 
										alignedWord.getOrthography().toString() : "");
								data += (orthoWord.length() > 0 ? (data.length() > 0 ? " " : "") + orthoWord : "");
							}
						} else if(systemTier == SystemTierType.IPATarget) {
							data = (group.getIPATarget() != null ? group.getIPATarget().toString() : "");
						} else if(systemTier == SystemTierType.IPAActual) {
							data = (group.getIPAActual() != null ? group.getIPAActual().toString() : "");
						}
					} else {
						if(group.getTier(tier) != null)
							data = (group.getTier(tier) != null ? group.getTier(tier, String.class) : "");
					}
					addWords(tgTier, data, currentStart, groupEnd);
					currentStart = groupEnd;
				} else if(type == Segmentation.SYLLABLE) {
					if(systemTier == SystemTierType.IPATarget ||
							systemTier == SystemTierType.IPAActual) {
						final IPATranscript ipa =
								(systemTier == SystemTierType.IPATarget ? group.getIPATarget() : group.getIPAActual());
						if(ipa.length() == 0) {
							tgTier.addInterval(groupStart, groupEnd, "");
						} else {
							addSyllables(tgTier, systemTier == SystemTierType.IPATarget ? group.getIPATarget() : group.getIPAActual(), currentStart, groupEnd);
						}
						currentStart = groupEnd;
					}
				} else if(type == Segmentation.PHONE) {
					if(systemTier == SystemTierType.IPATarget ||
							systemTier == SystemTierType.IPAActual) {
						final IPATranscript ipa =
								(systemTier == SystemTierType.IPATarget ? group.getIPATarget() : group.getIPAActual());
						if(ipa.length() == 0) {
							tgTier.addInterval(groupStart, groupEnd, "");
						} else {
							addPhones(tgTier, systemTier == SystemTierType.IPATarget ? group.getIPATarget() : group.getIPAActual(), currentStart, groupEnd);
						}
						currentStart = groupEnd;
					}
				}
			}
		}
	}
public void addTierToTextGrid ( Record record , String tier , Segmentation type , TextGrid textgrid , String tgName ) { IntervalTier tgTier = findIntervalTier ( textgrid , tgName ) ; if ( tgTier == null ) return ; final MediaSegment mediaSeg = record . getSegment ( ) . getGroup ( 0 ) ; if ( mediaSeg == null || ( mediaSeg . getEndValue ( ) - mediaSeg . getStartValue ( ) ) <= 0 ) return ; double startTime = mediaSeg . getStartValue ( ) / 1000.0 ; double endTime = mediaSeg . getEndValue ( ) / 1000.0 ; final TextInterval lastInterval = ( tgTier . numberOfIntervals ( ) > 0 ? tgTier . interval ( tgTier . numberOfIntervals ( ) ) : null ) ; final double lastIntervalEnd = ( lastInterval != null ? lastInterval . getXmax ( ) : 0.0 ) ; if ( lastIntervalEnd < startTime ) { tgTier . addInterval ( lastIntervalEnd , startTime , "" ) ; } else if ( lastIntervalEnd > startTime ) { startTime = lastIntervalEnd ; } final SystemTierType systemTier = SystemTierType . tierFromString ( tier ) ; final double totalTime = endTime - startTime ; final double groupLength = totalTime / record . numberOfGroups ( ) ; if ( type == Segmentation . TIER ) { final Tier < String > t = record . getTier ( tier , String . class ) ; final String tierData = ( t == null ? "" : t . toString ( ) ) ; setupSingleIntervalTier ( textgrid , tgTier , tierData , startTime , endTime ) ; } else { double currentStart = startTime ; double dataEnd = endTime ; for ( int i = 0 ; i < record . numberOfGroups ( ) ; i ++ ) { final Group group = record . getGroup ( i ) ; final double groupStart = currentStart ; final double groupEnd = ( i == record . numberOfGroups ( ) - 1 ? dataEnd : groupStart + groupLength ) ; if ( type == Segmentation . GROUP ) { String data = "" ; if ( systemTier != null ) { if ( systemTier == SystemTierType . Orthography ) { data = ( group . getOrthography ( ) != null ? group . getOrthography ( ) . toString ( ) : "" ) ; } else if ( systemTier == SystemTierType . IPATarget ) { data = ( group . getIPATarget ( ) != null ? group . getIPATarget ( ) . toString ( ) : "" ) ; } else if ( systemTier == SystemTierType . IPAActual ) { data = ( group . getIPAActual ( ) != null ? group . getIPAActual ( ) . toString ( ) : "" ) ; } } else { if ( group . getTier ( tier ) != null ) data = group . getTier ( tier , String . class ) ; } addGroup ( tgTier , data , currentStart , groupEnd ) ; currentStart = groupEnd ; } else if ( type == Segmentation . WORD ) { String data = "" ; if ( systemTier != null ) { if ( systemTier == SystemTierType . Orthography ) { for ( int wIdx = 0 ; wIdx < group . getAlignedWordCount ( ) ; wIdx ++ ) { final Word alignedWord = group . getAlignedWord ( wIdx ) ; final String orthoWord = ( alignedWord != null && alignedWord . getOrthography ( ) != null ? alignedWord . getOrthography ( ) . toString ( ) : "" ) ; data += ( orthoWord . length ( ) > 0 ? ( data . length ( ) > 0 ? " " : "" ) + orthoWord : "" ) ; } } else if ( systemTier == SystemTierType . IPATarget ) { data = ( group . getIPATarget ( ) != null ? group . getIPATarget ( ) . toString ( ) : "" ) ; } else if ( systemTier == SystemTierType . IPAActual ) { data = ( group . getIPAActual ( ) != null ? group . getIPAActual ( ) . toString ( ) : "" ) ; } } else { if ( group . getTier ( tier ) != null ) data = ( group . getTier ( tier ) != null ? group . getTier ( tier , String . class ) : "" ) ; } addWords ( tgTier , data , currentStart , groupEnd ) ; currentStart = groupEnd ; } else if ( type == Segmentation . SYLLABLE ) { if ( systemTier == SystemTierType . IPATarget || systemTier == SystemTierType . IPAActual ) { final IPATranscript ipa = ( systemTier == SystemTierType . IPATarget ? group . getIPATarget ( ) : group . getIPAActual ( ) ) ; if ( ipa . length ( ) == 0 ) { tgTier . addInterval ( groupStart , groupEnd , "" ) ; } else { addSyllables ( tgTier , systemTier == SystemTierType . IPATarget ? group . getIPATarget ( ) : group . getIPAActual ( ) , currentStart , groupEnd ) ; } currentStart = groupEnd ; } } else if ( type == Segmentation . PHONE ) { if ( systemTier == SystemTierType . IPATarget || systemTier == SystemTierType . IPAActual ) { final IPATranscript ipa = ( systemTier == SystemTierType . IPATarget ? group . getIPATarget ( ) : group . getIPAActual ( ) ) ; if ( ipa . length ( ) == 0 ) { tgTier . addInterval ( groupStart , groupEnd , "" ) ; } else { addPhones ( tgTier , systemTier == SystemTierType . IPATarget ? group . getIPATarget ( ) : group . getIPAActual ( ) , currentStart , groupEnd ) ; } currentStart = groupEnd ; } } } } }
Export the specified tier from the given Phon record
 into the given text grid with specified name.
Export the specified tier from the given Phon record into the given text grid with specified name .
// create the new textgrid tier // add a new empty interval // adjust start time for this record // check if we a processing a built-in tier // calculate some values for interval times // if the exportType is TIER, we create a 3-interval tier // this takes care of all flat tiers