sfsys.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c) 1983-2013 Martin Atkins, Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * Sound Filing System - resident library interface header
  23. *
  24. * Copyright M. C. Atkins, 1987, 1988, 1989, 1990, RWD revisions 1997,1998,199,2000,2001
  25. * All Rights Reserved.
  26. *
  27. * Portable version for WindowsNT, DOS and Unix
  28. */
  29. #ifndef __CDP_SFSYS_INCLUDED__
  30. #define __CDP_SFSYS_INCLUDED__
  31. /*
  32. * $Log: sfsys.h%v $
  33. * Revision 4.1 1994/10/31 15:19:34 martin
  34. * Starting with RCS
  35. *
  36. */
  37. /*
  38. * This version of sfsys.h was introduced with version 2.33 of sfsys.prg
  39. *
  40. * ver 3.04 31/ 1/93 MCA - changes introducing license control
  41. * ver 3.05 25/ 5/93 MCA - add sfdata
  42. */
  43. /* RWD: SFSYSEX version (c) CDP 2000,2001 */
  44. /* NB this now defines only the snd~ level routines.
  45. all (deprecated) sf~ routines now in sffuncs.h
  46. MAY BE REMOVED ALTOGETHER IN TIME!
  47. */
  48. /* RWD March 2008 added MC_SURR_5_0 */
  49. /* RWD Aug 2009 sfsys64, restored this, needed by dirsf */
  50. /* NB: sfsys64: sf_direct.length now unsigned, so will report a 4GB file size */
  51. /* Feb 2010: discover if we are 64 bit */
  52. #ifdef __GNUC__
  53. # ifdef __LP64__
  54. # define CDPLONG64
  55. # endif
  56. #endif
  57. #ifdef ENABLE_PVX
  58. #include "wavdefs.h"
  59. #endif
  60. extern int _sfverno;
  61. #define sfverno() (_sfverno) /* sfsys version number */
  62. /* SFSYSEX: new structures; may be extended even further... */
  63. typedef enum {wt_wave,wt_analysis,wt_formant,wt_transposition,wt_pitch,wt_binenv} wavetype;
  64. typedef enum {SHORT8,SHORT16,FLOAT32,INT_32,INT2424,INT2432,INT2024,INT_MASKED} sampletype;
  65. /* very likely we will support .au and .snd too, and maybe even raw */
  66. #ifdef ENABLE_PVX
  67. // could have some knock-on effects. Alternative may be just to flag pvx file as WAVE_EX
  68. typedef enum wt_format {FMT_UNKNOWN = -1,WAVE,WAVE_EX,AIFF,AIFC,PVOCEX} fileformat;
  69. #else
  70. typedef enum wt_format {FMT_UNKNOWN = -1,WAVE,WAVE_EX,AIFF,AIFC} fileformat;
  71. #endif
  72. typedef enum {CDP_CREATE_NORMAL,CDP_CREATE_TEMPORARY,CDP_CREATE_RDONLY} cdp_create_mode;
  73. /* MC_WAVE_EX is a temporary one to cover abstruse infile formats! */
  74. typedef enum { STDWAVE,MC_STD,MC_MONO,MC_STEREO,MC_QUAD,MC_LCRS,MC_BFMT,MC_DOLBY_5_1,MC_SURR_5_0,MC_SURR_6_1,MC_SURR_7_1,MC_CUBE,MC_WAVE_EX } channelformat;
  75. /* the main format structure, to be used for all new programs! */
  76. /* this may expand to include new things, possibly PVOC-EX too */
  77. /* NB any new fields should be added at the bottom ONLY!. Best of all is to define a new
  78. * structure, with SFPROPS as the first element
  79. */
  80. typedef struct sfprops
  81. {
  82. int srate;
  83. int chans;
  84. wavetype type;
  85. sampletype samptype;
  86. fileformat format;
  87. channelformat chformat;
  88. int origsize; /*RWD 3:2000 needs UPDATING in pvoc to register all new sample types */
  89. int origrate;
  90. int origchans; /* pitch, formant,transpos only */
  91. int specenvcnt; /* formants only */
  92. float arate;
  93. int winlen; /*aka Mlen*/
  94. int decfac; /* aka Dfac*/
  95. /*RWD.6.99 for envelope files*/
  96. float window_size;
  97. } SFPROPS;
  98. /* SFSYSEX PEAK chunk support: use CHPEAK to avoid likely name clashes (e.g. Release 4!) */
  99. typedef struct chpeak {
  100. float value; /* absolute value: 0dBFS = 1.0 */
  101. unsigned int position; /* in (muti-channel)sample frames */
  102. } CHPEAK;
  103. /*
  104. * This is the Sound Filing System initialisation routine, which must be
  105. * called before doing ANYTHING else with the SFSystem.
  106. * It returns 0 on success, or 1 on failure.
  107. */
  108. /* the name is not used! */
  109. extern int sflinit(const char *progname);
  110. /*
  111. * Print a description of the most-recent error on stderr.
  112. * calling this only makes sense after an error-return from
  113. * a SFSystem routine.
  114. */
  115. extern void sfperror(const char *msg);
  116. /*
  117. * return the string that would be printed by sfperror.
  118. */
  119. extern char * sferrstr(void);
  120. /*
  121. * return the numeric error indication.
  122. */
  123. extern int sferrno(void); //RWD Oct 2025 addewd void
  124. extern int sfinit(void); /* deprecated */
  125. #define sfexit exit /* deprecated */
  126. /*
  127. * the access routine, etc
  128. */
  129. extern void sffinish(void);
  130. /* PEAK support, all sample formats:
  131. * ability to write full, minimum or PEAK-only header
  132. */
  133. /* header-size flags*/
  134. #define SFILE_CDP (2)
  135. #define SFILE_PEAKONLY (1)
  136. #define SFILE_MINIMUM (0)
  137. /* support opening read-only files - e.g from CD, also multiple opens */
  138. /* All new progs should use RDONLY wherever possible, when opening existing files */
  139. #define CDP_OPEN_RDONLY (0)
  140. #define CDP_OPEN_RDWR (1)
  141. /* return 0 for true */
  142. int sndreadpeaks(int sfd,int channels,CHPEAK peakdata[],int *peaktime);
  143. int sndputpeaks(int sfd,int channels,const CHPEAK peakdata[]);
  144. /*
  145. * Next routines for buffered access to files
  146. */
  147. int snd_getchanformat(int sfd, channelformat *chformat);
  148. const char * snd_getfilename(int ifd);
  149. int sndgetchanmask(int sfd);
  150. extern int sndopen(const char *name);
  151. /* sfsysEx extended version - supports all formats.
  152. * requires use of all snd~Ex functions */
  153. extern int sndopenEx(const char *name, int auto_scale,int access);
  154. extern int sndcreat(const char *name, int size, int stype);
  155. /* sfsysEx extended version*/
  156. extern int sndcreat_formatted(const char *fn, int size, int stype,
  157. int channels,int srate,cdp_create_mode mode); /*SFSYS98*/
  158. /*need this for WAVE_EX,etc*/
  159. int
  160. sndcreat_ex(const char *name, int size,SFPROPS *props,int min_header,cdp_create_mode mode);
  161. /* read sfile props into new structure, all in one go */
  162. extern int snd_headread(int fd,SFPROPS *props);
  163. extern int sndclose(int sfd);
  164. extern int sndsize(int sfd);
  165. extern int sndseek(int sfd, int dist, int whence);
  166. extern int sndtell(int sfd);
  167. extern int sndsetbuf(int sfd, char *buf, int bufsize);
  168. extern int snd_makepath(char path[],const char* sfname);
  169. /*RWD OCT97: as above, for sndfiles*/
  170. extern int sndgetwordsize(int sfd);
  171. /* set sndfile for deletion */
  172. extern int sndunlink(int sndfd);
  173. /*recognise shortcuts in WIN32 - ~should~ be portable to unix/mac alias
  174. */
  175. int snd_is_shortcut(int sfd);
  176. /*#endif*/
  177. /* calls sfformat internally */
  178. int snd_fileformat(int sfd, fileformat *pfmt);
  179. /*
  180. * float-oriented operations : DEPRECATED; see below for extended versions
  181. */
  182. extern int fgetfloat(float *fp, int sfd);
  183. extern int fputfloat(float *fp, int sfd);
  184. extern int fgetfbuf(float *fp, int count, int sfd);
  185. extern int fputfbuf(float *fp, int count, int sfd);
  186. /*
  187. * short-oriented operations : DEPRECATED;
  188. */
  189. extern int fgetshort(short *sp, int sfd);
  190. extern int fputshort(short *sp, int sfd);
  191. extern int fgetsbuf(short *sp, int count, int sfd);
  192. extern int fputsbuf(short *sp, int count, int sfd);
  193. extern int sndgetprop(int sfd, char *propname, char *dest, int lim);
  194. extern int sndputprop(int sfd, char *propname, char *src, int size);
  195. extern int sndrmprop(int sfd, char *propname);
  196. extern int snddirprop(int sfd, int (*func)(char *propname, int propsize));
  197. /* sfsysEx extended versions, supporting all formats */
  198. /* set expect_floats if reading from a type-1 32bit file known to be floats */
  199. /* it is best not to mix: e.g don;t use fgetfloatEx with sndtell() */
  200. extern int fgetfloatEx(float *fp, int sfd,int expect_floats);
  201. extern int fgetfbufEx(float *fp, int count, int sfd,int expect_floats);
  202. extern int fputfloatEx(float *fp, int sfd);
  203. extern int fputfbufEx(float *fp, int count, int sfd);
  204. extern int sndcloseEx(int sfd);
  205. extern int fgetshortEx(short *sp, int sfd,int expect_floats);
  206. extern int fgetsbufEx(short *sp, int count, int sfd,int expect_floats);
  207. extern int fputshortEx(short *sp, int sfd);
  208. extern int fputsbufEx(short *sp, int count, int sfd);
  209. extern int sndseekEx(int sfd, int dist, int whence); /*uses buffered sf_routines*/
  210. extern int sndsizeEx(int sfd);
  211. extern int sndtellEx(int sfd);
  212. extern int sf_getpvxfno(int sfd); /* RWD: pvsys has separate list of files, so CDP file ids won't apply directly. */
  213. /*
  214. * definitions for use with sfdir
  215. */
  216. /*
  217. * values for the flags field
  218. */
  219. #define SFFLG_EMPTY (1) /* the SFile is empty */
  220. #define SFFLG_LAST (2) /* the SFile is the last on the disk */
  221. #define SFFLG_SAMPLE (4) /* sample type was set (only sfdir) */
  222. #define SFFLG_ISOPEN (8) /* file is open (internal use only) */
  223. #define SFFLG_TODELETE (16) /* file to be deleted (ditto) */
  224. #define SFFLG_IOERROR (32) /* sfdir only - error reading file information */
  225. /*
  226. * selectors for sfdir
  227. */
  228. #define SFDIR_EMPTY (1) /* call func with empty SFiles */
  229. #define SFDIR_USED (2) /* call func with non-empty SFiles */
  230. #define SFDIR_SAMPLE (4) /* call func with (non-empty) sample SFiles */
  231. #define SFDIR_ALL (3) /* call func with all SFiles */
  232. #define SFDIR_IGPREFIX (16) /* show files irrespective of prefix */
  233. #define SFDIR_FOUND (0) /* function accepted file */
  234. #define SFDIR_NOTFOUND (1) /* no file was accepted */
  235. #define SFDIR_ERROR (-1) /* and error occured during sfdir */
  236. /*
  237. * Other useful constants
  238. */
  239. /* NB in time, these will be REMOVED! */
  240. #define SECSIZE (512) /* bytes/sector on the disk */
  241. #define LOGSECSIZE (9) /* log2(ditto) */
  242. #ifdef SFFUNCS
  243. #define SF_MAGIC (0x15927624) /* value of _sfmagic() */
  244. #define SF_CMAGIC (0x27182835) /* magic number for configuration */
  245. #define SF_UNLKMAGIC (0x46689275) /* magic value for rdiskunlck() */
  246. #endif
  247. #define SF_MAXFILES (1000) /* max no. of SFfiles open at once */
  248. #define MAXSNDFILES (1000) /* max no. of sndfiles open at once */
  249. /* RWD reduced property size, to assist bad unix progs to read soundifles! */
  250. /* also, we are reducing dependency on CDP props, through new file formats */
  251. #define PROPSIZE (800) /* size of the property area in hdrs */
  252. //#ifndef _WINNT_
  253. #ifndef MSC_VER
  254. # ifdef MAXSHORT
  255. # undef MAXSHORT
  256. # endif
  257. # define MAXSHORT (32767.0) /* maxint for shorts (as a float); used EVERYWHERE! */
  258. # ifdef MAXLONG
  259. # undef MAXLONG
  260. # endif
  261. /* NOT USED in CDP */
  262. # define MAXLONG (2147483648.0) /* RWD 18:March 2008, was 214748367 */
  263. # ifdef MAXINT
  264. # undef MAXINT
  265. # endif
  266. # define MAXINT MAXLONG
  267. #endif
  268. /* NB: not used in sfsys or CDP */
  269. #define MAX24BIT (8388607.0)
  270. /*
  271. * sample types
  272. */
  273. #define SAMP_SHORT (0) /* 16-bit short integers */
  274. #define SAMP_FLOAT (1) /* 32-bit (IEEE) floating point */
  275. /* sfsysEx Extended type support */
  276. #define SAMP_BYTE (2) /*recognize 8bit soundfiles*/
  277. #define SAMP_LONG (3) /* four most likely formats*/
  278. #define SAMP_2424 (4)
  279. #define SAMP_2432 (5) /* illegal for AIFF, and probably for WAVE too , but what the...*/
  280. #define SAMP_2024 (6) /* curioisity value: used by Alesis ADAT */
  281. #define SAMP_MASKED (7) /*some weird WAVE_EX format!*/
  282. /*
  283. * Generate the program registration number
  284. */
  285. #define PROGRAM_NUMBER(pn) unsigned long _SF_prognum = (pn)
  286. /*
  287. * Values for sferrno
  288. */
  289. #define EBASEERR (100)
  290. #define ESFNOSPACE (EBASEERR+ 0)
  291. #define ESFNOTOPEN (EBASEERR+ 1)
  292. #define ESFRDERR (EBASEERR+ 2)
  293. #define ESFWRERR (EBASEERR+ 3)
  294. #define ESFNOSFD (EBASEERR+ 4)
  295. #define ESFNOTFOUND (EBASEERR+ 5)
  296. #define ESFNOMEM (EBASEERR+ 6)
  297. #define ESFDUPFNAME (EBASEERR+ 7)
  298. #define ESFBADMAG (EBASEERR+ 8)
  299. #define ESFNOCONFIG (EBASEERR+ 9)
  300. #define ESFNOTINIT (EBASEERR+11)
  301. #define ESFCONSIST (EBASEERR+12)
  302. #define ESFNOSTYPE (EBASEERR+13)
  303. #define ESFBADRATE (EBASEERR+14)
  304. #define ESFBADNCHANS (EBASEERR+15)
  305. #define ESFBADPARAM (EBASEERR+16)
  306. #define ESFNOSEEK (EBASEERR+17)
  307. #define ESFBADSRCDST (EBASEERR+18)
  308. #define ESFLOSEDATA (EBASEERR+19)
  309. #define ESFBADADDR (EBASEERR+20)
  310. #define ESFPTHTOLONG (EBASEERR+21)
  311. #define ESFILLSFNAME (EBASEERR+22)
  312. #define ESFPREFTOLONG (EBASEERR+23)
  313. #define ESFLOCKED (EBASEERR+24)
  314. #define ESFILLREGNAM (EBASEERR+25)
  315. #define ESFNOSTRMR (EBASEERR+26)
  316. #define ESFHARDCONFIG (EBASEERR+27)
  317. #define ESFUNDERRUN (EBASEERR+28)
  318. #define ESFBUFFOPER (EBASEERR+29)
  319. #define ESFSETBUFFER (EBASEERR+30)
  320. #define ESFOVERRUN (EBASEERR+31)
  321. #define ESFNOTPLAYING (EBASEERR+32)
  322. #define ESFPLAYING (EBASEERR+33)
  323. #define ESFUSESDMA (EBASEERR+34)
  324. #define ESFRECING (EBASEERR+35)
  325. #define ESFNOTRECING (EBASEERR+36)
  326. #define ESFREADONLY (EBASEERR+37)
  327. #define ELASTERR (ESFREADONLY)
  328. /*
  329. * Atari things we we will also have to emulate
  330. */
  331. void *Malloc(long size);
  332. void Mfree(void *);
  333. /*CDP98: now a func, so can round negative numbers properly!*/
  334. /* NB in WIN32 sfsys.c: this is implemented in asssembler for great speed! */
  335. //RWD Oct 2025: should no longer be needed as lround() now available across platforms
  336. extern int cdp_round(double val);
  337. /* unix math.h does not include these macros */
  338. #ifndef __cplusplus
  339. #ifndef min
  340. #define min(x,y) ( ((x)>(y)) ? (y) : (x) )
  341. #endif
  342. #ifndef max
  343. #define max(x,y) ( ((x)>(y)) ? (x) : (y) )
  344. #endif
  345. #endif
  346. /*
  347. * Globals internal to the porting library
  348. */
  349. extern int rsferrno;
  350. extern char *rsferrstr;
  351. extern char *props_errstr;
  352. /*RWD: yet to be tested: only for 32bit platforms */
  353. #define IS_DENORMAL(f) (((*(unsigned int*)&f)&0x7f800000)==0)
  354. /* use like this:
  355. if (IS_DENORMAL(x)) x = 0.0f;
  356. */
  357. int sndrewind(int sfd);
  358. # ifdef ENABLE_PVX
  359. /* PVOCEX file access */
  360. /* return 0=false, 1 = true */
  361. int sf_ispvx(int sfd);
  362. /* return -1 for error in fd, id can be >=0 */
  363. int get_pvxfd(int fd,PVOCDATA *pvxdata);
  364. # endif
  365. #endif