## # This script essentially does nothing but it shows different ways to use filenames in Praat scripts. # This script demonstrates 3 different ways [marked as sections (A) to (C)] to handle files. # # A) Setting the absolute path of a file # B) Different methods to assign a filename to a variable that is later used in the script # Including selecting all files in a directory (B.4) or all subdirectories (B.5) # C) Different ways to put parts of a filename (directory path, basename, extension, other name parts) together. # # # Note that 'spaces' in file names are not allowed in Praat, i.e., "my file" is wrong, "my_file" is correct! # Furthermore, avoid any 'non-standard' characters in file names (e.g. no / or \ or .) # since they can be part of the 'path' of a directory or are changed to an underline! # Stick only to basic letters (i.e. no umlaute) and underline, and you are on the save side. # Note also the correct use of single quotes and double quotes (and that sometimes you need both, # like in: # read from file: "'filename$'" # # Version 1.0, Henning Reetz, 14-june-2007 first version # Version 2.0, Henning Reetz, 23-jan.-2009 include subdirectory handling, more comments # Version 2.1, Henning Reetz, 05-june-2009 subdirectory handling includes now present directory # Version 3.0, Henning Reetz, 07-dec.-2014 adjusted to new PRAAT script syntax (5.4) # Version 3.1, Henning Reetz, 16-dec.-2014 added 'removeObject:' # # in parts tested with Praat 5.4 ## # Clear the Information window; not really necessary, but it is easier to see (error-)messages from the script, in case there are any clearinfo ## A) Set the 'absolute path' for the file; i.e. the directory, where the file is located # with a 'path' that can always be found on a particular disk # Note: The direction of slashes are different in Windows (\) and Mac/UNIX (/), but Praat accept both, # i.e., you can use a Praat skript from a Mac on a Windows machine and vice versa # But there is a difference in how an absolute path looks like on these machines. # Note: If Praat cannot find a file, it adds the present absolute path in front # of the filename. If this also doesn't work, you get an error message with this full file name, # which might be definitely wrong (e.g. if you started your path with './'). # A.1a. UNIX and MAC (the user 'Carl' has a subdirectory 'Sounds' in his 'Documents' directory: # Note: do not forget the '/' at the end! directory$ = "/Users/Carl/Documents/Sounds/" # A.1b. MAC (the data is on an USB stick with the name 'UNTITLED' in the subdirectory 'Sounds'): # Note: do not forget the '/' at the end! directory$ = "/Volumes/UNTITLED/Sounds/" # A.2. WINDOWS (the user 'Carl' has a subdirectory 'Sounds' in his 'My files' directory: # Note: do not forget the '\' at the end! directory$ = "F:\mydisk\Carl\My files\Sounds\" # A.3. Use the present directory (i.e. the directory where you called this script) (any system) # It is often useful to have a 'path' variable even though you set it to 'empty' as done here: # the script is more flexible and can be used with only one change in case you decide to used a different path directory$ = "" # A.4. For those who use different sub-directories for eg. '.wav' files and '.TextGrid' files # This example shows the 'relative path' from the current directory; the specific directories have the names # 'wav_files' and 'grid_files' wav_directory$ = "wav_files/" grid_directory$ = "grid_files/" ## B) Different methods to assign a filename to a variable that is later used in the script # B.1: Assign a name to a variable # Note that we omit the extension (e.g. '.wav') to be more flexible later in the script; # for example, to use the name for '.wav' and '.TextGrid' files. base_name$ = "g071a000" # B.2: Get file name with a form # The string after 'form' appears as title of the window (i.e., here it will be 'File name'). # 'comment' text is written inside the form window, i.e. the 'comment's are texts that appear in the # form-window on the screen at run-time. # Attention: A 'form' may only be used once in a Praat script ! # Note: The variables are directory$ and base_name$ in the script, although there is no # $-sign attached to them in the form. form File name: comment Please give the directory path where the file is located. comment Leave this field empty if you want to use the current directory. word directory comment Please give the file name you want to analyse: word base_name endform printline Base name: 'base_name$', Directory: 'directory$' # B.3: Browse interactively for a file # (not really possible, as far as I know; I use here simply the 'pause' command; very stupid, I know...) # (To get the name of the selected file, you have to extract the name with the 'selected$' construction in B.4. pause Please use 'Open → Read from file…', select a file, and click 'continue' after that. # B.4: Get names of all files (i.e. 'list files') in a directory/folder # (In this example we assume to open all '.wav' files and their related '.TextGrid' files; # usually only '.TextGrid' files are there when a '.wav' file exists but sometimes # '.wav' files exist without a '.TextGrid', we list only the '.TextGrid' files and # check whether a '.wav' file can be found for it; only then both are opened. # Note that 'directory$' must be defined here (see above under 'A')! clearinfo directory$ = "" # First make a list of all file names ending in '.TextGrid' in 'grid_list' and set up a counter for # user entertainment. Create Strings as file list: "grid_list", "*.TextGrid" number_of_files = Get number of strings nr_completed = 0 # Go now one-by-one with a 'for' loop thru this list for i_file to number_of_files selectObject: "Strings grid_list" grid_name$ = Get string: i_file Read from file: "'directory$''grid_name$'" # Check whether the related '.wav' file is there and entertain user base_name$ = selected$ ("TextGrid") print Handling 'base_name$' sound_file_name$ = directory$+base_name$+".wav" # test whether this file is readable if fileReadable (sound_file_name$) # Success! Now we can read the wav-file (note again the quots, which must be here around the variable!). Read from file: "'sound_file_name$'" # --> Do whatever you want to do here <-- # end of handling one TextGrid- and wav-file printline finished. removeObject: "Sound 'base_name$'" removeObject: "TextGrid 'base_name$'" # count the successful processed files by incrementing the counter for this by one. nr_completed += 1 # No audio-file readable else printline failed! No sound file 'directory$''base_name$'.wav found. <*** Remove endif endfor # clean up and tel user we're done. removeObject: "Strings grid_list" printline Done. 'nr_completed' of 'number_of_files' TextGrid files processed. # B.5: Like Method B.4 above, but this handles all files in the present and all direct sub-directories # of the present directory (but not any sub-sub-directories down the line). Handling all # sub-sub-...-directories is a much more complex business and is treated in the skript # 'Handling_all_subdirectories.praat' separately. # In this example we open all '.wav' files and their related '.TextGrid' files. clearinfo directory$ = "" # create a list of all sub-directories in 'directory$' Create Strings as directory list: "directory_list", "'directory$'*" number_of_directories = Get number of strings nr_completed = 0 nr_textgrid = 0 # add the '0'.th directory (i.e. the starting 'directory$' itself) for i_dir from 0 to number_of_directories # treat the starting directory (i.e. 'directory$' itself) if (i_dir == 0) sub_directory$ = directory$ # treat all sub-directories else selectObject: "Strings directory_list" grid_directory_name$ = Get string: 'i_dir' sub_directory$ = directory$+grid_directory_name$+"/" endif # create a list of all files in this sub-directory that end in ".TextGrid" Create Strings as file list: "grid_list", "'sub_directory$'*.TextGrid" number_of_files = Get number of strings nr_textgrid += number_of_files for i_file to number_of_files selectObject: "Strings grid_list" grid_file_name$ = Get string: 'i_file' Read from file: "'sub_directory$''grid_file_name$'" base_name$ = selected$ ("TextGrid") print Handling 'base_name$' # try to open a .wav file for this TextGrid ext$ = ".wav" sound_file_name$ = sub_directory$+base_name$+ext$ if fileReadable (sound_file_name$) Read from file: "'sound_file_name$'" # --> Do whatever you want to do here <-- printline finished. removeObject: "Sound 'base_name$'" removeObject: "TextGrid 'base_name$'" nr_completed += 1 else printline failed! No sound file 'sub_directory$''base_name$''ext$' found. <*** Remove endif endfor removeObject: "Strings grid_list" endfor # clean up and go home removeObject: "Strings directory_list" printline Done. 'nr_completed' of 'nr_textgrid' TextGrid files processed. ## C) Different ways to put parts of a filename (directory path, basename, extension, other name parts) together. # C.1: Combine a path (stored in 'directory$', which must end with a slash ("/")), # 'base'file name (stored in 'base_name$') and extension together 'on the fly' to open a file Read from file: "'directory$''base_name$'.TextGrid" # C.2: Do the same, but store the new name in a new variable # This has the advantage that you can use this name easily several times, e.g. to inform the user later # build three filenames for different purposes wav_file$ = directory$+base_name$+".wav" textgrid_file$ = directory$+base_name$+".TextGrid" result_file$ = directory$+base_name$+"_results.txt" # read, e.g., '.wav' and '.TextGrid' files Read from file: "'wav_file$'" Read from file: "'textgrid_file$'" # write, e.g., into a result file the base file name with a header for some (fictive) data filedelete 'result_file$' fileappend 'result_file$' 'base_name$''tab$'label'tab$'duration (ms.)'newline$' # inform the user (also fictively) printline Files 'wav_file$' and 'textgrid_file$' are opened and one header line written to 'result_file$'. # clean up the mess we created by selecting everything in the Object list and removing it. select all Remove # script ends here printline Done. Go home.