Added framework for creaky voice pitch change
[sgc2.git] / tables2scripts.praat
blobd281a97aae7d59dee0c5ecb3444b05f3be795d6f
2 # tables2scripts
3
4 #     Praat script converting .Table files into praat scripts
5 #     
6 #     Copyright (C) 2010  R.J.J.H. van Son and the Netherlands Cancer Institute
7
8 #     This program is free software; you can redistribute it and/or modify
9 #     it under the terms of the GNU General Public License as published by
10 #     the Free Software Foundation; either version 2 of the License, or
11 #     (at your option) any later version.
12
13 #     This program is distributed in the hope that it will be useful,
14 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #     GNU General Public License for more details.
17
18 #     You should have received a copy of the GNU General Public License
19 #     along with this program; if not, write to the Free Software
20 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
21
23 # form Source directory and target file
24 #       sentence Source_directory       Data
25 #       sentence Target_file    CreateTables.praat
26 # endform
28 Text writing preferences... UTF-8
30 source_directory$ = "Data"
31 target_file$ = "CreateTables.praat"
32 call tables2scripts "'source_directory$'" 'target_file$'
34 source_directory$ = "wordlists"
35 target_file$ = "CreateWordlists.praat"
36 call tables2scripts "'source_directory$'" 'target_file$'
38 # Definitions
39 procedure tables2scripts .sourceDir$ .targetFile$
40         # Iterate over all tables in the directory
41         if fileReadable(.sourceDir$)
42                 deleteFile(.targetFile$)
43                 # Start new file
44                 fileappend '.targetFile$' # 'target_file$''newline$'
45                 fileappend '.targetFile$' # 'newline$'
46                 fileappend '.targetFile$' # Automatically created code!'newline$'
47                 fileappend '.targetFile$' # Any changes will be lost when this file is regenerated.'newline$'
48                 fileappend '.targetFile$' # Adapt the original Tables instead of this code.'newline$'
49                 fileappend '.targetFile$' # To regenerate the original Tables, run a Praat script'newline$'
50                 fileappend '.targetFile$' # with the following code:'newline$'
51                 fileappend '.targetFile$' # 'newline$'
52                 fileappend '.targetFile$' #: include tables2scripts.praat'newline$'
53                 fileappend '.targetFile$' #: call Create<TableName>'newline$'
54                 fileappend '.targetFile$' #: Write to table file... <TableName>.Table'newline$'
55                 fileappend '.targetFile$' # 'newline$'
56                 fileappend '.targetFile$' # Where <TableName> is the name of the table.'newline$'
57                 fileappend '.targetFile$' # Move the Table file to the desired location (eg, Data/)'newline$'
58                 fileappend '.targetFile$' # 'newline$'
59                 fileappend '.targetFile$' # Tables and this code are licensed under the GNU GPL version 2'newline$'
60                 fileappend '.targetFile$' # or later.'newline$'
61                 fileappend '.targetFile$' # 'newline$'
62                 
63                 # 
64                 .nameStart = rindex(.targetFile$, "/")+1
65                 .nameEnd = rindex(.targetFile$, ".") -.nameStart
66                 .newTableNameList$ = mid$(.targetFile$, .nameStart, .nameEnd)
67                 Create Table with column names... '.newTableNameList$' 0 Name
69                 # Create a list of Tables with Paths
70                 Create Table with column names... ListOfTables 0 Name Directory
71                 recursion = 0
72                 call createListOfTables ListOfTables '.sourceDir$'
73         
74                 select Table ListOfTables
75            .numOfTables = Get number of rows
76                 for .i to .numOfTables
77                          select Table ListOfTables
78                         .table$ = Get value... '.i' Name
79                         .tableDir$ = Get value... '.i' Directory
80                         # Get Table
81                         Read from file... '.tableDir$'/'.table$'
82                         .tableName$ = selected$("Table")
83                         if .tableName$ = "wordlist" or .tableName$ = "table"
84                                 .nameStart = rindex(.tableDir$, "/")+1
85                                 .nameEnd = length(.tableDir$)+1 -.nameStart
86                                 .newTableName$ = mid$(.tableDir$, .nameStart, .nameEnd)
87                                 select Table '.tableName$'
88                                 Rename... '.newTableName$'
89                                 .tableName$ = selected$("Table")
90                         endif
92                         select Table '.newTableNameList$'
93                         Append row
94                         .currentTableNum = Get number of rows
95                         Set string value... '.currentTableNum' Name '.tableName$'
97                         # Convert table
98                         call table2procedure '.tableName$' '.targetFile$'
99                         select Table '.tableName$'
100                         Remove
101                 endfor
103                 # Convert table
104                 call table2procedure '.newTableNameList$' '.targetFile$'
106                 select Table '.newTableNameList$'
107                 plus Table ListOfTables
108                 Remove
109         else
110                 exit Directory not found: '.sourceDir$'
111         endif
112 endproc
114 # Convert a single table to a Praat script
115 procedure table2procedure .tableName$ .targetFile$
116         select Table '.tableName$'
117         
118         .space$ = " "
119         # Collect information
120         .numberOfColumns = Get number of columns
121         .numberOfRows = Get number of rows
122         
123         # Set name of procedure as variable
124         .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
125         fileappend '.targetFile$' 'newline$'procCreate'.tableVariableName$'$ = "'.tableName$'"'newline$'
126         
127         # Start output
128         fileappend '.targetFile$' procedure Create'.tableVariableName$''newline$'
129         # Create table with columns
130         fileappend '.targetFile$' 'tab$'Create Table with column names... '.tableName$' '.numberOfRows''newline$'
131         fileappend '.targetFile$' 'tab$'...
132         for .col to .numberOfColumns
133                 .colName$ = Get column label... '.col'
134                 fileappend '.targetFile$' '.space$''.colName$'
135         endfor
136         fileappend '.targetFile$' 'newline$'
137         fileappend '.targetFile$' 'tab$'# Fill table values'newline$'
139         # Fill the table
140         for .row to .numberOfRows
141                 fileappend '.targetFile$' 'tab$'# Row '.row''newline$'
142                 for .col to .numberOfColumns
143                         .label$ = Get column label... '.col'
144                         .value$ = Get value... '.row' '.label$'
145                         fileappend '.targetFile$' 'tab$'Set string value... '.row' '.label$' '.value$''newline$'
146                 endfor
147         endfor
149         fileappend '.targetFile$' endproc'newline$'
150 endproc
152 # .listName$ is name of table to recieve all file names
153 # Labels are Name and Directory
154 # Who says you cannot do recursion in Praat?
155 # This is eerily fragile code.
156 recursion = 0
157 procedure createListOfTables .listName$ .topDirectory$
158         recursion += 1
159         .listName'recursion'$ = .listName$
160         .topDirectory'recursion'$ = .topDirectory$
161         # Files
162         .currentTopDirectory$ = .topDirectory'recursion'$
163     Create Strings as file list... Files '.currentTopDirectory$'/*.Table
164         .numOfFiles'recursion' = Get number of strings
165         for .i to .numOfFiles'recursion'
166                 select Strings Files
167                 .table'recursion'$ = Get string... '.i'
168                 
169                 .currentListName$ = .listName'recursion'$
170                 select Table '.currentListName$'
171                 Append row
172                 .numRows = Get number of rows
173                 .currentTable$ = .table'recursion'$
174                 .currentTopDirectory$ = .topDirectory'recursion'$
175                 Set string value... '.numRows' Name '.currentTable$'
176                 Set string value... '.numRows' Directory '.currentTopDirectory$'
177         endfor
178         select Strings Files
179         Remove
180         # Recurse into directories
181         .currentTopDirectory$ = .topDirectory'recursion'$
182     Create Strings as directory list... Directories '.currentTopDirectory$'
183         .numOfDirectories'recursion' = Get number of strings
184         for .i'recursion' to .numOfDirectories'recursion'
185                 select Strings Directories
186                 .currentI = .i'recursion'
187                 .directory'recursion'$ = Get string... '.currentI'
188                 .currentTopDirectory$ = .topDirectory'recursion'$
189                 .currentDirectory$ = .directory'recursion'$
190                 call createListOfTables '.listName$' '.currentTopDirectory$'/'.currentDirectory$'
191         endfor
192         select Strings Directories
193         Remove
194         recursion -= 1
195 endproc