libpraat.dll is not required anymore
[sgc.git] / praat.c
blob46d40ba68c9159262c74775ea3d6cc6f70546266
1 #include "sgc.h"
2 #include "praat.h"
3 #include "signals.h"
5 #include <unistd.h> /* F_OK ?? */
7 #include "melder.h"
8 #include "NUMmachar.h"
9 #include "gsl_errno.h"
11 #include "site.h"
12 #include "machine.h"
13 #include "Strings.h"
14 #include "Sound.h"
15 #include "Vector.h"
16 #include "Thing.h"
17 #include "Pitch_to_Sound.h"
18 #include "Data.h"
21 gpointer sound_init(void *args)
23 Sound melderSoundFromMic;
25 #ifndef PRAATEXTERNAL
26 gchar *argv[2] = { "Praat", "niets" };
27 praat_init("Praat", 2, argv);
28 praat_uvafon_init();
29 #endif
31 NUMmachar ();
32 gsl_set_error_handler_off ();
33 // Melder_setMaximumAsynchronicity (Melder_SYNCHRONOUS);
35 melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, 1.0);
36 forget(melderSoundFromMic);
37 // Melder_clearError();
38 Melder_flushError (NULL);
39 g_thread_exit(NULL);
40 return NULL;
43 static void playInPraat(gchar *path) {
44 if (g_access(path, F_OK) == 0) {
45 structMelderFile file;
46 Sound melderSoundFromFile;
47 Melder_pathToFile(Melder_peekUtf8ToWcs(path), & file);
48 melderSoundFromFile = Sound_readFromSoundFile (& file);
49 if (! melderSoundFromFile) {
50 g_warning(_("Praat cannot open the file!"));
51 } else {
52 Sound_play(melderSoundFromFile, NULL, NULL);
53 forget(melderSoundFromFile);
55 } else {
56 g_warning(_("The file %s just doesn't exist!"), path);
58 Melder_clearError();
59 // Melder_flushError (NULL);
63 static void playVoice() {
64 if (treevalid) {
65 gchar *string;
66 gchar *path;
67 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
68 path = voicepath(string);
70 playInPraat(path);
71 g_free(path);
72 g_free(string);
76 static void playHum() {
77 if (treevalid) {
78 gchar *string;
79 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
80 gchar *path;
81 gchar *upper = g_malloc(4);
82 g_ascii_dtostr(upper, 4, upperRegister);
83 #ifdef WINCOMPAT
84 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".wav", NULL);
85 #else
86 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".Pitch", NULL);
87 #endif
88 g_free(upper);
90 #ifdef WINCOMPAT
91 playInPraat(path);
92 #else
93 if (g_access(path, F_OK) == 0) {
94 structMelderFile file;
95 Pitch melderPitchFromFile;
96 #ifdef MINGW
97 __declspec(dllimport) Pitch_Table classPitch;
98 #endif
99 Thing_recognizeClassesByName (classPitch, NULL);
100 Melder_pathToFile(Melder_peekUtf8ToWcs(path), & file);
101 melderPitchFromFile = Data_readFromTextFile (& file);
102 if (melderPitchFromFile != NULL) {
103 Pitch_hum(melderPitchFromFile, 0, 0);
104 forget(melderPitchFromFile);
105 } else {
106 // g_debug("PITCH IS NULL!");
109 #endif
110 g_free(path);
111 g_free(string);
112 Melder_clearError();
117 static void playBefore() {
118 GtkWidget *gimiVoice = glade_xml_get_widget(xml, "checkmenuitemBeforeVoice");
119 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemBeforeHum");
121 /* Check play Example */
122 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiVoice))) {
123 /* Play Voice */
124 playVoice();
127 /* Play Hum */
128 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
129 playHum();
133 gpointer example(void *args) {
134 GtkWidget *gimiVoice = glade_xml_get_widget(xml, "checkmenuitemExampleVoice");
135 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemExampleHum");
137 g_idle_add(setButtonsFalse, NULL);
139 /* Check play Example */
140 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiVoice))) {
141 /* Play Voice */
142 playVoice();
145 /* Play Hum */
146 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
147 playHum();
150 g_idle_add(setButtonsTrue, NULL);
151 g_thread_exit(NULL);
152 return NULL;
155 gpointer play(void *args) {
156 if (treevalid) {
157 gchar *path = pathToPlay();
158 g_idle_add(setButtonsFalse, NULL);
159 if (path != NULL) {
160 playInPraat(path);
161 g_free(path);
163 g_idle_add(setButtonsTrue, NULL);
165 g_thread_exit(NULL);
166 return NULL;
169 gpointer record(void *args) {
170 if (treevalid) {
171 gchar *string;
172 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
173 // g_debug("%s %d welcome!\n\n\n", __func__, rand());
174 g_idle_add(setButtonsFalse, NULL);
175 playBefore();
176 if (g_access(RECORDPATH, F_OK) == 0) {
177 Sound melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, RECORDTIME);
179 if (! melderSoundFromMic) {
180 g_warning(_("No sound from praat!"));
181 } else {
182 GString *filename = g_string_sized_new(128);
183 GString *cmd = g_string_sized_new(128);
184 gchar *path;
185 structMelderFile file;
187 g_string_printf(filename, "%s.wav", string);
189 path = g_build_filename(RECORDPATH, filename->str, NULL);
190 g_string_free(filename, TRUE);
192 Vector_scale(melderSoundFromMic, 0.99);
193 Melder_pathToFile(Melder_peekUtf8ToWcs(path), &file);
194 Sound_writeToAudioFile16 (melderSoundFromMic, &file, Melder_WAV);
195 forget(melderSoundFromMic);
197 g_chdir(SCRIPTPATH);
198 #ifdef PRAATEXTERNAL
200 g_string_printf(cmd, "%s SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", PRAATBIN, G_DIR_SEPARATOR_S, path, string, upperRegister);
201 // g_debug(cmd->str);
202 // g_debug("EXECUTES! ");
203 system(cmd->str);
204 // g_debug("DONE!\n");
206 #else
207 g_string_printf(cmd, "SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", G_DIR_SEPARATOR_S, path, string, upperRegister);
208 praat_executeScriptFromFileNameWithArguments(cmd->str);
209 Melder_clearError();
210 #endif
211 g_free(path);
213 g_chdir("..");
214 g_string_free(cmd, TRUE);
216 } else {
217 g_warning(_("Missing recording directory %s."), RECORDPATH);
219 g_free(string);
220 Melder_clearError();
221 // Melder_flushError (NULL);
222 g_idle_add(setButtonsTrue, NULL);
223 // g_debug("%s bye!\n", __func__);
225 g_thread_exit(NULL);
226 return NULL;