File: Vosim.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 62,416 kB
  • sloc: ansic: 192,636; cpp: 14,151; javascript: 9,654; objc: 9,181; java: 3,337; python: 3,333; sh: 1,783; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 126
file content (201 lines) | stat: -rw-r--r-- 6,347 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* VOSIM.C: VOice SIMulation implementation

   Copyright 2008 rasmus ekman

   rasmus ekman March 13, 2008, for Csound.

    This file is part of Csound.

    The Csound Library 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.

    Csound 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 Csound; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA
*/


/* Programmer note:
 * Pre- and post-conditions are noted at function level.
 * These are essential for the code to work at all.
 * There are some complications because we accept weird user input:
 * (a) kfund < 0: This is forced to positive - no point in "backward" events.
 * (b) kform == 0: This leads to infinite length pulse, ie silence.
 * (c) kform < 0: Table is read backward.
 *     If table is symmetric, kform and -kform should give bit-identical outputs.
 *
 * Don't fiddle with the code unless you understand how the table read
 * params pulsephs and pulseinc are used both to read table and to indicate that
 * a new pulse should start, and that pulseinc can be negative.
 */

//#include "csdl.h"
#include "csoundCore.h"
#include "interlocks.h"
#include <math.h>
#include <limits.h>

typedef struct {
        OPDS h;
        MYFLT *ar, *amp, *kfund, *kform, *kdamp, *knofpulse, *kpulsemul,
              *iftab, *iskip;
        FUNC *ftable;
        int32 timrem;    /* samples left of event */
        int32 pulstogo;  /* count of pulses to produce in burst */
        int32 pulsephs;  /* index into table of this pulse (= MAXLEN / kform) */
        int32 pulseinc;  /* increment in table of pulse */
        MYFLT pulseamp;  /* amp of current pulse */
        MYFLT ampdecay;  /* subtract from amp on new pulse */
        MYFLT lenfact;   /* increase length of next pulse */
} VOSIM;


/* Post: unless skipping init, timrem == 0 */
int32_t vosimset(CSOUND* csound, VOSIM *p)
{
    if (*p->iskip)
      return OK;

    p->ftable = csound->FTFind(csound, p->iftab);
    if (UNLIKELY(p->ftable == NULL)) {
      return csound->InitError(csound, Str("vosim: pulse table not found"));
    }

     p->timrem = p->pulstogo = p->pulsephs = p->pulseinc = 0;
     p->pulseamp = p->ampdecay = p->lenfact = FL(0.0);
     return OK;
}


/* Pre: timrem == 0.
  * Post:
  *    IF kform >= 0, pulsephs >= FMAXLEN.
  *    ELSE pulsephs < 0.
  *    timrem > 0.
  */
void vosim_event(CSOUND* csound, VOSIM *p)
{
    MYFLT fundabs = FABS(*p->kfund);
    /* count of pulses, (+1 since decr at start of pulse) */
    p->pulstogo = 1+(int32)*p->knofpulse;
    if (UNLIKELY(fundabs == FL(0.0))) {                /* infinitely long event */
      p->timrem = INT_MAX;
      csound->Warning(csound,
                      Str("vosim: zero kfund. 'Infinite' length event generated."));
    }
    else {
        p->timrem = (int32)(CS_ESR / fundabs);
        if (UNLIKELY(p->timrem == 0)) {
          p->timrem = CS_KSMPS;
          p->pulstogo = 0;
          csound->Warning(csound,
                          Str("vosim: kfund (%f) > sr. Generating ksmps silence."),
                          *p->kfund);
        }
    }
    p->pulseinc = (int32)(*p->kform * csound->sicvt);
    p->pulsephs = (p->pulseinc >= 0)? MAXLEN : -1;   /* starts a new pulse */
    p->ampdecay = *p->kdamp;
    /* increase initial amp, since it's reduced at pulse start */
    p->pulseamp = *p->amp + p->ampdecay;
    /* if negative, table is read alternately back-/forward */
    p->lenfact  = *p->kpulsemul;
    /* reduce table rate, since it's increased at pulse start */
    if (p->lenfact != FL(0.0))
      p->pulseinc /= p->lenfact;
}


/* Pre: pulsephs >= FMAXLEN OR pulsephs < 0.
 * Post:
 *    pulstogo is decremented or zero.
 *    0 <= pulsephs < FMAXLEN.
 */
void vosim_pulse(CSOUND* csound, VOSIM *p)
{
    IGN(csound);
    int32 pulselen;
    p->pulsephs &= PHMASK;
    p->pulseinc *= p->lenfact;
    /* If pulse can't fit in remaining event time, skip and generate silence */
    pulselen = (p->pulseinc != FL(0.0))?
                (int32)FABS(FMAXLEN / p->pulseinc) : INT_MAX;
    if (p->pulstogo-- <= 0 || pulselen > p->timrem) {
      p->pulstogo = 0;
    }
    p->pulseamp -= p->ampdecay;
}


int32_t vosim(CSOUND* csound, VOSIM *p)
{
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;
    MYFLT *ar = p->ar;
    MYFLT *ftdata;
    int32  lobits;

    FUNC *ftp = p->ftable;
    if (UNLIKELY(ftp == NULL)) goto err1;
    ftdata = ftp->ftable;
    lobits = ftp->lobits;

    if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (n=offset; n<nsmps; n++) {
      /* new event? */
      if (p->timrem == 0)
        vosim_event(csound, p);

      /* new pulse? */
      if (p->pulsephs >= MAXLEN || p->pulsephs < 0)
        vosim_pulse(csound, p);

      if (p->pulstogo > 0) {
        /* produce one sample */
        p->pulsephs &= PHMASK;
        ar[n] = *(ftdata + (p->pulsephs >> lobits)) * p->pulseamp;
        --p->timrem;
        p->pulsephs += p->pulseinc;
      }
      else {
        /* silence after last pulse in burst: */
        /* bypass regular synthesis and fill output with zeros */
        while (p->timrem && n<nsmps) {
          ar[n] = FL(0.0);
          --p->timrem;
          n++;
        }
        n--;
      }
    }
    return OK;
 err1:
    return csound->PerfError(csound, &(p->h),
                             Str("vosim: not initialised"));
}


/* ar   vosim   kamp, kFund, kForm, kDamp, kPulseCount, kPulseFactor,
                ifn [, iskip] */

#define S(x)    sizeof(x)

static OENTRY vosim_localops[] = {
  { "vosim", S(VOSIM), TR, 3, "a", "kkkkkkio", (SUBR)vosimset, (SUBR)vosim }
};


LINKAGE_BUILTIN(vosim_localops)