-2

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.

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?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
user3486308
  • 609
  • 3
  • 16
  • 36

1 Answers1

2

My short answer is yes.

Please confirm if I understood what you asked.

Please consider what happens when you call system:

  1. Your program will start the execution of a shell;
  2. This shell will read the command line you passed to it;
  3. The shell will start the programm you passed to system() -- in your case it is espeak;
  4. The program will receive the options you passed to it;
  5. Finally, the program will execute.

So, the items from 1 to 3 (and maybe 4) are all overhead when you use system(), versus when you start the same functionality in your source code.

If you find a way to call the same functionality of espeak via a function call in your program, then you'll have faster response times.

I don't know about espeak, so I will try to answer in generic terms.

  • 1st, it is not always easy to have the same functionality of a running program in your source code. Some programs have their functionality spread around a myriad of options, and it may be difficult to understand that and to create a function in your code to do what you can easily express them to to via command line;
  • 2nd, you will have to evaluate the cost benefit of the transformation. With current high performance processors and the plethora of memory, starting a shell and then your program possibly will not take too much time. That overhead is particularly negligible if you will only start espeak just a few times, with relatively long text to be spoken.
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
  • Thank you Hilton, you exactly got my mean and perfectly answered my question. – user3486308 Nov 26 '17 at 17:41
  • You may have written too little.   Your answer is “yes” — to what question?   The question title is “Is using [the] `system()` function inside C++ code a (as?) fast way as using source codes?”   You seem to be saying no, `system()` is *not* as fast as calling the API.   Or am I misunderstanding you? – G-Man Says 'Reinstate Monica' Nov 26 '17 at 18:43
  • I'm not a native speaker of English, and I'm sorry if I was not clear. Indeed, the idea is that calling system() will incur in additional steps of loading and starting programs -- the shell and the program espeak. While, if you include the functionality of espeak in your code, it will be available as soon as your program starts to run. Got it ? – Hilton Fernandes Nov 26 '17 at 22:46