Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Text To Speech
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
DevX
n00b
n00b


Joined: 08 Apr 2004
Posts: 26

PostPosted: Mon Dec 20, 2004 8:27 am    Post subject: Text To Speech Reply with quote

Here is a little app that will read html(from the web or local), post script, text files, and strings right of the command line. Make sure you have the requirements. I find it handy to use in init scripts and for allerts, like ip traffic notices.

Code:

/*

make sure you have emerged:

festival
mbrola
html2text
pstotext

gcc say.c -o say

./say -s "Testing 1 2 3."

*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

#define FALSE 0
#define TRUE 1

char pchFileData[8192];
char pchString[8192];

char pchCmdStringStart[] = "festival -b '(voice_us1_mbrola)' '(SayText \"\0";
char pchCmdStringEnd[] = "\")'\0";


/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>                                                                                                >>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
int main(int argc, char **argv)
{
   FILE *   fpFile;
   char *   pchStr;   
   int      iFileSize, iOffset=0, iIndex, iBlockSize=55, iBlockCount;
   int      bSentenceBlock=TRUE;
   
   if ( argc == 1 )
   {
      printf("Usage: say -s \"this text will be read out.\"\n");
      printf("       say -f file://this_local/file_will_be_read.html    \n");
      printf("       say -f http://this_remote/file_will_be_read.html\n");
      printf("       say -f this_local_postscript_file_will_be_read.ps\n");
      printf("       say -f this_text_file_will_be_read\n");
      
   }
   memset(pchString, 0, 8192);
   
   if ( !strcmp(argv[1], "-s" ) )
   {
      strcpy(pchString, pchCmdStringStart);
      for ( iIndex = 2; iIndex < argc; iIndex++)
      {   /*
         **   C h e c k   F o r   U n w a n t e d   C h a r a c t e r s
         */
