another minor fix
[sgc.git] / signals.c
blob4b6224dc9f474b4d544ff8b0647d98db1114e983
1 #include "sgc.h"
2 #include "signals.h"
4 gdouble upperRegister = 200.0;
6 gchar * voicepath(gchar *string) {
7 gchar *path = g_strjoin(NULL, base, G_DIR_SEPARATOR_S, string, ".wav", NULL);
8 if (g_access(path, F_OK) != 0) {
9 g_free(path);
10 path = g_strjoin(NULL, g_getenv(STORAGE), G_DIR_SEPARATOR_S, STOREAS, G_DIR_SEPARATOR_S, PINYINPATH, G_DIR_SEPARATOR_S, string, ".wav", NULL);
12 return path;
15 static void setButtonByFileExists(GtkWidget *button, gchar *path) {
16 if (g_access(path, F_OK) == 0) {
17 gtk_widget_set_sensitive(button, TRUE);
18 } else {
19 gtk_widget_set_sensitive(button, FALSE);
24 static void setButtonExample() {
25 GtkWidget *buttonExample = glade_xml_get_widget(xml, "buttonExample");
26 if (treevalid) {
27 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemExampleHum");
28 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
29 gchar *string;
30 gchar *path;
32 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
33 path = voicepath(string);
35 setButtonByFileExists(buttonExample, path);
37 g_free(path);
38 g_free(string);
39 } else {
40 gtk_widget_set_sensitive(buttonExample, TRUE);
42 } else {
43 gtk_widget_set_sensitive(buttonExample, FALSE);
47 gchar * pathToPlay() {
48 gchar *path = NULL;
49 #ifdef LASTEXAMPLE
50 path = g_build_filename(SCRIPTPATH, "lastExample.wav", NULL);
51 #else
52 if (treevalid) {
53 gchar *string;
54 GString *filename = g_string_sized_new(128);
55 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
56 g_string_printf(filename, "%s.wav", string);
57 path = g_build_filename(RECORDPATH, filename->str, NULL);
58 g_string_free(filename, TRUE);
60 #endif
61 return path;
64 static void setButtonPlay() {
65 GtkWidget *buttonPlay = glade_xml_get_widget(xml, "buttonPlay");
66 gchar *path = pathToPlay();
67 if (path != NULL) {
68 setButtonByFileExists(buttonPlay, path);
69 g_free(path);
73 gboolean setButtonsTrue() {
74 GtkWidget *buttonRecord = glade_xml_get_widget(xml, "buttonRecord");
76 GtkWidget *drawingareaPitch = glade_xml_get_widget(xml, "drawingareaPitch");
77 gchar *feedbackTXT;
78 gchar *contents;
81 gtk_widget_set_sensitive(buttonRecord, TRUE);
82 setButtonPlay();
83 setButtonExample();
85 gtk_widget_queue_draw(drawingareaPitch);
87 feedbackTXT = g_build_filename(SCRIPTPATH, G_DIR_SEPARATOR_S, "feedback.txt", NULL);
89 if (g_file_get_contents(feedbackTXT, &contents, NULL, NULL) == TRUE) {
90 // g_debug("Sets label!\n");
91 GtkWidget *label = glade_xml_get_widget(xml, "labelFeedback");
92 gtk_label_set_text(GTK_LABEL(label), contents);
93 // g_debug("Label set!\n");
94 g_unlink(feedbackTXT);
96 g_free(feedbackTXT);
98 return FALSE;
101 gboolean setButtonsFalse() {
102 GtkWidget *buttonRecord = glade_xml_get_widget(xml, "buttonRecord");
103 GtkWidget *buttonPlay = glade_xml_get_widget(xml, "buttonPlay");
104 GtkWidget *buttonExample = glade_xml_get_widget(xml, "buttonExample");
105 // g_debug("%s welcome!\n", __func__);
107 gtk_widget_set_sensitive(buttonRecord, FALSE);
108 gtk_widget_set_sensitive(buttonPlay, FALSE);
109 gtk_widget_set_sensitive(buttonExample, FALSE);
111 // g_debug("%s bye!\n", __func__);
113 return FALSE;
116 void on_buttonRecord_clicked(GtkWidget *widget, gpointer user_data) {
117 if (treevalid) {
118 gchar *string;
119 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
122 if (string == NULL) {
123 GtkWidget *dialog = gtk_message_dialog_new(NULL,
124 GTK_DIALOG_MODAL,
125 GTK_MESSAGE_WARNING,
126 GTK_BUTTONS_OK,
127 _("Don't have any (more) tests"));
128 gtk_dialog_run (GTK_DIALOG (dialog));
129 gtk_widget_destroy (dialog);
130 /* Nothing to do? */
131 } else {
132 /* Start record Thread */
134 // g_debug("%s creates record", __func__);
136 g_thread_create((GThreadFunc)record, NULL, FALSE, NULL);
138 // g_debug("%s is done", __func__);
139 g_free(string);
144 void on_buttonExample_clicked(GtkWidget *widget, gpointer user_data) {
145 /* Play Thread */
146 /* Start example Thread */
147 g_thread_create((GThreadFunc)example, widget, FALSE, NULL);
150 void on_buttonPlay_clicked(GtkWidget *widget, gpointer user_data) {
151 /* Play Thread */
152 /* Start play Thread */
153 g_thread_create((GThreadFunc)play, widget, FALSE, NULL);
156 gboolean prev(GtkWidget *previous) {
157 gboolean ret = FALSE;
158 if (treevalid) {
159 /* Code From anchient Sylpheed */
160 GtkTreePath *path;
161 GtkTreeIter prev;
163 path = gtk_tree_model_get_path(GTK_TREE_MODEL(liststore), &mainIter);
165 if ((ret = gtk_tree_path_prev(path)) == TRUE) {
166 gtk_tree_model_get_iter(GTK_TREE_MODEL(liststore), &prev, path);
167 if (previous != NULL) {
168 mainIter = prev;
173 gtk_tree_path_free(path);
177 return ret;
180 gboolean next(GtkWidget *next) {
181 if (treevalid) {
182 if (next != NULL) {
183 return gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore), &mainIter);
184 } else {
185 GtkTreeIter next = mainIter;
186 return gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore), &next);
188 } else {
189 return FALSE;
193 void updateWidgets() {
194 GtkWidget *labelFeedback = glade_xml_get_widget(xml, "labelFeedback");
196 if (treevalid) {
197 gchar *string;
198 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
199 GString *label = g_string_new(string);
201 g_string_prepend(label, "<span size=\"xx-large\">");
202 g_string_append(label, "</span>");
203 gtk_label_set_markup(GTK_LABEL(labelFeedback), label->str);
204 g_string_free(label, TRUE);
206 g_free(string);
207 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(glade_xml_get_widget(xml, "treeviewWords"))), &mainIter);
208 } else {
209 gtk_label_set_markup(GTK_LABEL(labelFeedback), "");
212 gtk_widget_set_sensitive(glade_xml_get_widget(xml, "buttonRecord"), treevalid);
213 gtk_widget_set_sensitive(glade_xml_get_widget(xml, "imagemenuitemSave"), treevalid);
214 gtk_widget_set_sensitive(glade_xml_get_widget(xml, "imagemenuitemSaveAs"), treevalid);
216 gtk_widget_set_sensitive(glade_xml_get_widget(xml, "buttonPrevious"), prev(NULL));
217 gtk_widget_set_sensitive(glade_xml_get_widget(xml, "buttonNext"), next(NULL));
219 setButtonPlay();
220 setButtonExample();
222 gtk_widget_queue_draw(glade_xml_get_widget(xml, "drawingareaPitch"));
225 void on_Save(GtkWidget *chooser, gpointer user_data) {
226 fileSave(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser)));
229 void on_dialogAdd_close(GtkWidget *button, gpointer user_data) {
230 GtkWidget *buttonAdd = glade_xml_get_widget(xml, "buttonAdd");
231 gtk_widget_hide(gtk_widget_get_toplevel(button));
232 gtk_widget_grab_focus(buttonAdd);
235 void on_menuitemVoice_activate(GtkWidget *widget, gpointer user_data) {
236 const gchar *name = gtk_widget_get_name(widget);
237 GtkWidget *drawingareaPitch = glade_xml_get_widget(xml, "drawingareaPitch");
238 if (g_str_equal(name, "menuitemVoice_Male_Low") == TRUE) upperRegister = 150.0;
239 if (g_str_equal(name, "menuitemVoice_Male") == TRUE) upperRegister = 200.0;
240 if (g_str_equal(name, "menuitemVoice_Female") == TRUE) upperRegister = 300.0;
241 if (g_str_equal(name, "menuitemVoice_Child") == TRUE) upperRegister = 450.0;
242 gtk_widget_queue_draw(drawingareaPitch);
245 void paint (GtkWidget *widget, GdkEventExpose *eev, gpointer data) {
246 if (treevalid) {
247 gchar *string;
248 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
250 if (string != NULL) {
251 cairo_t *cr;
252 GString *filename = g_string_sized_new (200);
253 GString *cmd = g_string_sized_new (200);
254 GString *recordedPT = g_string_sized_new(100);
256 #ifdef PITCHTIER
257 g_string_printf(filename, "%s%s%s-%0.f.PitchTier", PITCHPATH, G_DIR_SEPARATOR_S, string, upperRegister);
258 g_string_printf(recordedPT, "%s%s%s.PitchTier", RECORDPATH, G_DIR_SEPARATOR_S, string);
259 #else
260 g_string_printf(filename, "%s%s%s-%0.f.Pitch", PITCHPATH, G_DIR_SEPARATOR_S, string, upperRegister);
261 g_string_printf(recordedPT, "%s%s%s.Pitch", RECORDPATH, G_DIR_SEPARATOR_S, string);
262 #endif
264 if (g_access(filename->str, F_OK) == -1) {
265 g_chdir(SCRIPTPATH);
266 #ifdef PRAATEXTERNAL
267 g_string_printf(cmd, "%s DrawToneContour.praat %s %0.f", PRAATBIN, string, upperRegister);
268 // g_debug("%s", cmd->str);
269 system(cmd->str);
270 #else
271 g_string_printf(cmd, "DrawToneContour.praat %s %0.f", string, upperRegister);
272 praat_executeScriptFromFileNameWithArguments(cmd->str);
273 Melder_clearError();
274 #endif
276 g_chdir("..");
279 cr = gdk_cairo_create (widget->window);
280 cairo_set_line_width (cr, 1);
281 cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
282 CAIRO_FONT_WEIGHT_BOLD);
285 if (g_access(filename->str, F_OK) == 0) {
286 cairo_move_to (cr, 5, 15);
287 cairo_set_source_rgb (cr, 0,0,0);
288 cairo_show_text (cr, _("Reference Pitch"));
291 drawPitchTier(cr, filename->str,
292 widget->allocation.width,
293 widget->allocation.height, upperRegister + 100.0);
295 g_string_free(filename, TRUE);
296 // g_debug("%s", recordedPT->str);
298 if (g_access(recordedPT->str, F_OK) == 0) {
299 cairo_move_to (cr, 15, 30);
300 cairo_set_source_rgb (cr, 1,0,0);
301 cairo_show_text (cr, _("Your Pitch"));
303 drawPitchTier(cr, recordedPT->str,
304 widget->allocation.width,
305 widget->allocation.height, upperRegister + 100.0);
308 g_string_free(recordedPT, TRUE);
309 cairo_destroy (cr);
311 g_free(string);
315 void on_menuitemShuffle_activate(GtkWidget *menuitem, gpointer data) {
316 int i;
317 GtkTreeIter a, b;
318 gint amount = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(liststore), NULL) - 1;
319 if (amount > 0) {
320 for (i = 0; i <= amount; i++) {
321 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(liststore),
322 &a, NULL,
323 g_random_int_range(0, amount));
324 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(liststore),
325 &b, NULL,
326 g_random_int_range(0, amount));
327 gtk_list_store_swap(GTK_LIST_STORE(liststore), &a, &b);
329 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
330 updateWidgets();