comparison tools/edwave.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 edwave.c Edit a given range of a wave in binary short (16-bit) numbers.
3 -------- Write results on the stdout.
4
5 The `range' option sets the start and duration of the process.
6 Its arguments are of the form: range=a-b (where start=a and duration=b-a+1)
7 or: range=a (where start=a and duration=1 )
8 The arguments can be in time units (ms, s) or samples (no units), and both
9 "min" (start of file) and "max" (end of file) are recognised.
10 Samples in a file are numbered 0,1,2,...,max.
11
12 The `operation' option sets the edit operation:
13 extract = output the part of the input which is inclusively within
14 the given range.
15 delete = output the part of the input which is NOT inclusively
16 within the given range.
17
18 These operation types can be given in unambiguous abreviated form.
19
20 */
21
22
23 #include <stdio.h>
24 #include <math.h>
25 #include "options.h"
26 #include "strmatch.h"
27 #include "units.h"
28
29 char applic[] = "Edit a given range of a wave." ;
30
31 static char *helpstr, *debugstr, *sampstr, *rangestr, *opstr, *typestr, *sizestr ;
32
33 static Options option[] = {
34 { "help" , "off" , &helpstr , "help" , DEBUG },
35 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
36 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
37 { "range" , "min-max" , &rangestr , "inclusive time range wrt start" , VAL },
38 { "operation" , "extract" , &opstr , "edit operation: {extract, delete}" , VAL },
39 { "type" , "short" , &typestr , "datatype" , VAL },
40 { "size" , "off" , &sizestr , "print wave size {s, ms}" , VAL },
41 ( char * ) 0 } ;
42
43
44 int samplerate ;
45 int type ; /* datatype index */
46
47
48 main(argc, argv)
49 int argc;
50 char **argv;
51 {
52 FILE *fp ;
53 int i, n ;
54 int a, b ;
55 float y ;
56
57 fp = openopts( option, argc, argv ) ;
58 if ( !isoff( helpstr ) )
59 helpopts( helpstr, argv[0], applic, option ) ;
60
61 samplerate = to_Hz( sampstr, 0 ) ;
62
63 if ( ( type = typeindex( typestr ) ) < 0 ) {
64 fprintf( stderr, "edwave: bad type [%s]\n", typestr ) ;
65 exit( 1 ) ;
66 }
67
68 if ( range( rangestr, &a, &b, samplerate ) == 0 ) {
69 fprintf(stderr,"edwave: bad range \n" ) ;
70 exit( 1 ) ;
71 }
72 n = b-a+1 ;
73
74 if ( ison( debugstr ) ) {
75 fprintf( stderr, "range=%s a=%d b=%d n=%d\n", rangestr,a,b,n ) ;
76 exit( 0 ) ;
77 }
78
79 if ( !isoff( sizestr ) ) {
80 for ( i = 0 ; readitem( &y, type, 1, fp ) ; i++ )
81 ;
82 if ( isstr( sizestr, "s" ) ) printf( "%.3f\n", (float)i/samplerate ) ;
83 else if ( isstr( sizestr, "ms" ) ) printf( "%.1f\n", ((float)i/samplerate)*1000. ) ;
84 else if ( ison( sizestr ) ) printf( "%d\n", i ) ; /* samples */
85 else {
86 fprintf( stderr,"edwave: unknown units for size [%s]\n", sizestr ) ;
87 exit( 1 ) ;
88 }
89 }
90
91 else if ( a == (-1) ) /* special case of range=max */
92 edmax( fp ) ;
93
94 else if ( iststr( opstr, "extract" ) ) {
95
96 if ( ( i = seektype( a, type, fp ) ) < a ) {
97 fprintf( stderr,"edwave: eof after %d items \n", i ) ;
98 exit( 1 ) ;
99 }
100 for ( i = 0 ; ( b < 0 || i < n ) && readitem( &y, type, 1, fp ) ; i++ )
101 writeitem( &y, type, 1, stdout ) ;
102 if ( i < n && b >= 0 ) {
103 fprintf( stderr,"edwave: eof after %d items \n", a+i ) ;
104 exit( 1 ) ;
105 }
106 }
107
108 else if ( iststr( opstr, "delete" ) ) {
109 for ( i = 0 ; i < a && readitem( &y, type, 1, fp ) ; i++ )
110 writeitem( &y, type, 1, stdout ) ;
111 if ( i < a ) {
112 fprintf( stderr,"edwave: eof after %d items \n", i ) ;
113 exit( 1 ) ;
114 }
115 if ( ( i = seektype( n, type, fp ) ) < n ) {
116 fprintf( stderr,"edwave: eof after %d items \n", a+i ) ;
117 exit( 1 ) ;
118 }
119 while ( readitem( &y, type, 1, fp ) )
120 writeitem( &y, type, 1, stdout ) ;
121 }
122
123 else {
124 fprintf( stderr, "edwave: unknown operation [%s]\n", opstr ) ;
125 exit( 1 ) ;
126 }
127
128 fclose( fp ) ;
129 }
130
131
132 /*
133 Special case of range=max or range=max-max
134 */
135
136 edmax( fp )
137 FILE *fp ;
138 {
139 float x, y ;
140
141 if ( iststr( opstr, "extract" ) ) {
142 while ( readitem( &y, type, 1, fp ) )
143 x = y ;
144 writeitem( &x, type, 1, stdout ) ;
145 }
146
147 else if ( iststr( opstr, "delete" ) ) {
148
149 if ( readitem( &x, type, 1, fp ) && readitem( &y, type, 1, fp ) ) {
150 do {
151 writeitem( &x, type, 1, stdout ) ;
152 x = y ;
153 } while ( readitem( &y, type, 1, fp ) ) ;
154 }
155 }
156
157 else {
158 fprintf( stderr, "edwave: unknown operation [%s]\n", opstr ) ;
159 exit( 1 ) ;
160 }
161
162 fclose( fp ) ;
163 }