Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Praat scripting language #6620

Merged
merged 8 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -1335,3 +1335,6 @@
[submodule "vendor/grammars/zephir-sublime"]
path = vendor/grammars/zephir-sublime
url = https://github.com/phalcon/zephir-sublime
[submodule "vendor/grammars/Praat-tmLanguage"]
path = vendor/grammars/Praat-tmLanguage
url = https://github.com/JJWRoeloffs/Praat-tmLanguage.git
lildude marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ vendor/grammars/PHP-Twig.tmbundle:
- text.html.twig
vendor/grammars/PogoScript.tmbundle:
- source.pogoscript
vendor/grammars/Praat-tmLanguage:
- source.praat
vendor/grammars/RDoc.tmbundle:
- text.rdoc
vendor/grammars/Racket:
Expand Down
13 changes: 13 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5328,6 +5328,19 @@ PowerShell:
interpreters:
- pwsh
language_id: 293
Praat:
type: programming
color: "#c8506d"
tm_scope: source.praat
ace_mode: praat
extensions:
- ".praat"
- ".praat-script"
- ".praat_script"
- ".praatscript"
lildude marked this conversation as resolved.
Show resolved Hide resolved
interpreters:
- praat
lildude marked this conversation as resolved.
Show resolved Hide resolved
language_id: 106029007
Prisma:
type: data
color: "#0c344b"
Expand Down
175 changes: 175 additions & 0 deletions samples/Praat/dynamicity.praat
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
form Textgrid Convolution
comment calculate a Moving Average across a textgrid.
comment does not perform an fft convolution, instead using a dumb algorithm.
comment presumes the textgrid is selected.
real steps_per_second 5
real window_length_sec 5
optionmenu kernel 1
option moving_average
# option gaussian
optionmenu normalisation 1
option amount
option window_length
option none
endform

nrTiers = Get number of tiers
isInterval = Is interval tier: 1

if nrTiers != 1
exitScript: "Can only use dynamicity.praat on a TextGrid with exactly one Tier"
endif

if isInterval == 1
@preposess_intervals
@calculate_nr_steps
@interval_convolution
@write_to_textgrid
else
@calculate_nr_steps
@point_convolution
@write_to_textgrid
endif


procedure preposess_intervals
nrIntervals = Get number of intervals: 1

for i to nrIntervals
label$ = Get label of interval: 1, i
if label$ <> "" and label$ != "MISSING"
variable = number(label$)
Set interval text: 1, i, string$(variable)
endif
endfor
endproc

procedure calculate_nr_steps
start = Get start time
end = Get end time
duration = end - start
nrSteps = floor((duration-window_length_sec)*steps_per_second)
if nrSteps < 0
nrSteps = 0
endif
endproc

procedure interval_convolution
k=1
movingStart = start
movingEnd = movingStart + window_length_sec
nrIntervals = Get number of intervals: 1

if nrIntervals == 0
nrSteps = 0
k = 0
endif

result# = zero#(nrSteps)
for i to nrSteps
intervalEnd = Get end time of interval: 1, k
while (intervalEnd < movingStart) and (k < nrIntervals)
k+=1
intervalEnd = Get end time of interval: 1, k
endwhile

movingSum = 0
amountSummed = 0

j=0
intervalStart = Get start time of interval: 1, k+j
while (intervalStart < movingEnd) and (k+j <= nrIntervals)
label$ = Get label of interval: 1, k+j
if label$ <> "" and label$ != "MISSING"
movingSum += number(label$)
amountSummed+= 1
endif

j+=1
if (k+j <= nrIntervals)
intervalStart = Get start time of interval: 1, k+j
endif
endwhile

movingStart+=(1/steps_per_second)
movingEnd+=(1/steps_per_second)

if normalisation == 1
result#[i] = movingSum/amountSummed
elsif normalisation == 2
result#[i] = movingSum/window_length_sec
elsif normalisation == 3
result#[i] = movingSum
endif
endfor
endproc

procedure point_convolution
k=1
movingStart = start
movingEnd = movingStart + window_length_sec
nrPoints = Get number of points: 1

if nrPoints == 0
nrSteps = 0
k = 0
endif

result# = zero#(nrSteps)
for i to nrSteps
pointStart = Get time of point: 1, k
while (pointStart < movingStart) and (k < nrPoints)
k+=1
pointStart = Get time of point: 1, k
endwhile

movingSum = 0
amountSummed = 0

j=0
pointEnd = Get time of point: 1, k+j
while (pointEnd < movingEnd) and (k+j <= nrPoints)
movingSum += 1

j+=1
if (k+j <= nrPoints)
pointEnd = Get time of point: 1, k+j
endif
endwhile

