## This script extracts and plays marked vowels of the selected type in files from a selected decade.
## Vowels must be manually marked, and decade is deduced from the file name.
## Vowels can be extracted either in isolation or in the context of a target word.
## The script is designed to qualitatively assess vowel differences that are measured. With the use of the script, 
## a large quantity of vowels can be browsed relatively quickly.
##
## The script is written by jthoegersen@hum.ku.dk, GNU/GPL
##

form Vowel Extractor
choice Vowel 1
    button e
    button æ
    button ANLong
    button ANShort
    button AMShort
    button AMLong
    button C
    button O
    button å
    button ræ
    button ru
    button Aj
    button oj

choice Target 1
    button 1950s
    button 1960s
    button 1970s
    button 1980s
    button 1990s
    button 2000s

choice Context 2
    button In isolation
    button In target word
endform

Create Strings as file list... files L:\Korpus\vokaler\final\*.TextGrid

select Strings files
strings = Get number of strings

for string to strings

    select Strings files

    string$ = Get string... string
    filename$ = replace$ ("'string$'", ".TextGrid", "", 1)

    ### Select files ###
    variable$ = replace_regex$ ("'filename$'", "([0-9]+_)?([0-9]_)?[0-9]{8}_", "", 1)
    variable$ = replace_regex$ ("'variable$'", "_?F", "", 1)

    if variable$ = vowel$
        
        file$ = replace_regex$ ("'filename$'", "_'variable$'_?F", "", 1)
        file2$ = replace_regex$ ("'file$'", "^([0-9]+_)?([0-9]_)?([0-9]_)*", "", 1)
        year$ = replace_regex$ ("'file2$'", "[0-9]{4}$", "", 1)
        decade$ = replace_regex$ ("'year$'", "[0-9]$", "0", 1)
        decade$ = "'decade$'s"

        if decade$ = target$

            Open long sound file... L:\Korpus\lydfiler\'file$'.wav
            Read from file... L:\Korpus\vokaler\final\'string$'
    
            hits = Get number of intervals... 3

            for hit to hits

                select TextGrid 'filename$'

                label$ = Get label of interval... 3 hit

                if label$ <> ""

                    start = Get start point... 3 hit
                    end = Get end point... 3 hit
                    mid = start+((end-start)/2)

                    if context$ = "In target word"
                        ortointerval = Get interval at time... 1 mid
                        start = Get start point... 1 ortointerval
                        end = Get end point... 1 ortointerval
                        end = end+0.01
                    endif
    
                    select LongSound 'file$'

                    Extract part... start-0.01 end+0.01 no
                    Play
                    Remove

                    beginPause ("Continue?")
                    loop = endPause("Continue", "Next file", "Abort", 1)

                    if loop = 2
                        hit = hits
                    endif

                    if loop = 3
                        hit = hits
                        string = strings
                    endif
                endif
            endfor

            select LongSound 'file$'
            Remove
            select TextGrid 'filename$'
            Remove
        endif
    endif
endfor

select Strings files
Remove