comparison tools/convert.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 convert.c
3 -----------
4 Convert <number><units> to new units given by the "to" option.
5 <units> are:
6 p sample points
7 s time in seconds
8 ms time in millisecs
9 Hz frequency in Hz
10 kHz frequency in kHz
11
12 Time in seconds or miliseconds is converted to and from sample points using
13 the given samplerate. Time (in seconds, miliseconds or samples) is converted
14 to and from frequency by reciprocal.
15
16 Example:
17
18 Convert 100 samples to time in ms, and the 10ms to a number of samples, both
19 at the default sample-rate of 20kHz. Then at a sample-rate of 10kHz print
20 the number of samples in 32ms.
21
22 convert to=ms 100p to=p 10ms samplerate=10kHz to=p 32ms
23
24 Convert 0.1 seconds to sample points.
25
26 convert -topoints 0.1sec
27 */
28
29 #include <stdio.h>
30 #include <math.h>
31 #include "options.h"
32 #include "units.h"
33 #include "strmatch.h"
34
35 char applic[] = "Convert units: frequency, time, and sample numbers." ;
36 char usage[] = "convert [options] arguments " ;
37
38 static char *helpstr, *debugstr, *sampstr, *tostr ;
39
40 static Options option[] = {
41 { "help" , "off" , &helpstr , "help" , DEBUG },
42 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
43 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
44 { "to" , "points" , &tostr , "convert to <units>" , VAL },
45 ( char * ) 0 } ;
46
47
48 int samplerate ;
49
50 main(argc, argv)
51 int argc ;
52 char *argv[] ;
53 {
54 int i, helpunits() ;
55
56 i = argc ;
57 while ( i > 0 ) {
58
59 if ( i == argc ) /* first time, to account for prog name */
60 i = getopts( option, i, &argv[argc-i] ) ;
61 else /* subsequent times */
62 i = getopts( option, i+1, &argv[argc-i-1] ) ;
63 if ( !isoff( helpstr ) )
64 helpopts2( helpstr, argv[0], applic, usage, option, helpunits ) ;
65
66 samplerate = to_Hz( sampstr, 0 ) ;
67
68 for( ; i > 0 && !isopt( argv[argc-i] ) ; --i )
69 convert( argv[argc-i] ) ;
70 }
71 }
72
73
74 convert( s )
75 char *s ;
76 {
77 switch ( listindex( units, tostr ) ) {
78 case (-2) : fprintf(stderr,"ambiguous units [%s]\n", tostr ) ;
79 exit( 1 ) ;
80 case (-1) : fprintf(stderr,"unknown units [%s]\n", tostr ) ;
81 exit( 1 ) ;
82 case 0 : /* to points */
83 printf( "%dp\n" , (int)to_p( s, samplerate ) ) ;
84 break ;
85 case 1 : /* to seconds */
86 printf( "%.3fs\n" , to_s( s, samplerate ) ) ;
87 break ;
88 case 2 : /* to ms */
89 printf( "%.3fms\n" , to_ms( s, samplerate ) ) ;
90 break ;
91 case 3 : /* to Hz */
92 printf( "%.3fHz\n" , to_Hz( s, samplerate ) ) ;
93 break ;
94 case 4 : /* to kHz */
95 printf( "%.3fkHz\n" , to_kHz( s, samplerate ) ) ;
96 break ;
97 case 5 : /* to ERB */
98 printf( "%.3ferb\n" , to_erb( s ) ) ;
99 break ;
100 }
101 }
102
103
104 helpunits()
105 {
106 fprintf(stderr,"\nunits: \n");
107 fprintf(stderr," points sample points \n");
108 fprintf(stderr," seconds time in seconds \n");
109 fprintf(stderr," ms time in millisecs \n");
110 fprintf(stderr," Hz frequency in Hz \n");
111 fprintf(stderr," kHz frequency in kHz \n");
112 exit( 1 ) ;
113 }