pvfileio.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. ssamp.lsamp = 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. int32_t pvoc_createfile(const char *filename,
  327. DWORD fftlen,DWORD overlap,DWORD chans,
  328. DWORD format,int32_t srate,
  329. pv_stype stype,pv_wtype wtype,
  330. float wparam,float *fWindow,DWORD dwWinlen)
  331. {
  332. int32_t i;
  333. long N,D;
  334. char *pname;
  335. PVOCFILE *pfile = NULL;
  336. float winparam = 0.0f;
  337. N = fftlen; /* keep the CARL varnames for now */
  338. D = overlap;
  339. if(N == 0 || chans <=0 || filename==NULL || D > N) {
  340. pv_errstr = "\npvsys: bad arguments";
  341. return -1;
  342. }
  343. if(/*format < PVOC_AMP_FREQ ||*/ format > PVOC_COMPLEX) { /* RWD unsigned, so nothing < 0 possible */
  344. pv_errstr = "\npvsys: bad format parameter";
  345. return -1;
  346. }
  347. if(!(wtype >= PVOC_DEFAULT && wtype <= PVOC_CUSTOM)){
  348. pv_errstr = "\npvsys: bad window type";
  349. return -1;
  350. }
  351. /* load it, but ca't write until we have a PVXW chunk definition...*/
  352. if(wtype==PVOC_CUSTOM){
  353. }
  354. if(wtype==PVOC_DEFAULT)
  355. wtype = PVOC_HAMMING;
  356. if(wtype==PVOC_KAISER)
  357. if(wparam != 0.0f)
  358. winparam = wparam;
  359. /*will need an internal default for window parameters...*/
  360. for(i=0;i < MAXFILES;i++)
  361. if(files[i]==NULL)
  362. break;
  363. if(i==MAXFILES) {
  364. pv_errstr = "\npvsys: too many files open";
  365. return -1;
  366. }
  367. pfile = (PVOCFILE *) malloc(sizeof(PVOCFILE));
  368. if(pfile==NULL){
  369. pv_errstr = "\npvsys: no memory";
  370. return -1;
  371. }
  372. pname = (char *) malloc(strlen(filename)+1);
  373. if(pname == NULL){
  374. free(pfile);
  375. pv_errstr = "\npvsys: no memory";
  376. return -1;
  377. }
  378. pfile->customWindow = NULL;
  379. /* setup rendering inforamtion */
  380. prepare_pvfmt(&pfile->fmtdata,chans,srate,stype);
  381. strcpy(pname,filename);
  382. pfile->pvdata.wWordFormat = PVOC_IEEE_FLOAT;
  383. pfile->pvdata.wAnalFormat = (WORD) format;
  384. pfile->pvdata.wSourceFormat = (stype == STYPE_IEEE_FLOAT ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM);
  385. pfile->pvdata.wWindowType = wtype;
  386. pfile->pvdata.nAnalysisBins = (N>>1) + 1;
  387. if(dwWinlen==0)
  388. pfile->pvdata.dwWinlen = N;
  389. else
  390. pfile->pvdata.dwWinlen = dwWinlen;
  391. if(D==0)
  392. pfile->pvdata.dwOverlap = N/8;
  393. else
  394. pfile->pvdata.dwOverlap = D;
  395. pfile->pvdata.dwFrameAlign = pfile->pvdata.nAnalysisBins * 2 * sizeof(float);
  396. pfile->pvdata.fAnalysisRate = (float)srate / (float) pfile->pvdata.dwOverlap;
  397. pfile->pvdata.fWindowParam = winparam;
  398. pfile->to_delete = 0;
  399. pfile->readonly = 0;
  400. pfile->needsupdate = 0; //could be overruled later, but only once!
  401. if(fWindow!= NULL){
  402. pfile->customWindow = malloc(dwWinlen * sizeof(float));
  403. if(pfile->customWindow==NULL){
  404. pv_errstr = "\npvsys: no memory for custom window";
  405. return -1;
  406. }
  407. memcpy((char *)(pfile->customWindow),(char *)fWindow,dwWinlen * sizeof(float));
  408. }
  409. pfile->fd = open(filename,O_BINARY | O_CREAT | O_RDWR | O_TRUNC,_S_IWRITE | _S_IREAD);
  410. if(pfile->fd < 0){
  411. free(pname);
  412. if(pfile->customWindow)
  413. free(pfile->customWindow);
  414. free(pfile);
  415. pv_errstr = "\npvsys: unable to create file";
  416. return -1;
  417. }
  418. pfile->propsoffset = 0;
  419. pfile->datachunkoffset = 0;
  420. pfile->nFrames = 0;
  421. pfile->FramePos = 0;
  422. pfile->curpos = 0;
  423. pfile->name = pname;
  424. pfile->do_byte_reverse = !byte_order();
  425. files[i] = pfile;
  426. if(!pvoc_writeheader(i)) {
  427. close(pfile->fd);
  428. remove(pfile->name);
  429. free(pfile->name);
  430. if(pfile->customWindow)
  431. free(pfile->customWindow);
  432. free(pfile);
  433. files[i] = NULL;
  434. return -1;
  435. }
  436. return i;
  437. }
  438. int32_t pvoc_getpvxprops(int32_t ifd, PVOCDATA *data)
  439. {
  440. if(files[ifd]==NULL)
  441. return 0;
  442. #ifdef _DEBUG
  443. assert(data != NULL);
  444. #endif
  445. memcpy((char*) data,(char*) &files[ifd]->pvdata,sizeof(PVOCDATA));
  446. return 1;
  447. }
  448. /* tell pvsys to update pvocdata on close; return 0 for success */
  449. /* only other way to do this is to test initial params != 0 */
  450. /* I want to control this explicitly, for now */
  451. int32_t pvoc_set_needsupdate(int32_t ifd)
  452. {
  453. if(files[ifd]==NULL)
  454. return -1;
  455. if(files[ifd]->readonly)
  456. return -1;
  457. files[ifd]->needsupdate = 1;
  458. return 0;
  459. }
  460. //returns 0 for success
  461. int32_t pvoc_canupdate(int32_t ifd)
  462. {
  463. if(files[ifd]==NULL || files[ifd]->readonly)
  464. return -1;
  465. if(files[ifd]->needsupdate)
  466. return 0;
  467. else
  468. return -1;
  469. }
  470. //ONLY when creating new file using 2-stages.called last thing before file close.
  471. #if 0
  472. int32_t pvoc_updateprops(int32_t fd, const PVOCDATA *data)
  473. {
  474. DWORD pos;
  475. if(files[fd]==NULL)
  476. return -1;
  477. if(files[fd]->readonly)
  478. return -1;
  479. if(files[fd]->needsupdate==0)
  480. return -1;
  481. #ifdef _DEBUG
  482. // the only field that MUST be set, needed for read/write frames
  483. assert(files[fd]->pvdata.nAnalysisBins == data->nAnalysisBins);
  484. #endif
  485. // need to seek to propsoffset, then we can call write_pvocdata
  486. pos = lseek(files[fd]->fd,files[fd]->propsoffset,SEEK_SET);
  487. if(pos != files[fd]->propsoffset){
  488. pv_errstr = "\npvsys: error updating pvoc props";
  489. return 0;
  490. }
  491. return 1;
  492. }
  493. #endif
  494. int32_t pvoc_openfile(const char *filename,PVOCDATA *data,WAVEFORMATEX *fmt)
  495. {
  496. int32_t i;
  497. WAVEFORMATPVOCEX wfpx;
  498. char *pname;
  499. PVOCFILE *pfile = NULL;
  500. // long size = sizeof(WAVEFORMATPVOCEX);
  501. if(data==NULL || fmt==NULL){
  502. pv_errstr = "\npvsys: Internal error: NULL data arrays";
  503. return -1;
  504. }
  505. for(i=0;i < MAXFILES;i++)
  506. if(files[i]==NULL)
  507. break;
  508. if(i==MAXFILES){
  509. pv_errstr = "\npvsys: too many files open";
  510. return -1;
  511. }
  512. pfile = (PVOCFILE *) malloc(sizeof(PVOCFILE));
  513. if(pfile==NULL){
  514. pv_errstr = "\npvsys: no memory for file data";
  515. return -1;
  516. }
  517. pfile->customWindow = NULL;
  518. pname = (char *) malloc(strlen(filename)+1);
  519. if(pname == NULL){
  520. free(pfile);
  521. pv_errstr = "\npvsys: no memory";
  522. return -1;
  523. }
  524. pfile->fd = open(filename,O_BINARY | O_RDONLY,_S_IREAD);
  525. if(pfile->fd < 0){
  526. free(pname);
  527. free(pfile);
  528. pv_errstr = "\npvsys: unable to create file";
  529. return -1;
  530. }
  531. strcpy(pname,filename);
  532. pfile->datachunkoffset = 0;
  533. pfile->nFrames = 0;
  534. pfile->curpos = 0;
  535. pfile->FramePos = 0;
  536. pfile->name = pname;
  537. pfile->do_byte_reverse = !byte_order();
  538. pfile->readonly = 1;
  539. pfile->needsupdate = 0; // just to state it explicitly!
  540. pfile->to_delete = 0;
  541. files[i] = pfile;
  542. if(!pvoc_readheader(i,&wfpx)){
  543. close(pfile->fd);
  544. free(pfile->name);
  545. if(pfile->customWindow)
  546. free(pfile->customWindow);
  547. free(pfile);
  548. files[i] = NULL;
  549. return -1;
  550. }
  551. memcpy((char *)data, (char *)&(wfpx.data),sizeof(PVOCDATA));
  552. memcpy((char *)fmt,(char *)&(wfpx.wxFormat.Format),SIZEOF_WFMTEX);
  553. files[i] = pfile;
  554. return i;
  555. }
  556. static int32_t pvoc_readfmt(int32_t fd,int32_t byterev,WAVEFORMATPVOCEX *pWfpx)
  557. {
  558. DWORD dword;
  559. WORD word;
  560. #ifdef _DEBUG
  561. assert(fd >= 0);
  562. assert(pWfpx);
  563. #endif
  564. if(read(fd,(char *) &(pWfpx->wxFormat.Format.wFormatTag),sizeof(WORD)) != sizeof(WORD)
  565. || read(fd,(char *) &(pWfpx->wxFormat.Format.nChannels),sizeof(WORD)) != sizeof(WORD)
  566. || read(fd,(char *) &(pWfpx->wxFormat.Format.nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  567. || read(fd,(char *) &(pWfpx->wxFormat.Format.nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
  568. || read(fd,(char *) &(pWfpx->wxFormat.Format.nBlockAlign),sizeof(WORD)) != sizeof(WORD)
  569. || read(fd,(char *) &(pWfpx->wxFormat.Format.wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
  570. || read(fd,(char *) &(pWfpx->wxFormat.Format.cbSize),sizeof(WORD)) != sizeof(WORD)){
  571. pv_errstr = "\npvsys: error reading Source format data";
  572. return 0;
  573. }
  574. if(byterev){
  575. word = pWfpx->wxFormat.Format.wFormatTag;
  576. pWfpx->wxFormat.Format.wFormatTag= REVWBYTES(word);
  577. word = pWfpx->wxFormat.Format.nChannels;
  578. pWfpx->wxFormat.Format.nChannels = REVWBYTES(word);
  579. dword = pWfpx->wxFormat.Format.nSamplesPerSec;
  580. pWfpx->wxFormat.Format.nSamplesPerSec = REVDWBYTES(dword);
  581. dword = pWfpx->wxFormat.Format.nAvgBytesPerSec;
  582. pWfpx->wxFormat.Format.nAvgBytesPerSec = REVDWBYTES(dword);
  583. word = pWfpx->wxFormat.Format.nBlockAlign;
  584. pWfpx->wxFormat.Format.nBlockAlign = REVWBYTES(word);
  585. word = pWfpx->wxFormat.Format.wBitsPerSample;
  586. pWfpx->wxFormat.Format.wBitsPerSample = REVWBYTES(word);
  587. word = pWfpx->wxFormat.Format.cbSize;
  588. pWfpx->wxFormat.Format.cbSize = REVWBYTES(word);
  589. }
  590. /* the first clues this is pvx format...*/
  591. if(pWfpx->wxFormat.Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE){
  592. pv_errstr = "\npvsys: not a WAVE_EX file";
  593. return 0;
  594. }
  595. if(pWfpx->wxFormat.Format.cbSize != 62){
  596. pv_errstr = "\npvsys: bad size for fmt chunk";
  597. return 0;
  598. }
  599. if(read(fd,(char *) &(pWfpx->wxFormat.Samples.wValidBitsPerSample),sizeof(WORD)) != sizeof(WORD)
  600. || read(fd,(char *) &(pWfpx->wxFormat.dwChannelMask),sizeof(DWORD)) != sizeof(DWORD)
  601. || read(fd,(char *) &(pWfpx->wxFormat.SubFormat),sizeof(GUID)) != sizeof(GUID)){
  602. pv_errstr = "\npvsys: error reading Extended format data";
  603. return 0;
  604. }
  605. if(byterev){
  606. word = pWfpx->wxFormat.Samples.wValidBitsPerSample;
  607. pWfpx->wxFormat.Samples.wValidBitsPerSample = REVWBYTES(word);
  608. dword = pWfpx->wxFormat.dwChannelMask;
  609. pWfpx->wxFormat.dwChannelMask = REVDWBYTES(dword);
  610. dword = pWfpx->wxFormat.SubFormat.Data1;
  611. pWfpx->wxFormat.SubFormat.Data1 = REVDWBYTES(dword);
  612. word = pWfpx->wxFormat.SubFormat.Data2;
  613. pWfpx->wxFormat.SubFormat.Data2 = REVWBYTES(word);
  614. word = pWfpx->wxFormat.SubFormat.Data3;
  615. pWfpx->wxFormat.SubFormat.Data3 = REVWBYTES(word);
  616. /* don't need to reverse the char array */
  617. }
  618. /* ... but this is the clincher */
  619. if(!compare_guids(&(pWfpx->wxFormat.SubFormat),&KSDATAFORMAT_SUBTYPE_PVOC)){
  620. pv_errstr = "\npvsys: not a PVOCEX file";
  621. return 0;
  622. }
  623. if(read(fd,(char *) &(pWfpx->dwVersion),sizeof(DWORD)) != sizeof(DWORD)
  624. || read(fd,(char *) &(pWfpx->dwDataSize),sizeof(DWORD)) != sizeof(DWORD)
  625. || read(fd,(char *) &(pWfpx->data),sizeof(PVOCDATA)) != sizeof(PVOCDATA)){
  626. pv_errstr = "\npvsys: error reading Extended pvoc format data";
  627. return 0;
  628. }
  629. if(byterev){
  630. SND_SAMP ssamp;
  631. dword = pWfpx->dwVersion;
  632. pWfpx->dwVersion = REVDWBYTES(dword);
  633. /* check it now! */
  634. if(pWfpx->dwVersion != PVX_VERSION){
  635. pv_errstr = "\npvsys: unknown pvocex Version";
  636. return 0;
  637. }
  638. dword = pWfpx->dwDataSize;
  639. pWfpx->dwDataSize = REVDWBYTES(dword);
  640. word = pWfpx->data.wWordFormat;
  641. pWfpx->data.wWordFormat= REVWBYTES(word);
  642. word = pWfpx->data.wAnalFormat;
  643. pWfpx->data.wAnalFormat= REVWBYTES(word);
  644. word = pWfpx->data.wSourceFormat;
  645. pWfpx->data.wSourceFormat= REVWBYTES(word);
  646. word = pWfpx->data.wWindowType;
  647. pWfpx->data.wWindowType= REVWBYTES(word);
  648. dword = pWfpx->data.nAnalysisBins;
  649. pWfpx->data.nAnalysisBins = REVDWBYTES(dword);
  650. dword = pWfpx->data.dwWinlen;
  651. pWfpx->data.dwWinlen = REVDWBYTES(dword);
  652. dword = pWfpx->data.dwOverlap;
  653. pWfpx->data.dwOverlap = REVDWBYTES(dword);
  654. dword = pWfpx->data.dwFrameAlign;
  655. pWfpx->data.dwFrameAlign = REVDWBYTES(dword);
  656. ssamp.fsamp = pWfpx->data.fAnalysisRate;
  657. //dword = * (DWORD *)&(pWfpx->data.fAnalysisRate); /* RWD TODO: use union */
  658. //dword = REVDWBYTES(dword);
  659. ssamp.lsamp = REVDWBYTES(ssamp.lsamp);
  660. //pWfpx->data.fAnalysisRate = *(float *)&dword;
  661. pWfpx->data.fAnalysisRate = ssamp.fsamp;
  662. ssamp.fsamp = pWfpx->data.fWindowParam;
  663. //dword = * (DWORD *)&(pWfpx->data.fWindowParam);
  664. //dword = REVDWBYTES(dword);
  665. ssamp.lsamp = REVDWBYTES(ssamp.lsamp);
  666. //pWfpx->data.fWindowParam = *(float *)&dword;
  667. pWfpx->data.fWindowParam = ssamp.fsamp;
  668. }
  669. if(pWfpx->dwVersion != PVX_VERSION){
  670. pv_errstr = "\npvsys: unknown pvocex Version";
  671. return 0;
  672. }
  673. return 1;
  674. }
  675. static int32_t pvoc_readheader(int32_t ifd,WAVEFORMATPVOCEX *pWfpx)
  676. {
  677. DWORD tag, size,riffsize;
  678. int32_t fmtseen = 0, /* dataseen = 0,*/ windowseen = 0;
  679. // DWORD windowlength = 0;
  680. #ifdef _DEBUG
  681. assert(pWfpx);
  682. assert(files[ifd]);
  683. assert(files[ifd]->fd >= 0);
  684. size = sizeof(WAVEFORMATEXTENSIBLE);
  685. size += 2 * sizeof(DWORD);
  686. size += sizeof(PVOCDATA);
  687. #endif
  688. if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)
  689. || read(files[ifd]->fd,(char *) &size,sizeof(DWORD)) != sizeof(DWORD)){
  690. pv_errstr = "\npvsys: error reading header";
  691. return 0;
  692. }
  693. if(files[ifd]->do_byte_reverse)
  694. size = REVDWBYTES(size);
  695. else
  696. tag = REVDWBYTES(tag);
  697. if(tag != TAG('R','I','F','F')){
  698. pv_errstr = "\npvsys: not a RIFF file";
  699. return 0;
  700. }
  701. if(size < 24 * sizeof(DWORD) + SIZEOF_FMTPVOCEX){
  702. pv_errstr = "\npvsys: file too small";
  703. return 0;
  704. }
  705. riffsize = size;
  706. if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)){
  707. pv_errstr = "\npvsys: error reading header";
  708. return 0;
  709. }
  710. if(!files[ifd]->do_byte_reverse)
  711. tag = REVDWBYTES(tag);
  712. if(tag != TAG('W','A','V','E')){
  713. pv_errstr = "\npvsys: not a WAVE file";
  714. return 0;
  715. }
  716. riffsize -= sizeof(DWORD);
  717. /*loop for chunks */
  718. while(riffsize > 0){
  719. if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)
  720. || read(files[ifd]->fd,(char *) &size,sizeof(DWORD)) != sizeof(DWORD)){
  721. pv_errstr = "\npvsys: error reading header";
  722. return 0;
  723. }
  724. if(files[ifd]->do_byte_reverse)
  725. size = REVDWBYTES(size);
  726. else
  727. tag = REVDWBYTES(tag);
  728. riffsize -= 2 * sizeof(DWORD);
  729. switch(tag){
  730. case TAG('f','m','t',' '):
  731. /* bail out if not a pvoc file: not trying to read all WAVE formats!*/
  732. if(size < SIZEOF_FMTPVOCEX){
  733. pv_errstr = "\npvsys: not a PVOC-EX file";
  734. return 0;
  735. }
  736. if(!pvoc_readfmt(files[ifd]->fd,files[ifd]->do_byte_reverse,pWfpx)){
  737. pv_errstr = "\npvsys: error reading format chunk";
  738. return 0;
  739. }
  740. riffsize -= SIZEOF_FMTPVOCEX;
  741. fmtseen = 1;
  742. memcpy((char *)&(files[ifd]->fmtdata),(char *)&(pWfpx->wxFormat),SIZEOF_WFMTEX);
  743. memcpy((char *)&(files[ifd]->pvdata),(char *)&(pWfpx->data),sizeof(PVOCDATA));
  744. break;
  745. case TAG('P','V','X','W'):
  746. if(!fmtseen){
  747. pv_errstr = "\npvsys: PVXW chunk found before fmt chunk.";
  748. return 0;
  749. }
  750. if(files[ifd]->pvdata.wWindowType!=PVOC_CUSTOM){
  751. /*whaddayado? can you warn the user and continue?*/
  752. pv_errstr = "\npvsys: PVXW chunk found but custom window not specified";
  753. return 0;
  754. }
  755. files[ifd]->customWindow = malloc(files[ifd]->pvdata.dwWinlen * sizeof(float));
  756. if(files[ifd]->customWindow == NULL){
  757. pv_errstr = "\npvsys: no memory for custom window data.";
  758. return 0;
  759. }
  760. if(pvoc_readWindow(files[ifd]->fd,files[ifd]->do_byte_reverse,
  761. files[ifd]->customWindow,files[ifd]->pvdata.dwWinlen)
  762. != (int)(files[ifd]->pvdata.dwWinlen * sizeof(float))){
  763. pv_errstr = "\npvsys: error reading window data.";
  764. return 0;
  765. }
  766. windowseen = 1;
  767. break;
  768. case TAG('d','a','t','a'):
  769. if(riffsize - size != 0){
  770. pv_errstr = "\npvsys: bad RIFF file";
  771. return 0;
  772. }
  773. if(!fmtseen){
  774. pv_errstr = "\npvsys: bad format, data chunk before fmt chunk";
  775. return 0;
  776. }
  777. if(files[ifd]->pvdata.wWindowType==PVOC_CUSTOM)
  778. if(!windowseen){
  779. pv_errstr = "\npvsys: custom window chunk PVXW not found";
  780. return 0;
  781. }
  782. files[ifd]->datachunkoffset = lseek(files[ifd]->fd,0,SEEK_CUR);
  783. files[ifd]->curpos = files[ifd]->datachunkoffset;
  784. /* not m/c frames, for now */
  785. files[ifd]->nFrames = size / files[ifd]->pvdata.dwFrameAlign;
  786. return 1;
  787. break;
  788. default:
  789. /* skip any onknown chunks */
  790. riffsize -= 2 * sizeof(DWORD);
  791. if(lseek(files[ifd]->fd,size,SEEK_CUR) < 0){
  792. pv_errstr = "\npvsys: error skipping unknown WAVE chunk";
  793. return 0;
  794. }
  795. riffsize -= size;
  796. break;
  797. }
  798. }
  799. /* if here, something very wrong!*/
  800. pv_errstr = "\npvsys: bad format in RIFF file";
  801. return 0;
  802. }
  803. static int32_t pvoc_writeheader(int ofd)
  804. {
  805. int32_t tag,size,version;
  806. WORD validbits;
  807. const GUID *pGuid = &KSDATAFORMAT_SUBTYPE_PVOC;
  808. #ifdef _DEBUG
  809. assert(files[ofd] != NULL);
  810. assert(files[ofd]->fd >=0);
  811. #endif
  812. tag = TAG('R','I','F','F');
  813. size = 0;
  814. if(files[ofd]->do_byte_reverse)
  815. size = REVDWBYTES(size);
  816. if(!files[ofd]->do_byte_reverse)
  817. tag = REVDWBYTES(tag);
  818. if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)
  819. || write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)) {
  820. pv_errstr = "\npvsys: error writing header";
  821. return 0;
  822. }
  823. tag = TAG('W','A','V','E');
  824. if(!files[ofd]->do_byte_reverse)
  825. tag = REVDWBYTES(tag);
  826. if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)){
  827. pv_errstr = "\npvsys: error writing header";
  828. return 0;
  829. }
  830. tag = TAG('f','m','t',' ');
  831. size = SIZEOF_WFMTEX + sizeof(WORD) +
  832. sizeof(DWORD)
  833. + sizeof(GUID)
  834. + 2*sizeof(DWORD)
  835. + sizeof(PVOCDATA);
  836. if(files[ofd]->do_byte_reverse)
  837. size = REVDWBYTES(size);
  838. if(!files[ofd]->do_byte_reverse)
  839. tag = REVDWBYTES(tag);
  840. if(write(files[ofd]->fd,(char *)&tag,sizeof(int32_t)) != sizeof(int32_t)
  841. || write(files[ofd]->fd,(char *)&size,sizeof(int32_t)) != sizeof(int32_t)) {
  842. pv_errstr = "\npvsys: error writing header";
  843. return 0;
  844. }
  845. /* we need to record where we are, as we may have to update fmt data before file close */
  846. files[ofd]->fmtchunkoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
  847. if(write_fmt(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->fmtdata)) != SIZEOF_WFMTEX){
  848. pv_errstr = "\npvsys: error writing fmt chunk";
  849. return 0;
  850. }
  851. validbits = files[ofd]->fmtdata.wBitsPerSample; /*nothing fancy here */
  852. if(files[ofd]->do_byte_reverse)
  853. validbits = REVWBYTES(validbits);
  854. if(write(files[ofd]->fd,(char *) &validbits,sizeof(WORD)) != sizeof(WORD)){
  855. pv_errstr = "\npvsys: error writing fmt chunk";
  856. return 0;
  857. }
  858. /* we will take this from a WAVE_EX file, in due course */
  859. size = 0; /*dwChannelMask*/
  860. if(write(files[ofd]->fd,(char *)&size,sizeof(DWORD)) != sizeof(DWORD)){
  861. pv_errstr = "\npvsys: error writing fmt chunk";
  862. return 0;
  863. }
  864. if(write_guid(files[ofd]->fd,files[ofd]->do_byte_reverse,pGuid) != sizeof(GUID)){
  865. pv_errstr = "\npvsys: error writing fmt chunk";
  866. return 0;
  867. }
  868. version = 1;
  869. size = sizeof(PVOCDATA);
  870. if(files[ofd]->do_byte_reverse){
  871. version = REVDWBYTES(version);
  872. size = REVDWBYTES(size);
  873. }
  874. if(write(files[ofd]->fd,&version,sizeof(int32_t)) != sizeof(int32_t)
  875. || write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)){
  876. pv_errstr = "\npvsys: error writing fmt chunk";
  877. return 0;
  878. }
  879. /* RWD new 2022 so we can update all pvocex props on closing new file */
  880. files[ofd]->propsoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
  881. if(write_pvocdata(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->pvdata)) != sizeof(PVOCDATA)){
  882. pv_errstr = "\npvsys: error writing fmt chunk";
  883. return 0;
  884. }
  885. /* VERY experimental; may not even be a good idea...*/
  886. if(files[ofd]->customWindow){
  887. tag = TAG('P','V','X','W');
  888. size = files[ofd]->pvdata.dwWinlen * sizeof(float);
  889. if(files[ofd]->do_byte_reverse)
  890. size = REVDWBYTES(size);
  891. else
  892. tag = REVDWBYTES(tag);
  893. if(write(files[ofd]->fd,(char *)&tag,sizeof(int32_t)) != sizeof(int32_t)
  894. || write(files[ofd]->fd,(char *)&size,sizeof(int32_t)) != sizeof(int32_t)) {
  895. pv_errstr = "\npvsys: error writing header";
  896. return 0;
  897. }
  898. if(pvoc_writeWindow(files[ofd]->fd,
  899. files[ofd]->do_byte_reverse,
  900. files[ofd]->customWindow,
  901. files[ofd]->pvdata.dwWinlen)!= (int)(files[ofd]->pvdata.dwWinlen * sizeof(float))){
  902. pv_errstr = "\npvsys: error writing window data.";
  903. return 0;
  904. }
  905. }
  906. /* no other chunks to write yet! */
  907. tag = TAG('d','a','t','a');
  908. if(!files[ofd]->do_byte_reverse)
  909. tag = REVDWBYTES(tag);
  910. if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)){
  911. pv_errstr = "\npvsys: write error writing header";
  912. return 0;
  913. }
  914. /* we need to update size later on...*/
  915. size = 0;
  916. if(write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)){
  917. pv_errstr = "\npvsys: write error writing header";
  918. return 0;
  919. }
  920. files[ofd]->datachunkoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
  921. files[ofd]->curpos = files[ofd]->datachunkoffset;
  922. return 1;
  923. }
  924. /* called by pvoc_closefile() */
  925. /* which checks "readonly", if not set, file is newly created */
  926. static int32_t pvoc_updateheader(int ofd)
  927. {
  928. int32_t riffsize,datasize;
  929. DWORD pos;
  930. #ifdef _DEBUG
  931. assert(files[ofd]);
  932. assert(files[ofd]->fd >= 0);
  933. //assert(files[ofd]->curpos == lseek(files[ofd]->fd,0,SEEK_CUR));
  934. assert(!files[ofd]->readonly);
  935. #endif
  936. datasize = files[ofd]->curpos - files[ofd]->datachunkoffset;
  937. pos = lseek(files[ofd]->fd,files[ofd]->datachunkoffset-sizeof(DWORD),SEEK_SET);
  938. if(pos != files[ofd]->datachunkoffset-sizeof(DWORD)){
  939. pv_errstr = "\npvsys: seek error updating data chunk";
  940. return 0;
  941. }
  942. if(files[ofd]->do_byte_reverse)
  943. datasize = REVDWBYTES(datasize);
  944. if(write(files[ofd]->fd,(char *) &datasize,sizeof(DWORD)) != sizeof(DWORD)){
  945. pv_errstr = "\npvsys: write error updating data chunk";
  946. return 0;
  947. }
  948. riffsize = files[ofd]->curpos - 2* sizeof(DWORD);
  949. if(files[ofd]->do_byte_reverse)
  950. riffsize = REVDWBYTES(riffsize);
  951. pos = lseek(files[ofd]->fd,sizeof(DWORD),SEEK_SET);
  952. if(pos != sizeof(DWORD)){
  953. pv_errstr = "\npvsys: seek error updating riff chunk";
  954. return 0;
  955. }
  956. if(write(files[ofd]->fd,(char *) &riffsize,sizeof(DWORD)) != sizeof(DWORD)){
  957. pv_errstr = "\npvsys: write error updating riff chunk";
  958. return 0;
  959. }
  960. // update PVOCDATA and WAVEFORMATEX fields if required
  961. if(files[ofd]->needsupdate){
  962. #ifdef _DEBUG
  963. fprintf(stderr,"updating in pvoc_update_header()\n");
  964. #endif
  965. WORD validbits;
  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. // need to update validbits in case we need it, at least make it sensible
  977. // this should have been updated earlier
  978. validbits = files[ofd]->fmtdata.wBitsPerSample;
  979. if(files[ofd]->do_byte_reverse)
  980. validbits = REVWBYTES(validbits);
  981. if(write(files[ofd]->fd,(char *) &validbits,sizeof(WORD)) != sizeof(WORD)){
  982. pv_errstr = "\npvsys: error writing extended fmt chunk";
  983. return 0;
  984. }
  985. pos = lseek(files[ofd]->fd,files[ofd]->propsoffset,SEEK_SET);
  986. if(pos != files[ofd]->propsoffset){
  987. pv_errstr = "\npvsys: seek error updating pvx data";
  988. return 0;
  989. }
  990. //int32_t write_pvocdata(int32_t fd,int32_t byterev,const PVOCDATA *pData)
  991. pos = write_pvocdata(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->pvdata));
  992. if(pos != sizeof(PVOCDATA)){
  993. pv_errstr = "\npvsys: write error updating pvx data";
  994. return 0;
  995. }
  996. }
  997. pos = lseek(files[ofd]->fd,0,SEEK_END);
  998. if(pos < 0){
  999. pv_errstr = "\npvsys: seek error seeking to end of file";
  1000. return 0;
  1001. }
  1002. return 1;
  1003. }
  1004. int32_t pvoc_update_closefile(int ofd, const PVOCDATA *data, const WAVEFORMATEXTENSIBLE *wfx)
  1005. {
  1006. if(files[ofd]==NULL){
  1007. pv_errstr = "\npvsys: file does not exist";
  1008. return 0;
  1009. }
  1010. if(files[ofd]->fd < 0){
  1011. pv_errstr = "\npvsys: file not open";
  1012. return 0;
  1013. }
  1014. if(files[ofd]->readonly) {
  1015. pv_errstr = "\ncannot update readonly file"; // probably an input file
  1016. }
  1017. else {
  1018. memcpy((char*) &files[ofd]->fmtdata, (char*) &wfx->Format, sizeof(WAVEFORMATEX));
  1019. memcpy((char*) &files[ofd]->pvdata, (char*) data, sizeof(PVOCDATA));
  1020. }
  1021. return pvoc_closefile(ofd);
  1022. }
  1023. int32_t pvoc_closefile(int ofd)
  1024. {
  1025. if(files[ofd]==NULL){
  1026. pv_errstr = "\npvsys: file does not exist";
  1027. return 0;
  1028. }
  1029. if(files[ofd]->fd < 0){
  1030. pv_errstr = "\npvsys: file not open";
  1031. return 0;
  1032. }
  1033. if(!files[ofd]->readonly) {
  1034. if(!pvoc_updateheader(ofd))
  1035. return 0;
  1036. }
  1037. close(files[ofd]->fd);
  1038. if(files[ofd]->to_delete && !(files[ofd]->readonly))
  1039. remove(files[ofd]->name);
  1040. free(files[ofd]->name);
  1041. free(files[ofd]);
  1042. files[ofd] = 0;
  1043. return 1;
  1044. }
  1045. /* does not directly address m/c streams, or alternative numeric formats, yet
  1046. * so for m/c files, write each frame in turn, for each channel.
  1047. * The format requires multi-channel frames to be interleaved in the usual way:
  1048. * if nChannels= 4, the file will contain:
  1049. * frame[0][0],frame[0][1],frame[0][2],frame[0][3],frme[1][0],frame[1][1].....
  1050. *
  1051. * The idea is to offer e.g. a floats version and a longs version ONLY, but
  1052. * independently of the underlying representation, so that the user can write a floats
  1053. * block, even though the underlying format might be longs or doubles. Most importantly,
  1054. * the user does not have to deal with byte-reversal, which would otherwise always be the case
  1055. * it the user had direct access to the file.
  1056. *
  1057. * So these functions are the most likely to change over time!.
  1058. *
  1059. * return 0 for error, 1 for success. This could change....
  1060. */
  1061. int32_t pvoc_putframes(int ofd,const float *frame,int32_t numframes)
  1062. {
  1063. DWORD i;
  1064. DWORD towrite; /* count in 'words' */
  1065. int32_t temp,*lfp;
  1066. if(files[ofd]==NULL){
  1067. pv_errstr = "\npvsys: bad file descriptor";
  1068. return 0;
  1069. }
  1070. if(files[ofd]->fd < 0){
  1071. pv_errstr = "\npvsys: file not open";
  1072. return 0;
  1073. }
  1074. /* NB doubles not supported yet */
  1075. towrite = files[ofd]->pvdata.nAnalysisBins * 2 * numframes;
  1076. if(files[ofd]->do_byte_reverse){
  1077. /* do this without overwriting source data! */
  1078. lfp = (int32_t *) frame;
  1079. for(i=0;i < towrite; i++){
  1080. temp = *lfp++;
  1081. temp = REVDWBYTES(temp);
  1082. if(write(files[ofd]->fd,(char *) &temp,sizeof(int32_t)) != sizeof(int32_t)){
  1083. pv_errstr = "\npvsys: error writing data";
  1084. return 0;
  1085. }
  1086. }
  1087. }
  1088. else {
  1089. if(write(files[ofd]->fd,(char *) frame,towrite * sizeof(float)) != (int32_t)(towrite * sizeof(float))){
  1090. pv_errstr = "\npvsys: error writing data";
  1091. return 0;
  1092. }
  1093. }
  1094. files[ofd]->FramePos += numframes;
  1095. files[ofd]->nFrames += numframes;
  1096. files[ofd]->curpos += towrite * sizeof(float);
  1097. return 1;
  1098. }
  1099. /* Simplistic read function
  1100. * best practice here is to read nChannels frames *
  1101. * return -1 for error, 0 for EOF, else numframes read
  1102. */
  1103. int32_t pvoc_getframes(int32_t ifd,float *frames,DWORD nframes)
  1104. {
  1105. int32_t i;
  1106. int32_t toread;
  1107. int32_t oval,temp,*lfp;
  1108. int32_t got;
  1109. int32_t rc = -1;
  1110. if(files[ifd]==NULL){
  1111. pv_errstr = "\npvsys: bad file descriptor";
  1112. return rc;
  1113. }
  1114. if(files[ifd]->fd < 0){
  1115. pv_errstr = "\npvsys: file not open";
  1116. return rc;
  1117. }
  1118. toread = files[ifd]->pvdata.nAnalysisBins * 2 * nframes;
  1119. if(files[ifd]->do_byte_reverse){
  1120. lfp = (int32_t *) frames;
  1121. #ifdef SINGLE_FLOAT
  1122. for(i=0;i < toread;i++){
  1123. if((got=read(files[ifd]->fd,(char *) &temp,sizeof(int32_t))) <0){
  1124. pv_errstr = "\npvsys: error reading data";
  1125. return rc;
  1126. }
  1127. if(got < sizeof(int32_t)){
  1128. /* file size incorrect? */
  1129. return 0; /* assume EOF */
  1130. }
  1131. temp = REVDWBYTES(temp);
  1132. *lfp++ = temp;
  1133. }
  1134. #else
  1135. /* much faster on G4!!! */
  1136. got = read(files[ifd]->fd,(char *)frames,toread * sizeof(float));
  1137. if(got < 0){
  1138. pv_errstr = "\npvsys: error reading data";
  1139. return rc;
  1140. }
  1141. for(i=0;i < got / sizeof(float);i++){
  1142. temp = *lfp;
  1143. oval = REVDWBYTES(temp);
  1144. *lfp++ = oval;
  1145. }
  1146. if(got < (int)(toread * sizeof(float))){
  1147. /* some (user?) error in file size: return integral number of frames read*/
  1148. toread = got / (files[ifd]->pvdata.nAnalysisBins * 2 * sizeof(float));
  1149. /* RWD 4:2002 need to revise this too */
  1150. nframes = toread;
  1151. }
  1152. #endif
  1153. rc = nframes; /*RWD 4:2002 */
  1154. }
  1155. else{
  1156. if((got = read(files[ifd]->fd,(char *)frames,toread * sizeof(float))) < (int32_t)(toread * sizeof(float))){
  1157. if(got < 0){
  1158. pv_errstr = "\npvsys: error reading data";
  1159. return rc;
  1160. }
  1161. else if(got < (int)(toread * sizeof(float))){
  1162. /* some (user?) error in file size: return integral number of frames read*/
  1163. toread = got / (files[ifd]->pvdata.nAnalysisBins * 2 * sizeof(float));
  1164. rc = toread;
  1165. /* RWD 4:2002 need to revise this too */
  1166. nframes = toread;
  1167. }
  1168. }
  1169. else
  1170. rc = nframes;
  1171. }
  1172. /*files[ifd]->curpos += (toread * sizeof(float));*/
  1173. files[ifd]->curpos += got; /* RWD 4:2002 */
  1174. files[ifd]->FramePos += nframes;
  1175. return rc;
  1176. }
  1177. // return 0 for success, -1 for error
  1178. int32_t pvoc_rewind(int32_t ifd,int32_t skip_first_frame)
  1179. {
  1180. int32_t rc = -1;
  1181. int32_t fd;
  1182. int32_t pos;
  1183. int32_t skipsize = 0;
  1184. int32_t skipframes = 0;
  1185. if(files[ifd]==NULL){
  1186. pv_errstr = "\npvsys: bad file descriptor";
  1187. return rc;
  1188. }
  1189. if(files[ifd]->fd < 0){
  1190. pv_errstr = "\npvsys: file not open";
  1191. return rc;
  1192. }
  1193. skipframes = files[ifd]->fmtdata.nChannels;
  1194. //skipsize = files[ifd]->pvdata.dwFrameAlign * skipframes;
  1195. fd = files[ifd]->fd;
  1196. pos = files[ifd]->datachunkoffset;
  1197. if(skip_first_frame){
  1198. skipsize = files[ifd]->pvdata.dwFrameAlign * skipframes;
  1199. pos += skipsize;
  1200. }
  1201. if(lseek(fd,pos,SEEK_SET) != pos ) {
  1202. pv_errstr = "\npvsys: error rewinding file";
  1203. return rc;
  1204. }
  1205. files[ifd]->curpos = /* files[ifd]->datachunkoffset + skipsize */ pos;
  1206. files[ifd]->FramePos = skipframes;
  1207. return 0;
  1208. }
  1209. /* may be more to do in here later on */
  1210. int32_t pvsys_release(void)
  1211. {
  1212. int32_t i;
  1213. for(i=0;i < MAXFILES;i++){
  1214. if(files[i]) {
  1215. #ifdef _DEBUG
  1216. fprintf(stderr,"\nDEBUG WARNING: file %d still open!\n",i);
  1217. #endif
  1218. if(!pvoc_closefile(i)){
  1219. pv_errstr = "\npvsys: unable to close file on termination";
  1220. return 0;
  1221. }
  1222. }
  1223. }
  1224. return 1;
  1225. }
  1226. /*return raw framecount: channel-agnostic for now */
  1227. int32_t pvoc_framecount(int32_t ifd)
  1228. {
  1229. if(files[ifd]==NULL)
  1230. return -1;
  1231. return files[ifd]->nFrames;
  1232. }
  1233. /* RWD Jan 2014 */
  1234. // return -1 for error, else framepos
  1235. int32_t pvoc_framepos(int32_t ifd)
  1236. {
  1237. if(files[ifd]==NULL)
  1238. return -1;
  1239. return files[ifd]->FramePos;
  1240. }
  1241. // return -1 for error, 0 for success
  1242. //NB framecount always disregards chans. But offset represents m/c block: offset*2 to get frames
  1243. int32_t pvoc_seek_mcframe(int32_t ifd, int32_t offset, int32_t mode)
  1244. {
  1245. // DWORD mcframealign;
  1246. DWORD rawoffset;
  1247. int32_t rc = -1;
  1248. int32_t mcframealign;
  1249. if(files[ifd]==NULL)
  1250. return -1;
  1251. mcframealign = files[ifd]->pvdata.dwFrameAlign * files[ifd]->fmtdata.nChannels;
  1252. rawoffset = offset * mcframealign;
  1253. switch(mode){
  1254. case SEEK_SET:
  1255. // offset is m/c quantity, e.g. 2 * frame for stereo
  1256. if(offset >= (files[ifd]->nFrames / files[ifd]->fmtdata.nChannels)){
  1257. pv_errstr = "\npvsys: seek target beyond end of file";
  1258. break;
  1259. }
  1260. rawoffset += files[ifd]->datachunkoffset;
  1261. if(lseek(files[ifd]->fd,rawoffset,SEEK_SET) != rawoffset ) {
  1262. pv_errstr = "\npvsys: seek error, SEEK_SET";
  1263. return -1;
  1264. }
  1265. files[ifd]->curpos = rawoffset;
  1266. files[ifd]->FramePos = offset * files[ifd]->fmtdata.nChannels;
  1267. rc = 0;
  1268. break;
  1269. case SEEK_END:
  1270. // go to end of file + offset, offset <= 0
  1271. if(offset > 0){
  1272. pv_errstr = "\npvsys: seek target before start of file, offset must be <= 0";
  1273. break;
  1274. }
  1275. #ifdef _DEBUG
  1276. fprintf(stderr,"pvoc_seek_mcframe: fd = %d\n",ifd);
  1277. #endif
  1278. //NB not relative to datachunkoffset in this case
  1279. if(lseek(files[ifd]->fd,rawoffset,SEEK_END) < 0){
  1280. pv_errstr = "\npvsys: seek error, SEEK_END";
  1281. return -1;
  1282. }
  1283. #ifdef _DEBUG
  1284. fprintf(stderr,"pvoc_seek_mcframe: files[%d]->nFrames = %d\n",ifd,files[ifd]->nFrames);
  1285. #endif
  1286. files[ifd]->FramePos = files[ifd]->nFrames - (offset * files[ifd]->fmtdata.nChannels);
  1287. files[ifd]->curpos = files[ifd]->FramePos * files[ifd]->pvdata.dwFrameAlign;
  1288. #ifdef _DEBUG
  1289. fprintf(stderr,"pvoc_seek_mcframe: got curpos = %d\n",files[ifd]->curpos);
  1290. #endif
  1291. rc = 0; //success!
  1292. break;
  1293. case SEEK_CUR:
  1294. // covered by pvoc_framepos(), equiv to a pvoc_tell() function.
  1295. pv_errstr = "\npvsys: SEEK_CUR mode not supported yet!";
  1296. break;
  1297. }
  1298. return rc;
  1299. }