Zip toegevoegd. Zelf testen!
[sgc.git] / praat.c
blobca044a7aeb79f9d4055227853a7e2fd9b0f612cf
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);
42 static void playInPraat(gchar *path) {
43 if (g_access(path, F_OK) == 0) {
44 structMelderFile file;
45 Sound melderSoundFromFile;
46 Melder_pathToFile(path, & file);
47 melderSoundFromFile = Sound_readFromSoundFile (& file);
48 if (! melderSoundFromFile) {
49 g_warning(_("Praat cannot open the file!"));
50 } else {
51 Sound_play(melderSoundFromFile, NULL, NULL);
52 forget(melderSoundFromFile);
54 } else {
55 g_warning(_("The file %s just doesn't exist!"), path);
57 Melder_clearError();
58 // Melder_flushError (NULL);
62 static void playVoice() {
63 if (treevalid) {
64 gchar *string;
65 gchar *path;
66 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
67 path = voicepath(string);
69 playInPraat(path);
70 g_free(path);
71 g_free(string);
75 static void playHum() {
76 if (treevalid) {
77 gchar *string;
78 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
79 gchar *path;
80 gchar *upper = g_malloc(4);
81 g_ascii_dtostr(upper, 4, upperRegister);
82 #ifdef WINCOMPAT
83 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".wav", NULL);
84 #else
85 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".Pitch", NULL);
86 #endif
87 g_free(upper);
89 #ifdef WINCOMPAT
90 playInPraat(path);
91 #else
92 if (g_access(path, F_OK) == 0) {
93 structMelderFile file;
94 Pitch melderPitchFromFile;
95 #ifdef MINGW
96 __declspec(dllimport) Pitch_Table classPitch;
97 #endif
98 Thing_recognizeClassesByName (classPitch, NULL);
99 Melder_pathToFile(path, & file);
100 melderPitchFromFile = Data_readFromTextFile (& file);
101 if (melderPitchFromFile != NULL) {
102 Pitch_hum(melderPitchFromFile, 0, 0);
103 forget(melderPitchFromFile);
104 } else {
105 // g_debug("PITCH IS NULL!");
108 #endif
109 g_free(path);
110 g_free(string);
111 Melder_clearError();
116 static void playBefore() {
117 GtkWidget *gimiVoice = glade_xml_get_widget(xml, "checkmenuitemBeforeVoice");
118 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemBeforeHum");
120 /* Check play Example */
121 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiVoice))) {
122 /* Play Voice */
123 playVoice();
126 /* Play Hum */
127 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
128 playHum();
132 gpointer example(void *args) {
133 GtkWidget *gimiVoice = glade_xml_get_widget(xml, "checkmenuitemExampleVoice");
134 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemExampleHum");
136 g_idle_add(setButtonsFalse, NULL);
138 /* Check play Example */
139 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiVoice))) {
140 /* Play Voice */
141 playVoice();
144 /* Play Hum */
145 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
146 playHum();
149 g_idle_add(setButtonsTrue, NULL);
150 g_thread_exit(NULL);
153 gpointer play(void *args) {
154 if (treevalid) {
155 gchar *path = pathToPlay();
156 g_idle_add(setButtonsFalse, NULL);
157 if (path != NULL) {
158 playInPraat(path);
159 g_free(path);
161 g_idle_add(setButtonsTrue, NULL);
162 g_thread_exit(NULL);
166 gpointer record(void *args) {
167 if (treevalid) {
168 gchar *string;
169 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
170 // g_debug("%s %d welcome!\n\n\n", __func__, rand());
171 g_idle_add(setButtonsFalse, NULL);
172 playBefore();
173 if (g_access(RECORDPATH, F_OK) == 0) {
174 Sound melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, RECORDTIME);
176 if (! melderSoundFromMic) {
177 g_warning(_("No sound from praat!"));
178 } else {
179 GString *filename = g_string_sized_new(128);
180 GString *cmd = g_string_sized_new(128);
181 gchar *path;
182 structMelderFile file;
184 g_string_printf(filename, "%s.wav", string);
186 path = g_build_filename(RECORDPATH, filename->str, NULL);
187 g_string_free(filename, TRUE);
189 Vector_scale(melderSoundFromMic, 0.99);
190 Melder_pathToFile(path, &file);
191 Sound_writeToAudioFile16 (melderSoundFromMic, &file, Melder_WAV);
192 forget(melderSoundFromMic);
194 g_chdir(SCRIPTPATH);
195 #ifdef PRAATEXTERNAL
197 g_string_printf(cmd, "%s SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", PRAATBIN, G_DIR_SEPARATOR_S, path, string, upperRegister);
198 // g_debug(cmd->str);
199 // g_debug("EXECUTES! ");
200 system(cmd->str);
201 // g_debug("DONE!\n");
203 #else
204 g_string_printf(cmd, "SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", G_DIR_SEPARATOR_S, path, string, upperRegister);
205 praat_executeScriptFromFileNameWithArguments(cmd->str);
206 Melder_clearError();
207 #endif
208 g_free(path);
210 g_chdir("..");
211 g_string_free(cmd, TRUE);
213 } else {
214 g_warning(_("Missing recording directory %s."), RECORDPATH);
216 g_free(string);
217 Melder_clearError();
218 // Melder_flushError (NULL);
219 g_idle_add(setButtonsTrue, NULL);
220 // g_debug("%s bye!\n", __func__);
221 g_thread_exit(NULL);