diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/convert.c	Fri May 20 15:19:45 2011 +0100
@@ -0,0 +1,113 @@
+/*
+ convert.c
+-----------
+Convert <number><units> to new units given by the "to" option.
+<units> are:
+	p       sample points
+	s       time in seconds
+	ms      time in millisecs
+	Hz      frequency in Hz
+	kHz     frequency in kHz
+
+Time in seconds or miliseconds is converted to and from sample points using
+the given samplerate. Time (in seconds, miliseconds or samples) is converted
+to and from frequency by reciprocal.
+
+Example:
+
+Convert 100 samples to time in ms, and the 10ms to a number of samples, both
+at the default sample-rate of 20kHz. Then at a sample-rate of 10kHz print
+the number of samples in 32ms.
+
+    convert to=ms 100p to=p 10ms samplerate=10kHz to=p 32ms
+
+Convert 0.1 seconds to sample points.
+
+    convert -topoints 0.1sec
+*/
+
+#include <stdio.h>
+#include <math.h>
+#include "options.h"
+#include "units.h"
+#include "strmatch.h"
+
+char applic[] = "Convert units: frequency, time, and sample numbers." ;
+char usage[]  = "convert [options] arguments " ;
+
+static char *helpstr, *debugstr, *sampstr, *tostr ;
+
+static Options option[] = {
+    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                          , DEBUG   },
+    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"              , DEBUG   },
+    {   "samplerate",   "20kHz"     ,  &sampstr     ,   "samplerate "                   , VAL     },
+    {   "to"        ,   "points"    ,  &tostr       ,   "convert to <units>"            , VAL     },
+   ( char * ) 0 } ;
+
+
+int  samplerate ;
+
+main(argc, argv)
+int   argc ;
+char *argv[] ;
+{
+    int  i, helpunits() ;
+
+    i = argc ;
+    while ( i > 0 ) {
+
+	if ( i == argc )        /* first time, to account for prog name */
+	    i = getopts( option, i, &argv[argc-i] ) ;
+	else                    /* subsequent times */
+	    i = getopts( option, i+1, &argv[argc-i-1] ) ;
+	if ( !isoff( helpstr ) )
+	    helpopts2( helpstr, argv[0], applic, usage, option, helpunits ) ;
+
+	samplerate = to_Hz( sampstr, 0 ) ;
+
+	for(  ; i > 0 && !isopt( argv[argc-i] ) ; --i )
+	    convert( argv[argc-i] ) ;
+    }
+}
+
+
+convert( s )
+char *s ;
+{
+    switch ( listindex( units, tostr ) ) {
+	case (-2) : fprintf(stderr,"ambiguous units [%s]\n", tostr ) ;
+		    exit( 1 ) ;
+	case (-1) : fprintf(stderr,"unknown units [%s]\n", tostr ) ;
+		    exit( 1 ) ;
+	case   0  : /* to points */
+		    printf( "%dp\n"   , (int)to_p( s, samplerate  ) ) ;
+		    break ;
+	case   1  : /* to seconds */
+		    printf( "%.3fs\n"   , to_s( s, samplerate  ) ) ;
+		    break ;
+	case   2  : /* to ms */
+		    printf( "%.3fms\n"  , to_ms( s, samplerate ) ) ;
+		    break ;
+	case   3  : /* to Hz */
+		    printf( "%.3fHz\n"  , to_Hz( s, samplerate   ) ) ;
+		    break ;
+	case   4  : /* to kHz */
+		    printf( "%.3fkHz\n" , to_kHz( s, samplerate  ) ) ;
+		    break ;
+	case   5  : /* to ERB */
+		    printf( "%.3ferb\n" , to_erb( s ) ) ;
+		    break ;
+    }
+}
+
+
+helpunits()
+{
+    fprintf(stderr,"\nunits: \n");
+    fprintf(stderr,"            points      sample points     \n");
+    fprintf(stderr,"            seconds     time in seconds   \n");
+    fprintf(stderr,"            ms          time in millisecs \n");
+    fprintf(stderr,"            Hz          frequency in Hz   \n");
+    fprintf(stderr,"            kHz         frequency in kHz  \n");
+    exit( 1 ) ;
+}