tomwalters@0: /* tomwalters@0: edwave.c Edit a given range of a wave in binary short (16-bit) numbers. tomwalters@0: -------- Write results on the stdout. tomwalters@0: tomwalters@0: The `range' option sets the start and duration of the process. tomwalters@0: Its arguments are of the form: range=a-b (where start=a and duration=b-a+1) tomwalters@0: or: range=a (where start=a and duration=1 ) tomwalters@0: The arguments can be in time units (ms, s) or samples (no units), and both tomwalters@0: "min" (start of file) and "max" (end of file) are recognised. tomwalters@0: Samples in a file are numbered 0,1,2,...,max. tomwalters@0: tomwalters@0: The `operation' option sets the edit operation: tomwalters@0: extract = output the part of the input which is inclusively within tomwalters@0: the given range. tomwalters@0: delete = output the part of the input which is NOT inclusively tomwalters@0: within the given range. tomwalters@0: tomwalters@0: These operation types can be given in unambiguous abreviated form. tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "options.h" tomwalters@0: #include "strmatch.h" tomwalters@0: #include "units.h" tomwalters@0: tomwalters@0: char applic[] = "Edit a given range of a wave." ; tomwalters@0: tomwalters@0: static char *helpstr, *debugstr, *sampstr, *rangestr, *opstr, *typestr, *sizestr ; tomwalters@0: tomwalters@0: static Options option[] = { tomwalters@0: { "help" , "off" , &helpstr , "help" , DEBUG }, tomwalters@0: { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, tomwalters@0: { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL }, tomwalters@0: { "range" , "min-max" , &rangestr , "inclusive time range wrt start" , VAL }, tomwalters@0: { "operation" , "extract" , &opstr , "edit operation: {extract, delete}" , VAL }, tomwalters@0: { "type" , "short" , &typestr , "datatype" , VAL }, tomwalters@0: { "size" , "off" , &sizestr , "print wave size {s, ms}" , VAL }, tomwalters@0: ( char * ) 0 } ; tomwalters@0: tomwalters@0: tomwalters@0: int samplerate ; tomwalters@0: int type ; /* datatype index */ tomwalters@0: tomwalters@0: tomwalters@0: main(argc, argv) tomwalters@0: int argc; tomwalters@0: char **argv; tomwalters@0: { tomwalters@0: FILE *fp ; tomwalters@0: int i, n ; tomwalters@0: int a, b ; tomwalters@0: float y ; tomwalters@0: tomwalters@0: fp = openopts( option, argc, argv ) ; tomwalters@0: if ( !isoff( helpstr ) ) tomwalters@0: helpopts( helpstr, argv[0], applic, option ) ; tomwalters@0: tomwalters@0: samplerate = to_Hz( sampstr, 0 ) ; tomwalters@0: tomwalters@0: if ( ( type = typeindex( typestr ) ) < 0 ) { tomwalters@0: fprintf( stderr, "edwave: bad type [%s]\n", typestr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( range( rangestr, &a, &b, samplerate ) == 0 ) { tomwalters@0: fprintf(stderr,"edwave: bad range \n" ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: n = b-a+1 ; tomwalters@0: tomwalters@0: if ( ison( debugstr ) ) { tomwalters@0: fprintf( stderr, "range=%s a=%d b=%d n=%d\n", rangestr,a,b,n ) ; tomwalters@0: exit( 0 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( !isoff( sizestr ) ) { tomwalters@0: for ( i = 0 ; readitem( &y, type, 1, fp ) ; i++ ) tomwalters@0: ; tomwalters@0: if ( isstr( sizestr, "s" ) ) printf( "%.3f\n", (float)i/samplerate ) ; tomwalters@0: else if ( isstr( sizestr, "ms" ) ) printf( "%.1f\n", ((float)i/samplerate)*1000. ) ; tomwalters@0: else if ( ison( sizestr ) ) printf( "%d\n", i ) ; /* samples */ tomwalters@0: else { tomwalters@0: fprintf( stderr,"edwave: unknown units for size [%s]\n", sizestr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: else if ( a == (-1) ) /* special case of range=max */ tomwalters@0: edmax( fp ) ; tomwalters@0: tomwalters@0: else if ( iststr( opstr, "extract" ) ) { tomwalters@0: tomwalters@0: if ( ( i = seektype( a, type, fp ) ) < a ) { tomwalters@0: fprintf( stderr,"edwave: eof after %d items \n", i ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: for ( i = 0 ; ( b < 0 || i < n ) && readitem( &y, type, 1, fp ) ; i++ ) tomwalters@0: writeitem( &y, type, 1, stdout ) ; tomwalters@0: if ( i < n && b >= 0 ) { tomwalters@0: fprintf( stderr,"edwave: eof after %d items \n", a+i ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: else if ( iststr( opstr, "delete" ) ) { tomwalters@0: for ( i = 0 ; i < a && readitem( &y, type, 1, fp ) ; i++ ) tomwalters@0: writeitem( &y, type, 1, stdout ) ; tomwalters@0: if ( i < a ) { tomwalters@0: fprintf( stderr,"edwave: eof after %d items \n", i ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: if ( ( i = seektype( n, type, fp ) ) < n ) { tomwalters@0: fprintf( stderr,"edwave: eof after %d items \n", a+i ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: while ( readitem( &y, type, 1, fp ) ) tomwalters@0: writeitem( &y, type, 1, stdout ) ; tomwalters@0: } tomwalters@0: tomwalters@0: else { tomwalters@0: fprintf( stderr, "edwave: unknown operation [%s]\n", opstr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: fclose( fp ) ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: /* tomwalters@0: Special case of range=max or range=max-max tomwalters@0: */ tomwalters@0: tomwalters@0: edmax( fp ) tomwalters@0: FILE *fp ; tomwalters@0: { tomwalters@0: float x, y ; tomwalters@0: tomwalters@0: if ( iststr( opstr, "extract" ) ) { tomwalters@0: while ( readitem( &y, type, 1, fp ) ) tomwalters@0: x = y ; tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: } tomwalters@0: tomwalters@0: else if ( iststr( opstr, "delete" ) ) { tomwalters@0: tomwalters@0: if ( readitem( &x, type, 1, fp ) && readitem( &y, type, 1, fp ) ) { tomwalters@0: do { tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: x = y ; tomwalters@0: } while ( readitem( &y, type, 1, fp ) ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: else { tomwalters@0: fprintf( stderr, "edwave: unknown operation [%s]\n", opstr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: fclose( fp ) ; tomwalters@0: }