Skip to contents

Interval text that matches the silence pattern is replaced with "silent". All others are replaced with "sounding".

Usage

convert_tier_to_silences

Format

A Praat script

textgrid_in

path of the textgrid file to read in

target_tier

tier to copy

silence_regex

regular expression (regex) used to indentify silences. Current default is ^$|sil|sp which treats empty strings (^$) or (|) a string containing sil or a string containing sp as silences.

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")

# We have the phonemes and silences in "bird house"
tg_data <- readtextgrid::read_textgrid(tg)
tg_data[tg_data$tier_num == 2, c("tier_name", "xmin", "xmax", "text")]
#> # A tibble: 10 x 4
#>    tier_name  xmin  xmax text 
#>    <chr>     <dbl> <dbl> <chr>
#>  1 phones     0     0.25 "sil"
#>  2 phones     0.25  0.35 "B"  
#>  3 phones     0.35  0.49 "ER1"
#>  4 phones     0.49  0.52 "D"  
#>  5 phones     0.52  0.55 "sp" 
#>  6 phones     0.55  0.59 "HH" 
#>  7 phones     0.59  1.04 "AW1"
#>  8 phones     1.04  1.16 "S"  
#>  9 phones     1.16  1.39 "sp" 
#> 10 phones     1.39  1.41 ""

f_convert_tier_to_silences <- wrap_praat_script(
  script_code_to_run = convert_tier_to_silences,
  returning = "last-argument"
)

tg_data2 <- tg |> 
  f_convert_tier_to_silences("phones", "^$|sil|sp", tg_out) |>
  readtextgrid::read_textgrid()

tg_data2[tg_data2$tier_num == 2, c("tier_name", "xmin", "xmax", "text")]
#> # A tibble: 10 x 4
#>    tier_name  xmin  xmax text    
#>    <chr>     <dbl> <dbl> <chr>   
#>  1 phones     0     0.25 silent  
#>  2 phones     0.25  0.35 sounding
#>  3 phones     0.35  0.49 sounding
#>  4 phones     0.49  0.52 sounding
#>  5 phones     0.52  0.55 silent  
#>  6 phones     0.55  0.59 sounding
#>  7 phones     0.59  1.04 sounding
#>  8 phones     1.04  1.16 sounding
#>  9 phones     1.16  1.39 silent  
#> 10 phones     1.39  1.41 silent

Praat source code

print(f_convert_tier_to_silences, condense = FALSE)
function (textgrid_in = NULL, target_tier = "phones", silence_regex =
"$|sil|sp", textgrid_out = NULL)
# <wrapped_praat_script>
# returning: "last-argument"
form Convert annotations into "silence" and "sounding"
  sentence Textgrid_in
  sentence Target_tier phones
  sentence Silence_regex ^$|sil|sp
  sentence Textgrid_out
endform
Read from file: textgrid_in$
@findNumberForTier: target_tier$
Replace interval texts:
... findNumberForTier.result, 1, 0,
... silence_regex$, "silent", "Regular Expressions"
Replace interval texts:
... findNumberForTier.result, 1, 0,
... "^((?!silent).)*$", "sounding", "Regular Expressions"
Save as text file: textgrid_out$
# 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