Wissen van *.wav *.pitch *.pitchTier
[sgc.git] / praat.c
blobd57308ae5519e85dbf25751f78579808a8ffe8d8
1 #include "sgc.h"
2 #include <unistd.h> /* F_OK ?? */
4 #include "melder.h"
5 #include "NUMmachar.h"
6 #include "gsl_errno.h"
8 #include "site.h"
9 #include "machine.h"
10 #include "Strings.h"
11 #include "Sound.h"
12 #include "Vector.h"
13 #include "Thing.h"
14 #include "Pitch_to_Sound.h"
15 #include "Data.h"
18 gpointer sound_init(void *args)
20 Sound melderSoundFromMic;
22 NUMmachar ();
23 gsl_set_error_handler_off ();
24 Melder_setMaximumAsynchronicity (Melder_SYNCHRONOUS);
26 melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, 1.0);
27 forget(melderSoundFromMic);
28 Melder_flushError (NULL);
29 g_thread_exit(NULL);
32 void playInPraat(gchar *path) {
33 if (g_access(path, F_OK) == 0) {
34 structMelderFile file;
35 Sound melderSoundFromFile;
36 Melder_pathToFile(path, & file);
37 melderSoundFromFile = Sound_readFromSoundFile (& file);
38 if (! melderSoundFromFile) {
39 g_warning("Praat cannot open the file!\n");
40 } else {
41 Sound_play(melderSoundFromFile, NULL, NULL);
42 forget(melderSoundFromFile);
44 } else {
45 g_warning("The file %s just don't exist!\n", path);
47 Melder_flushError (NULL);
51 void playVoice() {
52 if (treevalid) {
53 gchar *string;
54 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
55 gchar *path = g_strjoin(NULL, PINYINPATH, G_DIR_SEPARATOR_S, string, ".wav", NULL);
56 playInPraat(path);
57 g_free(path);
58 g_free(string);
62 void playHum() {
63 if (treevalid) {
64 gchar *string;
65 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
66 gchar *path;
67 gchar *upper = g_malloc(4);
68 g_ascii_dtostr(upper, 4, upperRegister);
69 #ifdef WINCOMPAT
70 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".wav", NULL);
71 #else
72 path = g_strjoin(NULL, PITCHPATH, G_DIR_SEPARATOR_S, string, "-", upper, ".Pitch", NULL);
73 #endif
74 g_free(upper);
76 #ifdef WINCOMPAT
77 playInPraat(path);
78 #else
79 if (g_access(path, F_OK) == 0) {
80 structMelderFile file;
81 Pitch melderPitchFromFile;
82 #ifdef MINGW
83 __declspec(dllimport) Pitch_Table classPitch;
84 #endif
85 Thing_recognizeClassesByName (classPitch, NULL);
86 Melder_pathToFile(path, & file);
87 melderPitchFromFile = Data_readFromTextFile (& file);
88 if (melderPitchFromFile != NULL) {
89 Pitch_hum(melderPitchFromFile, 0, 0);
90 forget(melderPitchFromFile);
91 } else {
92 g_debug("PITCH IS NULL!");
95 #endif
96 g_free(path);
97 g_free(string);
98 Melder_flushError (NULL);
103 void playBefore() {
104 GtkWidget *gimiChinese = glade_xml_get_widget(xml, "checkmenuitemBeforeVoice");
105 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemBeforeHum");
107 /* Check play Example */
108 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiChinese))) {
109 /* Play Voice */
110 playVoice();
113 /* Play Hum */
114 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
115 playHum();
119 gpointer example(void *args) {
120 GtkWidget *gimiChinese = glade_xml_get_widget(xml, "checkmenuitemExampleVoice");
121 GtkWidget *gimiHum = glade_xml_get_widget(xml, "checkmenuitemExampleHum");
123 g_idle_add(setButtonsFalse, NULL);
125 /* Check play Example */
126 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiChinese))) {
127 /* Play Voice */
128 playVoice();
131 /* Play Hum */
132 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gimiHum))) {
133 playHum();
136 g_idle_add(setButtonsTrue, NULL);
137 g_thread_exit(NULL);
140 gpointer play(void *args) {
141 if (treevalid) {
142 gchar *string;
143 gchar *path;
144 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
145 g_idle_add(setButtonsFalse, NULL);
146 path = g_strjoin(NULL, RECORDPATH, G_DIR_SEPARATOR_S, string, ".wav", NULL);
147 playInPraat(path);
148 g_free(path);
149 g_free(string);
150 g_idle_add(setButtonsTrue, NULL);
151 g_thread_exit(NULL);
156 gpointer record(void *args) {
157 if (treevalid) {
158 gchar *string;
159 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &mainIter, COL_TEXT, &string, -1);
160 g_debug("%s %d welcome!\n\n\n", __func__, rand());
161 g_idle_add(setButtonsFalse, NULL);
162 playBefore();
163 if (g_access(RECORDPATH, F_OK) == 0) {
164 Sound melderSoundFromMic = Sound_recordFixedTime (1, 1.0, 0.5, 44100, 5.0);
166 if (! melderSoundFromMic) {
167 g_warning("No sound from praat!\n");
168 } else {
169 GString *filename = g_string_sized_new(128);
170 GString *cmd = g_string_sized_new(128);
171 gchar *path;
172 structMelderFile file;
174 g_string_printf(filename, "%s.wav", string);
176 path = g_build_filename(RECORDPATH, filename->str, NULL);
177 g_string_free(filename, TRUE);
179 Vector_scale(melderSoundFromMic, 0.99);
180 Melder_pathToFile(path, &file);
181 Sound_writeToAudioFile16 (melderSoundFromMic, &file, Melder_WAV);
182 forget(melderSoundFromMic);
184 g_chdir(SCRIPTPATH);
185 g_string_printf(cmd, "%s SGC_ToneProt.praat ..%s%s %s %0.f 3 none 0", PRAATBIN, G_DIR_SEPARATOR_S, path, string, upperRegister);
186 g_free(path);
187 g_debug(cmd->str);
188 g_debug("EXECUTES! ");
189 system(cmd->str);
190 g_debug("DONE!\n");
191 g_chdir("..");
192 g_string_free(cmd, TRUE);
194 } else {
195 g_warning("Missing recording directory %s.", RECORDPATH);
197 g_free(string);
198 Melder_flushError (NULL);
199 g_idle_add(setButtonsTrue, NULL);
200 g_debug("%s bye!\n", __func__);
201 g_thread_exit(NULL);