Sorry if the question title is not well, but I am trying to explain what I mean:
As I know I can use system()
function to using Linux terminal's commands inside my C++ code. For example system("aplay sound.wav");
. I don't know can I write all the Linux commands like this or not, but aplay
works.
So, my question is here: I want to use espeak
in my C++ program.I like espeak reads each string I pass trough it(something like what aplay
does in above code but respect to "strings"). Is it better to call it by system()
function or it's better to write a code like this inside my C++ code and change the char* text
whenever I wanted to read a new string?:
#include <string.h>
#include <malloc.h>
#include <espeak-ng/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 = {"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;
}
Which one is faster?
Generally speaking, I would suggest preferring to use library functions, or to write code that provides required functionality, before you consider using a call of system()
to execute another program.
If there is an appropriate API (aka a set of library functions) that directly provides required functionality, your program will probably be more reliable if it uses that API. Obviously, this assumes the library functions work correctly, and there is sufficient documentation to use them correctly. The risks here are related to buggy or poorly documented library functions, and responsiveness of the authors of those libraries in addressing concerns.
If there is not a suitable API available, then the next option is to implement required functionality yourself (using code and libraries you have access to) then - assuming you do a reasonable job - then, again, your program work reasonably well. The risks here are under your control - how well you understand what you're trying to do, how capable you are of designing and implementing the required functions (e.g. how far beyond your understanding is the work?), and the effort you put into do things well.
As a last resort, you can use a system()
call. The problem here is the behaviour of system()
is formally implementation-defined, so can vary between host systems, compilers and their standard libraries. The second part is that you have to deal with the behaviour of the program (or shell) that is executed using the system()
call - for example, if the author of a third-party program introduces new functionality, it can actually change how that program runs - for example, adding a GUI, changing command line options, etc. While library functions CAN change in ways that will break your program, third-party programs executed using a system()
call are MUCH more likely to change over time in ways that will break your program.
I'm not suggesting you should never use system()
. But view it as an option if other approaches are prohibitive, rather than the first tool you reach for.