File: recSchema.cpp

package info (click to toggle)
faust 2.14.4~repack2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 276,136 kB
  • sloc: cpp: 231,578; ansic: 15,403; sh: 10,871; java: 6,917; objc: 4,085; makefile: 3,002; cs: 1,077; ruby: 951; python: 885; xml: 550; yacc: 516; lex: 233; lisp: 201
file content (222 lines) | stat: -rw-r--r-- 7,194 bytes parent folder | download | duplicates (2)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/

#include <algorithm>
#include <iostream>

#include "exception.hh"
#include "recSchema.h"

using namespace std;

/**
 * Creates a new recursive schema (s1 ~ s2). The smallest component is
 * enlarged to the width of the other. The left and right horizontal
 * margins are computed according to the number of internal connections.
 */
schema* makeRecSchema(schema* s1, schema* s2)
{
    schema* a = makeEnlargedSchema(s1, s2->width());
    schema* b = makeEnlargedSchema(s2, s1->width());
    double  m = dWire * max(b->inputs(), b->outputs());
    double  w = a->width() + 2 * m;

    return new recSchema(a, b, w);
}

/**
 * Constructor of a recursive schema (s1 ~ s2). The two components
 * are supposed to have the same width.
 */
recSchema::recSchema(schema* s1, schema* s2, double width)
    : schema(s1->inputs() - s2->outputs(), s1->outputs(), width, s1->height() + s2->height()),
      fSchema1(s1),
      fSchema2(s2)
{
    // this version only accepts legal expressions of same width
    faustassert(s1->inputs() >= s2->outputs());
    faustassert(s1->outputs() >= s2->inputs());
    faustassert(s1->width() >= s2->width());

    // create the input and output points
    for (unsigned int i = 0; i < inputs(); i++) fInputPoint.push_back(point(0, 0));
    for (unsigned int i = 0; i < outputs(); i++) fOutputPoint.push_back(point(0, 0));
}

/**
 * The two subschema are placed centered vertically, s2 on top of s1.
 * The input and output points are computed.
 */
void recSchema::place(double ox, double oy, int orientation)
{
    beginPlace(ox, oy, orientation);

    double dx1 = (width() - fSchema1->width()) / 2;
    double dx2 = (width() - fSchema2->width()) / 2;

    // place the two sub diagrams
    if (orientation == kLeftRight) {
        fSchema2->place(ox + dx2, oy, kRightLeft);
        fSchema1->place(ox + dx1, oy + fSchema2->height(), kLeftRight);
    } else {
        fSchema1->place(ox + dx1, oy, kRightLeft);
        fSchema2->place(ox + dx2, oy + fSchema1->height(), kLeftRight);
    }

    // adjust delta space to orientation
    if (orientation == kRightLeft) {
        dx1 = -dx1;
    }

    // place input points
    for (unsigned int i = 0; i < inputs(); i++) {
        point p        = fSchema1->inputPoint(i + fSchema2->outputs());
        fInputPoint[i] = point(p.x - dx1, p.y);
    }

    // place output points
    for (unsigned int i = 0; i < outputs(); i++) {
        point p         = fSchema1->outputPoint(i);
        fOutputPoint[i] = point(p.x + dx1, p.y);
    }

    endPlace();
}

/**
 * The input points s1 ~ s2
 */
point recSchema::inputPoint(unsigned int i) const
{
    return fInputPoint[i];
}

/**
 * The output points s1 ~ s2
 */
point recSchema::outputPoint(unsigned int i) const
{
    return fOutputPoint[i];
}

/**
 * Draw the two subschema s1 and s2 as well as the implicit feedback
 * delays between s1 and s2.
 */
void recSchema::draw(device& dev)
{
    faustassert(placed());

    // draw the two subdiagrams
    fSchema1->draw(dev);
    fSchema2->draw(dev);

    // draw the implicit feedback delay to each fSchema2 input
    double dw = (orientation() == kLeftRight) ? dWire : -dWire;
    for (unsigned int i = 0; i < fSchema2->inputs(); i++) {
        const point& p = fSchema1->outputPoint(i);
        drawDelaySign(dev, p.x + i * dw, p.y, dw / 2);
    }
}

/**
 * Draw the delay sign of a feedback connection
 */
void recSchema::drawDelaySign(device& dev, double x, double y, double size)
{
    dev.trait(x - size / 2, y, x - size / 2, y - size);
    dev.trait(x - size / 2, y - size, x + size / 2, y - size);
    dev.trait(x + size / 2, y - size, x + size / 2, y);
}

/**
 * Draw the two subschema s1 and s2 as well as the feedback
 * connections between s1 and s2, and the feedfrom connections
 * beetween s2 and s1.
 */
void recSchema::collectTraits(collector& c)
{
    faustassert(placed());

    // draw the two subdiagrams
    fSchema1->collectTraits(c);
    fSchema2->collectTraits(c);

    // draw the feedback connections to each fSchema2 input
    for (unsigned int i = 0; i < fSchema2->inputs(); i++) {
        collectFeedback(c, fSchema1->outputPoint(i), fSchema2->inputPoint(i), i * dWire, outputPoint(i));
    }

    // draw the non recursive output lines
    for (unsigned int i = fSchema2->inputs(); i < outputs(); i++) {
        point p = fSchema1->outputPoint(i);
        point q = outputPoint(i);
        c.addTrait(trait(p, q));  // in->out order
    }

    // draw the input lines
    unsigned int skip = fSchema2->outputs();
    for (unsigned int i = 0; i < inputs(); i++) {
        point p = inputPoint(i);
        point q = fSchema1->inputPoint(i + skip);
        c.addTrait(trait(p, q));  // in->out order
    }

    // draw the feedfront connections from each fSchema2 output
    for (unsigned int i = 0; i < fSchema2->outputs(); i++) {
        collectFeedfront(c, fSchema2->outputPoint(i), fSchema1->inputPoint(i), i * dWire);
    }
}

/**
 * Draw a feedback connection between two points with an horizontal
 * displacement dx
 */
void recSchema::collectFeedback(collector& c, const point& src, const point& dst, double dx, const point& out)
{
    double ox = src.x + ((orientation() == kLeftRight) ? dx : -dx);
    double ct = (orientation() == kLeftRight) ? dWire / 2 : -dWire / 2;

    point up(ox, src.y - ct);
    point br(ox + ct / 2.0, src.y);

    c.addOutput(up);
    c.addOutput(br);
    c.addInput(br);

    c.addTrait(trait(up, point(ox, dst.y)));
    c.addTrait(trait(point(ox, dst.y), point(dst.x, dst.y)));
    c.addTrait(trait(src, br));
    c.addTrait(trait(br, out));
}

/**
 * Draw a feedfrom connection between two points with an horizontal
 * displacement dx
 */
void recSchema::collectFeedfront(collector& c, const point& src, const point& dst, double dx)
{
    double ox = src.x + ((orientation() == kLeftRight) ? -dx : dx);

    c.addTrait(trait(point(src.x, src.y), point(ox, src.y)));
    c.addTrait(trait(point(ox, src.y), point(ox, dst.y)));
    c.addTrait(trait(point(ox, dst.y), point(dst.x, dst.y)));
}