clean_start:if ( ( pchStr = strstr(argv[iIndex], "\r") ) )
         {
            *pchStr = ' ';
            goto clean_start;
         }
         if ( ( pchStr = strstr(argv[iIndex], "\n") ) )
         {
            *pchStr = ' ';
            goto clean_start;
         }
         if ( ( pchStr = strstr(argv[iIndex], "\"") ) )
         {
            *pchStr = ' ';
            goto clean_start;
         }
         if ( ( pchStr = strstr(argv[iIndex], "'") ) )
         {
            *pchStr = ' ';
            goto clean_start;
         }
         sprintf(pchString + strlen(pchString), "%s ", argv[iIndex]);
      }
      strcat(pchString, "\")'");
      system(pchString);
      return 0;
   }
   /*
   **   F i l e   B a s e d   R e a d   O u t
   */
   if ( ! strcmp(argv[1], "-f") )
   {
      /*
      **   F r o m   O f f s e t
      */
      if ( ( pchStr = strstr(argv[2], "+") ) )
      {
         *pchStr = 0;
         pchStr++;
         iOffset = strtol(pchStr, 0, 10);
      }
      /*
      **   R e a d   W e b   P a g e
      */
      if ( strstr(argv[2], ".html") )
      {
         if ( pchStr =  strstr(argv[2], "file://") )         // local file
         {
            sprintf(pchString, "html2text -nobs -o /tmp/tread.txt \"%s\"\0", argv[2]+7);
            system(pchString);
            printf(pchString);
            strcpy(argv[2], "/tmp/tread.txt");
            goto readit;
         }
         if ( pchStr =  strstr(argv[2], "http://") )         // web essite
         {
            sprintf(pchString, "html2text -nobs -o /tmp/tread.txt \"%s\"\0", argv[2]);
            system(pchString);
            printf(pchString);
            strcpy(argv[2], "/tmp/tread.txt");
            goto readit;
         }
      }
      /*
      **   R e a d   P o s t s c r i p t
      */
      if ( strstr(argv[2], ".ps") )                  // ps file
      {
         sprintf(pchString, "pstotext %s > \"/tmp/tread.txt\"\0", argv[2]);
         system(pchString);
         strcpy(argv[2], "/tmp/tread.txt");
         goto readit;
      }
      /*
      **   R e a d   T e x t   F i l e
      */
         printf("...");
         sprintf(pchString, "cp %s \"/tmp/tread.txt\"\0", argv[2]);
         system(pchString);
         strcpy(argv[2], "/tmp/tread.txt");

readit:
      if ( ( fpFile = fopen(argv[2], "r") ) == NULL )      // text file
      {
         printf("error: unable to open file %s\n", argv[2]);
         return ( 0 );
      }
      if ( fseek(fpFile, 0, SEEK_END) != 0 )
      {
         printf("error: unable to fseek SEEK_END of file %s\n", argv[2]);
         fclose(fpFile);
         return ( 0 );
      }
      if ( ( iFileSize = ftell(fpFile) ) == -1 )
      {
         printf("error: unable to ftell of file %s\n", argv[2]);
         fclose(fpFile);
         return ( 0 );
      }
      if ( fseek(fpFile, iOffset, SEEK_SET) != 0 )
      {
         printf("error: unable to fseek SEEK_SET of file %s\n", argv[2]);
         fclose(fpFile);
         return ( 0 );
      }
      iIndex = iOffset;
      for ( pchFileData[0] = iBlockCount = 0; ; iIndex++ )
      {
         pchFileData[ strlen(pchFileData) + 1 ] = 0;
         if ( fread(pchFileData+strlen(pchFileData), 1, 1, fpFile) != 1 )
         {
            if ( feof(fpFile) )
            {
               goto space;
            }
            printf("error: unable to read from file %s\n", argv[2]);
            fclose(fpFile);
            return ( 0 );
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == ' ' )
         {
            iBlockCount++;
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '.' )
         {
            if ( bSentenceBlock == TRUE )
            {
               goto output;
            }
            goto space;
         }
         /*
         **   C l e a n u p   U n w a n t e d   C h a r a c t e r s
         */
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '\n' )
         {
            pchFileData[ strlen(pchFileData) - 1 ] = ' ';
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '\r' )
         {
            pchFileData[ strlen(pchFileData) - 1 ] = ' ';
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '"' )
         {
            pchFileData[ strlen(pchFileData) - 1 ] = ' ';
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '\'' )
         {
            pchFileData[ strlen(pchFileData) - 1 ] = ' ';
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '_' )
         {
            pchFileData[ strlen(pchFileData) - 1 ] = ' ';
         }
         if ( pchFileData[ strlen(pchFileData) - 1 ] == '*' )
         {
            pchFileData[ strlen(pchFileData) - 1 ] = ' ';
         }
space:      if ( iBlockCount == iBlockSize || feof(fpFile) )
         {
output:         strcpy(pchString, pchCmdStringStart);
            strcat(pchString, pchFileData);
            strcat(pchString, pchCmdStringEnd);
            system(pchString);
            pchFileData[0] = iBlockCount   = 0;
            printf("%d\n", iIndex);
         }
         if ( feof(fpFile) )
         {
            break;
         }
      }
      fclose(fpFile);      
      return ( 0 );
   }
   /*
   **   S t a n d a r d   I n p u t
   */
   pchString[0] = 0;
   strcpy(pchString, pchCmdStringStart);   
   for ( iIndex = 0; ; iIndex++ )
   {
      iFileSize = read(STDIN_FILENO, pchString+strlen(pchString), 1);
      if ( pchString[ strlen(pchString) - 1 ] == '\n' )
         pchString[ strlen(pchString) - 1 ] = ' ';
      if ( pchString[ strlen(pchString) - 1 ] == '\r' )
         pchString[ strlen(pchString) - 1 ] = ' ';
      if ( iFileSize == 0 )
         break;
   }
   strcat(pchString, pchCmdStringEnd);
   printf(pchString);
   system(pchString);
   
   return ( 0 );
}


Please post any comments.
_________________
Still Dreaming
Back to top
View user's profile Send private message
AnXa
Apprentice
Apprentice


Joined: 06 Apr 2004
Posts: 250

PostPosted: Fri Dec 24, 2004 3:50 pm    Post subject: Reply with quote

This is best small program I know at this point. I can make use of say instead of huge import command. thanks.
_________________
The idea isn't about how do you see or hear it, it's about how do you experience it...
Back to top
View user's profile Send private message
WladyX
Guru
Guru


Joined: 25 Nov 2004
Posts: 503
Location: Romania

PostPosted: Fri Dec 24, 2004 10:27 pm    Post subject: Reply with quote

Really good, are there any other command line options besides -f and -s?
Is there a way to change the voice, except editing the source and recompiling?
thank you for this program, have you considered submitting it to freshmeat?
_________________
We are not alone.
Back to top
View user's profile Send private message
venkat
Tux's lil' helper
Tux's lil' helper


Joined: 18 Sep 2003
Posts: 110
Location: USA

PostPosted: Mon Dec 27, 2004 1:14 am    Post subject: Reply with quote

good hack dude! i made it handle pdf's too using pdf2html and modifying your "PS" section a bit. it doesn't parse filenames/paths with spaces correctly. but i really love this little program, i used it to read few ross anderson pages on the web on security. it was just awesome!

thank you! :-)
_________________
http://venkatu.dotgeek.org/blog/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum