view 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
line wrap: on
line source
/*
  edwave.c   Edit a given range of a wave in binary short (16-bit) numbers.
  --------   Write results on the stdout.

 The `range' option sets the start and duration of the process.
 Its arguments are of the form: range=a-b (where start=a and duration=b-a+1)
			    or: range=a   (where start=a and duration=1    )
 The arguments can be in time units (ms, s) or samples (no units), and both
 "min" (start of file) and "max" (end of file) are recognised.
 Samples in a file are numbered 0,1,2,...,max.

 The `operation' option sets the edit operation:
    extract     = output the part of the input which is inclusively within
		  the given range.
    delete      = output the part of the input which is NOT inclusively
		  within the given range.

 These operation types can be given in unambiguous abreviated form.

*/


#include <stdio.h>
#include <math.h>
#include "options.h"
#include "strmatch.h"
#include "units.h"

char applic[] = "Edit a given range of a wave." ;

static char *helpstr, *debugstr, *sampstr,  *rangestr, *opstr, *typestr, *sizestr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                                  , DEBUG },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"                      , DEBUG },
    {   "samplerate",   "20kHz"     ,  &sampstr     ,   "samplerate "                           , VAL   },
    {   "range"     ,   "min-max"   ,  &rangestr    ,   "inclusive time range wrt start"        , VAL   },
    {   "operation" ,   "extract"   ,  &opstr       ,   "edit operation: {extract, delete}"     , VAL   },
    {   "type"      ,   "short"     ,  &typestr     ,   "datatype"                              , VAL     },
    {   "size"      ,   "off"       ,  &sizestr     ,   "print wave size {s, ms}"               , VAL   },
   ( char * ) 0 } ;


int     samplerate ;
int     type       ;    /* datatype index */


main(argc, argv)
int    argc;
char **argv;
{
    FILE   *fp ;
    int     i, n ;
    int     a, b ;
    float   y    ;

    fp = openopts( option, argc, argv ) ;
    if ( !isoff( helpstr ) )
	helpopts( helpstr, argv[0], applic, option ) ;

    samplerate  = to_Hz( sampstr, 0 ) ;

    if ( ( type = typeindex( typestr ) ) < 0 ) {
	fprintf( stderr, "edwave: bad type [%s]\n", typestr ) ;
	exit( 1 ) ;
    }

    if ( range( rangestr, &a, &b, samplerate ) == 0 ) {
	fprintf(stderr,"edwave: bad range \n" ) ;
	exit( 1 ) ;
    }
    n = b-a+1 ;

    if ( ison( debugstr ) ) {
	fprintf( stderr, "range=%s  a=%d b=%d n=%d\n", rangestr,a,b,n ) ;
	exit( 0 ) ;
    }

    if ( !isoff( sizestr ) ) {
	for ( i = 0 ; readitem( &y, type, 1, fp )  ; i++ )
	    ;
	if ( isstr( sizestr, "s" ) )        printf( "%.3f\n",  (float)i/samplerate ) ;
	else if ( isstr( sizestr, "ms" ) )  printf( "%.1f\n", ((float)i/samplerate)*1000. ) ;
	else if ( ison( sizestr ) )         printf( "%d\n", i ) ; /* samples */
	else {
	    fprintf( stderr,"edwave: unknown units for size [%s]\n", sizestr ) ;
	    exit( 1 ) ;
	}
    }

    else if ( a == (-1) )       /* special case of range=max */
	edmax( fp ) ;

    else if ( iststr( opstr, "extract" ) ) {

	if ( ( i = seektype( a, type, fp ) ) < a ) {
	    fprintf( stderr,"edwave: eof after %d items \n", i ) ;
	    exit( 1 ) ;
	}
	for ( i = 0 ; ( b < 0  ||  i < n ) && readitem( &y, type, 1, fp ) ; i++ )
	    writeitem( &y, type, 1, stdout ) ;
	if ( i < n && b >= 0 ) {
	    fprintf( stderr,"edwave: eof after %d items \n", a+i ) ;
	    exit( 1 ) ;
	}
    }

    else if ( iststr( opstr, "delete" ) ) {
	for ( i = 0 ; i < a && readitem( &y, type, 1, fp ) ; i++ )
	    writeitem( &y, type, 1, stdout ) ;
	if ( i < a ) {
	    fprintf( stderr,"edwave: eof after %d items \n", i ) ;
	    exit( 1 ) ;
	}
	if ( ( i = seektype( n, type, fp ) ) < n ) {
	    fprintf( stderr,"edwave: eof after %d items \n", a+i ) ;
	    exit( 1 ) ;
	}
	while ( readitem( &y, type, 1, fp ) )
	    writeitem( &y, type, 1, stdout ) ;
    }

    else {
	fprintf( stderr, "edwave: unknown operation [%s]\n", opstr ) ;
	exit( 1 ) ;
    }

    fclose( fp ) ;
}


/*
Special case of range=max or range=max-max
*/

edmax( fp )
FILE *fp ;
{
    float x, y ;

    if ( iststr( opstr, "extract" ) ) {
	while ( readitem( &y, type, 1, fp ) )
	    x = y ;
	writeitem( &x, type, 1, stdout ) ;
    }

    else if ( iststr( opstr, "delete" ) ) {

	if ( readitem( &x, type, 1, fp ) && readitem( &y, type, 1, fp ) ) {
	    do {
		writeitem( &x, type, 1, stdout ) ;
		x = y ;
	    } while ( readitem( &y, type, 1, fp ) ) ;
	}
    }

    else {
	fprintf( stderr, "edwave: unknown operation [%s]\n", opstr ) ;
	exit( 1 ) ;
    }

    fclose( fp ) ;
}