I am trying to pas some strings to espeak and it will say them by this code:
#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
//#include <cstring>
using namespace std;
int main()
{
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
char Voice[] = {"English"};
int i=0;
char text[11][200] {"hi ",
"This is...",
"you can ... ",
"I am ,,,, " ,
"you can ... ",
"hope you ... ",
"come in ... ",
"if you ... ",
"you will... ",
"I hope ... ",
"Take care "
};
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
output = AUDIO_OUTPUT_PLAYBACK;
espeak_Initialize(output, Buflength, path, Options );
espeak_SetVoiceByName(Voice);
const char *langNativeString = "en_US";
espeak_VOICE voice= {0};
voice.languages = langNativeString;
voice.name = "US";
voice.variant = 2;
voice.gender = 1;
// Size = strlen(text)+1;
for (;;)
{
for(i=0; i<11; i++)
{
Size = sizeof(text[i]);
system("eog --fullscreen --disable-gallery --single-window 1.jpg &");
usleep(3000000);
espeak_Synth( text[i], Size, position, position_type, end_position, flags,
unique_identifier, user_data );
espeak_Synchronize( );
system("eog --fullscreen --disable-gallery --single-window 1.jpg &");
usleep(3000000);
}
//fflush(stdout);
}
return 0;
}
But I get segmentation fault(core dumped) error. I tried to debugging the code and this is the error : Cannot open file: ../sysdeps/posix/system.c that occurs in this line of code:
system("eog --fullscreen --disable-gallery --single-window 1.jpg &");
. How can I fix this?
You think to have 11 arrays(sentences) but you actually have only 10.
These two
"I am glad too meet you here "
"you can see many science and technology products here ",
are actually just one since you miss the comma at the end
I did transport this part of code to outside of the main() function, and it works now without errors:
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
char Voice[] = {"English"};
int i=0;
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
Related
I'm having trouble identifying a possible source of memory corruption in the following code. Is it due to the fact that I don't call the free() function on char *stringToAdd?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 1024
char* add_strings(char *stringOne, char *stringTwo);
int main() {
char s = 'x';
char *stringOne = &s;
char *stringTwo = new char[1025]();
char *final = add_strings(stringOne, stringTwo);
return 0;
}
char* add_strings(char *stringOne, char *stringTwo) {
unsigned int lengthOfStringOne = strlen(stringOne);
unsigned int lengthOfStringTwo = strlen(stringTwo);
cout << lengthOfStringOne << endl;
cout << lengthOfStringTwo << endl;
char *stringToAdd = lengthOfStringOne < lengthOfStringTwo ? stringOne : stringTwo;
unsigned int lengthOfAdd = lengthOfStringOne < lengthOfStringTwo ? lengthOfStringOne : lengthOfStringTwo;
char *final = static_cast<char*>(calloc(MAX, sizeof(char)));
unsigned int avgLengthOfStrings = (lengthOfStringOne + lengthOfStringTwo) / 2;
if (avgLengthOfStrings < MAX) {
strncat(final, stringToAdd, lengthOfAdd);
printf("DONE\n");
} else {
printf("Average length of both input strings exceeds liimit.\n");
free(final);
return NULL;
}
return final;
}
So i have a program that records snippets of audio on a microphone array, it then timestamps the end time of the file. My problem is that the process of the program starting the recording takes a random amount of time to start and so the audio is a random length. I want a way to read the size of the file (kilobytes) then trim the file by a certain number of kilobytes so that the recordings are always the same length.
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include<signal.h>
#include<unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include "zlib.h"
long long int rem;
long long int ms1;
long long int unixtime;
using namespace std;
const char* Filenames;
int main(void) {
int system(const char *command);
int x;
struct timeval tp1;
rem = 5;
while (rem != 0) {
gettimeofday(&tp1, NULL);
ms1 = tp1.tv_sec * 1000ll + tp1.tv_usec / 1000ll;
rem = ms1 % 10000;
}
for (x=0; x<3; x++){
pid_t pid=fork();
if (pid==0){
execl("/home/pi/odas/bin/odaslive", "/home/pi/odas/bin/odaslive", "-vc", "/home/pi/odas/config/odaslive/matrix_creator.cfg", (char *)NULL);
exit(127);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(15000));
kill(pid, SIGINT);
}
gettimeofday(&tp1, NULL);
unixtime = tp1.tv_sec + tp1.tv_usec / 1000000ll;
std::string name1 = "/home/pi/matrix_creator_explore/postfiltered/postfiltered1_";
std::string name2 = ".raw";
std::string result1;
result1 = name1 + std::to_string(unixtime) + name2;
const char *cstr = result1.c_str();
rename ("/home/pi/matrix_creator_explore/postfiltered.raw", cstr);
std::string name3 = "/home/pi/matrix_creator_explore/tracked/tracked1_";
std::string name4 = ".raw";
std::string result2;
result2 = name3 + std::to_string(unixtime) + name4;
const char *cstr1 = result2.c_str();
rename ("/home/pi/matrix_creator_explore/tracked.raw", cstr1);
struct stat buf;
stat( cstr,&buf);
printf ("\n %i \n", buf.st_size);
}
}
This is my code. I want to get 5 strings from the user and espeak reads each of them when user interred it. But I get segmentation fault(core dumped) message.
#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>
int test()
{
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
char Voice[] = {"English"};
int i=0;
char text[1000];
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
output = AUDIO_OUTPUT_PLAYBACK;
espeak_Initialize(output, Buflength, path, Options );
espeak_SetVoiceByName(Voice);
const char *langNativeString = "en_US";
espeak_VOICE voice={0};
voice.languages = langNativeString;
voice.name = "US";
voice.variant = 2;
voice.gender = 1;
Size = strlen(text)+1;
for (i=0; i<5; i++)
{
scanf("%s ", &text);
printf("%s", text);
espeak_Synth( text, Size, position, position_type, end_position, flags,
unique_identifier, user_data );
espeak_Synchronize( );
fflush(stdout);
}
return 0;
}
int main(int argc, char* argv[] )
{
test();
return 0;
}
I tried some modification but none of them worked. I want the program works like this:
User input: hi
espeak says: hi
user input: one
espeak says: one
(for 5
inputs)
But when I try to interring more than 4 characters as input,it gives segmentation fault error!
The two main issues are:
you use strlen on an uninitialized array of chars;
the unique_identifier argument of espeak_Synth must be NULL or point to an unsigned int (see the source code) while now it is an unsigned pointer to random memory.
Move strlen after scanf, use NULL instead of unique_identifier and your code will suddenly work (kind of).
There are many other issues though: useless variables, uninitialized variables, no input sanitization and more. IMO a better approach would be to throw away the test function and rewrite it from scratch properly.
Addendum
This is how I'd rewrite the above code. It is still suboptimal (no input sanitization, no error checking) but IMO it is much cleaner.
#include <stdio.h>
#include <string.h>
#include <espeak/speak_lib.h>
static void say(const char *text)
{
static int initialized = 0;
if (! initialized) {
espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, NULL, 0);
espeak_SetVoiceByName("en");
initialized = 1;
}
espeak_Synth(text, strlen(text)+1,
0, POS_CHARACTER, 0,
espeakCHARS_UTF8, NULL, NULL);
espeak_Synchronize();
}
int main()
{
char text[1000];
int i;
for (i = 0; i < 5; ++i) {
scanf("%s", text);
say(text);
}
return 0;
}
I can compile this code without any errors:
#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
char Voice[] = {"English"};
char text[30] = {"this is a english test"};
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
int main(int argc, char* argv[] )
{
output = AUDIO_OUTPUT_PLAYBACK;
int I, Run = 1, L;
espeak_Initialize(output, Buflength, path, Options );
espeak_SetVoiceByName(Voice);
const char *langNativeString = "en"; //Default to US English
espeak_VOICE voice;
memset(&voice, 0, sizeof(espeak_VOICE)); // Zero out the voice first
voice.languages = langNativeString;
voice.name = "US";
voice.variant = 2;
voice.gender = 1;
// espeak_SetVoiceByProperties(&voice);
Size = strlen(text)+1;
espeak_Synth( text, Size, position, position_type, end_position, flags,
unique_identifier, user_data );
espeak_Synchronize( );
return 0;
}
But I get segmentation fault error when try to compile this code(putting all the code inside man():
#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>
int main(){
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
char Voice[] = {"English"};
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
output = AUDIO_OUTPUT_PLAYBACK;
int I, Run = 1, L;
espeak_Initialize(output, Buflength, path, Options );
espeak_SetVoiceByName(Voice);
const char *langNativeString = "en"; //Default to US English
espeak_VOICE voice;
memset(&voice, 0, sizeof(espeak_VOICE)); // Zero out the voice first
voice.languages = langNativeString;
voice.name = "US";
voice.variant = 2;
voice.gender = 1;
//espeak_SetVoiceByProperties(&voice);
char tx[]="hi there, my name is Eliyaas, what's your name?";
espeak_Synth( tx, strlen(tx)+1, position, position_type, end_position, flags,unique_identifier, user_data );
espeak_Synchronize( );
return 0;}
What is the difference and which line causes this error?
Is it possible to put all of this program inside main() function? How?
(Add more text to pass add more information needed to post error) (Add
more text to pass add more information needed to post error) (Add more
text to pass add more information needed to post error) (Add more text
to pass add more information needed to post error)
Global variables are zero initialized in C++, local variables are not and it is Undefined Behavior to read from them. You have plenty of them in your code, for example:
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
you need to edit your code and make sure that all variables are properly initialized. For example with memset if they are POD, the same way as it is done to voice.
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <vector>
#include <sstream>
#define SHMSIZE 1024
using namespace std;
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
struct process
{
int r;
string name;
vector<string> lines;
};
int main(int argc, char * argv[])
{
int firstRun = 1; //Skipping First Line of Assign-1.ip.
int quantum = 0; //For taking input of quantum.
int count = 0; //For number of processes.
int pchtoint;
string c;
char * pch; //For tokenization.
string reading_file; //Reading a line from file.
char * readarr; //Converting "reading_file" to readarr for tokenization.
process * p;
//=== Quantum Input ===//
cout<<"Enter Quantum size [1-1000]: ";
cin>>quantum;
while(quantum < 1 || quantum > 1000)
{
cout<<"Wrong input!!! Enter Again [1-1000]: ";
cin>>quantum;
}
//=====================//
//===Filing===//
ifstream read("Assign-2.ip");
if(read.is_open())
{
while(!read.eof())
{
getline(read, reading_file);
readarr = new char[reading_file.size() + 1];
for(int i = 0; i < reading_file.length(); i++)
{
readarr[i] = reading_file[i];
}
if(firstRun > 1)
{
int countingline = 0; //counting the number of lines in a process.
pch = strtok (readarr," ,");
while (pch != NULL)
{
c = pch[1];
pchtoint = atoi(c.c_str());
p[pchtoint-1].r++;
p[pchtoint-1].lines.push_back(pch);
for(int i = 0; i < p[pchtoint-1].lines.size(); i++)
cout<<p[pchtoint-1].name<<"=="<<p[pchtoint-1].lines.at(i)<<endl;
pch = strtok (NULL, " ,");
}
}
else
{
pch = strtok (readarr,",.-");
while (pch != NULL)
{
count++;
pch = strtok (NULL, ",.-");
}
p = new process[count];
string s = "p";
for(int i = 0; i < count; i++)
{
s = s + patch::to_string(i+1);
p[i].name = s;
s = s[0];
}
firstRun++;
}
}
}
else
{
cout<<"Cannot open file!!!"<<endl;
}
read.close();
return 0;
}
Enter Quantum size [1-1000]: 2
p1==p1-l1
p2==p2-l1
p3==p3-l1
p1==p1-l1
p1==p1-l2
p2==p2-l1
p2==p2-l2
p3==p3-l1
p3==p3-l2
p1==p1-l1
p1==p1-l2
p1==p1-l3
p3==p3-l1
p3==p3-l2
p3==p3-l3
p1==p1-l1
p1==p1-l2
p1==p1-l3
p1==p1-l4
Segmentation fault (core dumped)
I am reading data from a cvs file. and storing it in struct that is p here. but I don't know why it is giving segmentation fault. I am compiling it on ubuntu terminal.
The input file contains data:
P1, P2, P3,
p1-l1, p2-l1, p3-l1
p1-l2, p2-l2, p3-l2
p1-l3, , p3-l3
p1-l4, ,