movingStart+=(1/steps_per_second)
movingEnd+=(1/steps_per_second)

result#[i] = movingSum
endfor
endproc

procedure write_to_textgrid
tierName$ = Get tier name: 1
Insert interval tier: 2, tierName$ + "Dyn"

movingBoundary = (window_length_sec/2) - ((1/steps_per_second)/2)

if movingBoundary <= 0
start = 0.01
else
start = movingBoundary
endif

Insert boundary: 2, start

for i to nrSteps
movingBoundary+= 1/steps_per_second

if movingBoundary >= end
movingBoundary = end - (0.01 * nrSteps - (i + 1))
endif

Insert boundary: 2, movingBoundary
if result#[i] == undefined
Set interval text: 2, i+1, ""
else
Set interval text: 2, i+1, string$(result#[i])
endif
endfor
endproc
73 changes: 73 additions & 0 deletions samples/Praat/queryTable.praat
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
form Querying
integer tier 1
integer nrFormants 10
endform

idSounds = Create Strings as file list: "Sounds", "*.flac"
idGrids = Create Strings as file list: "Grids", "*.TextGrid"
nrGrids = Get number of strings

for i to nrGrids
selectObject: idSounds
sound$ = Get string: i
idSound = Read from file: sound$

idFormant = To Formant (burg): 0, 5, 5500, 0.025, 50

selectObject: idGrids
grid$ = Get string: i

idTable = Create Table with column names: grid$, 0, "Index Vowel Start End Duration F1 F2"
idGrid = Read from file: grid$

nrInt = Get number of intervals: tier
k = 1

for j to nrInt
lab$ = Get label of interval: tier, j

if lab$ = "aa" or lab$ = "u" or lab$ = "i"

index$ = string$(k)
start = Get start time of interval: tier, j
start$ = string$(start)
end = Get end time of interval: tier, j
end$ = string$(end)
dur = end - start
dur$ = string$(dur)

for l to nrFormants
row = l + (k-1)*nrFormants
time = start + dur*(l/nrFormants)

selectObject: idFormant
f1$ = Get value at time: 1, time, "hertz", "linear"
f2$ = Get value at time: 2, time, "hertz", "linear"

selectObject: idTable
Insert row: row
Set string value: row, "Index", index$
Set string value: row, "Vowel", lab$
Set string value: row, "Start", start$
Set string value: row, "End", end$
Set string value: row, "Duration", dur$
Set string value: row, "F1", f1$
Set string value: row, "F2", f2$

endfor

selectObject: idGrid
k = k + 1
endif

endfor

name$ = selected$("TextGrid")
selectObject: idTable
Save as tab-separated file: name$ + ".dat"

removeObject: idSound, idTable, idFormant, idGrid

endfor

removeObject: idGrids, idSounds
73 changes: 73 additions & 0 deletions samples/Praat/randomiseArticles.praat
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
orm Querying
integer tier 1
integer nrFormants 10
endform

idSounds = Create Strings as file list: "Sounds", "*.flac"
idGrids = Create Strings as file list: "Grids", "*.TextGrid"
nrGrids = Get number of strings

for i to nrGrids
selectObject: idSounds
sound$ = Get string: i
idSound = Read from file: sound$

idFormant = To Formant (burg): 0, 5, 5500, 0.025, 50

selectObject: idGrids
grid$ = Get string: i

idTable = Create Table with column names: grid$, 0, "Index Vowel Start End Duration F1 F2"
idGrid = Read from file: grid$

nrInt = Get number of intervals: tier
k = 1

for j to nrInt
lab$ = Get label of interval: tier, j

if lab$ = "aa" or lab$ = "u" or lab$ = "i"

index$ = string$(k)
start = Get start time of interval: tier, j
start$ = string$(start)
end = Get end time of interval: tier, j
end$ = string$(end)
dur = end - start
dur$ = string$(dur)

for l to nrFormants
row = l + (k-1)*nrFormants
time = start + dur*(l/nrFormants)

selectObject: idFormant
f1$ = Get value at time: 1, time, "hertz", "linear"
f2$ = Get value at time: 2, time, "hertz", "linear"

selectObject: idTable
Insert row: row
Set string value: row, "Index", index$
Set string value: row, "Vowel", lab$
Set string value: row, "Start", start$
Set string value: row, "End", end$
Set string value: row, "Duration", dur$
Set string value: row, "F1", f1$
Set string value: row, "F2", f2$

endfor

selectObject: idGrid
k = k + 1
endif

endfor

name$ = selected$("TextGrid")
selectObject: idTable
Save as tab-separated file: name$ + ".dat"

removeObject: idSound, idTable, idFormant, idGrid

endfor

removeObject: idGrids, idSounds