/* $Id: seutil.h,v 1.1 1998/10/05 14:49:29 schwarz Exp $ include/seutil.h 1.10.1998 Diemo Schwarz Definition of utility macros for the spectral envelope library libspecenv. $Log: seutil.h,v $ Revision 1.1 1998/10/05 14:49:29 schwarz Split up specenv.h into smaller include files seclasses.h, sefiles.h, separa.h, seutil.h to facilitate selective including and editing. New separate data structure seSpecEnv for spectral envelopes, which is no longer part of the estimation parameter structures. */ #ifndef _SEUTIL_H_ #define _SEUTIL_H_ #ifdef __cplusplus extern "C" { #endif #include #include #include #include /* // DATA GROUP: BASIC TYPE DEFINITIONS */ /* (todo: --> formesutil.lib) */ /*DOC: A boolean type. Danger: bool seems to be a C++ standard type of pointer size (i.e. on alphas 8 bytes to store 1 bit!) and true and false are reserved words. --> we can't define enum { false, true } bool, but will define Boolean and reuse false and true from C++. */ typedef enum { seFalse, seTrue } Boolean; #ifndef false #define false seFalse #endif #ifndef true #define true seTrue #endif /*DOC: Status type to be returned by all se-Functions */ typedef enum { seError, seOK } seReturn; #define seReturnFromBool(b) ((b) ? seOK : seError) #define seReturnToBool(b) ((b) == seOK ? true : false) /* pseudo-prototypes for cocoon, so that macros will appear in the doc */ #if COCOONSEYESONLY /*DOC: Convert boolean to status type */ seReturn seReturnFromBool (Boolean boo); /*DOC: Convert status type to boolean */ Boolean seReturnToBool (seReturn status); #endif /*DOC: Type alias for float to improve readability of the code */ typedef float Frequency; /*DOC: Type alias for float to improve readability of the code */ typedef float Amplitude; /*DOC: Type alias for float to improve readability of the code */ typedef float Phase; /*DOC: Type alias for float to improve readability of the code */ typedef double Time; /* // DATA GROUP: ERROR and MESSAGE HANDLING */ /*DOC: Error behaviour flags */ typedef enum { seEbNone = 0, seEbAbort = 1, seEbLog = 2, seEbPrint = 4, seEbNorm = seEbLog | seEbPrint, seEbFull = seEbAbort | seEbLog | seEbPrint /* default */ } seErrorBehaviour; /*DOC: seMessageLevel specifies the threshold for messages to be printed. Any db call with dblev above the current debug level (set with seSetMessageLevel) will not print anything. (This is the (only) exception of the enum member naming convention, for the sake of shortness.) */ typedef enum { db0, dbQuiet = db0, /* nothing */ db1, dbError = db1, /* error messages */ db2, dbWarn = db2, /* warning messages */ db3, dbMsg = db3, /* important messages (default) */ db4, dbInfo = db4, /* informational messages */ db5, dbVerb = db5, /* verbose information */ db6, /* light debugging */ db7, /* normal debugging */ db8, /* heavy debugging */ db9, /* you must be desperate */ dbNum /* number of message levels, MUST be last one */ } seMessageLevel; #ifdef DEBUG #define seDefaultMessageLevel db7 #else #define seDefaultMessageLevel dbInfo #endif extern int seErrnoSave; /* // FUNCTION GROUP: ERROR and MESSAGE HANDLING */ /*DOC: Initialise spectral envelope library */ seReturn seInit (); /*DOC: Deinitialise spectral envelope library */ seReturn seDeInit (); /*DOC: Set error flag */ seReturn seSetError (seReturn stat, char *file, int line); /*DOC: Return last error status */ seReturn seGetError (); /*DOC: Set error log file */ seReturn seSetErrorLogFile (char *filename); /*DOC: Set error behaviour */ seReturn seSetErrorBehaviour (seErrorBehaviour flags); /*DOC: Get error behaviour */ seErrorBehaviour seGetErrorBehaviour (); /*DOC: Set reporting level */ seReturn seSetMessageLevel (seMessageLevel msglev); /*DOC: Debug output. (Note that db calls will vanish when compiling for release version!) */ void db (seMessageLevel dblev, char *format, ...); /*DOC: Print message, according to message level. */ void sePrint (seMessageLevel dblev, char *format, ...); /*DOC: Print error message, file and line must have been set by seSetError before. f1 is the detailed message for the log file, f2 the brief message for stderr, both are used as formats for printf with the same arguments */ void seMessage (seMessageLevel dblev, char *f1, char *f2, ...); #define seClearError() seSetError (seOK, "", -1) #define seErrorReturn(msg, arg) seErrorReturn2(msg, msg, arg) /* msg1 is detailed error message for log file, msg2 is for stderr */ #define seErrorReturn2(msg1, msg2, arg) \ { \ seErrnoSave = errno; \ seSetError (seError, __FILE__, __LINE__); \ seMessage (dbError, msg1 "\n", msg2 "\n", arg); \ if (seGetErrorBehaviour () & seEbAbort) \ abort (); \ else \ return (seError); \ } #define seCheckStatus(msg, arg) \ { \ if (seGetError () == seError) \ seErrorReturn2 ("check status: " msg, msg, arg); \ } #define seCheck(action, msg, arg) seRequire ((action) == seOK, msg, arg) #define seRequire(action, message, arg) \ { /* in braces to avoid dangling-else problem */ \ if (!(action)) \ seErrorReturn2 ("Requirement '" #action "' failed:\n" message, \ message, arg); \ } /* pseudo-prototypes for cocoon, so that macros will appear in the doc */ #if COCOONSEYESONLY /*DOC: Clear error flag */ seReturn seClearError (); /*DOC: Return from function with status seError, printing error message msg with printf argument arg */ void seErrorReturn (char *msg, ANYTYPE arg); /*DOC: Return from function with status seError, printing error messages msg1 and msg2 with printf argument arg. [in] msg1 is detailed error message for log file, msg2 is for stderr */ void seErrorReturn2 (char *msg1, char *msg2, ANYTYPE arg); /*DOC: If error flag is set, return from function with status seError, printing error message msg with printf argument arg */ void seCheckStatus (char *msg, ANYTYPE arg); /*DOC: Evaluate expression action, if it returns false, return from function with status seError, printing error message message with printf argument arg */ void seRequire (Boolean action, char *message, ANYTYPE arg); /*DOC: Evaluate expression action, if it doesn't returns seOK, return from function with status seError, printing error message message with printf argument arg */ void seCheck (Boolean action, char *msg, ANYTYPE arg); #endif /* // FUNCTION GROUP: UTILITY MACROS */ #define sectosamp(sec, sr) ((int) ((sec) * (sr))) #define samptosec(samp, sr) ((samp) / (sr)) /* allocations */ #define NEW(type) ((type *) malloc (sizeof (type))) #define New(obj, type) \ { \ if (!((obj) = NEW (type))) \ seErrorReturn2 ("New: not enough memory for allocation of " #type " " \ #obj " (%d bytes)", "out of memory", sizeof(type)); \ } #define NEWARR(type, n) ((type *) malloc (sizeof (type) * (n))) #define NewArray(arr, type, num) \ { \ if (!((arr) = NEWARR (type, num))) \ seErrorReturn2 ("NewArray: not enough memory for allocation of " \ #type " [%d] array " #arr, "out of memory", num); \ } /* there's no standard place for min/max, so we define them once more */ #ifndef min #define min(a, b) ((a) < (b) ? (a) : (b)) #endif #ifndef max #define max(a, b) ((a) > (b) ? (a) : (b)) #endif #define limit(a, x, b) (min (max ((a), (x)), (b))) /* pseudo-prototypes for cocoon, so that macros will appear in the doc */ #if COCOONSEYESONLY /*DOC: Convert seconds to number of samples according to sample rate sr. */ int sectosamp (Time sec, float sr); /*DOC: Convert number of samples to seconds according to sample rate sr. */ Time samptosec (int samp, float sr); /*DOC: Allocation of one object of type type with check and seErrorReturn. [out] obj pointer to newly created object */ void New (type *obj, TYPE type); /*DOC: Allocation of num objects of type type with check and seErrorReturn. [out] arr pointer to newly created objects */ void NewArray (type *arr, TYPE type, int num); /*DOC: (There's no standard place for min/max, so we define them once more.) Min of two expressions (possibly evaluated twice!). */ ANYTYPE min (ANYTYPE a, ANYTYPE b); /*DOC: (There's no standard place for min/max, so we define them once more.) Max of two expressions (possibly evaluated twice!). */ ANYTYPE max (ANYTYPE a, ANYTYPE b); /*DOC: Limit expression x at to be within range a, b. */ ANYTYPE limit (ANYTYPE a, ANYTYPE x, ANYTYPE b); #endif /* // FUNCTION GROUP: UTILITY FUNCTIONS */ /*DOC: Integer to string [return] static string memory */ char *itoa (int i); /*DOC: Float to string [return] static string memory */ char *ftoa (float f); /*DOC: superseded by SdifStringToNV!!! changes str! */ char *strtoNV (char *string_with_spaces); /*DOC: Return output of uname() system function. */ char *machine (int flags); /*DOC: Return index of str in list of strings of length num, num if not found */ int seSearchStringInList (const char *str, const char **list, int num); /*DOC: superseded by PmLoadBreakPointOrConst!!! */ seReturn seScanNumberOrBpf (/*in */ const char *str, /*out*/ float *num, PmBreakPoint *bpf); /* // FUNCTION GROUP: TIME FUNCTIONS */ /*DOC: Initialise time printing */ void seBeginTime (char *msg); /*DOC: Print and compute relation between calculation time and real time. */ void sePrintTimeNum (float out_time, int number); /*DOC: Deinitialise time printing */ void seEndTime (); #ifdef __cplusplus } #endif #endif /* _SEUTIL_H_ */