remove debug
[sgc.git] / singleword / praat.c
blobf6e68dc0ca9e67073b193e4a1f91039ed605d8bf
1 #include "singleword.h"
3 #include "melder.h"
4 #include "NUMmachar.h"
5 #include "gsl_errno.h"
7 #include "site.h"
8 #include "machine.h"
9 #include "Strings.h"
10 #include "Sound.h"
11 #include "Vector.h"
12 #include "Thing.h"
13 #include "Pitch_to_Sound.h"
14 #include "Data.h"
16 gboolean setButtonsTrue() {
18 gchar *feedbackTXT;
19 gchar *contents;
20 gboolean setButton = TRUE;
21 GtkWidget *buttonRecord = glade_xml_get_widget(xml, "buttonRecord");
22 GtkWidget *buttonPlay = glade_xml_get_widget(xml, "buttonPlay");
23 GtkWidget *buttonHum = glade_xml_get_widget(xml, "buttonHum");
24 GtkWidget *drawingareaPitch = glade_xml_get_widget(xml, "drawingareaPitch");
25 gchar *path = g_build_filename(RECORDPATH, "singleword.wav", NULL);
26 if (g_access(path, F_OK) == -1) setButton = FALSE;
27 g_free(path);
28 gtk_widget_set_sensitive(buttonPlay, setButton);
29 gtk_widget_set_sensitive(buttonRecord, TRUE);
30 gtk_widget_set_sensitive(buttonHum, TRUE);
33 gtk_widget_queue_draw(drawingareaPitch);
35 feedbackTXT = g_build_filename(SCRIPTPATH, G_DIR_SEPARATOR_S, "feedback.txt", NULL);
37 if (g_file_get_contents(feedbackTXT, &contents, NULL, NULL) == TRUE) {
38 GtkWidget *label = glade_xml_get_widget(xml, "labelFeedback");
39 gtk_label_set_text(GTK_LABEL(label), contents);
40 g_unlink(feedbackTXT);
42 g_free(feedbackTXT);
44 return FALSE;
47 gboolean setButtonsFalse() {
48 GtkWidget *buttonRecord = glade_xml_get_widget(xml, "buttonRecord");
49 GtkWidget *buttonPlay = glade_xml_get_widget(xml, "buttonPlay");
50 GtkWidget *buttonHum = glade_xml_get_widget(xml, "buttonHum");
52 gtk_widget_set_sensitive(buttonRecord, FALSE);
53 gtk_widget_set_sensitive(buttonPlay, FALSE);
54 gtk_widget_set_sensitive(buttonHum, FALSE);
55 return FALSE;
58 gpointer sound_init(void *args)
60 Sound melderSoundFromMic;
61 #ifndef PRAATEXTERNAL
62 gchar *argv[2] = { "Praat", "niets" };
63 praat_init("Praat", 2, argv);
64 praat_uvafon_init();
65 #endif
67 Melder_setMaximumAsynchronicity (Melder_SYNCHRONOUS);
69 melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, 1.0);
70 forget(melderSoundFromMic);
71 Melder_clearError();
72 g_idle_add(setButtonsTrue, NULL);
73 g_thread_exit(NULL);
76 static void playInPraat(gchar *path) {
77 if (g_access(path, F_OK) == 0) {
78 structMelderFile file;
79 Sound melderSoundFromFile;
80 Melder_pathToFile(path, & file);
81 melderSoundFromFile = Sound_readFromSoundFile (& file);
82 if (! melderSoundFromFile) {
83 g_warning(_("Praat cannot open the file!"));
84 } else {
85 Sound_play(melderSoundFromFile, NULL, NULL);
86 forget(melderSoundFromFile);
88 } else {
89 g_warning(_("The file %s just doesn't exist!"), path);
91 Melder_clearError();
94 gpointer playHum(void *args) {
95 if (active != NULL) {
96 GString *filename = g_string_sized_new (200);
97 g_string_printf(filename, "%s%s%s-%0.f.wav", PITCHPATH, G_DIR_SEPARATOR_S, active, upperRegister);
98 g_idle_add(setButtonsFalse, NULL);
99 playInPraat(filename->str);
100 g_string_free(filename, TRUE);
101 g_idle_add(setButtonsTrue, NULL);
103 g_thread_exit(NULL);
107 gpointer play(void *args) {
108 gchar *path = g_build_filename(RECORDPATH, "singleword.wav", NULL);
109 g_idle_add(setButtonsFalse, NULL);
110 playInPraat(path);
111 g_free(path);
112 g_idle_add(setButtonsTrue, NULL);
113 g_thread_exit(NULL);
116 gpointer record(void *args) {
117 if (active != NULL) {
118 g_idle_add(setButtonsFalse, NULL);
119 if (g_access(RECORDPATH, F_OK) == 0) {
120 Sound melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, RECORDTIME);
121 if (! melderSoundFromMic) {
122 g_warning(_("No sound from praat!"));
123 } else {
124 GString *cmd = g_string_sized_new(128);
125 gchar *path;
126 structMelderFile file;
128 path = g_build_filename(RECORDPATH, "singleword.wav", NULL);
131 Vector_scale(melderSoundFromMic, 0.99);
132 Melder_pathToFile(path, &file);
133 Sound_writeToAudioFile16 (melderSoundFromMic, &file, Melder_WAV);
134 forget(melderSoundFromMic);
136 g_chdir(SCRIPTPATH);
137 #ifdef PRAATEXTERNAL
138 g_string_printf(cmd, "%s SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", PRAATBIN, G_DIR_SEPARATOR_S, path, active, upperRegister);
139 system(cmd->str);
140 #else
141 g_string_printf(cmd, "SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", G_DIR_SEPARATOR_S, path, active, upperRegister);
142 praat_executeScriptFromFileNameWithArguments(cmd->str);
143 Melder_clearError();
144 #endif
146 g_free(path);
147 g_chdir("..");
148 g_string_free(cmd, TRUE);
150 } else {
151 g_warning(_("Missing recording directory %s."), RECORDPATH);
153 Melder_clearError();
154 g_idle_add(setButtonsTrue, NULL);
156 g_thread_exit(NULL);