| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486 |
- /* pvfileio.c */
- /* pvocex format test routines*/
- /* Initial version RWD May 2000.
- * All rights reserved: work in progress!
- * Manifestly not a complete API yet!
- * In particular, error returns are kept very simplistic at the moment.
- * (and are not even very consistent yet...)
- * In time, a full set of error values and messages will be developed.
- *
- * NB: the RIFF<WAVE> functions only look for, and accept, a PVOCEX format file.
- * NB also: if windows.h is included anywhere (should be no need in this file,
- * or in pvfileio.h),
- * the WAVE_FORMAT~ symbols may need to be #ifndef-ed.
- */
- /* very simple CUSTOM window chunk:
- *
- * <PVXW><size><data>
- *
- * where size as usual gives the size of the data in bytes.
- * the size in samples much match dwWinlen (which may not be the same as N (fft length)
- * the sample type must be the same as that of the pvoc data itself
- * (only floatsams supported so far)
- * values must be normalized to peak of 1.0
- */
- //#ifdef WINDOWS
- //#include "stdafx.h"
- //#endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <memory.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #ifdef _WIN32
- #include <io.h>
- #endif
- #ifdef unix
- #include <unistd.h>
- #define O_BINARY (0)
- #define _S_IWRITE S_IWRITE
- #define _S_IREAD S_IREAD
- #endif
- #ifdef _DEBUG
- #include <assert.h>
- #endif
- #include "pvdefs.h"
- #include "pvfileio.h"
- /* NB probably no good on 64bit platforms */
- #define REVDWBYTES(t) ( (((t)&0xff) << 24) | (((t)&0xff00) << 8) | (((t)&0xff0000) >> 8) | (((t)>>24) & 0xff) )
- #define REVWBYTES(t) ( (((t)&0xff) << 8) | (((t)>>8) &0xff) )
- #define TAG(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) )
- #define WAVE_FORMAT_EXTENSIBLE (0xFFFE)
- #ifndef WAVE_FORMAT_PCM
- #define WAVE_FORMAT_PCM (0x0001)
- #endif
- #define WAVE_FORMAT_IEEE_FLOAT (0x0003)
- const GUID KSDATAFORMAT_SUBTYPE_PVOC = {
- 0x8312b9c2,
- 0x2e6e,
- 0x11d4,
- { 0xa8, 0x24, 0xde, 0x5b, 0x96, 0xc3, 0xab, 0x21 }
- };
- static char *pv_errstr = "";
- #define MAXFILES (16)
- /* or any desired larger number: will be dynamically allocated one day */
- typedef union {
- int32_t lsamp;
- float fsamp;
- unsigned char bytes[4];
- } SND_SAMP;
- typedef struct pvoc_file {
- WAVEFORMATEX fmtdata;
- PVOCDATA pvdata;
- /* RWD 2022 to help CDP pvocex extension, requires two-stage file creation/update */
- int32_t propsoffset;
- int32_t needsupdate; //for two-stage creation/update CDP-style
- int32_t fmtchunkoffset;
- int32_t datachunkoffset;
- int32_t nFrames; /* no of frames in file */
- int32_t FramePos; /* where we are in file */
- int32_t curpos;
- int32_t fd;
- int32_t to_delete;
- int32_t readonly;
- int32_t do_byte_reverse;
- char *name;
- float *customWindow;
-
- } PVOCFILE;
- static PVOCFILE *files[MAXFILES];
- static int32_t pvoc_writeheader(int32_t ofd);
- static int32_t pvoc_readheader(int32_t ifd,WAVEFORMATPVOCEX *pWfpx);
- int32_t pvoc_getdatasize_bytes(int32_t fd)
- {
- int32_t datachunksize, framesize;
-
- if(files[fd]==NULL)
- return -1;
- if(files[fd]->nFrames == 0)
- return 0;
- framesize = files[fd]->pvdata.nAnalysisBins * 2 * sizeof(float);
- datachunksize = files[fd]->nFrames * framesize * files[fd]->fmtdata.nChannels;
- return datachunksize;
- }
- static int32_t write_guid(int32_t fd,int32_t byterev,const GUID *pGuid)
- {
- int32_t written;
- #ifdef _DEBUG
- assert(fd >= 0);
- assert(pGuid);
- #endif
- if(byterev){
- GUID guid;
- guid.Data1 = REVDWBYTES(pGuid->Data1);
- guid.Data2 = REVWBYTES(pGuid->Data2);
- guid.Data3 = REVWBYTES(pGuid->Data3);
- memcpy((char *) (guid.Data4),(char *) (pGuid->Data4),8);
- written = write(fd,(char *) &guid,sizeof(GUID));
- }
- else
- written = write(fd,(char *) pGuid,sizeof(GUID));
- return written;
- }
- static int32_t compare_guids(const GUID *gleft, const GUID *gright)
- {
- const char *left = (const char *) gleft, *right = (const char *) gright;
- return !memcmp(left,right,sizeof(GUID));
- }
- static int32_t write_pvocdata(int32_t fd,int32_t byterev,const PVOCDATA *pData)
- {
- int32_t written;
- // int32_t dwval;
- #ifdef _DEBUG
- assert(fd >= 0);
- assert(pData);
- #endif
- if(byterev){
- int32_t revdwval;
- PVOCDATA data;
- SND_SAMP ssamp;
- data.wWordFormat = REVWBYTES(pData->wWordFormat);
- data.wAnalFormat = REVWBYTES(pData->wAnalFormat);
- data.wSourceFormat = REVWBYTES(pData->wSourceFormat);
- data.wWindowType = REVWBYTES(pData->wWindowType);
- data.nAnalysisBins = REVDWBYTES(pData->nAnalysisBins);
- data.dwWinlen = REVDWBYTES(pData->dwWinlen);
- data.dwOverlap = REVDWBYTES(pData->dwOverlap);
- data.dwFrameAlign = REVDWBYTES(pData->dwFrameAlign);
-
- ssamp.fsamp = pData->fAnalysisRate;
- //dwval = * (int32_t *) &(pData->fAnalysisRate);
- ssamp.lsamp = REVDWBYTES(/*dwval*/ ssamp.lsamp);
- //data.fAnalysisRate = * (float *) &dwval;
- data.fAnalysisRate = ssamp.fsamp;
-
- ssamp.fsamp = pData->fWindowParam;
- //dwval = * ( int32_t *) &(pData->fWindowParam);
- revdwval = REVDWBYTES(ssamp.lsamp);
- //dwval = REVDWBYTES(dwval);
- //data.fWindowParam = * (float *) &dwval;
- data.fWindowParam = ssamp.fsamp;
-
- written = write(fd,(char *) &data,sizeof(PVOCDATA));
- }
- else
- written = write(fd,(char *) pData,sizeof(PVOCDATA));
- return written;
- }
- static int32_t write_fmt(int fd, int byterev,const WAVEFORMATEX *pfmt)
- {
- /*NB have to write out each element, as not guaranteed alignment othewise.
- * Consider it documentation. */
- #ifdef _DEBUG
- assert(fd >=0);
- assert(pfmt);
- assert(pfmt->cbSize == 62);
- #endif
- if(byterev){
- WAVEFORMATEX fmt;
- fmt.wFormatTag = REVWBYTES(pfmt->wFormatTag);
- fmt.nChannels = REVWBYTES(pfmt->nChannels);
- fmt.nSamplesPerSec = REVDWBYTES(pfmt->nSamplesPerSec);
- fmt.nAvgBytesPerSec = REVDWBYTES(pfmt->nAvgBytesPerSec);
- fmt.nBlockAlign = REVWBYTES(pfmt->nBlockAlign);
- fmt.wBitsPerSample = REVWBYTES(pfmt->wBitsPerSample);
- fmt.cbSize = REVWBYTES(pfmt->cbSize);
- if(write(fd,(char *) &(fmt.wFormatTag),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(fmt.nChannels),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(fmt.nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
- || write(fd,(char *) &(fmt.nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
- || write(fd,(char *) &(fmt.nBlockAlign),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(fmt.wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(fmt.cbSize),sizeof(WORD)) != sizeof(WORD))
- return 0;
- }
- else {
- if(write(fd,(char *) &(pfmt->wFormatTag),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(pfmt->nChannels),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(pfmt->nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
- || write(fd,(char *) &(pfmt->nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
- || write(fd,(char *) &(pfmt->nBlockAlign),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(pfmt->wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
- || write(fd,(char *) &(pfmt->cbSize),sizeof(WORD)) != sizeof(WORD))
- return 0;
- }
- return SIZEOF_WFMTEX;
- }
- static int32_t pvoc_writeWindow(int32_t fd,int32_t byterev,float *window,DWORD length)
- {
- if(byterev){
- /* don't corrupt source array! */
- DWORD i;
- int32_t lval, *lp = (int32_t *) window;
- for(i=0;i < length; i++){
- lval = *lp++;
- lval = REVDWBYTES(lval);
- if(write(fd,(char *)&lval,sizeof(int32_t)) != sizeof(int32_t))
- return 0;
- }
- }
- else{
- if(write(fd,(char *) window,length * sizeof(float)) != (int32_t)(length*sizeof(float)))
- return 0;
- }
- return (int32_t)(length * sizeof(float));
- }
- static int32_t pvoc_readWindow(int fd, int byterev, float *window,DWORD length)
- {
- if(byterev){
- DWORD i;
- int32_t got,oval,lval, *lp = (int32_t *) window;
- #ifdef SINGLE_FLOAT
- for(i=0;i < length;i++){
- if(read(fd,(char *)&lval,sizeof(int32_t)) != sizeof(int32_t))
- return 0;
- oval = REVDWBYTES(lval);
- *lp++ = oval;
- }
- #else
- /* read whole block then swap...should be faster */
- got = read(fd,(char *) window,length * sizeof(float));
- if(got != (int)(length * sizeof(float)))
- return 0;
- /* then byterev */
- for(i=0;i < length;i++){
- lval = *lp;
- oval = REVDWBYTES(lval);
- *lp++ = oval;
- }
- #endif
- }
- else{
- if(read(fd,(char *) window,length * sizeof(float)) != (int)(length * sizeof(float)))
- return 0;
- }
- return length * sizeof(float);
- }
- const char *pvoc_errorstr()
- {
- return (const char *) pv_errstr;
- }
- /* thanks to the SNDAN programmers for this! */
- /* return 0 for big-endian machine, 1 for little-endian machine*/
- /* probably no good for 16bit swapping though */
- static int32_t byte_order()
- {
- int32_t one = 1;
- char* endptr = (char *) &one;
- return (*endptr);
- }
- /***** loosely modelled on CDP sfsys ******
- * This is a static array, but coul be made dynamic in an OOP sort of way.
- * The idea is that all low-level operations and data
- * are completely hidden from the user, so that internal format changes can be made
- * with little or no disruption to the public functions.
- * But avoiding the full monty of a C++ implementation.
- *******************************************/
- int32_t init_pvsys(void)
- {
- int32_t i;
- if(files[0] != NULL) {
- pv_errstr = "\npvsys: already initialized";
- return 0;
- }
- for(i = 0;i < MAXFILES;i++)
- files[i] = NULL;
- return 1;
- }
- static void prepare_pvfmt(WAVEFORMATEX *pfmt,DWORD chans, DWORD srate,
- pv_stype stype)
- {
- #ifdef _DEBUG
- assert(pfmt);
- #endif
- pfmt->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
- pfmt->nChannels = (WORD) chans;
- pfmt->nSamplesPerSec = srate;
- switch(stype){
- case(STYPE_16):
- pfmt->wBitsPerSample = (WORD)16;
- pfmt->nBlockAlign = (WORD)(chans * 2 * sizeof(char));
-
- break;
- case(STYPE_24):
- pfmt->wBitsPerSample = (WORD) 24;
- pfmt->nBlockAlign = (WORD)(chans * 3 * sizeof(char));
- break;
- case(STYPE_32):
- case(STYPE_IEEE_FLOAT):
- pfmt->wBitsPerSample = (WORD) 32;
- pfmt->nBlockAlign = (WORD)(chans * 4 * sizeof(char));
- break;
- default:
- #ifdef _DEBUG
- assert(0);
- #endif
- break;
- }
- pfmt->nAvgBytesPerSec = pfmt->nBlockAlign * srate;
- /* if we have extended WindowParam fields, or something ,will need to adjust this */
- pfmt->cbSize = 62;
- }
- /* lots of different ways of doing this! */
- /* we will need one in the form:
- * in pvoc_fmtcreate(const char *fname,PVOCDATA *p_pvfmt, WAVEFORMATEX *p_wvfmt);
- */
- /* a simple minimalist function to begin with!*/
- /*set D to 0, and/or dwWinlen to 0, to use internal default */
- /* fWindow points to userdef window, or is NULL */
- /* NB currently this does not enforce a soundfile extension; probably it should... */
- /* POSSIBLE TODO: expand arglist to enforce O_EXCL mode? */
- /* currently handled externally in CDP sfsys using file_exists() */
- int32_t pvoc_createfile(const char *filename,
- DWORD fftlen,DWORD overlap,DWORD chans,
- DWORD format,int32_t srate,
- pv_stype stype,pv_wtype wtype,
- float wparam,float *fWindow,DWORD dwWinlen)
- {
-
- int32_t i;
- long N,D;
- char *pname;
- PVOCFILE *pfile = NULL;
- float winparam = 0.0f;
- N = fftlen; /* keep the CARL varnames for now */
- D = overlap;
- if(N == 0 || chans <=0 || filename==NULL || D > N) {
- pv_errstr = "\npvsys: bad arguments";
- return -1;
- }
- if(/*format < PVOC_AMP_FREQ ||*/ format > PVOC_COMPLEX) { /* RWD unsigned, so nothing < 0 possible */
- pv_errstr = "\npvsys: bad format parameter";
- return -1;
- }
- if(!(wtype >= PVOC_DEFAULT && wtype <= PVOC_CUSTOM)){
- pv_errstr = "\npvsys: bad window type";
- return -1;
- }
- /* load it, but ca't write until we have a PVXW chunk definition...*/
- if(wtype==PVOC_CUSTOM){
- }
- if(wtype==PVOC_DEFAULT)
- wtype = PVOC_HAMMING;
- if(wtype==PVOC_KAISER)
- if(wparam != 0.0f)
- winparam = wparam;
- /*will need an internal default for window parameters...*/
- for(i=0;i < MAXFILES;i++)
- if(files[i]==NULL)
- break;
- if(i==MAXFILES) {
- pv_errstr = "\npvsys: too many files open";
- return -1;
- }
- pfile = (PVOCFILE *) malloc(sizeof(PVOCFILE));
- if(pfile==NULL){
- pv_errstr = "\npvsys: no memory";
- return -1;
- }
- pname = (char *) malloc(strlen(filename)+1);
- if(pname == NULL){
- free(pfile);
- pv_errstr = "\npvsys: no memory";
- return -1;
- }
- pfile->customWindow = NULL;
- /* setup rendering inforamtion */
- prepare_pvfmt(&pfile->fmtdata,chans,srate,stype);
- strcpy(pname,filename);
- pfile->pvdata.wWordFormat = PVOC_IEEE_FLOAT;
- pfile->pvdata.wAnalFormat = (WORD) format;
- pfile->pvdata.wSourceFormat = (stype == STYPE_IEEE_FLOAT ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM);
- pfile->pvdata.wWindowType = wtype;
- pfile->pvdata.nAnalysisBins = (N>>1) + 1;
- if(dwWinlen==0)
- pfile->pvdata.dwWinlen = N;
- else
- pfile->pvdata.dwWinlen = dwWinlen;
- if(D==0)
- pfile->pvdata.dwOverlap = N/8;
- else
- pfile->pvdata.dwOverlap = D;
- pfile->pvdata.dwFrameAlign = pfile->pvdata.nAnalysisBins * 2 * sizeof(float);
- pfile->pvdata.fAnalysisRate = (float)srate / (float) pfile->pvdata.dwOverlap;
- pfile->pvdata.fWindowParam = winparam;
- pfile->to_delete = 0;
- pfile->readonly = 0;
- pfile->needsupdate = 0; //could be overruled later, but only once!
-
- if(fWindow!= NULL){
- pfile->customWindow = malloc(dwWinlen * sizeof(float));
- if(pfile->customWindow==NULL){
- pv_errstr = "\npvsys: no memory for custom window";
- return -1;
- }
- memcpy((char *)(pfile->customWindow),(char *)fWindow,dwWinlen * sizeof(float));
- }
- pfile->fd = open(filename,O_BINARY | O_CREAT | O_RDWR | O_TRUNC,_S_IWRITE | _S_IREAD);
- if(pfile->fd < 0){
- free(pname);
- if(pfile->customWindow)
- free(pfile->customWindow);
- free(pfile);
- pv_errstr = "\npvsys: unable to create file";
- return -1;
- }
- pfile->propsoffset = 0;
- pfile->datachunkoffset = 0;
- pfile->nFrames = 0;
- pfile->FramePos = 0;
- pfile->curpos = 0;
- pfile->name = pname;
- pfile->do_byte_reverse = !byte_order();
- files[i] = pfile;
- if(!pvoc_writeheader(i)) {
- close(pfile->fd);
- remove(pfile->name);
- free(pfile->name);
- if(pfile->customWindow)
- free(pfile->customWindow);
- free(pfile);
- files[i] = NULL;
- return -1;
- }
- return i;
- }
- int32_t pvoc_getpvxprops(int32_t ifd, PVOCDATA *data)
- {
- if(files[ifd]==NULL)
- return 0;
- #ifdef _DEBUG
- assert(data != NULL);
- #endif
- memcpy((char*) data,(char*) &files[ifd]->pvdata,sizeof(PVOCDATA));
- return 1;
- }
- /* tell pvsys to update pvocdata on close; return 0 for success */
- /* only other way to do this is to test initial params != 0 */
- /* I want to control this explicitly, for now */
- int32_t pvoc_set_needsupdate(int32_t ifd)
- {
- if(files[ifd]==NULL)
- return -1;
- if(files[ifd]->readonly)
- return -1;
- files[ifd]->needsupdate = 1;
- return 0;
- }
- //returns 0 for success
- int32_t pvoc_canupdate(int32_t ifd)
- {
- if(files[ifd]==NULL || files[ifd]->readonly)
- return -1;
- if(files[ifd]->needsupdate)
- return 0;
- else
- return -1;
- }
- //ONLY when creating new file using 2-stages.called last thing before file close.
- #if 0
- int32_t pvoc_updateprops(int32_t fd, const PVOCDATA *data)
- {
- DWORD pos;
-
- if(files[fd]==NULL)
- return -1;
- if(files[fd]->readonly)
- return -1;
- if(files[fd]->needsupdate==0)
- return -1;
- #ifdef _DEBUG
- // the only field that MUST be set, needed for read/write frames
- assert(files[fd]->pvdata.nAnalysisBins == data->nAnalysisBins);
- #endif
- // need to seek to propsoffset, then we can call write_pvocdata
- pos = lseek(files[fd]->fd,files[fd]->propsoffset,SEEK_SET);
- if(pos != files[fd]->propsoffset){
- pv_errstr = "\npvsys: error updating pvoc props";
- return 0;
- }
- return 1;
- }
- #endif
- int32_t pvoc_openfile(const char *filename,PVOCDATA *data,WAVEFORMATEX *fmt)
- {
- int32_t i;
- WAVEFORMATPVOCEX wfpx;
- char *pname;
- PVOCFILE *pfile = NULL;
- // long size = sizeof(WAVEFORMATPVOCEX);
-
- if(data==NULL || fmt==NULL){
- pv_errstr = "\npvsys: Internal error: NULL data arrays";
- return -1;
- }
- for(i=0;i < MAXFILES;i++)
- if(files[i]==NULL)
- break;
- if(i==MAXFILES){
- pv_errstr = "\npvsys: too many files open";
- return -1;
- }
- pfile = (PVOCFILE *) malloc(sizeof(PVOCFILE));
- if(pfile==NULL){
- pv_errstr = "\npvsys: no memory for file data";
- return -1;
- }
- pfile->customWindow = NULL;
- pname = (char *) malloc(strlen(filename)+1);
- if(pname == NULL){
- free(pfile);
- pv_errstr = "\npvsys: no memory";
- return -1;
- }
- pfile->fd = open(filename,O_BINARY | O_RDONLY,_S_IREAD);
- if(pfile->fd < 0){
- free(pname);
- free(pfile);
- pv_errstr = "\npvsys: unable to create file";
- return -1;
- }
- strcpy(pname,filename);
- pfile->datachunkoffset = 0;
- pfile->nFrames = 0;
- pfile->curpos = 0;
- pfile->FramePos = 0;
- pfile->name = pname;
- pfile->do_byte_reverse = !byte_order();
- pfile->readonly = 1;
- pfile->needsupdate = 0; // just to state it explicitly!
- files[i] = pfile;
- if(!pvoc_readheader(i,&wfpx)){
- close(pfile->fd);
- free(pfile->name);
- if(pfile->customWindow)
- free(pfile->customWindow);
- free(pfile);
- files[i] = NULL;
- return -1;
- }
- memcpy((char *)data, (char *)&(wfpx.data),sizeof(PVOCDATA));
- memcpy((char *)fmt,(char *)&(wfpx.wxFormat.Format),SIZEOF_WFMTEX);
- files[i] = pfile;
- return i;
- }
- static int32_t pvoc_readfmt(int32_t fd,int32_t byterev,WAVEFORMATPVOCEX *pWfpx)
- {
- DWORD dword;
- WORD word;
- #ifdef _DEBUG
- assert(fd >= 0);
- assert(pWfpx);
- #endif
- if(read(fd,(char *) &(pWfpx->wxFormat.Format.wFormatTag),sizeof(WORD)) != sizeof(WORD)
- || read(fd,(char *) &(pWfpx->wxFormat.Format.nChannels),sizeof(WORD)) != sizeof(WORD)
- || read(fd,(char *) &(pWfpx->wxFormat.Format.nSamplesPerSec),sizeof(DWORD)) != sizeof(DWORD)
- || read(fd,(char *) &(pWfpx->wxFormat.Format.nAvgBytesPerSec),sizeof(DWORD)) != sizeof(DWORD)
- || read(fd,(char *) &(pWfpx->wxFormat.Format.nBlockAlign),sizeof(WORD)) != sizeof(WORD)
- || read(fd,(char *) &(pWfpx->wxFormat.Format.wBitsPerSample),sizeof(WORD)) != sizeof(WORD)
- || read(fd,(char *) &(pWfpx->wxFormat.Format.cbSize),sizeof(WORD)) != sizeof(WORD)){
- pv_errstr = "\npvsys: error reading Source format data";
- return 0;
- }
- if(byterev){
- word = pWfpx->wxFormat.Format.wFormatTag;
- pWfpx->wxFormat.Format.wFormatTag= REVWBYTES(word);
- word = pWfpx->wxFormat.Format.nChannels;
- pWfpx->wxFormat.Format.nChannels = REVWBYTES(word);
- dword = pWfpx->wxFormat.Format.nSamplesPerSec;
- pWfpx->wxFormat.Format.nSamplesPerSec = REVDWBYTES(dword);
- dword = pWfpx->wxFormat.Format.nAvgBytesPerSec;
- pWfpx->wxFormat.Format.nAvgBytesPerSec = REVDWBYTES(dword);
- word = pWfpx->wxFormat.Format.nBlockAlign;
- pWfpx->wxFormat.Format.nBlockAlign = REVWBYTES(word);
- word = pWfpx->wxFormat.Format.wBitsPerSample;
- pWfpx->wxFormat.Format.wBitsPerSample = REVWBYTES(word);
- word = pWfpx->wxFormat.Format.cbSize;
- pWfpx->wxFormat.Format.cbSize = REVWBYTES(word);
- }
- /* the first clues this is pvx format...*/
- if(pWfpx->wxFormat.Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE){
- pv_errstr = "\npvsys: not a WAVE_EX file";
- return 0;
- }
- if(pWfpx->wxFormat.Format.cbSize != 62){
- pv_errstr = "\npvsys: bad size for fmt chunk";
- return 0;
- }
- if(read(fd,(char *) &(pWfpx->wxFormat.Samples.wValidBitsPerSample),sizeof(WORD)) != sizeof(WORD)
- || read(fd,(char *) &(pWfpx->wxFormat.dwChannelMask),sizeof(DWORD)) != sizeof(DWORD)
- || read(fd,(char *) &(pWfpx->wxFormat.SubFormat),sizeof(GUID)) != sizeof(GUID)){
- pv_errstr = "\npvsys: error reading Extended format data";
- return 0;
- }
- if(byterev){
- word = pWfpx->wxFormat.Samples.wValidBitsPerSample;
- pWfpx->wxFormat.Samples.wValidBitsPerSample = REVWBYTES(word);
- dword = pWfpx->wxFormat.dwChannelMask;
- pWfpx->wxFormat.dwChannelMask = REVDWBYTES(dword);
- dword = pWfpx->wxFormat.SubFormat.Data1;
- pWfpx->wxFormat.SubFormat.Data1 = REVDWBYTES(dword);
- word = pWfpx->wxFormat.SubFormat.Data2;
- pWfpx->wxFormat.SubFormat.Data2 = REVWBYTES(word);
- word = pWfpx->wxFormat.SubFormat.Data3;
- pWfpx->wxFormat.SubFormat.Data3 = REVWBYTES(word);
- /* don't need to reverse the char array */
- }
- /* ... but this is the clincher */
- if(!compare_guids(&(pWfpx->wxFormat.SubFormat),&KSDATAFORMAT_SUBTYPE_PVOC)){
- pv_errstr = "\npvsys: not a PVOCEX file";
- return 0;
- }
- if(read(fd,(char *) &(pWfpx->dwVersion),sizeof(DWORD)) != sizeof(DWORD)
- || read(fd,(char *) &(pWfpx->dwDataSize),sizeof(DWORD)) != sizeof(DWORD)
- || read(fd,(char *) &(pWfpx->data),sizeof(PVOCDATA)) != sizeof(PVOCDATA)){
- pv_errstr = "\npvsys: error reading Extended pvoc format data";
- return 0;
- }
- if(byterev){
- SND_SAMP ssamp;
- dword = pWfpx->dwVersion;
- pWfpx->dwVersion = REVDWBYTES(dword);
- /* check it now! */
- if(pWfpx->dwVersion != PVX_VERSION){
- pv_errstr = "\npvsys: unknown pvocex Version";
- return 0;
- }
- dword = pWfpx->dwDataSize;
- pWfpx->dwDataSize = REVDWBYTES(dword);
- word = pWfpx->data.wWordFormat;
- pWfpx->data.wWordFormat= REVWBYTES(word);
- word = pWfpx->data.wAnalFormat;
- pWfpx->data.wAnalFormat= REVWBYTES(word);
- word = pWfpx->data.wSourceFormat;
- pWfpx->data.wSourceFormat= REVWBYTES(word);
- word = pWfpx->data.wWindowType;
- pWfpx->data.wWindowType= REVWBYTES(word);
- dword = pWfpx->data.nAnalysisBins;
- pWfpx->data.nAnalysisBins = REVDWBYTES(dword);
- dword = pWfpx->data.dwWinlen;
- pWfpx->data.dwWinlen = REVDWBYTES(dword);
- dword = pWfpx->data.dwOverlap;
- pWfpx->data.dwOverlap = REVDWBYTES(dword);
- dword = pWfpx->data.dwFrameAlign;
- pWfpx->data.dwFrameAlign = REVDWBYTES(dword);
- ssamp.fsamp = pWfpx->data.fAnalysisRate;
- //dword = * (DWORD *)&(pWfpx->data.fAnalysisRate); /* RWD TODO: use union */
- //dword = REVDWBYTES(dword);
- ssamp.lsamp = REVDWBYTES(ssamp.lsamp);
- //pWfpx->data.fAnalysisRate = *(float *)&dword;
- pWfpx->data.fAnalysisRate = ssamp.fsamp;
-
- ssamp.fsamp = pWfpx->data.fWindowParam;
- //dword = * (DWORD *)&(pWfpx->data.fWindowParam);
- //dword = REVDWBYTES(dword);
- ssamp.lsamp = REVDWBYTES(ssamp.lsamp);
- //pWfpx->data.fWindowParam = *(float *)&dword;
- pWfpx->data.fWindowParam = ssamp.fsamp;
- }
- if(pWfpx->dwVersion != PVX_VERSION){
- pv_errstr = "\npvsys: unknown pvocex Version";
- return 0;
- }
- return 1;
- }
- static int32_t pvoc_readheader(int32_t ifd,WAVEFORMATPVOCEX *pWfpx)
- {
- DWORD tag, size,riffsize;
- int32_t fmtseen = 0, /* dataseen = 0,*/ windowseen = 0;
- // DWORD windowlength = 0;
- #ifdef _DEBUG
- assert(pWfpx);
- assert(files[ifd]);
- assert(files[ifd]->fd >= 0);
- size = sizeof(WAVEFORMATEXTENSIBLE);
- size += 2 * sizeof(DWORD);
- size += sizeof(PVOCDATA);
- #endif
- if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)
- || read(files[ifd]->fd,(char *) &size,sizeof(DWORD)) != sizeof(DWORD)){
- pv_errstr = "\npvsys: error reading header";
- return 0;
- }
- if(files[ifd]->do_byte_reverse)
- size = REVDWBYTES(size);
- else
- tag = REVDWBYTES(tag);
- if(tag != TAG('R','I','F','F')){
- pv_errstr = "\npvsys: not a RIFF file";
- return 0;
- }
- if(size < 24 * sizeof(DWORD) + SIZEOF_FMTPVOCEX){
- pv_errstr = "\npvsys: file too small";
- return 0;
- }
- riffsize = size;
- if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)){
- pv_errstr = "\npvsys: error reading header";
- return 0;
- }
- if(!files[ifd]->do_byte_reverse)
- tag = REVDWBYTES(tag);
- if(tag != TAG('W','A','V','E')){
- pv_errstr = "\npvsys: not a WAVE file";
- return 0;
- }
- riffsize -= sizeof(DWORD);
- /*loop for chunks */
- while(riffsize > 0){
- if(read(files[ifd]->fd,(char *) &tag,sizeof(DWORD)) != sizeof(DWORD)
- || read(files[ifd]->fd,(char *) &size,sizeof(DWORD)) != sizeof(DWORD)){
- pv_errstr = "\npvsys: error reading header";
- return 0;
- }
- if(files[ifd]->do_byte_reverse)
- size = REVDWBYTES(size);
- else
- tag = REVDWBYTES(tag);
- riffsize -= 2 * sizeof(DWORD);
- switch(tag){
- case TAG('f','m','t',' '):
- /* bail out if not a pvoc file: not trying to read all WAVE formats!*/
- if(size < SIZEOF_FMTPVOCEX){
- pv_errstr = "\npvsys: not a PVOC-EX file";
- return 0;
- }
- if(!pvoc_readfmt(files[ifd]->fd,files[ifd]->do_byte_reverse,pWfpx)){
- pv_errstr = "\npvsys: error reading format chunk";
- return 0;
- }
- riffsize -= SIZEOF_FMTPVOCEX;
- fmtseen = 1;
- memcpy((char *)&(files[ifd]->fmtdata),(char *)&(pWfpx->wxFormat),SIZEOF_WFMTEX);
- memcpy((char *)&(files[ifd]->pvdata),(char *)&(pWfpx->data),sizeof(PVOCDATA));
- break;
- case TAG('P','V','X','W'):
- if(!fmtseen){
- pv_errstr = "\npvsys: PVXW chunk found before fmt chunk.";
- return 0;
- }
- if(files[ifd]->pvdata.wWindowType!=PVOC_CUSTOM){
- /*whaddayado? can you warn the user and continue?*/
- pv_errstr = "\npvsys: PVXW chunk found but custom window not specified";
- return 0;
- }
- files[ifd]->customWindow = malloc(files[ifd]->pvdata.dwWinlen * sizeof(float));
- if(files[ifd]->customWindow == NULL){
- pv_errstr = "\npvsys: no memory for custom window data.";
- return 0;
- }
- if(pvoc_readWindow(files[ifd]->fd,files[ifd]->do_byte_reverse,
- files[ifd]->customWindow,files[ifd]->pvdata.dwWinlen)
- != (int)(files[ifd]->pvdata.dwWinlen * sizeof(float))){
- pv_errstr = "\npvsys: error reading window data.";
- return 0;
- }
- windowseen = 1;
- break;
- case TAG('d','a','t','a'):
- if(riffsize - size != 0){
- pv_errstr = "\npvsys: bad RIFF file";
- return 0;
- }
-
- if(!fmtseen){
- pv_errstr = "\npvsys: bad format, data chunk before fmt chunk";
- return 0;
- }
- if(files[ifd]->pvdata.wWindowType==PVOC_CUSTOM)
- if(!windowseen){
- pv_errstr = "\npvsys: custom window chunk PVXW not found";
- return 0;
- }
- files[ifd]->datachunkoffset = lseek(files[ifd]->fd,0,SEEK_CUR);
- files[ifd]->curpos = files[ifd]->datachunkoffset;
- /* not m/c frames, for now */
- files[ifd]->nFrames = size / files[ifd]->pvdata.dwFrameAlign;
- return 1;
- break;
- default:
- /* skip any onknown chunks */
- riffsize -= 2 * sizeof(DWORD);
- if(lseek(files[ifd]->fd,size,SEEK_CUR) < 0){
- pv_errstr = "\npvsys: error skipping unknown WAVE chunk";
- return 0;
- }
- riffsize -= size;
- break;
- }
- }
- /* if here, something very wrong!*/
- pv_errstr = "\npvsys: bad format in RIFF file";
- return 0;
- }
- static int32_t pvoc_writeheader(int ofd)
- {
- int32_t tag,size,version;
- WORD validbits;
-
- const GUID *pGuid = &KSDATAFORMAT_SUBTYPE_PVOC;
- #ifdef _DEBUG
- assert(files[ofd] != NULL);
- assert(files[ofd]->fd >=0);
- #endif
- tag = TAG('R','I','F','F');
- size = 0;
- if(files[ofd]->do_byte_reverse)
- size = REVDWBYTES(size);
- if(!files[ofd]->do_byte_reverse)
- tag = REVDWBYTES(tag);
- if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)
- || write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)) {
- pv_errstr = "\npvsys: error writing header";
- return 0;
- }
- tag = TAG('W','A','V','E');
- if(!files[ofd]->do_byte_reverse)
- tag = REVDWBYTES(tag);
- if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)){
- pv_errstr = "\npvsys: error writing header";
- return 0;
- }
- tag = TAG('f','m','t',' ');
- size = SIZEOF_WFMTEX + sizeof(WORD) +
- sizeof(DWORD)
- + sizeof(GUID)
- + 2*sizeof(DWORD)
- + sizeof(PVOCDATA);
- if(files[ofd]->do_byte_reverse)
- size = REVDWBYTES(size);
- if(!files[ofd]->do_byte_reverse)
- tag = REVDWBYTES(tag);
- if(write(files[ofd]->fd,(char *)&tag,sizeof(int32_t)) != sizeof(int32_t)
- || write(files[ofd]->fd,(char *)&size,sizeof(int32_t)) != sizeof(int32_t)) {
- pv_errstr = "\npvsys: error writing header";
- return 0;
- }
- /* we need to record where we are, as we may have to update fmt data before file close */
- files[ofd]->fmtchunkoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
-
- if(write_fmt(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->fmtdata)) != SIZEOF_WFMTEX){
- pv_errstr = "\npvsys: error writing fmt chunk";
- return 0;
- }
- validbits = files[ofd]->fmtdata.wBitsPerSample; /*nothing fancy here */
- if(files[ofd]->do_byte_reverse)
- validbits = REVWBYTES(validbits);
- if(write(files[ofd]->fd,(char *) &validbits,sizeof(WORD)) != sizeof(WORD)){
- pv_errstr = "\npvsys: error writing fmt chunk";
- return 0;
- }
- /* we will take this from a WAVE_EX file, in due course */
- size = 0; /*dwChannelMask*/
- if(write(files[ofd]->fd,(char *)&size,sizeof(DWORD)) != sizeof(DWORD)){
- pv_errstr = "\npvsys: error writing fmt chunk";
- return 0;
- }
- if(write_guid(files[ofd]->fd,files[ofd]->do_byte_reverse,pGuid) != sizeof(GUID)){
- pv_errstr = "\npvsys: error writing fmt chunk";
- return 0;
- }
- version = 1;
- size = sizeof(PVOCDATA);
- if(files[ofd]->do_byte_reverse){
- version = REVDWBYTES(version);
- size = REVDWBYTES(size);
- }
- if(write(files[ofd]->fd,&version,sizeof(int32_t)) != sizeof(int32_t)
- || write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)){
- pv_errstr = "\npvsys: error writing fmt chunk";
- return 0;
- }
- /* RWD new 2022 so we can update all pvocex props on closing new file */
- files[ofd]->propsoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
-
- if(write_pvocdata(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->pvdata)) != sizeof(PVOCDATA)){
- pv_errstr = "\npvsys: error writing fmt chunk";
- return 0;
- }
- /* VERY experimental; may not even be a good idea...*/
- if(files[ofd]->customWindow){
- tag = TAG('P','V','X','W');
- size = files[ofd]->pvdata.dwWinlen * sizeof(float);
- if(files[ofd]->do_byte_reverse)
- size = REVDWBYTES(size);
- else
- tag = REVDWBYTES(tag);
- if(write(files[ofd]->fd,(char *)&tag,sizeof(int32_t)) != sizeof(int32_t)
- || write(files[ofd]->fd,(char *)&size,sizeof(int32_t)) != sizeof(int32_t)) {
- pv_errstr = "\npvsys: error writing header";
- return 0;
- }
- if(pvoc_writeWindow(files[ofd]->fd,
- files[ofd]->do_byte_reverse,
- files[ofd]->customWindow,
- files[ofd]->pvdata.dwWinlen)!= (int)(files[ofd]->pvdata.dwWinlen * sizeof(float))){
- pv_errstr = "\npvsys: error writing window data.";
- return 0;
- }
- }
- /* no other chunks to write yet! */
- tag = TAG('d','a','t','a');
- if(!files[ofd]->do_byte_reverse)
- tag = REVDWBYTES(tag);
- if(write(files[ofd]->fd,&tag,sizeof(int32_t)) != sizeof(int32_t)){
- pv_errstr = "\npvsys: write error writing header";
- return 0;
- }
- /* we need to update size later on...*/
- size = 0;
- if(write(files[ofd]->fd,&size,sizeof(int32_t)) != sizeof(int32_t)){
- pv_errstr = "\npvsys: write error writing header";
- return 0;
- }
- files[ofd]->datachunkoffset = lseek(files[ofd]->fd,0,SEEK_CUR);
- files[ofd]->curpos = files[ofd]->datachunkoffset;
- return 1;
- }
- /* called by pvoc_closefile() */
- /* which checks "readonly", if not set, file is newly created */
- static int32_t pvoc_updateheader(int ofd)
- {
- int32_t riffsize,datasize;
- DWORD pos;
- #ifdef _DEBUG
- assert(files[ofd]);
- assert(files[ofd]->fd >= 0);
- //assert(files[ofd]->curpos == lseek(files[ofd]->fd,0,SEEK_CUR));
- assert(!files[ofd]->readonly);
- #endif
- datasize = files[ofd]->curpos - files[ofd]->datachunkoffset;
- pos = lseek(files[ofd]->fd,files[ofd]->datachunkoffset-sizeof(DWORD),SEEK_SET);
- if(pos != files[ofd]->datachunkoffset-sizeof(DWORD)){
- pv_errstr = "\npvsys: seek error updating data chunk";
- return 0;
- }
- if(files[ofd]->do_byte_reverse)
- datasize = REVDWBYTES(datasize);
- if(write(files[ofd]->fd,(char *) &datasize,sizeof(DWORD)) != sizeof(DWORD)){
- pv_errstr = "\npvsys: write error updating data chunk";
- return 0;
- }
- riffsize = files[ofd]->curpos - 2* sizeof(DWORD);
- if(files[ofd]->do_byte_reverse)
- riffsize = REVDWBYTES(riffsize);
- pos = lseek(files[ofd]->fd,sizeof(DWORD),SEEK_SET);
- if(pos != sizeof(DWORD)){
- pv_errstr = "\npvsys: seek error updating riff chunk";
- return 0;
- }
- if(write(files[ofd]->fd,(char *) &riffsize,sizeof(DWORD)) != sizeof(DWORD)){
- pv_errstr = "\npvsys: write error updating riff chunk";
- return 0;
- }
-
- // update PVOCDATA and WAVEFORMATEX fields if required
- if(files[ofd]->needsupdate){
- #ifdef _DEBUG
- fprintf(stderr,"updating in pvoc_update_header()\n");
- #endif
- pos = lseek(files[ofd]->fd,files[ofd]->fmtchunkoffset,SEEK_SET);
- if(pos != files[ofd]->fmtchunkoffset){
- pv_errstr = "\npvsys: seek error updating fmt data";
- return 0;
- }
- pos = write_fmt(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->fmtdata));
- if(pos != SIZEOF_WFMTEX){
- pv_errstr = "\npvsys: write error updating fmt data";
- return 0;
- }
-
- pos = lseek(files[ofd]->fd,files[ofd]->propsoffset,SEEK_SET);
- if(pos != files[ofd]->propsoffset){
- pv_errstr = "\npvsys: seek error updating pvx data";
- return 0;
- }
- //int32_t write_pvocdata(int32_t fd,int32_t byterev,const PVOCDATA *pData)
- pos = write_pvocdata(files[ofd]->fd,files[ofd]->do_byte_reverse,&(files[ofd]->pvdata));
- if(pos != sizeof(PVOCDATA)){
- pv_errstr = "\npvsys: write error updating pvx data";
- return 0;
- }
- }
- pos = lseek(files[ofd]->fd,0,SEEK_END);
- if(pos < 0){
- pv_errstr = "\npvsys: seek error seeking to end of file";
- return 0;
- }
-
- return 1;
- }
- int32_t pvoc_update_closefile(int ofd, const PVOCDATA *data, const WAVEFORMATEXTENSIBLE *wfx)
- {
- if(files[ofd]==NULL){
- pv_errstr = "\npvsys: file does not exist";
- return 0;
- }
-
- if(files[ofd]->fd < 0){
- pv_errstr = "\npvsys: file not open";
- return 0;
- }
- if(files[ofd]->readonly) {
- pv_errstr = "\ncannot update readonly file"; // probably an input file
- }
- else {
- memcpy((char*) &files[ofd]->fmtdata, (char*) &wfx->Format, sizeof(WAVEFORMATEX));
- memcpy((char*) &files[ofd]->pvdata, (char*) data, sizeof(PVOCDATA));
- }
- return pvoc_closefile(ofd);
- }
- int32_t pvoc_closefile(int ofd)
- {
- if(files[ofd]==NULL){
- pv_errstr = "\npvsys: file does not exist";
- return 0;
- }
- if(files[ofd]->fd < 0){
- pv_errstr = "\npvsys: file not open";
- return 0;
- }
- if(!files[ofd]->readonly) {
- if(!pvoc_updateheader(ofd))
- return 0;
- }
- close(files[ofd]->fd);
- if(files[ofd]->to_delete && !(files[ofd]->readonly))
- remove(files[ofd]->name);
- free(files[ofd]->name);
- free(files[ofd]);
- files[ofd] = 0;
- return 1;
- }
- /* does not directly address m/c streams, or alternative numeric formats, yet
- * so for m/c files, write each frame in turn, for each channel.
- * The format requires multi-channel frames to be interleaved in the usual way:
- * if nChannels= 4, the file will contain:
- * frame[0][0],frame[0][1],frame[0][2],frame[0][3],frme[1][0],frame[1][1].....
- *
- * The idea is to offer e.g. a floats version and a longs version ONLY, but
- * independently of the underlying representation, so that the user can write a floats
- * block, even though the underlying format might be longs or doubles. Most importantly,
- * the user does not have to deal with byte-reversal, which would otherwise always be the case
- * it the user had direct access to the file.
- *
- * So these functions are the most likely to change over time!.
- *
- * return 0 for error, 1 for success. This could change....
- */
- int32_t pvoc_putframes(int ofd,const float *frame,int32_t numframes)
- {
- DWORD i;
- DWORD towrite; /* count in 'words' */
- int32_t temp,*lfp;
-
- if(files[ofd]==NULL){
- pv_errstr = "\npvsys: bad file descriptor";
- return 0;
- }
- if(files[ofd]->fd < 0){
- pv_errstr = "\npvsys: file not open";
- return 0;
- }
- /* NB doubles not supported yet */
-
- towrite = files[ofd]->pvdata.nAnalysisBins * 2 * numframes;
-
- if(files[ofd]->do_byte_reverse){
- /* do this without overwriting source data! */
- lfp = (int32_t *) frame;
- for(i=0;i < towrite; i++){
- temp = *lfp++;
- temp = REVDWBYTES(temp);
- if(write(files[ofd]->fd,(char *) &temp,sizeof(int32_t)) != sizeof(int32_t)){
- pv_errstr = "\npvsys: error writing data";
- return 0;
- }
- }
- }
- else {
- if(write(files[ofd]->fd,(char *) frame,towrite * sizeof(float)) != (int32_t)(towrite * sizeof(float))){
- pv_errstr = "\npvsys: error writing data";
- return 0;
- }
- }
- files[ofd]->FramePos += numframes;
- files[ofd]->nFrames += numframes;
- files[ofd]->curpos += towrite * sizeof(float);
- return 1;
- }
- /* Simplistic read function
- * best practice here is to read nChannels frames *
- * return -1 for error, 0 for EOF, else numframes read
- */
- int32_t pvoc_getframes(int32_t ifd,float *frames,DWORD nframes)
- {
- int32_t i;
- int32_t toread;
- int32_t oval,temp,*lfp;
- int32_t got;
- int32_t rc = -1;
- if(files[ifd]==NULL){
- pv_errstr = "\npvsys: bad file descriptor";
- return rc;
- }
- if(files[ifd]->fd < 0){
- pv_errstr = "\npvsys: file not open";
- return rc;
- }
- toread = files[ifd]->pvdata.nAnalysisBins * 2 * nframes;
- if(files[ifd]->do_byte_reverse){
- lfp = (int32_t *) frames;
- #ifdef SINGLE_FLOAT
- for(i=0;i < toread;i++){
- if((got=read(files[ifd]->fd,(char *) &temp,sizeof(int32_t))) <0){
- pv_errstr = "\npvsys: error reading data";
- return rc;
- }
- if(got < sizeof(int32_t)){
- /* file size incorrect? */
- return 0; /* assume EOF */
- }
- temp = REVDWBYTES(temp);
- *lfp++ = temp;
- }
- #else
- /* much faster on G4!!! */
- got = read(files[ifd]->fd,(char *)frames,toread * sizeof(float));
- if(got < 0){
- pv_errstr = "\npvsys: error reading data";
- return rc;
- }
- for(i=0;i < got / sizeof(float);i++){
- temp = *lfp;
- oval = REVDWBYTES(temp);
- *lfp++ = oval;
- }
- if(got < (int)(toread * sizeof(float))){
- /* some (user?) error in file size: return integral number of frames read*/
- toread = got / (files[ifd]->pvdata.nAnalysisBins * 2 * sizeof(float));
- /* RWD 4:2002 need to revise this too */
- nframes = toread;
- }
-
- #endif
- rc = nframes; /*RWD 4:2002 */
- }
- else{
- if((got = read(files[ifd]->fd,(char *)frames,toread * sizeof(float))) < (int32_t)(toread * sizeof(float))){
- if(got < 0){
- pv_errstr = "\npvsys: error reading data";
- return rc;
- }
- else if(got < (int)(toread * sizeof(float))){
- /* some (user?) error in file size: return integral number of frames read*/
- toread = got / (files[ifd]->pvdata.nAnalysisBins * 2 * sizeof(float));
- rc = toread;
- /* RWD 4:2002 need to revise this too */
- nframes = toread;
- }
- }
- else
- rc = nframes;
- }
- /*files[ifd]->curpos += (toread * sizeof(float));*/
- files[ifd]->curpos += got; /* RWD 4:2002 */
- files[ifd]->FramePos += nframes;
- return rc;
- }
- // return 0 for success, -1 for error
- int32_t pvoc_rewind(int32_t ifd,int32_t skip_first_frame)
- {
- int32_t rc = -1;
- int32_t fd;
- int32_t pos;
- int32_t skipsize = 0;
- int32_t skipframes = 0;
- if(files[ifd]==NULL){
- pv_errstr = "\npvsys: bad file descriptor";
- return rc;
- }
- if(files[ifd]->fd < 0){
- pv_errstr = "\npvsys: file not open";
- return rc;
- }
- skipframes = files[ifd]->fmtdata.nChannels;
- //skipsize = files[ifd]->pvdata.dwFrameAlign * skipframes;
-
-
- fd = files[ifd]->fd;
- pos = files[ifd]->datachunkoffset;
- if(skip_first_frame){
- skipsize = files[ifd]->pvdata.dwFrameAlign * skipframes;
- pos += skipsize;
- }
- if(lseek(fd,pos,SEEK_SET) != pos ) {
- pv_errstr = "\npvsys: error rewinding file";
- return rc;
- }
- files[ifd]->curpos = /* files[ifd]->datachunkoffset + skipsize */ pos;
- files[ifd]->FramePos = skipframes;
- return 0;
- }
- /* may be more to do in here later on */
- int32_t pvsys_release(void)
- {
- int32_t i;
- for(i=0;i < MAXFILES;i++){
- if(files[i]) {
- #ifdef _DEBUG
- fprintf(stderr,"\nDEBUG WARNING: file %d still open!\n",i);
- #endif
- if(!pvoc_closefile(i)){
- pv_errstr = "\npvsys: unable to close file on termination";
- return 0;
- }
- }
- }
- return 1;
- }
- /*return raw framecount: channel-agnostic for now */
- int32_t pvoc_framecount(int32_t ifd)
- {
- if(files[ifd]==NULL)
- return -1;
- return files[ifd]->nFrames;
- }
- /* RWD Jan 2014 */
- // return -1 for error, else framepos
- int32_t pvoc_framepos(int32_t ifd)
- {
- if(files[ifd]==NULL)
- return -1;
-
- return files[ifd]->FramePos;
- }
- // return -1 for error, 0 for success
- //NB framecount always disregards chans. But offset represents m/c block: offset*2 to get frames
- int32_t pvoc_seek_mcframe(int32_t ifd, int32_t offset, int32_t mode)
- {
- // DWORD mcframealign;
- DWORD rawoffset;
- int32_t rc = -1;
- int32_t mcframealign;
-
- if(files[ifd]==NULL)
- return -1;
- mcframealign = files[ifd]->pvdata.dwFrameAlign * files[ifd]->fmtdata.nChannels;
- rawoffset = offset * mcframealign;
- switch(mode){
- case SEEK_SET:
- // offset is m/c quantity, e.g. 2 * frame for stereo
- if(offset >= (files[ifd]->nFrames / files[ifd]->fmtdata.nChannels)){
- pv_errstr = "\npvsys: seek target beyond end of file";
- break;
- }
- rawoffset += files[ifd]->datachunkoffset;
- if(lseek(files[ifd]->fd,rawoffset,SEEK_SET) != rawoffset ) {
- pv_errstr = "\npvsys: seek error, SEEK_SET";
- return -1;
- }
- files[ifd]->curpos = rawoffset;
- files[ifd]->FramePos = offset * files[ifd]->fmtdata.nChannels;
- rc = 0;
- break;
- case SEEK_END:
- // go to end of file + offset, offset <= 0
- if(offset > 0){
- pv_errstr = "\npvsys: seek target before start of file, offset must be <= 0";
- break;
- }
- #ifdef _DEBUG
- fprintf(stderr,"pvoc_seek_mcframe: fd = %d\n",ifd);
- #endif
- //NB not relative to datachunkoffset in this case
- if(lseek(files[ifd]->fd,rawoffset,SEEK_END) < 0){
- pv_errstr = "\npvsys: seek error, SEEK_END";
- return -1;
- }
- #ifdef _DEBUG
- fprintf(stderr,"pvoc_seek_mcframe: files[%d]->nFrames = %d\n",ifd,files[ifd]->nFrames);
- #endif
- files[ifd]->FramePos = files[ifd]->nFrames - (offset * files[ifd]->fmtdata.nChannels);
- files[ifd]->curpos = files[ifd]->FramePos * files[ifd]->pvdata.dwFrameAlign;
- #ifdef _DEBUG
- fprintf(stderr,"pvoc_seek_mcframe: got curpos = %d\n",files[ifd]->curpos);
- #endif
- rc = 0; //success!
- break;
- case SEEK_CUR:
- // covered by pvoc_framepos(), equiv to a pvoc_tell() function.
- pv_errstr = "\npvsys: SEEK_CUR mode not supported yet!";
- break;
- }
- return rc;
- }
|