[go: up one dir, main page]

File: saytime.c

package info (click to toggle)
saytime 1.0-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 224 kB
  • ctags: 58
  • sloc: ansic: 413; makefile: 67; sh: 67
file content (539 lines) | stat: -rw-r--r-- 9,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
** saytime - audio time hack for the SPARCstation.
**
** Copyright (C) 1990 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/
/* modified on 2002/2/20 by Oohara Yuuma */

#include <stdio.h>
#include <time.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>

#define DEFAULT_SOUND_DEVICE "/dev/audio"
#define DEFAULT_SOUND_DIR "/usr/share/saytime"
#define DEFAULT_TIME_FORMAT "%P%l%M%S"

/* Here are the defines for the thirty different phrases the program
** needs.  If you want to substitute your own voice for mine, I suggest
** you record the following lines:
**
**     The time is eight o'clock exactly.
**     The time is eight oh-eight and one second.
**     The time is eight oh-eight and eight seconds.
**     One.  Two.  Three.  Four.  Five.  Six.  Seven.  Eight.  Nine.  Ten.
**     Eleven.  Twelve.  Thirteen.  Fourteen.  Fifteen.  Sixteen.  Seventeen.
**     Eighteen.  Nineteen.  Twenty.  Thirty.  Forty.  Fifty.
**
** Then use Sun's sound demo to dissect the lines into separate files.
** It's not really that much work, it took me about an hour.
*/

#define PH_ONE		1
#define PH_TWO		2
#define PH_THREE	3
#define PH_FOUR		4
#define PH_FIVE		5
#define PH_SIX		6
#define PH_SEVEN	7
#define PH_EIGHT	8
#define PH_NINE		9
#define PH_TEN		10
#define PH_ELEVEN	11
#define PH_TWELVE	12
#define PH_THIRTEEN	13
#define PH_FOURTEEN	14
#define PH_FIFTEEN	15
#define PH_SIXTEEN	16
#define PH_SEVENTEEN	17
#define PH_EIGHTEEN	18
#define PH_NINETEEN	19
#define PH_TWENTY	20
#define PH_THIRTY	21
#define PH_FORTY	22
#define PH_FIFTY	23
#define PH_THE_TIME_IS	24
#define PH_OCLOCK	25
#define PH_OH		26
#define PH_EXACTLY	27
#define PH_AND		28
#define PH_SECOND	29
#define PH_SECONDS	30

int opt_to_stdout = 0;
char *opt_sound_device = NULL;
char *opt_sound_dir = DEFAULT_SOUND_DIR;
char *opt_time_format = NULL;
 
void saynumber(), saydigit(), sayphrase(), sayfile(), usage();
void sayformat(struct tm *, char *);

int main( argc, argv )
int argc;
char *argv[];
    {
    long clock;
    struct tm *t;
    char *opt_string="co:d:f:h";
    int  op ;

    while( (op=getopt(argc, argv, opt_string)) != -1) {

         switch (op) {
               case 'c' :  opt_to_stdout = 1;
                           break;
               case 'o' :  opt_sound_device = optarg;
                           break;
               case 'd' :  opt_sound_dir = optarg;
                           break;
               case 'f' :  opt_time_format = optarg;
                           break;
               case 'h' :
               default  :  usage();
         }
    }


    if (opt_to_stdout && opt_sound_device != NULL) {
	printf("Specifying alternate device and stdout makes no sense.\n");
	usage();
    }
    else if (opt_sound_device == NULL)
	opt_sound_device = DEFAULT_SOUND_DEVICE;

    if (opt_time_format == NULL)
        opt_time_format = DEFAULT_TIME_FORMAT;

    clock = time( (long *) 0 );
    t = localtime( &clock );

    sayformat(t, opt_time_format);
    
    /* sayclose( ); */
    return 0;
    }

