-
Notifications
You must be signed in to change notification settings - Fork 0
/
tts-FS-silent.c
95 lines (77 loc) · 2.95 KB
/
tts-FS-silent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
// Define a callback function to write the response data to a file
size_t write_data_callback(void *ptr, size_t size, size_t nmemb, FILE *stream) {
return fwrite(ptr, size, nmemb, stream);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <URL>\n", argv[0]);
return 1;
}
CURL *curl;
CURLcode res;
// Initialize libcurl
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Failed to initialize libcurl\n");
return 1;
}
// Set the URL for the HTTP request
const char *url = argv[1];
while (1) {
// Read the text data from stdin
// system("amixer set Capture cap"); //enable mic
char input_buffer[2048];
// printf("Enter the text to send (or 'exit' to quit): ");
if (!fgets(input_buffer, sizeof(input_buffer), stdin)) {
fprintf(stderr, "Failed to read input from stdin\n");
break; // Exit the loop on input error
}
// Remove the newline character if present
size_t input_len = strlen(input_buffer);
if (input_len > 0 && input_buffer[input_len - 1] == '\n') {
input_buffer[input_len - 1] = '\0';
}
// Check for the exit command
if (strcasecmp(input_buffer, "exit") == 0) {
break; // Exit the loop
}
// Set the text as POST data
char text[2058]; // Increased buffer size
snprintf(text, sizeof(text), "text=%s", input_buffer);
// Set the HTTP headers
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
// Set the HTTP request options
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, text);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set the write callback function to write the response to a file
FILE *file = fopen("tts.wav", "wb"); // Open the file for writing
if (!file) {
fprintf(stderr, "Failed to open file for writing\n");
return 1;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
// Perform the HTTP request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "HTTP request failed: %s\n", curl_easy_strerror(res));
} else {
fclose(file); // Close the file
// Play the generated tts.wav file using aplay
system("amixer -q -D pulse sset Capture toggle");
system("aplay tts.wav");
system("amixer -q -D pulse sset Capture toggle");
fflush(stdin);
}
// Clean up and release resources
curl_slist_free_all(headers);
}
curl_easy_cleanup(curl);
return 0;
}