-1

I found this code on the internet:

#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 = 1000, 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;
}

I only want the espeak reads my strings in my program, and the above code can do it, but I want to know, are all of this code necessary for that purpose? (I mean is it possible to simplifying it?)

***Also I like to know are there a way to using espeak as a system function? I mean system("espeak "something" "); ?

Hasani
  • 3,543
  • 14
  • 65
  • 125
  • 1
    Seems like a pretty straight-forward example? You have to read the manual about what those functions do. Also, there is no header file called malloc.h in the C language. – Lundin Dec 12 '17 at 07:48
  • 1
    How much simpler could it get? Looks pretty minimal to me. You seem to entirely misunderstand what `system()` does. If you can run it from the command shell, you can pass it as a `system()` argument - simple as that. Your example will have to be modified to process the string passed in `argv[1]` rather than `text`. – Clifford Dec 12 '17 at 10:44

1 Answers1

2

The usage of eSpeak itself seems pretty minimal - you need to read the documentation for that. There are some minor C coding simplifications possible, but perhaps hardly worth the effort:

The memset() is unnecessary. The structure can be initialised to zero thus:

espeak_VOICE voice = {0} ;

If you declare text thus:

char text[] = "this is a English test";

Then you can avoid using strlen() and replace Size with sizeof(text).

The variables I, Run and L are unused and can be removed.

To be able to pass the text as a string on the command line, and thus be able to issue system( "espeak \"Say Something\"") ; for example, you simply need to pass argv[1] to espeak_Synth() instead of text (but you will need to reinstate the strlen() call to get the size.

Clifford
  • 88,407
  • 13
  • 85
  • 165