pvfileio.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /* pvfileio.c */
  2. /* pvocex format test routines*/
  3. /* Initial version RWD May 2000.
  4. * All rights reserved: work in progress!
  5. * Manifestly not a complete API yet!
  6. * In particular, error returns are kept very simplistic at the moment.
  7. * (and are not even very consistent yet...)
  8. * In time, a full set of error values and messages will be developed.
  9. *
  10. * NB: the RIFF<WAVE> functions only look for, and accept, a PVOCEX format file.
  11. * NB also: if windows.h is included anywhere (should be no need in this file,
  12. * or in pvfileio.h),
  13. * the WAVE_FORMAT~ symbols may need to be #ifndef-ed.
  14. */
  15. /* very simple CUSTOM window chunk:
  16. *
  17. * <PVXW><size><data>
  18. *
  19. * where size as usual gives the size of the data in bytes.
  20. * the size in samples much match dwWinlen (which may not be the same as N (fft length)
  21. * the sample type must be the same as that of the pvoc data itself
  22. * (only floatsams supported so far)
  23. * values must be normalized to peak of 1.0
  24. */
  25. //#ifdef WINDOWS
  26. //#include "stdafx.h"
  27. //#endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <memory.h>
  31. #include <string.h>
  32. #include <fcntl.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #ifdef _WIN32
  36. #include <io.h>
  37. #endif
  38. #ifdef unix
  39. #include <unistd.h>
  40. #define O_BINARY (0)
  41. #define _S_IWRITE S_IWRITE
  42. #define _S_IREAD S_IREAD
  43. #endif
  44. #ifdef _DEBUG
  45. #include <assert.h>
  46. #endif
  47. #include "pvdefs.h"
  48. #include "pvfileio.h"
  49. /* NB probably no good on 64bit platforms */
  50. #define REVDWBYTES(t) ( (((t)&0xff) << 24) | (((t)&0xff00) << 8) | (((t)&0xff0000) >> 8) | (((t)>>24) & 0xff) )
  51. #define REVWBYTES(t) ( (((t)&0xff) << 8) | (((t)>>8) &0xff) )
  52. #define TAG(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) )
  53. #define WAVE_FORMAT_EXTENSIBLE (0xFFFE)
  54. #ifndef WAVE_FORMAT_PCM
  55. #define WAVE_FORMAT_PCM (0x0001)
  56. #endif
  57. #define WAVE_FORMAT_IEEE_FLOAT (0x0003)
  58. const GUID KSDATAFORMAT_SUBTYPE_PVOC = {
  59. 0x8312b9c2,
  60. 0x2e6e,
  61. 0x11d4,
  62. { 0xa8, 0x24, 0xde, 0x5b, 0x96, 0xc3, 0xab, 0x21 }
  63. };
  64. static char *pv_errstr = "";
  65. #define MAXFILES (16)
  66. /* or any desired larger number: will be dynamically allocated one day */
  67. typedef union {
  68. int32_t lsamp;
  69. float fsamp;
  70. unsigned char bytes[4];
  71. } SND_SAMP;
  72. typedef struct pvoc_file {
  73. WAVEFORMATEX fmtdata;
  74. PVOCDATA pvdata;
  75. /* RWD 2022 to help CDP pvocex extension, requires two-stage file creation/update */
  76. int32_t propsoffset;
  77. int32_t needsupdate; //for two-stage creation/update CDP-style
  78. int32_t fmtchunkoffset;
  79. int32_t datachunkoffset;
  80. int32_t nFrames; /* no of frames in file */
  81. int32_t FramePos; /* where we are in file */
  82. int32_t curpos;
  83. int32_t fd;
  84. int32_t to_delete;
  85. int32_t readonly;
  86. int32_t do_byte_reverse;
  87. char *name;
  88. float *customWindow;
  89. } PVOCFILE;
  90. static PVOCFILE *files[MAXFILES];
  91. static int32_t pvoc_writeheader(int32_t ofd);
  92. static int32_t pvoc_readheader(int32_t ifd,WAVEFORMATPVOCEX *pWfpx);
  93. int32_t pvoc_getdatasize_bytes(int32_t fd)
  94. {
  95. int32_t datachunksize, framesize;
  96. if(files[fd]==NULL)
  97. return -1;
  98. if(files[fd]->nFrames == 0)
  99. return 0;
  100. framesize = files[fd]->pvdata.nAnalysisBins * 2 * sizeof(float);
  101. datachunksize = files[fd]->nFrames * framesize * files[fd]->fmtdata.nChannels;
  102. return datachunksize;
  103. }
  104. static int32_t write_guid(int32_t fd,int32_t byterev,const GUID *pGuid)
  105. {
  106. int32_t written;
  107. #ifdef _DEBUG
  108. assert(fd >= 0);
  109. assert(pGuid);
  110. #endif
  111. if(byterev){
  112. GUID guid;
  113. guid.Data1 = REVDWBYTES(pGuid->Data1);
  114. guid.Data2 = REVWBYTES(pGuid->Data2);
  115. guid.Data3 = REVWBYTES(pGuid->Data3);
  116. memcpy((char *) (guid.Data4),(char *) (pGuid->Data4),8);
  117. written = write(fd,(char *) &guid,sizeof(GUID));
  118. }
  119. else
  120. written = write(fd,(char *) pGuid,sizeof(GUID));
  121. return written;
  122. }
  123. static int32_t compare_guids(const GUID *gleft, const GUID *gright)
  124. {
  125. const char *left = (const char *) gleft, *right = (const char *) gright;
  126. return !memcmp(left,right,sizeof(GUID));
  127. }
  128. static int32_t write_pvocdata(int32_t fd,int32_t byterev,const PVOCDATA *pData)
  129. {
  130. int32_t written;
  131. // int32_t dwval;
  132. #ifdef _DEBUG
  133. assert(fd >= 0);
  134. assert(pData);
  135. #endif
  136. if(byterev){
  137. int32_t revdwval;
  138. PVOCDATA data;
  139. SND_SAMP ssamp;
  140. data.wWordFormat = REVWBYTES(pData->wWordFormat);
  141. data.wAnalFormat = REVWBYTES(pData->wAnalFormat);
  142. data.wSourceFormat = REVWBYTES(pData->wSourceFormat);
  143. data.wWindowType = REVWBYTES(pData->wWindowType);
  144. data.nAnalysisBins = REVDWBYTES(pData->nAnalysisBins);
  145. data.dwWinlen = REVDWBYTES(pData->dwWinlen);
  146. data.dwOverlap = REVDWBYTES(pData->dwOverlap);
  147. data.dwFrameAlign = REVDWBYTES(pData->dwFrameAlign);
  148. ssamp.fsamp = pData->fAnalysisRate;
  149. //dwval = * (int32_t *) &(pData->fAnalysisRate);
  150. ssamp.lsamp = REVDWBYTES(/*dwval*/ ssamp.lsamp);
  151. //data.fAnalysisRate = * (float *) &dwval;
  152. data.fAnalysisRate = ssamp.fsamp;
  153. ssamp.fsamp = pData->fWindowParam;
  154. //dwval = * ( int32_t *) &(pData->fWindowParam);
  155. revdwval = REVDWBYTES(ssamp.lsamp);
  156. //dwval = REVDWBYTES(dwval);
  157. //data.fWindowParam = * (float *) &dwval;
  158. data.fWindowParam = ssamp.fsamp;
  159. written = write(fd,(char *) &data,sizeof(PVOCDATA));
  160. }
  161. else
  162. written = write(fd,(char *) pData,sizeof(PVOCDATA));
  163. return written;
  164. }
  165. static int32_t write_fmt(int fd, int byterev,const WAVEFORMATEX *pfmt)
  166. {
  167. /*NB have to write out each element, as not guaranteed alignment othewise.
  168. * Consider it documentation. */
  169. #ifdef _DEBUG
  170. assert(fd >=0);
  171. assert(pfmt);
  172. assert(pfmt->cbSize == 62);
  173. #endif
  174. if(byterev){
  175. WAVEFORMATEX fmt;
  176. fmt.wFormatTag = REVWBYTES(pfmt->wFormatTag);
  177. fmt.nChannels = REVWBYTES(pfmt->nChannels);
  178. fmt.nSamplesPerSec = REVDWBYTES(pfmt->nSamplesPerSec);
  179. fmt.nAvgBytesPerSec = REVDWBYTES(pfmt->nAvgBytesPerSec);
  180. fmt.nBlockAlign = REVWBYTES(pfmt->nBlockAlign);
  181. fmt.wBitsPerSample = REVWBYTES(pfmt->wBitsPerSample);
  182. fmt.cbSize = REVWBYTES(pfmt->cbSize);
  183. if(write(fd,(char *) &(fmt.wFormatTag),sizeof(WORD)) != sizeof(WORD)
  184. || write(fd,(char *) &(fmt.nChannels),sizeof(WORD)) != sizeof(WORD)
  185. || write(fd,(char *) &(fmt.nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  186. || write(fd,(char *) &(fmt.nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  187. || write(fd,(char *) &(fmt.nBlockAlign),sizeof(WORD)) != sizeof(WORD)
  188. || write(fd,(char *) &(fmt.wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
  189. || write(fd,(char *) &(fmt.cbSize),sizeof(WORD)) != sizeof(WORD))
  190. return 0;
  191. }
  192. else {
  193. if(write(fd,(char *) &(pfmt->wFormatTag),sizeof(WORD)) != sizeof(WORD)
  194. || write(fd,(char *) &(pfmt->nChannels),sizeof(WORD)) != sizeof(WORD)
  195. || write(fd,(char *) &(pfmt->nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  196. || write(fd,(char *) &(pfmt->nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  197. || write(fd,(char *) &(pfmt->nBlockAlign),sizeof(WORD)) != sizeof(WORD)
  198. || write(fd,(char *) &(pfmt->wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
  199. || write(fd,(char *) &(pfmt->cbSize),sizeof(WORD)) != sizeof(WORD))
  200. return 0;
  201. }
  202. return SIZEOF_WFMTEX;
  203. }
  204. static int32_t pvoc_writeWindow(int32_t fd,int32_t byterev,float *window,DWORD length)
  205. {
  206. if(byterev){
  207. /* don't corrupt source array! */
  208. DWORD i;
  209. int32_t lval, *lp = (int32_t *) window;
  210. for(i=0;i < length; i++){
  211. lval = *lp++;
  212. lval = REVDWBYTES(lval);
  213. if(write(fd,(char *)&lval,sizeof(int32_t)) != sizeof(int32_t))
  214. return 0;
  215. }
  216. }
  217. else{
  218. if(write(fd,(char *) window,length * sizeof(float)) != (int32_t)(length*sizeof(float)))
  219. return 0;
  220. }
  221. return (int32_t)(length * sizeof(float));
  222. }
  223. static int32_t pvoc_readWindow(int fd, int byterev, float *window,DWORD length)
  224. {
  225. if(byterev){
  226. DWORD i;
  227. int32_t got,oval,lval, *lp = (int32_t *) window;
  228. #ifdef SINGLE_FLOAT
  229. for(i=0;i < length;i++){
  230. if(read(fd,(char *)&lval,sizeof(int32_t)) != sizeof(int32_t))
  231. return 0;
  232. oval = REVDWBYTES(lval);
  233. *lp++ = oval;
  234. }
  235. #else
  236. /* read whole block then swap...should be faster */
  237. got = read(fd,(char *) window,length * sizeof(float));
  238. if(got != (int)(length * sizeof(float)))
  239. return 0;
  240. /* then byterev */
  241. for(i=0;i < length;i++){
  242. lval = *lp;
  243. oval = REVDWBYTES(lval);
  244. *lp++ = oval;
  245. }
  246. #endif
  247. }
  248. else{
  249. if(read(fd,(char *) window,length * sizeof(float)) != (int)(length * sizeof(float)))
  250. return 0;
  251. }
  252. return length * sizeof(float);
  253. }
  254. const char *pvoc_errorstr()
  255. {
  256. return (const char *) pv_errstr;
  257. }
  258. /* thanks to the SNDAN programmers for this! */
  259. /* return 0 for big-endian machine, 1 for little-endian machine*/
  260. /* probably no good for 16bit swapping though */
  261. static int32_t byte_order()
  262. {
  263. int32_t one = 1;
  264. char* endptr = (char *) &one;
  265. return (*endptr);
  266. }
  267. /***** loosely modelled on CDP sfsys ******
  268. * This is a static array, but coul be made dynamic in an OOP sort of way.
  269. * The idea is that all low-level operations and data
  270. * are completely hidden from the user, so that internal format changes can be made
  271. * with little or no disruption to the public functions.
  272. * But avoiding the full monty of a C++ implementation.
  273. *******************************************/
  274. int32_t init_pvsys(void)
  275. {
  276. int32_t i;
  277. if(files[0] != NULL) {
  278. pv_errstr = "\npvsys: already initialized";
  279. return 0;
  280. }
  281. for(i = 0;i < MAXFILES;i++)
  282. files[i] = NULL;
  283. return 1;
  284. }
  285. static void prepare_pvfmt(WAVEFORMATEX *pfmt,DWORD chans, DWORD srate,
  286. pv_stype stype)
  287. {
  288. #ifdef _DEBUG
  289. assert(pfmt);
  290. #endif
  291. pfmt->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
  292. pfmt->nChannels = (WORD) chans;
  293. pfmt->nSamplesPerSec = srate;
  294. switch(stype){
  295. case(STYPE_16):
  296. pfmt->wBitsPerSample = (WORD)16;
  297. pfmt->nBlockAlign = (WORD)(chans * 2 * sizeof(char));
  298. break;
  299. case(STYPE_24):
  300. pfmt->wBitsPerSample = (WORD) 24;
  301. pfmt->nBlockAlign = (WORD)(chans * 3 * sizeof(char));
  302. break;
  303. case(STYPE_32):
  304. case(STYPE_IEEE_FLOAT):
  305. pfmt->wBitsPerSample = (WORD) 32;
  306. pfmt->nBlockAlign = (WORD)(chans * 4 * sizeof(char));
  307. break;
  308. default:
  309. #ifdef _DEBUG
  310. assert(0);
  311. #endif
  312. break;
  313. }
  314. pfmt->nAvgBytesPerSec = pfmt->nBlockAlign * srate;
  315. /* if we have extended WindowParam fields, or something ,will need to adjust this */
  316. pfmt->cbSize = 62;
  317. }
  318. /* lots of different ways of doing this! */
  319. /* we will need one in the form:
  320. * in pvoc_fmtcreate(const char *fname,PVOCDATA *p_pvfmt, WAVEFORMATEX *p_wvfmt);
  321. */
  322. /* a simple minimalist function to begin with!*/
  323. /*set D to 0, and/or dwWinlen to 0, to use internal default */
  324. /* fWindow points to userdef window, or is NULL */
  325. /* NB currently this does not enforce a soundfile extension; probably it should... */
  326. /* POSSIBLE TODO: expand arglist to enforce O_EXCL mode? */
  327. /* currently handled externally in CDP sfsys using file_exists() */
  328. int32_t pvoc_createfile(const char *filename,
  329. DWORD fftlen,DWORD overlap,DWORD chans,
  330. DWORD format,int32_t srate,
  331. pv_stype stype,pv_wtype wtype,
  332. float wparam,float *fWindow,DWORD dwWinlen)
  333. {
  334. int32_t i;
  335. long N,D;
  336. char *pname;
  337. PVOCFILE *pfile = NULL;
  338. float winparam = 0.0f;
  339. N = fftlen; /* keep the CARL varnames for now */
  340. D = overlap;
  341. if(N == 0 || chans <=0 || filename==NULL || D > N) {
  342. pv_errstr = "\npvsys: bad arguments";
  343. return -1;
  344. }
  345. if(/*format < PVOC_AMP_FREQ ||*/ format > PVOC_COMPLEX) { /* RWD unsigned, so nothing < 0 possible */
  346. pv_errstr = "\npvsys: bad format parameter";
  347. return -1;
  348. }
  349. if(!(wtype >= PVOC_DEFAULT && wtype <= PVOC_CUSTOM)){
  350. pv_errstr = "\npvsys: bad window type";
  351. return -1;
  352. }
  353. /* load it, but ca't write until we have a PVXW chunk definition...*/
  354. if(wtype==PVOC_CUSTOM){
  355. }
  356. if(wtype==PVOC_DEFAULT)
  357. wtype = PVOC_HAMMING;
  358. if(wtype==PVOC_KAISER)
  359. if(wparam != 0.0f)
  360. winparam = wparam;
  361. /*will need an internal default for window parameters...*/
  362. for(i=0;i < MAXFILES;i++)
  363. if(files[i]==NULL)
  364. break;
  365. if(i==MAXFILES) {
  366. pv_errstr = "\npvsys: too many files open";
  367. return -1;
  368. }
  369. pfile = (PVOCFILE *) malloc(sizeof(PVOCFILE));
  370. if(pfile==NULL){
  371. pv_errstr = "\npvsys: no memory";
  372. return -1;
  373. }
  374. pname = (char *) malloc(strlen(filename)+1);
  375. if(pname == NULL){
  376. free(pfile);
  377. pv_errstr = "\npvsys: no memory";
  378. return -1;
  379. }
  380. pfile->customWindow = NULL;
  381. /* setup rendering inforamtion */
  382. prepare_pvfmt(&pfile->fmtdata,chans,srate,stype);
  383. strcpy(pname,filename);
  384. pfile->pvdata.wWordFormat = PVOC_IEEE_FLOAT;
  385. pfile->pvdata.wAnalFormat = (WORD) format;
  386. pfile->pvdata.wSourceFormat = (stype == STYPE_IEEE_FLOAT ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM);
  387. pfile->pvdata.wWindowType = wtype;
  388. pfile->pvdata.nAnalysisBins = (N>>1) + 1;
  389. if(dwWinlen==0)
  390. pfile->pvdata.dwWinlen = N;
  391. else
  392. pfile->pvdata.dwWinlen = dwWinlen;
  393. if(D==0)
  394. pfile->pvdata.dwOverlap = N/8;
  395. else
  396. pfile->pvdata.dwOverlap = D;
  397. pfile->pvdata.dwFrameAlign = pfile->pvdata.nAnalysisBins * 2 * sizeof(float);
  398. pfile->pvdata.fAnalysisRate = (float)srate / (float) pfile->pvdata.dwOverlap;
  399. pfile->pvdata.fWindowParam = winparam;
  400. pfile->to_delete = 0;
  401. pfile->readonly = 0;
  402. pfile->needsupdate = 0; //could be overruled later, but only once!
  403. if(fWindow!= NULL){
  404. pfile->customWindow = malloc(dwWinlen * sizeof(float));
  405. if(pfile->customWindow==NULL){
  406. pv_errstr = "\npvsys: no memory for custom window";
  407. return -1;
  408. }
  409. memcpy((char *)(pfile->customWindow),(char *)fWindow,dwWinlen * sizeof(float));
  410. }
  411. pfile->fd = open(filename,O_BINARY | O_CREAT | O_RDWR | O_TRUNC,_S_IWRITE | _S_IREAD);
  412. if(pfile->fd < 0){
  413. free(pname);
  414. if(pfile->customWindow)
  415. free(pfile->customWindow);
  416. free(pfile);
  417. pv_errstr = "\npvsys: unable to create file";
  418. return -1;
  419. }
  420. pfile->propsoffset = 0;
  421. pfile->datachunkoffset = 0;
  422. pfile->nFrames = 0;
  423. pfile->FramePos = 0;
  424. pfile->curpos = 0;
  425. pfile->name = pname;
  426. pfile->do_byte_reverse = !byte_order();
  427. files[i] = pfile;
  428. if(!pvoc_writeheader(i)) {
  429. close(pfile->fd);
  430. remove(pfile->name);
  431. free(pfile->name);
  432. if(pfile->customWindow)
  433. free(pfile->customWindow);
  434. free(pfile);
  435. files[i] = NULL;
  436. return -1;
  437. }
  438. return i;
  439. }
  440. int32_t pvoc_getpvxprops(int32_t ifd, PVOCDATA *data)
  441. {
  442. if(files[ifd]==NULL)
  443. return 0;
  444. #ifdef _DEBUG
  445. assert(data != NULL);
  446. #endif
  447. memcpy((char*) data,(char*) &files[ifd]->pvdata,sizeof(PVOCDATA));
  448. return 1;
  449. }
  450. /* tell pvsys to update pvocdata on close; return 0 for success */
  451. /* only other way to do this is to test initial params != 0 */
  452. /* I want to control this explicitly, for now */
  453. int32_t pvoc_set_needsupdate(int32_t ifd)
  454. {
  455. if(files[ifd]==NULL)
  456. return -1;
  457. if(files[ifd]->readonly)
  458. return -1;
  459. files[ifd]->needsupdate = 1;
  460. return 0;
  461. }
  462. //returns 0 for success
  463. int32_t pvoc_canupdate(int32_t ifd)
  464. {
  465. if(files[ifd]==NULL || files[ifd]->readonly)
  466. return -1;
  467. if(files[ifd]->needsupdate)
  468. return 0;
  469. else
  470. return -1;
  471. }
  472. //ONLY when creating new file using 2-stages.called last thing before file close.
  473. #if 0
  474. int32_t pvoc_updateprops(int32_t fd, const PVOCDATA *data)
  475. {
  476. DWORD pos;
  477. if(files[fd]==NULL)
  478. return -1;
  479. if(files[fd]->readonly)
  480. return -1;
  481. if(files[fd]->needsupdate==0)
  482. return -1;
  483. #ifdef _DEBUG
  484. // the only field that MUST be set, needed for read/write frames
  485. assert(files[fd]->pvdata.nAnalysisBins == data->nAnalysisBins);
  486. #endif
  487. // need to seek to propsoffset, then we can call write_pvocdata
  488. pos = lseek(files[fd]->fd,files[fd]->propsoffset,SEEK_SET);
  489. if(pos != files[fd]->propsoffset){
  490. pv_errstr = "\npvsys: error updating pvoc props";
  491. return 0;
  492. }
  493. return 1;
  494. }
  495. #endif
  496. int32_t pvoc_openfile(const char *filename,PVOCDATA *data,WAVEFORMATEX *fmt)
  497. {
  498. int32_t i;
  499. WAVEFORMATPVOCEX wfpx;
  500. char *pname;
  501. PVOCFILE *pfile = NULL;
  502. // long size = sizeof(WAVEFORMATPVOCEX);
  503. if(data==NULL || fmt==NULL){
  504. pv_errstr = "\npvsys: Internal error: NULL data arrays";
  505. return -1;
  506. }
  507. for(i=0;i < MAXFILES;i++)
  508. if(files[i]==NULL)
  509. break;
  510. if(i==MAXFILES){
  511. pv_errstr = "\npvsys: too many files open";
  512. return -1;
  513. }
  514. pfile = (PVOCFILE *) malloc(sizeof(PVOCFILE));
  515. if(pfile==NULL){
  516. pv_errstr = "\npvsys: no memory for file data";
  517. return -1;
  518. }
  519. pfile->customWindow = NULL;
  520. pname = (char *) malloc(strlen(filename)+1);
  521. if(pname == NULL){
  522. free(pfile);
  523. pv_errstr = "\npvsys: no memory";
  524. return -1;
  525. }
  526. pfile->fd = open(filename,O_BINARY | O_RDONLY,_S_IREAD);
  527. if(pfile->fd < 0){
  528. free(pname);
  529. free(pfile);
  530. pv_errstr = "\npvsys: unable to create file";
  531. return -1;
  532. }
  533. strcpy(pname,filename);
  534. pfile->datachunkoffset = 0;
  535. pfile->nFrames = 0;
  536. pfile->curpos = 0;
  537. pfile->FramePos = 0;
  538. pfile->name = pname;
  539. pfile->do_byte_reverse = !byte_order();
  540. pfile->readonly = 1;
  541. pfile->needsupdate = 0; // just to state it explicitly!
  542. files[i] = pfile;
  543. if(!pvoc_readheader(i,&wfpx)){
  544. close(pfile->fd);
  545. free(pfile->name);
  546. if(pfile->customWindow)
  547. free(pfile->customWindow);
  548. free(pfile);
  549. files[i] = NULL;
  550. return -1;
  551. }
  552. memcpy((char *)data, (char *)&(wfpx.data),sizeof(PVOCDATA));
  553. memcpy((char *)fmt,(char *)&(wfpx.wxFormat.Format),SIZEOF_WFMTEX);
  554. files[i] = pfile;
  555. return i;
  556. }
  557. static int32_t pvoc_readfmt(int32_t fd,int32_t byterev,WAVEFORMATPVOCEX *pWfpx)
  558. {
  559. DWORD dword;
  560. WORD word;
  561. #ifdef _DEBUG
  562. assert(fd >= 0);
  563. assert(pWfpx);
  564. #endif
  565. if(read(fd,(char *) &(pWfpx->wxFormat.Format.wFormatTag),sizeof(WORD)) != sizeof(WORD)
  566. || read(fd,(char *) &(pWfpx->wxFormat.Format.nChannels),sizeof(WORD)) != sizeof(WORD)
  567. || read(fd,(char *) &(pWfpx->wxFormat.Format.nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  568. || read(fd,(char *) &(pWfpx->wxFormat.Format.nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  569. || read(fd,(char *) &(pWfpx->wxFormat.Format.nBlockAlign),sizeof(WORD)) != sizeof(WORD)
  570. || read(fd,(char *) &(pWfpx->wxFormat.Format.wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
  571. || read(fd,(char *) &(pWfpx->wxFormat.Format.cbSize),sizeof(WORD)) != sizeof(WORD)){
  572. pv_errstr = "\npvsys: error reading Source format data";
  573. return 0;
  574. }
  575. if(byterev){
  576. word = pWfpx->wxFormat.Format.wFormatTag;
  577. pWfpx->wxFormat.Format.wFormatTag= REVWBYTES(word);
  578. word = pWfpx->wxFormat.Format.nChannels;
  579. pWfpx->wxFormat.Format.nChannels = REVWBYTES(word);
  580. dword = pWfpx->wxFormat.Format.nSamplesPerSec;
  581. pWfpx->wxFormat.Format.nSamplesPerSec = REVDWBYTES(dword);
  582. dword = pWfpx->wxFormat.Format.nAvgBytesPerSec;
  583. pWfpx->wxFormat.Format.nAvgBytesPerSec = REVDWBYTES(dword);
  584. word = pWfpx->wxFormat.Format.nBlockAlign;
  585. pWfpx->wxFormat.Format.nBlockAlign = REVWBYTES(word);
  586. word = pWfpx->wxFormat.Format.wBitsPerSample;
  587. pWfpx->wxFormat.Format.wBitsPerSample = REVWBYTES(word);
  588. word = pWfpx->wxFormat.Format.cbSize;
  589. pWfpx->wxFormat.Format.cbSize = REVWBYTES(word);
  590. }
  591. /* the first clues this is pvx format...*/
  592. if(pWfpx->wxFormat.Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE){
  593. pv_errstr = "\npvsys: not a WAVE_EX file";
  594. return 0;
  595. }
  596. if(pWfpx->wxFormat.Format.cbSize != 62){
  597. pv_errstr = "\npvsys: bad size for fmt chunk";
  598. return 0;
  599. }
  600. if(read(fd,(char *) &(pWfpx->wxFormat.Samples.wValidBitsPerSample),sizeof(WORD)) != sizeof(WORD)
  601. || read(fd,(char *) &(pWfpx->wxFormat.dwChannelMask),sizeof(DWORD)) != sizeof(DWORD)
  602. || read(fd,(char *) &(pWfpx->wxFormat.SubFormat),sizeof(GUID)) != sizeof(GUID)){
  603. pv_errstr = "\npvsys: error reading Extended format data";
  604. return 0;
  605. }
  606. if(byterev){
  607. word = pWfpx->wxFormat.Samples.wValidBitsPerSample;
  608. pWfpx->wxFormat.Samples.wValidBitsPerSample = REVWBYTES(word);
  609. dword = pWfpx->wxFormat.dwChannelMask;
  610. pWfpx->wxFormat.dwChannelMask = REVDWBYTES(dword);
  611. dword = pWfpx->wxFormat.SubFormat.Data1;
  612. pWfpx->wxFormat.SubFormat.Data1 = REVDWBYTES(dword);
  613. word = pWfpx->wxFormat.SubFormat.Data2;
  614. pWfpx->wxFormat.SubFormat.Data2 = REVWBYTES(word);
  615. word = pWfpx->wxFormat.SubFormat.Data3;
  616. pWfpx->wxFormat.SubFormat.Data3 = REVWBYTES(word);
  617. /* don't need to reverse the char array */
  618. }
  619. /* ... but this is the clincher */
  620. if(!compare_guids(&(pWfpx->wxFormat.SubFormat),&KSDATAFORMAT_SUBTYPE_PVOC)){
  621. pv_errstr = "\npvsys: not a PVOCEX file";
  622. return 0;
  623. }
  624. if(read(fd,(char *) &(pWfpx->dwVersion),sizeof(DWORD)) != sizeof(DWORD)
  625. || read(fd,(char *) &(pWfpx->dwDataSize),sizeof(DWORD)) != sizeof(DWORD)
  626. || read(fd,(char *) &(pWfpx->data),sizeof(PVOCDATA)) != sizeof(PVOCDATA)){
  627. pv_errstr = "\npvsys: error reading Extended pvoc format data";
  628. return 0;
  629. }
  630. if(byterev){
  631. SND_SAMP ssamp;
  632. dword = pWfpx->dwVersion;
  633. pWfpx->dwVersion = REVDWBYTES(dword);
  634. /* check it now! */
  635. if(pWfpx->dwVersion != PVX_VERSION){
  636. pv_errstr = "\npvsys: unknown pvocex Version";
  637. return 0;
  638. }
  639. dword = pWfpx->dwDataSize;
  640. pWfpx->dwDataSize = REVDWBYTES(dword);
  641. word = pWfpx->data.wWordFormat;
  642. pWfpx->data.wWordFormat= REVWBYTES(word);
  643. word = pWfpx->data.wAnalFormat;
  644. pWfpx->data.wAnalFormat= REVWBYTES(word);
  645. word = pWfpx->data.wSourceFormat;
  646. pWfpx->data.wSourceFormat= REVWBYTES(word);
  647. word = pWfpx->data.wWindowType;
  648. pWfpx->data.wWindowType= REVWBYTES(word);
  649. dword = pWfpx->data.nAnalysisBins;
  650. pWfpx->data.nAnalysisBins = REVDWBYTES(dword);
  651. dword = pWfpx->data.dwWinlen;
  652. pWfpx->data.dwWinlen = REVDWBYTES(dword);
  653. dword = pWfpx->data.dwOverlap;
  654. pWfpx->data.dwOverlap = REVDWBYTES(dword);
  655. dword = pWfpx->data.dwFrameAlign;
  656. pWfpx->data.dwFrameAlign = REVDWBYTES(dword);
  657. ssamp.fsamp = pWfpx->data.fAnalysisRate;
  658. //dword = * (DWORD *)&(pWfpx->data.fAnalysisRate); /* RWD TODO: use union */
  659. //dword = REVDWBYTES(dword);
  660. ssamp.lsamp = REVDWBYTES(ssamp.lsamp);
  661. //pWfpx->data.fAnalysisRate = *(float *)&dword;
  662. pWfpx->data.fAnalysisRate = ssamp.fsamp;
  663. ssamp.fsamp = pWfpx->data.fWindowParam;
  664. //dword = * (DWORD *)&(pWfpx->data.fWindowParam);
  665. //dword = REVDWBYTES(dword);
  666. ssamp.lsamp = REVDWBYTES(ssamp.lsamp);
  667. //pWfpx->data.fWindowParam = *(float *)&dword;
  668. pWfpx->data.fWindowParam = ssamp.fsamp;
  669. }
  670. if(pWfpx->dwVersion != PVX_VERSION){
  671. pv_errstr = "\npvsys: unknown pvocex Version";
  672. return 0;
  673. }
  674. return 1;
  675. }
  676. static int32_t pvoc_readheader(int32_t ifd,WAVEFORMATPVOCEX *pWfpx)
  677. {
  678. DWORD tag, size,riffsize;
  679. int32_t fmtseen = 0, /* dataseen = 0,*/ windowseen = 0;
  680. // DWORD windowlength = 0;
  681. #ifdef _DEBUG
  682. assert(pWfpx);
  683. assert(files[ifd]);
  684. assert(files[ifd]->fd >= 0);
  685. size = sizeof(WAVEFORMATEXTENSIBLE);
  686. size += 2 * sizeof(DWORD);
  687. size += sizeof(PVOCDATA);
  688. #endif
  689. if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)
  690. || read(files[ifd]->fd,(char *) &size,sizeof(DWORD)) != sizeof(DWORD)){
  691. pv_errstr = "\npvsys: error reading header";
  692. return 0;
  693. }
  694. if(files[ifd]->do_byte_reverse)
  695. size = REVDWBYTES(size);
  696. else
  697. tag = REVDWBYTES(tag);
  698. if(tag != TAG('R','I','F','F')){
  699. pv_errstr = "\npvsys: not a RIFF file";
  700. return 0;
  701. }
  702. if(size < 24 * sizeof(DWORD) + SIZEOF_FMTPVOCEX){
  703. pv_errstr = "\npvsys: file too small";
  704. return 0;
  705. }
  706. riffsize = size;
  707. if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)){
  708. pv_errstr = "\npvsys: error reading header";
  709. return 0;
  710. }
  711. if(!files[ifd]->do_byte_reverse)
  712. tag = REVDWBYTES(tag);
  713. if(tag != TAG('W','A','V','E')){
  714. pv_errstr = "\npvsys: not a WAVE file";
  715. return 0;
  716. }
  717. riffsize -= sizeof(DWORD);
  718. /*loop for chunks */
  719. while(riffsize > 0){
  720. if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)
  721. || read(files[ifd]->fd,(char *) &size,sizeof(DWORD)) != sizeof(DWORD)){
  722. pv_errstr = "\npvsys: error reading header";
  723. return 0;
  724. }
  725. if(files[ifd]->do_byte_reverse)
  726. size = REVDWBYTES(size);
  727. else
  728. tag = REVDWBYTES(tag);
  729. riffsize -= 2 * sizeof(DWORD);
  730. switch(tag){
  731. case TAG('f','m','t',' '):
  732. /* bail out if not a pvoc file: not trying to read all WAVE formats!*/
  733. if(size < SIZEOF_FMTPVOCEX){
  734. pv_errstr = "\npvsys: not a PVOC-EX file";
  735. return 0;
  736. }
  737. if(!pvoc_readfmt(files[ifd]->fd,files[ifd]->do_byte_reverse,pWfpx)){
  738. pv_errstr = "\npvsys: error reading format chunk";
  739. return 0;
  740. }
  741. riffsize -= SIZEOF_FMTPVOCEX;
  742. fmtseen = 1;
  743. memcpy((char *)&(files[ifd]->fmtdata),(char *)&(pWfpx->wxFormat),SIZEOF_WFMTEX);
  744. memcpy((char *)&(files[ifd]->pvdata),(char *)&(pWfpx->data),sizeof(PVOCDATA));
  745. break;
  746. case TAG('P','V','X','W'):
  747. if(!fmtseen){
  748. pv_errstr = "\npvsys: PVXW chunk found before fmt chunk.";
  749. return 0;
  750. }
  751. if(files[ifd]->pvdata.wWindowType!=PVOC_CUSTOM){
  752. /*whaddayado? can you warn the user and continue?*/
  753. pv_errstr = "\npvsys: PVXW chunk found but custom window not specified";
  754. return 0;
  755. }
  756. files[ifd]->customWindow = malloc(files[ifd]->pvdata.dwWinlen * sizeof(float));
  757. if(files[ifd]->customWindow == NULL){
  758. pv_errstr = "\npvsys: no memory for custom window data.";
  759. return 0;
  760. }
  761. if(pvoc_readWindow(files[ifd]->fd,files[ifd]->do_byte_reverse,
  762. files[ifd]->customWindow,files[ifd]->pvdata.dwWinlen)
  763. != (int)(files[ifd]->pvdata.dwWinlen * sizeof(float))){
  764. pv_errstr = "\npvsys: error reading window data.";
  765. return 0;
  766. }
  767. windowseen = 1;
  768. break;
  769. case TAG('d','a','t','a'):
  770. if(riffsize - size != 0){
  771. pv_errstr = "\npvsys: bad RIFF file";
  772. return 0;
  773. }
  774. if(!fmtseen){
  775. pv_errstr = "\npvsys: bad format, data chunk before fmt chunk";
  776. return 0;
  777. }
  778. if(files[ifd]->pvdata.wWindowType==PVOC_CUSTOM)
  779. if(!windowseen){
  780. pv_errstr = "\npvsys: custom window chunk PVXW not found";
  781. return 0;
  782. }
  783. files[ifd]->datachunkoffset = lseek(files[ifd]->fd,0,SEEK_CUR);
  784. files[ifd]->curpos = files[ifd]->datachunkoffset;
  785. /* not m/c frames, for now */
  786. files[ifd]->nFrames = size / files[ifd]->pvdata.dwFrameAlign;
  787. return 1;
  788. break;
  789. default:
  790. /* skip any onknown chunks */
  791. riffsize -= 2 * sizeof(DWORD);
  792. if(lseek(files[ifd]->fd,size,SEEK_CUR) < 0){
  793. pv_errstr = "\npvsys: error skipping unknown WAVE chunk";
  794. return 0;
  795. }
  796. riffsize -= size;
  797. break;
  798. }
  799. }
  800. /* if here, something very wrong!*/
  801. pv_errstr = "\npvsys: bad format in RIFF file";
  802. return 0;
  803. }
  804. static int32_t pvoc_writeheader(int ofd)
  805. {
  806. int32_t tag,size,version;
  807. WORD validbits;
  808. const GUID *pGuid = &KSDATAFORMAT_SUBTYPE_PVOC;
  809. #ifdef _DEBUG
  810. assert(files[ofd] != NULL);
  811. assert(files[ofd]->fd >=0);
  812. #endif
  813. tag = TAG('R','I','F','F');
  814. size = 0;
  815. if(files[ofd]->do_byte_reverse)
  816. size = REVDWBYTES(size);
  817. if(!files[ofd]->do_byte_reverse)
  818. tag = REVDWBYTES(tag);
  819. if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)
  820. || write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)) {
  821. pv_errstr = "\npvsys: error writing header";
  822. return 0;
  823. }
  824. tag = TAG('W','A','V','E');
  825. if(!files[ofd]->do_byte_reverse)
  826. tag = REVDWBYTES(tag);
  827. if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)){
  828. pv_errstr = "\npvsys: error writing header";
  829. return 0;
  830. }
  831. tag = TAG('f','m','t',' ');
  832. size = SIZEOF_WFMTEX + sizeof(WORD) +
  833. sizeof(DWORD)
  834. + sizeof(GUID)
  835. + 2*sizeof(DWORD)
  836. + sizeof(PVOCDATA);
  837. if(files[ofd]->do_byte_reverse)
  838. size = REVDWBYTES(size);
  839. if(!files[ofd]->do_byte_reverse)
  840. tag = REVDWBYTES(tag);
  841. if(write(files[ofd]->fd,(char *)&tag,sizeof(int32_t)) != sizeof(int32_t)
  842. || write(files[ofd]->fd,(char *)&size,sizeof(int32_t)) != sizeof(int32_t)) {
  843. pv_errstr = "\npvsys: error writing header";
  844. return 0;
  845. }
  846. /* we need to record where we are, as we may have to update fmt data before file close */
  847. files[ofd]->fmtchunkoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
  848. if(write_fmt(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->fmtdata)) != SIZEOF_WFMTEX){
  849. pv_errstr = "\npvsys: error writing fmt chunk";
  850. return 0;
  851. }
  852. validbits = files[ofd]->fmtdata.wBitsPerSample; /*nothing fancy here */
  853. if(files[ofd]->do_byte_reverse)
  854. validbits = REVWBYTES(validbits);
  855. if(write(files[ofd]->fd,(char *) &validbits,sizeof(WORD)) != sizeof(WORD)){
  856. pv_errstr = "\npvsys: error writing fmt chunk";
  857. return 0;
  858. }
  859. /* we will take this from a WAVE_EX file, in due course */
  860. size = 0; /*dwChannelMask*/
  861. if(write(files[ofd]->fd,(char *)&size,sizeof(DWORD)) != sizeof(DWORD)){
  862. pv_errstr = "\npvsys: error writing fmt chunk";
  863. return 0;
  864. }
  865. if(write_guid(files[ofd]->fd,files[ofd]->do_byte_reverse,pGuid) != sizeof(GUID)){
  866. pv_errstr = "\npvsys: error writing fmt chunk";
  867. return 0;
  868. }
  869. version = 1;
  870. size = sizeof(PVOCDATA);
  871. if(files[ofd]->do_byte_reverse){
  872. version = REVDWBYTES(version);
  873. size = REVDWBYTES(size);
  874. }
  875. if(write(files[ofd]->fd,&version,sizeof(int32_t)) != sizeof(int32_t)
  876. || write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)){
  877. pv_errstr = "\npvsys: error writing fmt chunk";
  878. return 0;
  879. }
  880. /* RWD new 2022 so we can update all pvocex props on closing new file */
  881. files[ofd]->propsoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
  882. if(write_pvocdata(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->pvdata)) != sizeof(PVOCDATA)){
  883. pv_errstr = "\npvsys: error writing fmt chunk";
  884. return 0;
  885. }
  886. /* VERY experimental; may not even be a good idea...*/
  887. if(files[ofd]->customWindow){
  888. tag = TAG('P','V','X','W');
  889. size = files[ofd]->pvdata.dwWinlen * sizeof(float);
  890. if(files[ofd]->do_byte_reverse)
  891. size = REVDWBYTES(size);
  892. else
  893. tag = REVDWBYTES(tag);
  894. if(write(files[ofd]->fd,(char *)&tag,sizeof(int32_t)) != sizeof(int32_t)
  895. || write(files[ofd]->fd,(char *)&size,sizeof(int32_t)) != sizeof(int32_t)) {
  896. pv_errstr = "\npvsys: error writing header";
  897. return 0;
  898. }
  899. if(pvoc_writeWindow(files[ofd]->fd,
  900. files[ofd]->do_byte_reverse,
  901. files[ofd]->customWindow,
  902. files[ofd]->pvdata.dwWinlen)!= (int)(files[ofd]->pvdata.dwWinlen * sizeof(float))){
  903. pv_errstr = "\npvsys: error writing window data.";
  904. return 0;
  905. }
  906. }
  907. /* no other chunks to write yet! */
  908. tag = TAG('d','a','t','a');
  909. if(!files[ofd]->do_byte_reverse)
  910. tag = REVDWBYTES(tag);
  911. if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)){
  912. pv_errstr = "\npvsys: write error writing header";
  913. return 0;
  914. }
  915. /* we need to update size later on...*/
  916. size = 0;
  917. if(write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)){
  918. pv_errstr = "\npvsys: write error writing header";
  919. return 0;
  920. }
  921. files[ofd]->datachunkoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
  922. files[ofd]->curpos = files[ofd]->datachunkoffset;
  923. return 1;
  924. }
  925. /* called by pvoc_closefile() */
  926. /* which checks "readonly", if not set, file is newly created */
  927. static int32_t pvoc_updateheader(int ofd)
  928. {
  929. int32_t riffsize,datasize;
  930. DWORD pos;
  931. #ifdef _DEBUG
  932. assert(files[ofd]);
  933. assert(files[ofd]->fd >= 0);
  934. //assert(files[ofd]->curpos == lseek(files[ofd]->fd,0,SEEK_CUR));
  935. assert(!files[ofd]->readonly);
  936. #endif
  937. datasize = files[ofd]->curpos - files[ofd]->datachunkoffset;
  938. pos = lseek(files[ofd]->fd,files[ofd]->datachunkoffset-sizeof(DWORD),SEEK_SET);
  939. if(pos != files[ofd]->datachunkoffset-sizeof(DWORD)){
  940. pv_errstr = "\npvsys: seek error updating data chunk";
  941. return 0;
  942. }
  943. if(files[ofd]->do_byte_reverse)
  944. datasize = REVDWBYTES(datasize);
  945. if(write(files[ofd]->fd,(char *) &datasize,sizeof(DWORD)) != sizeof(DWORD)){
  946. pv_errstr = "\npvsys: write error updating data chunk";
  947. return 0;
  948. }
  949. riffsize = files[ofd]->curpos - 2* sizeof(DWORD);
  950. if(files[ofd]->do_byte_reverse)
  951. riffsize = REVDWBYTES(riffsize);
  952. pos = lseek(files[ofd]->fd,sizeof(DWORD),SEEK_SET);
  953. if(pos != sizeof(DWORD)){
  954. pv_errstr = "\npvsys: seek error updating riff chunk";
  955. return 0;
  956. }
  957. if(write(files[ofd]->fd,(char *) &riffsize,sizeof(DWORD)) != sizeof(DWORD)){
  958. pv_errstr = "\npvsys: write error updating riff chunk";
  959. return 0;
  960. }
  961. // update PVOCDATA and WAVEFORMATEX fields if required
  962. if(files[ofd]->needsupdate){
  963. #ifdef _DEBUG
  964. fprintf(stderr,"updating in pvoc_update_header()\n");
  965. #endif
  966. pos = lseek(files[ofd]->fd,files[ofd]->fmtchunkoffset,SEEK_SET);
  967. if(pos != files[ofd]->fmtchunkoffset){
  968. pv_errstr = "\npvsys: seek error updating fmt data";
  969. return 0;
  970. }
  971. pos = write_fmt(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->fmtdata));
  972. if(pos != SIZEOF_WFMTEX){
  973. pv_errstr = "\npvsys: write error updating fmt data";
  974. return 0;
  975. }
  976. pos = lseek(files[ofd]->fd,files[ofd]->propsoffset,SEEK_SET);
  977. if(pos != files[ofd]->propsoffset){
  978. pv_errstr = "\npvsys: seek error updating pvx data";
  979. return 0;
  980. }
  981. //int32_t write_pvocdata(int32_t fd,int32_t byterev,const PVOCDATA *pData)
  982. pos = write_pvocdata(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->pvdata));
  983. if(pos != sizeof(PVOCDATA)){
  984. pv_errstr = "\npvsys: write error updating pvx data";
  985. return 0;
  986. }
  987. }
  988. pos = lseek(files[ofd]->fd,0,SEEK_END);
  989. if(pos < 0){
  990. pv_errstr = "\npvsys: seek error seeking to end of file";
  991. return 0;
  992. }
  993. return 1;
  994. }
  995. int32_t pvoc_update_closefile(int ofd, const PVOCDATA *data, const WAVEFORMATEXTENSIBLE *wfx)
  996. {
  997. if(files[ofd]==NULL){
  998. pv_errstr = "\npvsys: file does not exist";
  999. return 0;
  1000. }
  1001. if(files[ofd]->fd < 0){
  1002. pv_errstr = "\npvsys: file not open";
  1003. return 0;
  1004. }
  1005. if(files[ofd]->readonly) {
  1006. pv_errstr = "\ncannot update readonly file"; // probably an input file
  1007. }
  1008. else {
  1009. memcpy((char*) &files[ofd]->fmtdata, (char*) &wfx->Format, sizeof(WAVEFORMATEX));
  1010. memcpy((char*) &files[ofd]->pvdata, (char*) data, sizeof(PVOCDATA));
  1011. }
  1012. return pvoc_closefile(ofd);
  1013. }
  1014. int32_t pvoc_closefile(int ofd)
  1015. {
  1016. if(files[ofd]==NULL){
  1017. pv_errstr = "\npvsys: file does not exist";
  1018. return 0;
  1019. }
  1020. if(files[ofd]->fd < 0){
  1021. pv_errstr = "\npvsys: file not open";
  1022. return 0;
  1023. }
  1024. if(!files[ofd]->readonly) {
  1025. if(!pvoc_updateheader(ofd))
  1026. return 0;
  1027. }
  1028. close(files[ofd]->fd);
  1029. if(files[ofd]->to_delete && !(files[ofd]->readonly))
  1030. remove(files[ofd]->name);
  1031. free(files[ofd]->name);
  1032. free(files[ofd]);
  1033. files[ofd] = 0;
  1034. return 1;
  1035. }
  1036. /* does not directly address m/c streams, or alternative numeric formats, yet
  1037. * so for m/c files, write each frame in turn, for each channel.
  1038. * The format requires multi-channel frames to be interleaved in the usual way:
  1039. * if nChannels= 4, the file will contain:
  1040. * frame[0][0],frame[0][1],frame[0][2],frame[0][3],frme[1][0],frame[1][1].....
  1041. *
  1042. * The idea is to offer e.g. a floats version and a longs version ONLY, but
  1043. * independently of the underlying representation, so that the user can write a floats
  1044. * block, even though the underlying format might be longs or doubles. Most importantly,
  1045. * the user does not have to deal with byte-reversal, which would otherwise always be the case
  1046. * it the user had direct access to the file.
  1047. *
  1048. * So these functions are the most likely to change over time!.
  1049. *
  1050. * return 0 for error, 1 for success. This could change....
  1051. */
  1052. int32_t pvoc_putframes(int ofd,const float *frame,int32_t numframes)
  1053. {
  1054. DWORD i;
  1055. DWORD towrite; /* count in 'words' */
  1056. int32_t temp,*lfp;
  1057. if(files[ofd]==NULL){
  1058. pv_errstr = "\npvsys: bad file descriptor";
  1059. return 0;
  1060. }
  1061. if(files[ofd]->fd < 0){
  1062. pv_errstr = "\npvsys: file not open";
  1063. return 0;
  1064. }
  1065. /* NB doubles not supported yet */
  1066. towrite = files[ofd]->pvdata.nAnalysisBins * 2 * numframes;
  1067. if(files[ofd]->do_byte_reverse){
  1068. /* do this without overwriting source data! */
  1069. lfp = (int32_t *) frame;
  1070. for(i=0;i < towrite; i++){
  1071. temp = *lfp++;
  1072. temp = REVDWBYTES(temp);
  1073. if(write(files[ofd]->fd,(char *) &temp,sizeof(int32_t)) != sizeof(int32_t)){
  1074. pv_errstr = "\npvsys: error writing data";
  1075. return 0;
  1076. }
  1077. }
  1078. }
  1079. else {
  1080. if(write(files[ofd]->fd,(char *) frame,towrite * sizeof(float)) != (int32_t)(towrite * sizeof(float))){
  1081. pv_errstr = "\npvsys: error writing data";
  1082. return 0;
  1083. }
  1084. }
  1085. files[ofd]->FramePos += numframes;
  1086. files[ofd]->nFrames += numframes;
  1087. files[ofd]->curpos += towrite * sizeof(float);
  1088. return 1;
  1089. }
  1090. /* Simplistic read function
  1091. * best practice here is to read nChannels frames *
  1092. * return -1 for error, 0 for EOF, else numframes read
  1093. */
  1094. int32_t pvoc_getframes(int32_t ifd,float *frames,DWORD nframes)
  1095. {
  1096. int32_t i;
  1097. int32_t toread;
  1098. int32_t oval,temp,*lfp;
  1099. int32_t got;
  1100. int32_t rc = -1;
  1101. if(files[ifd]==NULL){
  1102. pv_errstr = "\npvsys: bad file descriptor";
  1103. return rc;
  1104. }
  1105. if(files[ifd]->fd < 0){
  1106. pv_errstr = "\npvsys: file not open";
  1107. return rc;
  1108. }
  1109. toread = files[ifd]->pvdata.nAnalysisBins * 2 * nframes;
  1110. if(files[ifd]->do_byte_reverse){
  1111. lfp = (int32_t *) frames;
  1112. #ifdef SINGLE_FLOAT
  1113. for(i=0;i < toread;i++){
  1114. if((got=read(files[ifd]->fd,(char *) &temp,sizeof(int32_t))) <0){
  1115. pv_errstr = "\npvsys: error reading data";
  1116. return rc;
  1117. }
  1118. if(got < sizeof(int32_t)){
  1119. /* file size incorrect? */
  1120. return 0; /* assume EOF */
  1121. }
  1122. temp = REVDWBYTES(temp);
  1123. *lfp++ = temp;
  1124. }
  1125. #else
  1126. /* much faster on G4!!! */
  1127. got = read(files[ifd]->fd,(char *)frames,toread * sizeof(float));
  1128. if(got < 0){
  1129. pv_errstr = "\npvsys: error reading data";
  1130. return rc;
  1131. }
  1132. for(i=0;i < got / sizeof(float);i++){
  1133. temp = *lfp;
  1134. oval = REVDWBYTES(temp);
  1135. *lfp++ = oval;
  1136. }
  1137. if(got < (int)(toread * sizeof(float))){
  1138. /* some (user?) error in file size: return integral number of frames read*/
  1139. toread = got / (files[ifd]->pvdata.nAnalysisBins * 2 * sizeof(float));
  1140. /* RWD 4:2002 need to revise this too */
  1141. nframes = toread;
  1142. }
  1143. #endif
  1144. rc = nframes; /*RWD 4:2002 */
  1145. }
  1146. else{
  1147. if((got = read(files[ifd]->fd,(char *)frames,toread * sizeof(float))) < (int32_t)(toread * sizeof(float))){
  1148. if(got < 0){
  1149. pv_errstr = "\npvsys: error reading data";
  1150. return rc;
  1151. }
  1152. else if(got < (int)(toread * sizeof(float))){
  1153. /* some (user?) error in file size: return integral number of frames read*/
  1154. toread = got / (files[ifd]->pvdata.nAnalysisBins * 2 * sizeof(float));
  1155. rc = toread;
  1156. /* RWD 4:2002 need to revise this too */
  1157. nframes = toread;
  1158. }
  1159. }
  1160. else
  1161. rc = nframes;
  1162. }
  1163. /*files[ifd]->curpos += (toread * sizeof(float));*/
  1164. files[ifd]->curpos += got; /* RWD 4:2002 */
  1165. files[ifd]->FramePos += nframes;
  1166. return rc;
  1167. }
  1168. // return 0 for success, -1 for error
  1169. int32_t pvoc_rewind(int32_t ifd,int32_t skip_first_frame)
  1170. {
  1171. int32_t rc = -1;
  1172. int32_t fd;
  1173. int32_t pos;
  1174. int32_t skipsize = 0;
  1175. int32_t skipframes = 0;
  1176. if(files[ifd]==NULL){
  1177. pv_errstr = "\npvsys: bad file descriptor";
  1178. return rc;
  1179. }
  1180. if(files[ifd]->fd < 0){
  1181. pv_errstr = "\npvsys: file not open";
  1182. return rc;
  1183. }
  1184. skipframes = files[ifd]->fmtdata.nChannels;
  1185. //skipsize = files[ifd]->pvdata.dwFrameAlign * skipframes;
  1186. fd = files[ifd]->fd;
  1187. pos = files[ifd]->datachunkoffset;
  1188. if(skip_first_frame){
  1189. skipsize = files[ifd]->pvdata.dwFrameAlign * skipframes;
  1190. pos += skipsize;
  1191. }
  1192. if(lseek(fd,pos,SEEK_SET) != pos ) {
  1193. pv_errstr = "\npvsys: error rewinding file";
  1194. return rc;
  1195. }
  1196. files[ifd]->curpos = /* files[ifd]->datachunkoffset + skipsize */ pos;
  1197. files[ifd]->FramePos = skipframes;
  1198. return 0;
  1199. }
  1200. /* may be more to do in here later on */
  1201. int32_t pvsys_release(void)
  1202. {
  1203. int32_t i;
  1204. for(i=0;i < MAXFILES;i++){
  1205. if(files[i]) {
  1206. #ifdef _DEBUG
  1207. fprintf(stderr,"\nDEBUG WARNING: file %d still open!\n",i);
  1208. #endif
  1209. if(!pvoc_closefile(i)){
  1210. pv_errstr = "\npvsys: unable to close file on termination";
  1211. return 0;
  1212. }
  1213. }
  1214. }
  1215. return 1;
  1216. }
  1217. /*return raw framecount: channel-agnostic for now */
  1218. int32_t pvoc_framecount(int32_t ifd)
  1219. {
  1220. if(files[ifd]==NULL)
  1221. return -1;
  1222. return files[ifd]->nFrames;
  1223. }
  1224. /* RWD Jan 2014 */
  1225. // return -1 for error, else framepos
  1226. int32_t pvoc_framepos(int32_t ifd)
  1227. {
  1228. if(files[ifd]==NULL)
  1229. return -1;
  1230. return files[ifd]->FramePos;
  1231. }
  1232. // return -1 for error, 0 for success
  1233. //NB framecount always disregards chans. But offset represents m/c block: offset*2 to get frames
  1234. int32_t pvoc_seek_mcframe(int32_t ifd, int32_t offset, int32_t mode)
  1235. {
  1236. // DWORD mcframealign;
  1237. DWORD rawoffset;
  1238. int32_t rc = -1;
  1239. int32_t mcframealign;
  1240. if(files[ifd]==NULL)
  1241. return -1;
  1242. mcframealign = files[ifd]->pvdata.dwFrameAlign * files[ifd]->fmtdata.nChannels;
  1243. rawoffset = offset * mcframealign;
  1244. switch(mode){
  1245. case SEEK_SET:
  1246. // offset is m/c quantity, e.g. 2 * frame for stereo
  1247. if(offset >= (files[ifd]->nFrames / files[ifd]->fmtdata.nChannels)){
  1248. pv_errstr = "\npvsys: seek target beyond end of file";
  1249. break;
  1250. }
  1251. rawoffset += files[ifd]->datachunkoffset;
  1252. if(lseek(files[ifd]->fd,rawoffset,SEEK_SET) != rawoffset ) {
  1253. pv_errstr = "\npvsys: seek error, SEEK_SET";
  1254. return -1;
  1255. }
  1256. files[ifd]->curpos = rawoffset;
  1257. files[ifd]->FramePos = offset * files[ifd]->fmtdata.nChannels;
  1258. rc = 0;
  1259. break;
  1260. case SEEK_END:
  1261. // go to end of file + offset, offset <= 0
  1262. if(offset > 0){
  1263. pv_errstr = "\npvsys: seek target before start of file, offset must be <= 0";
  1264. break;
  1265. }
  1266. #ifdef _DEBUG
  1267. fprintf(stderr,"pvoc_seek_mcframe: fd = %d\n",ifd);
  1268. #endif
  1269. //NB not relative to datachunkoffset in this case
  1270. if(lseek(files[ifd]->fd,rawoffset,SEEK_END) < 0){
  1271. pv_errstr = "\npvsys: seek error, SEEK_END";
  1272. return -1;
  1273. }
  1274. #ifdef _DEBUG
  1275. fprintf(stderr,"pvoc_seek_mcframe: files[%d]->nFrames = %d\n",ifd,files[ifd]->nFrames);
  1276. #endif
  1277. files[ifd]->FramePos = files[ifd]->nFrames - (offset * files[ifd]->fmtdata.nChannels);
  1278. files[ifd]->curpos = files[ifd]->FramePos * files[ifd]->pvdata.dwFrameAlign;
  1279. #ifdef _DEBUG
  1280. fprintf(stderr,"pvoc_seek_mcframe: got curpos = %d\n",files[ifd]->curpos);
  1281. #endif
  1282. rc = 0; //success!
  1283. break;
  1284. case SEEK_CUR:
  1285. // covered by pvoc_framepos(), equiv to a pvoc_tell() function.
  1286. pv_errstr = "\npvsys: SEEK_CUR mode not supported yet!";
  1287. break;
  1288. }
  1289. return rc;
  1290. }