frame.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /****************************************************************************
  2. *
  3. * Programs for processing sound files in raw- or WAV-format.
  4. * -- Useful functions for parsing command line options and
  5. * issuing errors, warnings, and chit chat.
  6. *
  7. * Name: frame.h
  8. * Version: see frame.c
  9. * Author: Mark Roberts <mark@manumark.de>
  10. *
  11. ****************************************************************************/
  12. /****************************************************************************
  13. * These are useful functions that all DSP programs might find handy
  14. ****************************************************************************/
  15. /* fileswitch for parseargs:
  16. The following are masks for several different ways of opening files.
  17. --------------------------------------------------------------------
  18. Bit 0: Open infile?
  19. Bit 1: Open infile as binary (as opposed to text)
  20. Bit 2: Open outfile?
  21. Bit 3: Open outfile as binary (as opposed to text)
  22. Bit 4: Do not complain about too many file arguments
  23. Bit 5: Open one file for input AND output, binary.
  24. */
  25. #define INTEXT (1+0)
  26. #define INBIN (1+2)
  27. #define OUTTEXT (4)
  28. #define OUTBIN (4+8)
  29. #define NOFILES (0)
  30. #define NOCOMPLAIN (16)
  31. #define IOBIN (32)
  32. #ifndef FALSE
  33. #define FALSE (0==1)
  34. #define TRUE (0==0)
  35. #endif
  36. extern int samplefrequency;
  37. extern unsigned short samplewidth;
  38. extern unsigned short channels;
  39. extern int wavout; /* TRUE iff out file is .WAV file */
  40. extern int iswav; /* TRUE iff in file was found to be a .WAV file */
  41. extern FILE *in, *out;
  42. extern char *infilename, *outfilename;
  43. extern int verboselevel;
  44. extern char *version; /* String to be issued as version string. Should
  45. be set by application. */
  46. extern char *usage; /* String to be issued as usage string. Should be
  47. set by application. */
  48. #define DEFAULTFREQ 44100
  49. #define BUFFSIZE 50000 /* How many samples to read in one go (preferred) */
  50. #define MINBUFFSIZE 5000 /* How many samples to read in one go (minimum) */
  51. /*************************************************
  52. * Types of errors handled by argerrornum() *
  53. *************************************************/
  54. typedef enum
  55. {
  56. ME_NOINT,
  57. ME_NODOUBLE,
  58. ME_NOTIME,
  59. ME_NOVOL,
  60. ME_NOSWITCH,
  61. ME_TOOMANYFILES,
  62. ME_HEADERONTEXTFILE,
  63. ME_NOINFILE,
  64. ME_NOOUTFILE,
  65. ME_NOIOFILE,
  66. ME_NOSTDIN,
  67. ME_NOSTDOUT,
  68. ME_NOSTDIO,
  69. ME_NOTENOUGHFILES,
  70. ME_THISCANTHAPPEN
  71. } Errornum;
  72. /* -----------------------------------------------------------------------
  73. Create memory and copy 'string', returning a pointer to the copy.
  74. NULL is returned if malloc fails.
  75. -----------------------------------------------------------------------*/
  76. extern char *malloccopy( char *string);
  77. /* -----------------------------------------------------------------------
  78. Start the stopwatch and make sure the user is informed at end of program.
  79. -----------------------------------------------------------------------*/
  80. extern void startstopwatch(void);
  81. /* -----------------------------------------------------------------------
  82. Writes the number of samples to result that are yet to be read from anyin.
  83. I.e. the number of remaining bytes is divided by the number of bytes per
  84. sample value, but not by the number of channels.
  85. Return values are TRUE on success, FALSE on failure.
  86. -----------------------------------------------------------------------*/
  87. extern int getremainingfilelength( FILE *anyin, long *result);
  88. /* -----------------------------------------------------------------------
  89. Read a .pk-header from 'anyin' and printf the entries.
  90. -----------------------------------------------------------------------*/
  91. void readpkheader( FILE *anyin);
  92. /* -----------------------------------------------------------------------
  93. Read a .WAV header from 'anyin'.
  94. If it is recognised, the data is used.
  95. Otherwise, we assume it's PCM-data and ignore the header.
  96. The global variable 'iswav' is set on success, otherwise cleared.
  97. -----------------------------------------------------------------------*/
  98. extern void readwavheader( FILE *anyin);
  99. /* -----------------------------------------------------------------------
  100. Write a .WAV header to 'out'.
  101. The filepointer is placed at the end of 'out' before operation.
  102. This should be called before any data is
  103. written, and again, when ALL the data has been written.
  104. First time, this positions the file pointer correctly; second time, the
  105. missing data can be inserted that wasn't known the first time round.
  106. -----------------------------------------------------------------------*/
  107. extern void makewavheader( void);
  108. /* --------------------------------------------------------------------
  109. Tests the character 'coal' for being a command line option character,
  110. momentarrily '/' or '-'.
  111. -------------------------------------------------------------------- */
  112. extern int isoptionchar (char coal);
  113. /* -----------------------------------------------------------------------
  114. Reads through the arguments on the lookout for an option starting
  115. with 'string'. The rest of the option is read as a time and passed
  116. to *result, where the result is meant to mean 'number of samples' in
  117. that time.
  118. On failure, *result is unchanged.
  119. return value is TRUE on success, FALSE otherwise.
  120. -----------------------------------------------------------------------*/
  121. extern int parsetimearg( int argcount, char *args[], char *string,
  122. int *result);
  123. /* -----------------------------------------------------------------------
  124. The string argument is read as a time and passed to *result, where
  125. the result is meant to mean 'number of samples' in that time. On
  126. failure, *result is unchanged.
  127. return value is TRUE on success, FALSE otherwise.
  128. -----------------------------------------------------------------------*/
  129. int parsetime(char *string, int *result);
  130. /* -----------------------------------------------------------------------
  131. The string argument is read as a frequency and passed
  132. to *result, where the result is meant to mean 'number of samples' in
  133. one cycle of that frequency.
  134. On failure, *result is unchanged.
  135. return value is TRUE on success, FALSE otherwise.
  136. -----------------------------------------------------------------------*/
  137. int parsefreq(char *string, double *result);
  138. /* --------------------------------------------------------------------
  139. Reads through the arguments on the lookout for a switch -'string'.
  140. return value is TRUE if one exists, FALSE otherwise.
  141. If characters remain after the switch, a fatal error is issued.
  142. -------------------------------------------------------------------- */
  143. extern int parseswitcharg( int argcount, char *args[], char *string);
  144. /* --------------------------------------------------------------------
  145. Reads through the arguments on the lookout for an option starting
  146. with 'string'. The rest of the option is read as an integer and
  147. passed to &result.
  148. On failure, &result is unchanged.
  149. return value is TRUE on success, FALSE otherwise.
  150. -------------------------------------------------------------------- */
  151. extern int parseintarg( int argcount, char *args[], char *string,
  152. int *result);
  153. /* --------------------------------------------------------------------
  154. Reads through the arguments on the lookout for a filename, i.e. anything
  155. that does not start with the optionchar. The filename is copied to
  156. newly allocated memory, a pointer to which is returned.
  157. The argument is marked as used. Therefore repeated use of this function
  158. will yield a complete list of filenames on the commandline.
  159. If malloc() fails, the function does not return.
  160. -------------------------------------------------------------------- */
  161. extern char *parsefilearg( int argcount, char *args[]);
  162. /* --------------------------------------------------------------------
  163. Reads through the arguments on the lookout for an option starting
  164. with 'string'. The rest of the option is read as a double and
  165. passed to *result.
  166. On failure, *result is unchanged.
  167. return value is TRUE on success, FALSE otherwise.
  168. -------------------------------------------------------------------- */
  169. extern int parsedoublearg( int argcount, char *args[], char *string,
  170. double *result);
  171. /* --------------------------------------------------------------------
  172. Reads through the arguments on the lookout for an option starting
  173. with 'string'. The rest of the option is read as a volume, i.e.
  174. absolute, percent or db. The result is passed to *result.
  175. On failure, *result is unchanged.
  176. -------------------------------------------------------------------- */
  177. extern int parsevolarg( int argcount, char *args[], char *string,
  178. double *result);
  179. /* --------------------------------------------------------------------
  180. Reads the specified string and interprets it as a volume. The string
  181. would be of the form 1.8 or 180% or 5db.
  182. On success, the return value is the relative volume, i.e. 1.8
  183. On failure, -1 is returned.
  184. -------------------------------------------------------------------- */
  185. extern int parsevolume(char *s, double *result);
  186. /* --------------------------------------------------------------------
  187. Reads through the arguments on the lookout for a switch -'string'.
  188. return value is TRUE if one exists, FALSE otherwise.
  189. If characters remain after the switch, a fatal error is issued.
  190. -------------------------------------------------------------------- */
  191. extern int parseswitch( char *found, char *wanted);
  192. /* --------------------------------------------------------------------
  193. Reports an error due to parsing the string 's' encountered on the
  194. command line.
  195. -------------------------------------------------------------------- */
  196. extern void argerror(char *s);
  197. /* --------------------------------------------------------------------
  198. Reports an error due to parsing the string 's' encountered on the
  199. command line. 'code' indicates the type of error.
  200. -------------------------------------------------------------------- */
  201. extern void argerrornum(char *s, Errornum code);
  202. /* --------------------------------------------------------------------
  203. Reports an error due to parsing the string 's' encountered on the
  204. command line. 'message' explains the type of error.
  205. -------------------------------------------------------------------- */
  206. extern void argerrortxt(char *s, char *message);
  207. /* --------------------------------------------------------------------
  208. Check for any remaining arguments and complain about their existence.
  209. If arguments are found, this function does not return.
  210. -------------------------------------------------------------------- */
  211. extern void checknoargs( int argcount, char *args[]);
  212. /* --------------------------------------------------------------------
  213. Parses the command line arguments as represented by the function
  214. arguments. Sets the global variables 'in', 'out', 'samplefrequency'
  215. and 'samplewidth' accordingly.
  216. According to 'fileswitch', in and out files are opened or not. See
  217. above for an explanation of 'fileswitch'.
  218. -------------------------------------------------------------------- */
  219. extern void parseargs( int argcount, char *args[], int fileswitch);
  220. /* --------------------------------------------------------------------
  221. Returns the index 'i' of the first argument that IS an option, and
  222. which begins with the label 's'. If there is none, -1.
  223. We also mark that option as done with, i.e. we cross it out.
  224. -------------------------------------------------------------------- */
  225. extern int findoption( int argcount, char *args[], char *s);
  226. /* --------------------------------------------------------------------
  227. Finishes off the .WAV header (if any) and exits correctly and formerly.
  228. -------------------------------------------------------------------- */
  229. extern int myexit (int value);
  230. /* --------------------------------------------------------------------
  231. Reads the stated input file bufferwise, calls the function 'work'
  232. with the proper values, and writes the result to the stated output file.
  233. Return value: TRUE on success, FALSE otherwise.
  234. -------------------------------------------------------------------- */
  235. extern int workloop( FILE *theinfile, FILE *theoutfile,
  236. int (*work)( short *buffer, int length) );
  237. /* --------------------------------------------------------------------
  238. Five functions for printing to stderr. Depending on the level of verbose,
  239. output may be supressed. fatalerror() is like error() but does not return.
  240. fatalperror() is like the standard function perror() but does not return.
  241. -------------------------------------------------------------------- */
  242. extern int chat( const char *format, ...);
  243. extern int inform( const char *format, ...);
  244. extern int error( const char *format, ...);
  245. extern void fatalerror( const char *format, ...);
  246. extern void fatalperror( const char *string);
  247. /* --------------------------------------------------------------------
  248. And one functions for printing to stdout.
  249. -------------------------------------------------------------------- */
  250. extern int say( const char *format, ...);
  251. /* --------------------------------------------------------------------
  252. Allocate memory for it and return a pointer to a string made up of
  253. the two argument strings.
  254. -------------------------------------------------------------------- */
  255. extern char *mallocconcat( char *one, char *two);
  256. /* --------------------------------------------------------------------
  257. Convert a sample value to decibel.
  258. -------------------------------------------------------------------- */
  259. extern double double2db( double value);
  260. /* --------------------------------------------------------------------
  261. Read 'size' samples from file 'in' and lose them.
  262. -------------------------------------------------------------------- */
  263. extern void readawaysamples( FILE *in, size_t size);