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