1 /*
2     pvread.c:
3 
4     Copyright (C) 1992, 2000 Richard Karpen, Richard Dobson
5 
6     This file is part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 
24 /**************************************************************/
25 /***********pvread ********************************************/
26 /*** By Richard Karpen - July-October 1992*********************/
27 /**************************************************************/
28 
29 #include "pvoc.h"
30 #include <math.h>
31 
32 /*RWD 10:9:2000 read pvocex file format */
33 #include "pvfileio.h"
34 static int32_t pvocex_loadfile(CSOUND *, const char *fname, PVREAD *p);
35 
36 #define WLN   1         /* time window is WLN*2*ksmps long */
37 #define OPWLEN (2*WLN*ksmps)    /* manifest used for final time wdw */
38 
FetchInOne(float * inp,MYFLT * buf,int32 fsize,MYFLT pos,int32 mybin)39 static void FetchInOne(
40     float   *inp,       /* pointer to input data */
41     MYFLT   *buf,       /* where to put our nice mag/pha pairs */
42     int32    fsize,      /* frame size we're working with */
43     MYFLT   pos,        /* fractional frame we want */
44     int32    mybin)
45 {
46     float   *frame0;
47     float   *frame1;
48     int32    base;
49     MYFLT   frac;
50     int32    twmybin = mybin+mybin;  /* Always used thus */
51 
52     /***** WITHOUT INFO ON WHERE LAST FRAME IS, MAY 'INTERP' BEYOND IT ****/
53     base = (int32)pos;               /* index of basis frame of interpolation */
54     frac = ((MYFLT)(pos - (MYFLT)base));
55     /* & how close to get to next */
56     frame0 = inp + ((int32) fsize + 2L) * base + twmybin;
57     frame1 = frame0 + ((int32) fsize + 2L);      /* addresses of both frames */
58     if (frac != 0.0) {  /* must have 2 cases to avoid possible segmentation */
59                         /* violations and failed computes, else may interp  */
60                         /* beyond valid data */
61       buf[0] = frame0[0] + frac * (frame1[0] - frame0[0]);
62       buf[1] = frame0[1] + frac * (frame1[1] - frame0[1]);
63     }
64     else {
65         /* frac is 0.0 i.e. just copy the source frame */
66       buf[0] = frame0[0];
67       buf[1] = frame0[1];
68     }
69 }
70 
pvreadset_(CSOUND * csound,PVREAD * p,int32_t stringname)71 int32_t pvreadset_(CSOUND *csound, PVREAD *p, int32_t stringname)
72 {
73     char      pvfilnam[256];
74 
75     if (stringname==0){
76       if (csound->ISSTRCOD(*p->ifilno))
77         strNcpy(pvfilnam,get_arg_string(csound, *p->ifilno), MAXNAME);
78       else csound->strarg2name(csound, pvfilnam, p->ifilno, "pvoc.",0);
79     }
80     else strNcpy(pvfilnam, ((STRINGDAT *)p->ifilno)->data, MAXNAME);
81 
82     if (pvocex_loadfile(csound, pvfilnam, p) == OK) {
83       p->prFlg = 1;
84       p->mybin = MYFLT2LRND(*p->ibin);
85       return OK;
86     }
87     return NOTOK;
88 }
89 
pvreadset(CSOUND * csound,PVREAD * p)90 int32_t pvreadset(CSOUND *csound, PVREAD *p){
91     return pvreadset_(csound,p,0);
92 }
93 
pvreadset_S(CSOUND * csound,PVREAD * p)94 int32_t pvreadset_S(CSOUND *csound, PVREAD *p){
95     return pvreadset_(csound,p,1);
96 }
97 
pvread(CSOUND * csound,PVREAD * p)98 int32_t pvread(CSOUND *csound, PVREAD *p)
99 {
100     MYFLT  frIndx;
101     MYFLT  buf[2];
102     int32_t    size = pvfrsiz(p);
103 
104     if (UNLIKELY((frIndx = *p->ktimpnt * p->frPrtim) < 0)) goto err1;
105     if (frIndx > p->maxFr) {  /* not past last one */
106       frIndx = (MYFLT)p->maxFr;
107       if (p->prFlg) {
108         p->prFlg = 0;   /* false */
109         csound->Warning(csound, Str("PVOC ktimpnt truncated to last frame"));
110       }
111     }
112     FetchInOne(p->frPtr, &(buf[0]), size, frIndx, p->mybin);
113     *p->kfreq = buf[1];
114     *p->kamp = buf[0];
115     return OK;
116  err1:
117     return csound->PerfError(csound, &(p->h), Str("PVOC timpnt < 0"));
118 }
119 
pvocex_loadfile(CSOUND * csound,const char * fname,PVREAD * p)120 static int32_t pvocex_loadfile(CSOUND *csound, const char *fname, PVREAD *p)
121 {
122     PVOCEX_MEMFILE  pp;
123 
124     if (UNLIKELY(csound->PVOCEX_LoadFile(csound, fname, &pp) != 0)) {
125       return csound->InitError(csound, Str("PVREAD cannot load %s"), fname);
126     }
127     /* have to reject m/c files for now, until opcodes upgraded */
128     if (UNLIKELY(pp.chans > 1)) {
129       return csound->InitError(csound, Str("pvoc-ex file %s is not mono"), fname);
130     }
131     /* ignore the window spec until we can use it! */
132     p->frSiz    = pp.fftsize;
133     p->frPtr    = (float*) pp.data;
134     p->baseFr   = 0;  /* point to first data frame */
135     p->maxFr    = pp.nframes - 1;
136     p->asr      = pp.srate;
137     /* highest possible frame index */
138     /* factor by which to mult expand phase diffs (ratio of samp spacings) */
139     p->frPrtim = CS_ESR / ((MYFLT) pp.overlap);
140     return OK;
141 }
142 
143