void  
usage( ) 
    {
    fprintf( stderr, "Usage: saytime [-ch] [-d dir] [-f fmt] [-o dev]\n" );
    fprintf( stderr, "Speak the current time through the computer's sound device.\n\n" );
    fprintf( stderr, "Options:\n" );
    fprintf( stderr, " -c\toutput to stdout\n" );
    fprintf( stderr, " -d dir\tspecify sound directory [%s]\n", 
        DEFAULT_SOUND_DIR ) ;
    fprintf( stderr, " -f fmt\tspecify alternate time format [%s]\n",
        DEFAULT_TIME_FORMAT ) ;
    fprintf( stderr, " -h\tbrief help message\n" );
    fprintf( stderr, " -o dev\tspecify audio device [%s]\n", 
        DEFAULT_SOUND_DEVICE );
    exit( 1 );
    }

/*

  Say time based on printf formatted string.
  
  %P : "The time is"
  %l : hour, 12-hour clock
  %k : hour, 24-hour clock
  %M : minutes
  %S : seconds 

*/
void
sayformat( t, fmt ) 
struct tm *t;
char *fmt;
    {

    while ( *fmt ) 
        {
        switch( *++fmt )
	    {
            case 'P':
            sayphrase( PH_THE_TIME_IS );
            break;

            case 'l':
            if ( t->tm_hour == 0 )
	        saynumber( 12, 0 );
            else if ( t->tm_hour > 12 )
	        saynumber( t->tm_hour - 12, 0 );
            else
	        saynumber( t->tm_hour, 0 );
            break;

            case 'k':
	    saynumber( t->tm_hour, 0 );
            break;

            case 'M':
	    if ( t->tm_min == 0 )
		sayphrase ( PH_OCLOCK );
	    else
		saynumber( t->tm_min, 1 );
            break;
            
	    case 'S':
            if ( t->tm_sec == 0 )
	        sayphrase( PH_EXACTLY );
            else
	        {
	        sayphrase( PH_AND );
                saynumber( t->tm_sec, 0 );
	        if ( t->tm_sec == 1 )
                    sayphrase( PH_SECOND );
                else
                    sayphrase( PH_SECONDS );
                }
            break;

	    default:
	    (void) fprintf( stderr, "Invalid format char '%c'\n", *fmt );
            exit( 1 );
            }
       fmt++;
       }
    }

void
saynumber( n, leadingzero )
int n, leadingzero;
    {
    int ones, tens;

     % 10;
    tens = n / 10;

    switch ( tens )
	{
	case 0:
	if ( leadingzero )
	    sayphrase( PH_OH );
	saydigit( ones );
	break;

	case 1:
	switch ( ones )
	    {
	    case 0:
	    sayphrase( PH_TEN );
	    break;

	    case 1:
	    sayphrase( PH_ELEVEN );
	    break;

	    case 2:
	    sayphrase( PH_TWELVE );
	    break;

	    case 3:
	    sayphrase( PH_THIRTEEN );
	    break;

	    case 4:
	    sayphrase( PH_FOURTEEN );
	    break;

	    case 5:
	    sayphrase( PH_FIFTEEN );
	    break;

	    case 6:
	    sayphrase( PH_SIXTEEN );
	    break;

	    case 7:
	    sayphrase( PH_SEVENTEEN );
	    break;

	    case 8:
	    sayphrase( PH_EIGHTEEN );
	    break;

	    case 9:
	    sayphrase( PH_NINETEEN );
	    break;

	    default:
	    (void) fprintf( stderr, "Shouldn't happen.\n" );
	    exit( 1 );
	    }
	break;

	case 2:
	sayphrase( PH_TWENTY );
	if ( ones != 0 )
	    saydigit( ones );
	break;

	case 3:
	sayphrase( PH_THIRTY );
	if ( ones != 0 )
	    saydigit( ones );
	break;

	case 4:
	sayphrase( PH_FORTY );
	if ( ones != 0 )
	    saydigit( ones );
	break;

	case 5:
	sayphrase( PH_FIFTY );
	if ( ones != 0 )
	    saydigit( ones );
	break;

	default:
	(void) fprintf( stderr, "Shouldn't happen.\n" );
	exit( 1 );
	}
    }

