pvfileio.c 47 KB

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