Lets try it...
[sgc.git] / praat.c
blob857a0689e41d56b5f593129bc33dc667e70acbab
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 NUMmachar ();
26 gsl_set_error_handler_off ();
27 Melder_setMaximumAsynchronicity (Melder_SYNCHRONOUS);
29 melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, 1.0);
30 forget(melderSoundFromMic);
31 Melder_clearError();
32 // Melder_flushError (NULL);
33 g_thread_exit(NULL);
36 static void playInPraat(gchar *path) {
37 if (g_access(path, F_OK) == 0) {
38 structMelderFile file;
39 Sound melderSoundFromFile;
40 Melder_pathToFile(path, & file);
41 melderSoundFromFile = Sound_readFromSoundFile (& file);
42 if (! melderSoundFromFile) {
43 g_warning(_("Praat cannot open the file!"));
44 } else {
45 Sound_play(melderSoundFromFile, NULL, NULL);
46 forget(melderSoundFromFile);
48 } else {
49 g_warning(_("The file %s just doesn't exist!"), path);
51 Melder_clearError();
52 // Melder_flushError (NULL);
56 static void playVoice() {
57 if (treevalid) {
58 gchar *string;
59 gchar *path;
60 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
61 path = voicepath(string);
63 playInPraat(path);
64 g_free(path);
65 g_free(string);
69 static void playHum() {
70 if (treevalid) {
71 gchar *string;
72 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
73 gchar *path;
74 gchar *upper = g_malloc(4);
75 g_ascii_dtostr(upper, 4, upperRegister);
76 #ifdef WINCOMPAT
77 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".wav", NULL);
78 #else
79 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".Pitch", NULL);
80 #endif
81 g_free(upper);
83 #ifdef WINCOMPAT
84 playInPraat(path);
85 #else
86 if (g_access(path, F_OK) == 0) {
87 structMelderFile file;
88 Pitch melderPitchFromFile;
89 #ifdef MINGW
90 __declspec(dllimport) Pitch_Table classPitch;
91 #endif
92 Thing_recognizeClassesByName (classPitch, NULL);
93 Melder_pathToFile(path, & file);
94 melderPitchFromFile = Data_readFromTextFile (& file);
95 if (melderPitchFromFile != NULL) {
96 Pitch_hum(melderPitchFromFile, 0, 0);
97 forget(melderPitchFromFile);
98 } else {
99 g_debug("PITCH IS NULL!");
102 #endif
103 g_free(path);
104 g_free(string);
105 Melder_clearError();
110 static void playBefore() {
111 GtkWidget *gimiVoice = glade_xml_get_widget(xml, "checkmenuitemBeforeVoice");
112 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemBeforeHum");
114 /* Check play Example */
115 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiVoice))) {
116 /* Play Voice */
117 playVoice();
120 /* Play Hum */
121 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
122 playHum();
126 gpointer example(void *args) {
127 GtkWidget *gimiVoice = glade_xml_get_widget(xml, "checkmenuitemExampleVoice");
128 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemExampleHum");
130 g_idle_add(setButtonsFalse, NULL);
132 /* Check play Example */
133 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiVoice))) {
134 /* Play Voice */
135 playVoice();
138 /* Play Hum */
139 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
140 playHum();
143 g_idle_add(setButtonsTrue, NULL);
144 g_thread_exit(NULL);
147 gpointer play(void *args) {
148 if (treevalid) {
149 gchar *path = pathToPlay();
150 if (path != NULL) {
151 playInPraat(path);
152 g_free(path);
154 g_idle_add(setButtonsTrue, NULL);
155 g_thread_exit(NULL);
159 gpointer record(void *args) {
160 if (treevalid) {
161 gchar *string;
162 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
163 // g_debug("%s %d welcome!\n\n\n", __func__, rand());
164 g_idle_add(setButtonsFalse, NULL);
165 playBefore();
166 if (g_access(RECORDPATH, F_OK) == 0) {
167 Sound melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, RECORDTIME);
169 if (! melderSoundFromMic) {
170 g_warning(_("No sound from praat!"));
171 } else {
172 GString *filename = g_string_sized_new(128);
173 GString *cmd = g_string_sized_new(128);
174 gchar *path;
175 structMelderFile file;
177 g_string_printf(filename, "%s.wav", string);
179 path = g_build_filename(RECORDPATH, filename->str, NULL);
180 g_string_free(filename, TRUE);
182 Vector_scale(melderSoundFromMic, 0.99);
183 Melder_pathToFile(path, &file);
184 Sound_writeToAudioFile16 (melderSoundFromMic, &file, Melder_WAV);
185 forget(melderSoundFromMic);
187 g_chdir(SCRIPTPATH);
188 g_string_printf(cmd, "%s SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", PRAATBIN, G_DIR_SEPARATOR_S, path, string, upperRegister);
189 g_free(path);
190 // g_debug(cmd->str);
191 // g_debug("EXECUTES! ");
192 system(cmd->str);
193 // g_debug("DONE!\n");
194 g_chdir("..");
195 g_string_free(cmd, TRUE);
197 } else {
198 g_warning(_("Missing recording directory %s."), RECORDPATH);
200 g_free(string);
201 Melder_clearError();
202 // Melder_flushError (NULL);
203 g_idle_add(setButtonsTrue, NULL);
204 // g_debug("%s bye!\n", __func__);
205 g_thread_exit(NULL);