Skip to contents

Duplicate (and rename) a textgrid tier.

Usage

duplicate_tier

Format

A Praat script

textgrid_in

path of the textgrid file to read in

target_tier

tier to copy

duplicate_name

name to use for the duplicated tier

duplicate_position

where to place the new tier: last (default), first, before or after the original.

textgrid_out

path of the textgrid file to create

Example usage

library(tjm.praat)

tg <- system.file(
  "demo-textgrids/merge-duplicate-intervals.TextGrid",
  package = "tjm.praat"
)

tg_out <- tempfile("test", fileext = ".TextGrid")

# Note that there are three tiers
tg |> 
  readtextgrid::read_textgrid() |> 
  magrittr::extract(c("tier_num", "tier_name")) |> 
  unique()
#> # A tibble: 3 x 2
#>   tier_num tier_name
#>      <dbl> <chr>    
#> 1        1 words    
#> 2        2 phones   
#> 3        3 silence

f_duplicate_tier <- wrap_praat_script(
  script_code_to_run = duplicate_tier,
  returning = "last-argument"
)

tg_out <- tg |> 
  f_duplicate_tier("phones", "duplicate-is-first", "first", tg_out) |> 
  f_duplicate_tier("phones", "duplicate-is-last", "last", tg_out) |> 
  f_duplicate_tier("phones", "duplicate-is-after", "after", tg_out) |> 
  f_duplicate_tier("phones", "duplicate-is-before", "before", tg_out)

# We duplicated "phones" four times and 
# placed the copy into separate locations
tg_out |> 
  readtextgrid::read_textgrid() |> 
  magrittr::extract(c("tier_num", "tier_name")) |> 
  unique()
#> # A tibble: 7 x 2
#>   tier_num tier_name          
#>      <dbl> <chr>              
#> 1        1 duplicate-is-first 
#> 2        2 words              
#> 3        3 duplicate-is-before
#> 4        4 phones             
#> 5        5 duplicate-is-after 
#> 6        6 silence            
#> 7        7 duplicate-is-last

Praat source code

print(f_duplicate_tier, condense = FALSE)
function (textgrid_in = NULL, target_tier = "phones", duplicate_name =
"phones2", duplicate_position = "1", textgrid_out = NULL)
# <wrapped_praat_script>
# returning: "last-argument"
form Duplicate a TextGrid tier
  sentence Textgrid_in
  sentence Target_tier phones
  sentence Duplicate_name phones2
  choice Duplicate_position: 1
    button last
    button first
    button before
    button after
  sentence Textgrid_out
endform
Read from file: textgrid_in$
# Find and duplicate tier
@findNumberForTier: target_tier$
tiers = Get number of tiers
@setDuplicatePosition: duplicate_position$, findNumberForTier.result, tiers
Duplicate tier:
... findNumberForTier.result,
... setDuplicatePosition.result,
... duplicate_name$
Save as text file: textgrid_out$
procedure setDuplicatePosition: .position$, .current_num, .total_tiers
    if .position$ = "last"
        .result = .total_tiers + 1
    endif
    if .position$ = "first"
        .result = 1
    endif
    if .position$ = "before"
        .result = .current_num
    endif
    if .position$ = "after"
        .result = .current_num + 1
    endif
endproc
# Find the number of the (last) tier with a given name
procedure findNumberForTier: .target_tier$
  .tiers = Get number of tiers
  .result = 0
  for .tier_i to .tiers
    .tier_i_name$ = Get tier name: .tier_i
      if .tier_i_name$ == .target_tier$
        .result = .tier_i
      endif
  endfor
endproc