| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- /*
- * Copyright (c) 1983-2023 Trevor Wishart and Composers Desktop Project Ltd
- * http://www.trevorwishart.co.uk
- * http://www.composersdesktop.com
- *
- This file is part of the CDP System.
- The CDP System is free software; you can redistribute it
- and/or modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- The CDP System is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with the CDP System; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA
- *
- */
- /* floatsam version */
- /*
- cmdline...
- sfedit twixt mode infile(s) outfile switch-times splicelen (segcnt) [-wweight] [-r]\n\n"
- SWITCH_TIMES = special
- SPLICETIME in MS = IS_SPLEN
- SWITCH_WEIGHT = IS_WEIGHT
- FLAG IS_NOTCYCL
- TO SPLICE BETWEEN A SERIES OF INFILES USING A LIST OF GIVEN TIMES
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <structures.h>
- #include <cdpmain.h>
- #include <tkglobals.h>
- #include <pnames.h>
- #include <edit.h>
- #include <processno.h>
- #include <modeno.h>
- #include <globcon.h>
- #include <logic.h>
- #include <filetype.h>
- #include <mixxcon.h>
- #include <flags.h>
- #include <speccon.h>
- #include <arrays.h>
- #include <special.h>
- #include <formants.h>
- #include <sfsys.h>
- #include <osbind.h>
- //#ifdef unix
- #define round(x) lround((x))
- //#endif
- static void GetTimes(int fileno,int n,double *lasttime, double *nexttime,dataptr dz);
- static int do_output(int fileno,int n,int limit,double lasttime,double nexttime,
- double splicestep,int *obufend,int *total_outsamps,dataptr dz);
- static int Convert_to_samp(double time,dataptr dz);
- static void do_startsplice_in_outbuf(float *buf,double splicestep,dataptr dz);
- static void do_startof_endsplice_in_outbuf(float *buf,int splicetodo,double splicestep,dataptr dz);
- static int output_samps(int segno,int firsttime,float *outbuf,int outsamps,int *obufend,dataptr dz);
- static int flush_outbuf(int segno,int obufend,dataptr dz);
- static void randperm(int z,int setlen,dataptr dz);
- static void hprefix(int z,int m,int setlen,dataptr dz);
- static void hinsert(int z,int m,int t,int setlen,dataptr dz);
- static void hshuflup(int z,int k,int setlen,dataptr dz);
- static int output_cut_sndfile(dataptr dz);
- static int setup_new_outfile(int n,dataptr dz);
- static void generate_splicelen(dataptr dz);
- static int Getnextfile(int n,int permcnt,dataptr dz);
- static void do_endsplice_in_outbuf(float *buf,double splicestep,dataptr dz);
- //TW replaced by global F_SECSIZE
- //#define FSECSIZE (256) /*RWD*/
- /********************************* DO_INTERSPLICE *************************************
- *
- * THE ALGORITHM
- *
- * A TWO-STAGE PROCESS:
- * 1) We read from an infile into a sector-aligned buffer
- * 2) We read from a point in inbuffer (not ness coinciding with its start(which is a sector boundary))
- * putting in splice at start of read (and, eventually, splice at end of all reads from this infile)
- * 3) WE copy each read to an outbuffer
- * 4) Except on very first use, outbuffer always retains a SPLICELEN-segment of data from the previous read
- * ready for splice to be made to next file copied into it.
- * 5) At start of this copy-to-outbuf we ADD the upsplice-part of new file to end downsplice of previous file
- * before proceeding to straightforward copying
- */
- int do_twixt(dataptr dz)
- {
- int exit_status, n = 0, limit, fileno;
- double lasttime=0.0, nexttime=0.0, splicestep;
- int obufend = 0, permcnt = 0, outsamps;
- splicestep = 1.0/(SPLICELEN/dz->infile->channels);
- if(dz->process != TWIXT || dz->mode != TRUE_EDIT)
- permcnt = dz->infilecnt + dz->iparam[IS_WEIGHT] - 1;
- if(dz->process == TWIXT && dz->mode == TRUE_EDIT)
- limit = dz->itemcnt - 1;
- else {
- switch(dz->mode) {
- case(IN_SEQUENCE):
- limit = dz->itemcnt - 1;
- break;
- default:
- limit = dz->iparam[IS_SEGCNT];
- break;
- }
- }
- dz->tempsize = (int)limit;
- while(n<limit) {
- fileno = Getnextfile(n,permcnt,dz);
- GetTimes(fileno,n,&lasttime,&nexttime,dz);
- if((exit_status = do_output(fileno,n,limit,lasttime,nexttime,splicestep,&obufend,&outsamps,dz))<0)
- return(exit_status);
- n++;
- }
- if((exit_status = flush_outbuf(n-1,obufend,dz))<0)
- return exit_status;
- if(dz->process == TWIXT && dz->mode == TRUE_EDIT) {
- if((exit_status = output_cut_sndfile(dz))<0)
- return exit_status;
- }
- return (FINISHED);
- }
- /********************************* GETTIMES *************************************/
- void GetTimes(int arrayno,int n,double *lasttime, double *nexttime,dataptr dz)
- {
- int k, j, lastperm;
- static int init = 1;
- int span = dz->itemcnt-1;
- if(dz->process == TWIXT) /* only one aray of times, for TWIXT */
- arrayno = 0;
- if(dz->process == TWIXT && dz->mode == TRUE_EDIT) {
- *lasttime = dz->parray[arrayno][n++];
- *nexttime = dz->parray[arrayno][n];
- } else {
- switch(dz->mode) {
- case(IN_SEQUENCE):
- *lasttime = dz->parray[arrayno][n++];
- *nexttime = dz->parray[arrayno][n];
- break;
- case(RAND_REORDER):
- n %= span;
- if(n == 0) {
- if(init) {
- randperm(0,span,dz);
- init = 0;
- } else {
- //TW UPDATE
- // lastperm = dz->iparray[arrayno][span-1];
- lastperm = dz->iparray[0][span-1];
- randperm(0,span,dz);
- //TW UPDATE
- // if(lastperm == dz->iparray[arrayno][0]) {
- // for(j=0;j < span; j++) {
- // if(dz->iparray[arrayno][j] != lastperm) {
- // dz->iparray[arrayno][0] = dz->iparray[arrayno][j];
- // dz->iparray[arrayno][j] = lastperm;
- if(lastperm == dz->iparray[0][0]) {
- for(j=0;j < span; j++) {
- if(dz->iparray[0][j] != lastperm) {
- dz->iparray[0][0] = dz->iparray[0][j];
- dz->iparray[0][j] = lastperm;
- break;
- }
- }
- }
- }
- }
- //TW UPDATE
- // k = dz->iparray[arrayno][n];
- k = dz->iparray[0][n];
- *lasttime = dz->parray[arrayno][k++];
- *nexttime = dz->parray[arrayno][k];
- break;
- case(RAND_SEQUENCE):
- k = (int)floor(drand48() * span);
- *lasttime = dz->parray[arrayno][k++];
- *nexttime = dz->parray[arrayno][k];
- break;
- }
- }
- }
- /********************************* DO_OUTPUT *************************************/
- /*RWD 6:2001: NB permcnt,n unused */
- int do_output(int fileno,int n,int limit,double lasttime,double nexttime,double splicestep,
- int *obufend,int *total_outsamps,dataptr dz)
- {
- int exit_status;
- static int init;
- int lastsamp = Convert_to_samp(lasttime,dz);
- int nextsamp = Convert_to_samp(nexttime,dz);
- int firsttime = 1;
- int remaining_samps_to_write, startbuf, offset, rotate, max_possible_outsamps, outsamps, startsplice;
- float *inbuf, *outbuf;
- if(nextsamp - lastsamp < SPLICELEN) /* Possible for final segment */
- return(FINISHED);
- lastsamp = max(0,lastsamp - SPLICELEN); /* adjust so upsplice starts before start of segment */
- remaining_samps_to_write = nextsamp - lastsamp;
- *total_outsamps = remaining_samps_to_write;
- startbuf = (lastsamp/dz->iparam[IS_SHSECSIZE]) * dz->iparam[IS_SHSECSIZE];
- offset = lastsamp - startbuf;
- rotate = dz->iparam[IS_SHSECSIZE] - offset;
- /*
- * x = offset, start of outbuf
- * y = SECTOR-minus-x left at end
- *
- * b_________________________________c
- * x | |y
- * |_____________INBUF positioned ___________|
- * so y can be copied to its start
- *
- * 1st time, output from b to c = sampbuflen - dz->iparam[IS_SHSECSIZE]
- * then copy 'y' to before start of inbuf
- * Other reads, output from a to c. = sampbuflen
- * then copy 'y' to before start of inbuf
- *
- * a_____b_________________________________c
- *|y| x |
- *|_____sampbuf = standardsize + SECTOR_________|
- *
- */
- inbuf = dz->sampbuf[0] + (dz->iparam[IS_SHSECSIZE] - offset);
- outbuf = inbuf + offset;
- max_possible_outsamps = dz->buflen - dz->iparam[IS_SHSECSIZE];
- sndseekEx(dz->ifd[fileno],startbuf,0);
- dz->total_samps_read = startbuf;
- do {
- if(n!=0 && (dz->process == TWIXT) && (dz->mode == TRUE_EDIT) && firsttime) {
- if((exit_status = setup_new_outfile(n,dz))<0)
- return(exit_status);
- }
- if((dz->ssampsread = fgetfbufEx(inbuf, dz->buflen,dz->ifd[fileno],0)) < 0) {
- sprintf(errstr,"Can't read samples from input soundfile.\n");
- return(SYSTEM_ERROR);
- }
- if(dz->ssampsread == 0)
- break;
- if(firsttime && !init)
- do_startsplice_in_outbuf(outbuf,splicestep,dz);
- else
- init = 0;
- if(max_possible_outsamps >= remaining_samps_to_write) {
- outsamps = remaining_samps_to_write;
- do_endsplice_in_outbuf(outbuf + outsamps - SPLICELEN,splicestep,dz);
- } else {
- outsamps = max_possible_outsamps;
- if ((startsplice = remaining_samps_to_write - SPLICELEN) < outsamps)
- do_startof_endsplice_in_outbuf(outbuf + startsplice,outsamps - startsplice,splicestep,dz);
- }
- output_samps(n,firsttime,outbuf,outsamps,obufend,dz);
- remaining_samps_to_write -= outsamps;
- memcpy((char *)dz->sampbuf[0],(char *)(outbuf + outsamps),rotate * sizeof(float));
- outbuf = dz->sampbuf[0]; /* after first output */
- max_possible_outsamps = dz->buflen;
- firsttime = 0;
- } while(remaining_samps_to_write > 0);
- if((dz->process == TWIXT) && (dz->mode == TRUE_EDIT) && (n != limit - 1)) {
- if((exit_status = flush_outbuf(n,*obufend,dz))<0)
- return exit_status;
- *obufend = 0;
- return output_cut_sndfile(dz);
- }
- return(FINISHED);
- }
- /********************************* CONVERT_TO_SAMP *************************************/
- int Convert_to_samp(double time,dataptr dz)
- {
- int sampcnt = (int)round(time * dz->infile->srate);
- sampcnt *= dz->infile->channels;
- return(sampcnt);
- }
- /********************************* DO_STARTSPLICE_IN_OUTBUF *************************************/
- void do_startsplice_in_outbuf(float *buf,double splicestep,dataptr dz)
- {
- int chans = dz->infile->channels;
- double q, mult = 0.0;
- int k, n = 0;
- while(n < SPLICELEN) {
- mult += splicestep;
- for(k = 0; k < chans; k++) {
- q = *buf * mult;
- *buf++ = (float) /*round*/(q);
- n++;
- }
- }
- }
- /********************************* DO_ENDSPLICE_IN_OUTBUF *************************************/
- void do_endsplice_in_outbuf(float *buf,double splicestep,dataptr dz)
- {
- int chans = dz->infile->channels;
- double q, mult = 1.0;
- int k, n = 0;
- while(n < SPLICELEN) {
- mult -= splicestep;
- for(k = 0; k < chans; k++) {
- q = *buf * mult;
- *buf++ = (float) /*round*/(q);
- n++;
- }
- }
- }
- /********************************* DO_STARTOF_ENDSPLICE_IN_OUTBUF *************************************/
- void do_startof_endsplice_in_outbuf(float *buf,int splicetodo,double splicestep,dataptr dz)
- {
- int chans = dz->infile->channels;
- double q, mult = 1.0;
- int k, n = 0;
- while(n < splicetodo) {
- mult -= splicestep;
- for(k = 0; k < chans; k++) {
- q = *buf * mult;
- *buf++ = (float) /*round*/(q);
- n++;
- }
- }
- }
- /********************************* OUTPUT_SAMPS *************************************/
- int output_samps(int segno,int firsttime,float *outbuf,int outsamps,int *obufend,dataptr dz)
- {
- /* true length of buf = buflen + SPLICELEN */
- int exit_status;
- int n, samps_to_copy, xs;
- int samps_to_write = dz->buflen ;
- int space_in_outbuf = dz->buflen + SPLICELEN - *obufend;
- float *thisbuf = dz->sampbuf[1] + *obufend;
- static int ob_firsttime = 1;
- if(ob_firsttime) {
- memcpy((char *)thisbuf,(char *)outbuf,outsamps * sizeof(float));
- *obufend = outsamps;
- ob_firsttime = 0;
- } else {
- if(firsttime && (dz->process != TWIXT || dz->mode != TRUE_EDIT)) {
- thisbuf -= SPLICELEN;
- for(n=0; n < SPLICELEN; n++)
- thisbuf[n] = (float)(thisbuf[n] + outbuf[n]);
- thisbuf += SPLICELEN;
- outbuf += SPLICELEN;
- samps_to_copy = outsamps - SPLICELEN;
- } else
- samps_to_copy = outsamps;
- if((xs = samps_to_copy - space_in_outbuf) < 0) {
- memcpy((char *)thisbuf,(char *)outbuf,samps_to_copy * sizeof(float));
- *obufend += samps_to_copy;
- } else {
- memcpy((char *)thisbuf,(char *)outbuf,space_in_outbuf * sizeof(float));
- samps_to_write = dz->buflen;
- superzargo = segno + 1;
- if((exit_status = write_samps(dz->sampbuf[1],samps_to_write,dz))<0)
- return(exit_status);
- thisbuf = dz->sampbuf[1];
- memcpy((char *)thisbuf,(char *)(thisbuf + dz->buflen),SPLICELEN * sizeof(float));
- memset((char *)(thisbuf+SPLICELEN),0,samps_to_write);
- *obufend = SPLICELEN;
- if(xs > 0) {
- thisbuf += *obufend;
- outbuf += space_in_outbuf;
- memcpy((char *)thisbuf,(char *)outbuf,xs * sizeof(float));
- *obufend += xs;
- }
- }
- }
- return(FINISHED);
- }
-
- /********************************* FLUSH_OUTBUF *************************************/
- int flush_outbuf(int segno,int obufend,dataptr dz)
- {
- int exit_status;
- int samps_to_write = obufend;
- superzargo = segno+1;
- if(samps_to_write > 0) {
- if((exit_status = write_samps(dz->sampbuf[1],samps_to_write,dz))<0)
- return(exit_status);
- }
- return(FINISHED);
- }
- /********************************* RANDPERM *************************************/
- void randperm(int z,int setlen,dataptr dz)
- {
- int n, t;
- for(n=0;n<setlen;n++) {
- t = (int)floor(drand48() * (n+1));
- if(t>=n)
- hprefix(z,n,setlen,dz);
- else
- hinsert(z,n,t,setlen,dz);
- }
- }
- /***************************** HINSERT **********************************
- *
- * Insert the value m AFTER the T-th element in iparray[].
- */
- void hinsert(int z,int m,int t,int setlen,dataptr dz)
- {
- hshuflup(z,t+1,setlen,dz);
- dz->iparray[z][t+1] = m;
- }
- /***************************** HPREFIX ************************************
- *
- * Insert the value m at start of the permutation iparray[].
- */
- void hprefix(int z,int m,int setlen,dataptr dz)
- {
- hshuflup(z,0,setlen,dz);
- dz->iparray[z][0] = m;
- }
- /****************************** HSHUFLUP ***********************************
- *
- * move set members in iparray[] upwards, starting from element k.
- */
- void hshuflup(int z,int k,int setlen,dataptr dz)
- {
- int n, *i;
- int y = setlen - 1;
- i = (dz->iparray[z]+y);
- for(n = y;n > k;n--) {
- *i = *(i-1);
- i--;
- }
- }
- /********************************** OUTPUT_CUT_SNDFILE ********************************/
- int output_cut_sndfile(dataptr dz)
- {
- int exit_status;
- char filename[64];
- if((exit_status = headwrite(dz->ofd,dz))<0)
- return(exit_status);
- dz->process_type = OTHER_PROCESS; /* stop process trying to deal with outfile later */
- dz->outfiletype = NO_OUTPUTFILE; /* stop process trying to deal with outfile later */
- if(sndcloseEx(dz->ofd) < 0) {
- fprintf(stdout,"WARNING: Can't close output soundfile %s\n",filename);
- fflush(stdout);
- }
- dz->ofd = -1;
- return(FINISHED);
- }
- /********************************** SETUP_NEW_OUTFILE ********************************/
- int setup_new_outfile(int n,dataptr dz)
- {
- int exit_status;
- char filename[64];
- strcpy(filename,dz->wordstor[0]);
- insert_new_number_at_filename_end(filename,n,1);
- dz->process_type = UNEQUAL_SNDFILE; /* allow sndfile to be created */
- dz->outfiletype = SNDFILE_OUT; /* allows header to be written */
- if((exit_status = create_sized_outfile(filename,dz))<0) {
- fprintf(stdout, "WARNING: Cannot create Soundfile %s : %s\n", filename, rsferrstr);
- fflush(stdout);
- dz->process_type = OTHER_PROCESS;
- dz->outfiletype = NO_OUTPUTFILE; /* stop process trying to deal with outfile later */
- dz->ofd = -1;
- return(GOAL_FAILED);
- }
- return(FINISHED);
- }
- /*************************** CREATE_TWIXT_BUFFERS **************************/
- int create_twixt_buffers(dataptr dz)
- {
- int bigsize;
- size_t bigbufsize;
- int framesize = F_SECSIZE * dz->infile->channels;
- if(dz->sbufptr == 0 || dz->sampbuf==0) {
- sprintf(errstr,"buffer pointers not allocated: create_sndbufs()\n");
- return(PROGRAM_ERROR);
- }
- bigbufsize = (size_t) Malloc(-1);
- bigbufsize -= (SPLICELEN + F_SECSIZE) * sizeof(float);
- bigbufsize /= dz->bufcnt;
- dz->buflen = (int)(bigbufsize/sizeof(float));
- dz->buflen = (dz->buflen / framesize) * framesize;
- if(dz->buflen <= 0)
- dz->buflen = framesize;
- bigsize = (dz->buflen * dz->bufcnt) + SPLICELEN + F_SECSIZE;
- if((dz->bigbuf = (float *)malloc(bigsize * sizeof(float))) == NULL) {
- sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
- return(PROGRAM_ERROR);
- }
- dz->sbufptr[0] = dz->sampbuf[0] = dz->bigbuf;
- dz->sbufptr[1] = dz->sampbuf[1] = dz->sampbuf[0] + dz->buflen + dz->iparam[IS_SHSECSIZE];
- dz->sampbuf[2] = dz->sampbuf[1] + dz->buflen + SPLICELEN;
- memset((char *)dz->bigbuf,0,bigsize * sizeof(float));
- return(FINISHED);
- }
- /*************************** TWIXT_PREPROCESS **************************/
- int twixt_preprocess(dataptr dz)
- {
- double *p, last, next = 0.0;
- int n;
- int minsamps = dz->insams[0];
- for(n = 1; n<dz->infilecnt;n++)
- minsamps = min(minsamps,dz->insams[n]);
- dz->duration = (double)(minsamps/dz->infile->channels)/(double)dz->infile->srate;
- if(dz->mode != TRUE_EDIT) {
- if((dz->iparray[1] = (int *)malloc((dz->infilecnt + dz->iparam[IS_WEIGHT] - 1) * sizeof(int)))==NULL) {
- sprintf(errstr,"INSUFFICIENT MEMORY to allocate file permutation store.\n");
- return(MEMORY_ERROR);
- }
- }
- generate_splicelen(dz);
- p = dz->parray[0];
- if((last = *p++) > dz->duration) {
- sprintf(errstr,"First time in datafile is beyond end of shortest input soundfile (%lf)\n",dz->duration);
- return(DATA_ERROR);
- }
- n = 2;
- while(n <= dz->itemcnt) {
- if((next = *p++) > dz->duration) {
- sprintf(errstr,"Times beyond number %d ( = %lf) in datafile are beyond end of shortest input soundfile (%lf)\n",
- n-1,last,dz->duration);
- return(DATA_ERROR);
- }
- if(next - last <= dz->param[IS_SPLICETIME]) {
- sprintf(errstr,"distant between splicepoints %d & %d (%lf) less than time needed for splices (%lf)\n",
- n,n-1,next-last,dz->param[IS_SPLICETIME]);
- return(DATA_ERROR);
- }
- last = next;
- n++;
- }
- if(dz->duration - next > dz->param[IS_SPLICETIME]) {
- if((dz->parray[0] = (double *)realloc((char *)dz->parray[0],(dz->itemcnt+1) * sizeof(double)))==NULL) {
- sprintf(errstr,"Insufficient memory to store time-switch data.\n");
- return(MEMORY_ERROR);
- }
- dz->parray[0][dz->itemcnt] = dz->duration;
- dz->itemcnt++;
- }
- return(FINISHED);
- }
- /*************************** SPHINX_PREPROCESS **************************/
- int sphinx_preprocess(dataptr dz)
- {
- double *p, last, next;
- int n, k;
- if((dz->iparray[1] = (int *)malloc((dz->infilecnt + dz->iparam[IS_WEIGHT] - 1) * sizeof(int)))==NULL) {
- sprintf(errstr,"INSUFFICIENT MEMORY to allocate file permutation store.\n");
- return(MEMORY_ERROR);
- }
- generate_splicelen(dz);
- for(k=0;k<dz->array_cnt;k++) {
- p = dz->parray[k];
- last = *p++;
- n = 2;
- while(n <= dz->itemcnt) {
- next = *p++;
- if(next - last <= dz->param[IS_SPLICETIME]) {
- sprintf(errstr,"distant between splicepoints %d & %d (%lf) less than time needed for splices (%lf)\n",
- n,n-1,next-last,dz->param[IS_SPLICETIME]);
- return(DATA_ERROR);
- }
- last = next;
- n++;
- }
- }
- return(FINISHED);
- }
- /*************************** GENERATE_SPLICELEN **************************/
- void generate_splicelen(dataptr dz)
- {
- double splicetime;
- int j;
- dz->iparam[IS_SHSECSIZE] = F_SECSIZE;
- splicetime = dz->param[IS_SPLEN] * MS_TO_SECS;
- j = round(splicetime * dz->infile->srate);
- j *= dz->infile->channels;
- SPLICELEN = j;
- dz->param[IS_SPLICETIME] = (double)(SPLICELEN/dz->infile->channels)/(double)dz->infile->srate;
- }
- /*************************** GETNEXTFILE **************************/
- int Getnextfile(int n,int permcnt,dataptr dz)
- {
- int lastperm, j, k;
- static int init = 1;
- if(dz->process == TWIXT && dz->mode == TRUE_EDIT)
- return 0;
- n %= permcnt;
- if((permcnt == dz->infilecnt) && !dz->vflag[IS_NOTCYCL]) /* with no weighting, and cyclic files specified */
- return n;
- if(n == 0) {
- if(init) {
- randperm(1,permcnt,dz);
- for(j=0;j < permcnt;j++) {
- if(dz->iparray[1][j] >= dz->infilecnt) /* substitute file1 for extra spaces in perm */
- dz->iparray[1][j] = 0;
- }
- if(dz->iparray[1][0] != 0) {
- for(j=1;j < permcnt;j++) { /* Force first file to be first !! */
- if(dz->iparray[1][j] == 0) {
- dz->iparray[1][j] = dz->iparray[1][0];
- dz->iparray[1][0] = 0;
- break;
- }
- }
- }
- if(!dz->vflag[IS_NOTCYCL]) {
- k = 1;
- for(j=1;j < permcnt;j++) { /* Force un-first files to be in order */
- if(dz->iparray[1][j] != 0)
- dz->iparray[1][j] = k++;
- }
- }
- init = 0;
- } else {
- lastperm = dz->iparray[1][permcnt - 1];
- randperm(1,permcnt,dz);
- for(j=0;j < permcnt;j++) { /* substitute file1 for extra spaces in perm */
- if(dz->iparray[1][j] >= dz->infilecnt)
- dz->iparray[1][j] = 0;
- } /* avoid repets at perm boundaries */
- if((dz->iparray[1][0] == lastperm) && ((lastperm != 0) || (dz->iparam[IS_WEIGHT] == 1))) {
- for(j=1;j < permcnt;j++) {
- if(dz->iparray[1][j] != lastperm) {
- dz->iparray[1][0] = dz->iparray[1][j];
- dz->iparray[1][j] = lastperm;
- break;
- }
- }
- }
- if(!dz->vflag[IS_NOTCYCL]) {
- k = 1;
- for(j=1;j < permcnt;j++) { /* Force un-first files to be in order */
- if(dz->iparray[1][j] != 0)
- dz->iparray[1][j] = k++;
- }
- }
- }
- }
- return dz->iparray[1][n];
- }
|