void
saydigit( n )
int n;
    {
    switch ( n )
	{
	case 1:
	sayphrase( PH_ONE );
	break;

	case 2:
	sayphrase( PH_TWO );
	break;

	case 3:
	sayphrase( PH_THREE );
	break;

	case 4:
	sayphrase( PH_FOUR );
	break;

	case 5:
	sayphrase( PH_FIVE );
	break;

	case 6:
	sayphrase( PH_SIX );
	break;

	case 7:
	sayphrase( PH_SEVEN );
	break;

	case 8:
	sayphrase( PH_EIGHT );
	break;

	case 9:
	sayphrase( PH_NINE );
	break;

	default:
	(void) fprintf( stderr, "Shouldn't happen.\n" );
	exit( 1 );
	}
    }

void
sayphrase( phrase )
int phrase;
    {
    switch ( phrase )
	{
	case PH_ONE:
	sayfile( "1.au" );
	break;

	case PH_TWO:
	sayfile( "2.au" );
	break;

	case PH_THREE:
	sayfile( "3.au" );
	break;

	case PH_FOUR:
	sayfile( "4.au" );
	break;

	case PH_FIVE:
	sayfile( "5.au" );
	break;

	case PH_SIX:
	sayfile( "6.au" );
	break;

	case PH_SEVEN:
	sayfile( "7.au" );
	break;

	case PH_EIGHT:
	sayfile( "8.au" );
	break;

	case PH_NINE:
	sayfile( "9.au" );
	break;

	case PH_TEN:
	sayfile( "10.au" );
	break;

	case PH_ELEVEN:
	sayfile( "11.au" );
	break;

	case PH_TWELVE:
	sayfile( "12.au" );
	break;

	case PH_THIRTEEN:
	sayfile( "13.au" );
	break;

	case PH_FOURTEEN:
	sayfile( "14.au" );
	break;

	case PH_FIFTEEN:
	sayfile( "15.au" );
	break;

	case PH_SIXTEEN:
	sayfile( "16.au" );
	break;

	case PH_SEVENTEEN:
	sayfile( "17.au" );
	break;

	case PH_EIGHTEEN:
	sayfile( "18.au" );
	break;

	case PH_NINETEEN:
	sayfile( "19.au" );
	break;

	case PH_TWENTY:
	sayfile( "20.au" );
	break;

	case PH_THIRTY:
	sayfile( "30.au" );
	break;

	case PH_FORTY:
	sayfile( "40.au" );
	break;

	case PH_FIFTY:
	sayfile( "50.au" );
	break;

	case PH_THE_TIME_IS:
	sayfile( "the_time_is.au" );
	break;

	case PH_OCLOCK:
	sayfile( "oclock.au" );
	break;

	case PH_OH:
	sayfile( "oh.au" );
	break;

	case PH_EXACTLY:
	sayfile( "exactly.au" );
	break;

	case PH_AND:
	sayfile( "and.au" );
	break;

	case PH_SECOND:
	sayfile( "second.au" );
	break;

	case PH_SECONDS:
	sayfile( "seconds.au" );
	break;

	default:
	(void) fprintf( stderr, "Shouldn't happen.\n" );
	exit( 1 );
	}
    }

void
sayfile( filename )
     char *filename;
{
  pid_t pid;
  int status;
  char pathname[200];

  if (snprintf(pathname, 200, "%s/%s", opt_sound_dir, filename) >= 200)
  {
    fprintf(stderr, "too long sound file name\n");
    exit(1);
  }
  pid = fork();
  if (pid < 0)
  {
    fprintf(stderr, "fork failed\n");
    exit(1);
  }
  else if (pid == 0)
  {
    /* child */
    if (opt_to_stdout)
    {
      execl("/usr/bin/sox", "sox", "-t.ul", "-c", "1",
            pathname, "-t", "raw", "/dev/stdout", NULL);
    }
    else
    {
      execl("/usr/bin/sox", "sox", "-t.ul", "-c", "1",
            pathname, "-t", "ossdsp", opt_sound_device, NULL);
    }
    fprintf(stderr, "execl failed\n");
    exit(1);
  }
  /* parent */
  if (waitpid(pid, &status, 0) != pid)
  {
    fprintf(stderr, "waitpid failed\n");
    exit(1);
  }
  if (status != 0)
  {
    fprintf(stderr, "child process returned a non-zero status %d\n",
            WEXITSTATUS(status));
    exit(1);
  }
}