another minor fix
[sgc.git] / singleword / signals.c
blobdda43facb48d7908d08d213d6cb0de8bbaff6e0d
1 #include "singleword.h"
2 #include <gdk/gdkkeysyms.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "praat.h"
6 #include "signals.h"
7 #include "cairo.h"
9 gchar *active = NULL;
11 void run_script(gchar *pinyin) {
12 if (strlen(pinyin) > 1) {
13 GString *cmd = g_string_sized_new (200);
14 g_chdir(SCRIPTPATH);
15 #ifdef PRAATEXTERNAL
16 g_string_printf(cmd, "%s DrawToneContour.praat %s %0.f", PRAATBIN, pinyin, upperRegister);
17 system(cmd->str);
18 #else
19 g_string_printf(cmd, "DrawToneContour.praat %s %0.f", pinyin, upperRegister);
20 praat_executeScriptFromFileNameWithArguments(cmd->str);
21 g_debug(cmd->str);
22 Melder_flushError(NULL);
23 // Melder_clearError();
25 #endif
26 g_chdir("..");
27 g_string_free(cmd, TRUE);
31 static void commit(GtkWidget *widget) {
32 if (active != NULL) g_free(active);
33 active = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)));
35 g_ascii_strdown(active, -1);
36 g_strcanon(active, "#012345abcdefghijklmnopqrstuvwxyz\n", '-');
38 gtk_entry_set_text(GTK_ENTRY(widget), active);
40 run_script(active);
42 gtk_widget_queue_draw(glade_xml_get_widget(xml, "drawingareaPitch"));
46 void entryPinyin_key_release_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer user_data) {
47 if (event != NULL && event->keyval == GDK_Return) {
48 commit(widget);
52 void buttonCommit_clicked_cb(GtkWidget *widget, gpointer user_data) {
53 commit(widget);
56 void buttonRecord_clicked_cb(GtkWidget *widget, gpointer user_data) {
57 g_thread_create((GThreadFunc)record, NULL, FALSE, NULL);
60 void buttonPlay_clicked_cb(GtkWidget *widget, gpointer user_data) {
61 g_thread_create((GThreadFunc)play, NULL, FALSE, NULL);
64 void buttonHum_clicked_cb(GtkWidget *widget, gpointer user_data) {
65 if (active != NULL) {
66 GString *filename = g_string_sized_new (200);
67 g_string_printf(filename, "%s%s%s-%0.f.PitchTier", PITCHPATH, G_DIR_SEPARATOR_S, active, upperRegister);
69 if (g_access(filename->str, F_OK) == -1) run_script(active);
71 g_thread_create((GThreadFunc)playHum, NULL, FALSE, NULL);
73 g_string_free(filename, TRUE);
77 void paint (GtkWidget *widget, GdkEventExpose *eev, gpointer data) {
78 if (active != NULL) {
79 cairo_t *cr;
80 GString *filename = g_string_sized_new (200);
81 GString *recordedPT = g_string_sized_new (200);
83 g_string_printf(filename, "%s%s%s-%0.f.PitchTier", PITCHPATH, G_DIR_SEPARATOR_S, active, upperRegister);
84 g_string_printf(recordedPT, "%s%s%s.PitchTier", RECORDPATH, G_DIR_SEPARATOR_S, active);
86 if (g_access(filename->str, F_OK) == -1) run_script(active);
88 cr = gdk_cairo_create (widget->window);
89 cairo_set_line_width (cr, 1);
90 cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
91 if (g_access(filename->str, F_OK) == 0) {
92 cairo_move_to (cr, 5, 15);
93 cairo_set_source_rgb (cr, 0,0,0);
94 cairo_show_text (cr, _("Reference Pitch"));
97 drawPitchTier(cr, filename->str,
98 widget->allocation.width,
99 widget->allocation.height, upperRegister + 100.0);
101 g_string_free(filename, TRUE);
103 if (g_access(recordedPT->str, F_OK) == 0) {
104 cairo_move_to (cr, 15, 30);
105 cairo_set_source_rgb (cr, 1,0,0);
106 cairo_show_text (cr, _("Your Pitch"));
108 drawPitchTier(cr, recordedPT->str,
109 widget->allocation.width,
110 widget->allocation.height, upperRegister + 100.0);
113 g_string_free(recordedPT, TRUE);
114 cairo_destroy (cr);
120 void removeRecordings() {
121 GDir *record;
122 gchar *file = g_build_filename(SCRIPTPATH, "lastExample.wav", NULL);
123 g_unlink(file);
124 g_free(file);
125 if ((record = g_dir_open(RECORDPATH, 0, NULL)) != NULL) {
126 const gchar *name;
127 while ((name = g_dir_read_name(record)) != NULL) {
128 if (g_str_has_suffix(name, ".wav") || g_str_has_suffix(name, ".Pitch") || g_str_has_suffix(name, ".PitchTier")) {
129 file = g_build_filename(RECORDPATH, name, NULL);
130 g_unlink(file);
131 g_free(file);
134 g_dir_close(record);