1 /***************************************************************************
2 * Copyright (C) 2010,2011 by Rynhardt Kruger *
3 * email: rynkruger@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see: *
17 * <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
22 #include <espeak/speak_lib.h>
37 const int ratemult
= 35;
41 char mainbuf
[1024*64];
42 int mainlen
= 1024*64;
50 char prevchar(FILE * source
);
51 int prevline(char * data
);
53 int nextline(char * data
);
59 while (! feof(source
)&& i
< 1024*64) {
60 int f
= fgetc(source
);
72 if (ftell(source
) > mainlen
) {
73 fseek(source
,0-(mainlen
+1024*64),SEEK_CUR
);
84 espeak_SetParameter(espeakRATE
,rate
*ratemult
,0);
90 char *val
= getenv("HOME");
92 strcat(spath
,"/.ipagerc");
93 FILE *f
= fopen(spath
,"w");
94 fprintf(f
,"speech %s %d %s\n", evoice
,rate
,epunct
);
100 char *val
= getenv("HOME");
102 strcat(spath
,"/.ipagerc");
103 FILE *f
= fopen(spath
,"r");
111 int k
= fscanf(f
,"speech %s %d %s\n", sv
,&sr
,sp
);
112 if (k
== EOF
) beep();
113 espeak_SetVoiceByName(sv
);
114 espeak_SetParameter(espeakRATE
,ratemult
*(rate
=sr
),0);
116 espeak_SetPunctuationList(wp
);
119 espeak_SetParameter(espeakPUNCTUATION
,espeakPUNCT_SOME
,0);
126 espeak_SetParameter(espeakRATE
,rate
*ratemult
,0);
130 move(getmaxy(stdscr
)-1,0);
132 mvaddstr(getmaxy(stdscr
)-1,0,"Enter punctuation list: ");
137 mvgetnstr(getmaxy(stdscr
)-1,getcurx(stdscr
),p
,50);
141 espeak_SetPunctuationList(wp
);
142 espeak_SetParameter(espeakPUNCTUATION
,espeakPUNCT_SOME
,0);
146 move(getmaxy(stdscr
)-1,0);
148 mvaddstr(getmaxy(stdscr
)-1,0,"Enter voice name: ");
152 mvgetnstr(getmaxy(stdscr
)-1,getcurx(stdscr
),v
,20);
155 espeak_SetVoiceByName(v
);
158 void readmarks(FILE * mf
) {
160 for (i
= 0; i
< 27; i
++)
161 fscanf(mf
,"%d\n",&marks
[i
]);
166 for (i
= 0; i
< 27; i
++)
171 FILE * mf
= fopen(markname
,"w");
173 for (i
= 0; i
< 26; i
++)
174 fprintf(mf
,"%d\n",marks
[i
]);
175 fprintf(mf
,"%d\n",linenum
);
179 int gotomark(int n
) {
190 void gotomarkchar() {
191 move(getmaxy(stdscr
)-1,0);
193 mvaddstr(getmaxy(stdscr
)-1,0,"mark: ");
196 if (m
< 'a' || m
> 'z' || marks
[m
-97] == 0) {
197 mvaddstr(getmaxy(stdscr
)-1,0,"Invalet mark! Press any key. ");
206 move(getmaxy(stdscr
)-1,0);
208 mvaddstr(getmaxy(stdscr
)-1,0,"mark: ");
211 if (m
< 'a' || m
> 'z') {
212 mvaddstr(getmaxy(stdscr
)-1,0,"Invalet mark! Press any key. ");
217 marks
[m
-97] = linenum
;
221 strcpy(markname
,filename
);
222 strcat(markname
,".ipm");
223 FILE * mf
= fopen(markname
,"r");
242 err
= nextline(data
);
246 move(getmaxy(stdscr
)-1,0);
248 mvaddstr(getmaxy(stdscr
)-1,0,"Enter line number: ");
253 mvgetnstr(getmaxy(stdscr
)-1,getcurx(stdscr
),snum
,5);
256 int err
= gotoline(num
);
258 mvaddstr(getmaxy(stdscr
)-1,0, "Invalet line number! Press any key. ");
264 int gotoline(int n
) {
274 for (; n
>l
&&err
==0; n
--)
275 err
= nextline(data
);
287 char data
[getmaxx(stdscr
)];
291 while (strcasestr(data
,sbuf
) == NULL
&& err
== 0)
292 err
= prevline(data
);
295 mvaddstr(getmaxy(stdscr
)-1,0,"Text not found! Press any key. ");
302 move(getmaxy(stdscr
)-1,0);
304 mvaddstr(getmaxy(stdscr
)-1,0,"Enter text to find: ");
307 mvgetnstr(getmaxy(stdscr
)-1,getcurx(stdscr
),sbuf
,100);
311 char data
[getmaxx(stdscr
)];
315 while (strcasestr(data
,sbuf
) == NULL
&& err
== 0)
316 err
= nextline(data
);
320 mvaddstr(getmaxy(stdscr
)-1,0,"Text not found! Press any key. ");
329 char data
[getmaxx(stdscr
)];
333 while (strcasestr(data
,sbuf
) == NULL
&& err
== 0)
334 err
= nextline(data
);
338 mvaddstr(getmaxy(stdscr
)-1,0,"Text not found! Press any key. ");
345 void speakcall(short * wav
, int size
, espeak_EVENT
* evt
) {
347 while (evt
[i
].type
!=0) {
348 if (evt
[i
].type
== espeakEVENT_END
)
349 chars
= evt
[i
].text_position
;
350 else if (evt
[i
].type
== espeakEVENT_MSG_TERMINATED
&& sayall
) {
359 return mainbuf
[mainpos
];
363 if (mainpos
< mainlen
-1)
365 else if (nextchunc() != 0) {
377 else if (prevchunc() != 0)
383 int gotopos(int pos
) {
388 while (pos
< datapos
) {
394 int nextline(char * data
) {
406 if (strlen(data
) == 0)
410 //sprintf(tmp,"espeak %s",data);
417 if (lastline
== linenum
)
420 char paragraph
[1200];
423 strcpy(paragraph
,"");
425 while (strlen(paragraph
) < 1000 && strcmp(retval
,"\n") != 0) {
427 strcat(paragraph
,retval
);
429 espeak_Synth(paragraph
,1140,0,POS_CHARACTER
,0,1,NULL
,NULL
);
432 void start_sayall() {
439 pthread_attr_init(&pa);
441 pthread_create(&thread,&pa,speak,0);
450 int i
= offset
+chars
;
456 char prevchar(FILE * source
) {
457 fseek(source
,-2, SEEK_CUR
);
458 return (char) getc(source
);
461 int prevline(char * data
) {
478 for (i
= 0, j
= strlen(data
)-1; i
<j
; i
++,j
--) {
494 int readpage(char * data
) {
499 while (i
< getmaxy(stdscr
)-1 && nextline(line
)==0) {
512 while (i
< getmaxy(stdscr
)-1&&err
==0) {
513 err
= nextline(data
);
521 int i
= getmaxy(stdscr
)-1;
530 char data
[140*79] = "";
535 sprintf(tmpstr
,"%s line %d filepos %d (%d)", filename
,linenum
,ftell(source
),mainlen
);
536 mvaddstr(getmaxy(stdscr
)-1,0,tmpstr
);
637 main(int argc
, char * argv
[]) {
639 fprintf(stderr
, "Usage: ipage <filename>\n");
643 strcpy(filename
,argv
[1]);
644 sprintf(cmd
,"fold -s \"%s\" | dos2unix -U > /tmp/ipage.tmp",filename
);
645 int err
= system(cmd
);
646 system("echo >> /tmp/ipage.tmp");
647 source
= fopen("/tmp/ipage.tmp", "r");
649 fprintf(stderr
,"File doesn't exist!\n");
652 espeak_Initialize(AUDIO_OUTPUT_PLAYBACK
,100,NULL
,1);
654 espeak_SetParameter(espeakRATE
,rate
*ratemult
,0);
655 espeak_SetParameter(espeakPITCH
,40,0);
656 espeak_SetParameter(espeakVOLUME
,100,0);
657 espeak_SetVoiceByName("en-us");
658 espeak_SetSynthCallback(&speakcall
);
662 system("rm /tmp/ipage.tmp");