File: svgplot.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (429 lines) | stat: -rw-r--r-- 15,854 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/************************************************************************
 IMPORTANT NOTE : this file contains two clearly delimited sections :
 the ARCHITECTURE section (in two parts) and the USER section. Each section
 is governed by its own copyright and license. Please check individually
 each section for license and copyright information.
 *************************************************************************/

/******************* BEGIN svgplot.cpp ****************/

/************************************************************************
 FAUST Architecture File
 Copyright (C) 2003-2023 GRAME, Centre National de Creation Musicale
 ---------------------------------------------------------------------
 This Architecture section 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 3 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, see <http://www.gnu.org/licenses/>.
 
 EXCEPTION : As a special exception, you may create a larger work
 that contains this FAUST architecture section and distribute
 that work under terms of your choice, so long as this FAUST
 architecture section is not modified.
 
 ************************************************************************
 ************************************************************************/

#include <cstdio>
#include <iostream>
#include <ostream>
#include <string>

#include "faust/gui/UI.h"
#include "faust/audio/channels.h"
#include "faust/dsp/dsp.h"
#include "faust/gui/console.h"
#include "faust/gui/DecoratorUI.h"
#include "faust/misc.h"

#ifdef SOUNDFILE
#include "faust/gui/SoundUI.h"
#endif

#include "plot.h"
#include "heatmap.h"

using namespace std;

// A class to count Probe
struct ProbeCounter : public GenericUI
{

    map<string, FAUSTFLOAT*> fProbeMap;
     
    void displayProbeHeaders()
    {
        int c = 0;
        if (fProbeMap.size() > 0) {
            printf(",\t");
            for (const auto& it : fProbeMap) {
                if (c > 0) {
                    printf(",\t");
                    printf("Probe %d", c + 1);
                }
                c++;
            }
        }
    }
    
    int getNumProbes()
    {
        return fProbeMap.size();
    }

    void displayProbe(signalsmith::plot::Plot2D &plot, signalsmith::plot::Line2D &probe, int frame, int chan, float& value)
    {
        if (fProbeMap.size() > 0) {
            for (int c = 0; c < fProbeMap.size(); c++) {
                for (const auto& it : fProbeMap) {
                    if (c == chan) {
                        probe.add(frame, (*it.second));
                        value = (*it.second);
                    }
                    c++;
                }
            }
        }
    }

    void declare(FAUSTFLOAT* zone,const char* key,const char* val)
    {
        if (strcmp(key,"probe") == 0) {
            fProbeMap[std::string(val)]=zone;
        }
    }
    
};

// The main SVG and HTML files creation class
class SVGPlot {

    private:
        
        // DSP
        FAUSTFLOAT** fDSPInputs;
        FAUSTFLOAT** fDSPOutputs;
        dsp* fDSP;
    
        // args
        FAUSTFLOAT fCountSamples;
        FAUSTFLOAT fSampleRate;
        FAUSTFLOAT fBufferSize;
        FAUSTFLOAT fStartSample;
        bool fSplit;
    
        // global plot
        signalsmith::plot::Plot2D fPlot;
        // plot for probes in split mode
        signalsmith::plot::Plot2D fSplot;
        // plot for lines in split mode
        signalsmith::plot::Plot2D fStplot;
    
        // Function to create buffers
        FAUSTFLOAT** createbuffer(int chan, int buffer_size)
        {
            FAUSTFLOAT** buffer = new FAUSTFLOAT*[chan];
            for (int i = 0; i < chan; i++) {
                buffer[i] = new FAUSTFLOAT[buffer_size];
                memset(buffer[i], 0, sizeof(FAUSTFLOAT) * buffer_size);
            }
            return buffer;
        }
        
        // Function to delete a buffer
        void deletebuffer (FAUSTFLOAT** buffer, int chan)
        {
            for (int i = 0; i <chan; i++) {
                delete [] buffer[i];
            }
            delete [] buffer;
        }
    
        void generateHTML(int channels, int num_probes, bool split)
        {
            ofstream file("mydsp.html"); // Create a file named "output.html"
            if (file.is_open()) {
                file << "<html>\n";
                file << "<head>\n";
                file << "<title>SVG Gallery</title>\n";
                file << "</head>\n";
                file << "<body>\n";
                file << "<img src=mydsp.svg>\n";
                if (split) {
                    for (int i = 0; i < channels; i++) {
                        file << "<img src=" << "mydsp_Channel" + std::to_string(i+1) << ".svg>\n";
                    }
                    for (int i = 0; i < num_probes; i++) {
                        file << "<img src=" << "mydsp_Probe" + std::to_string(i+1) << ".svg>\n";
                    }
                }
                file << "</body>\n";
                file << "</html>\n";
                file.close();
                
                cout << "mysvg.html;" << endl;
            } else {
                cerr << "Failed to create the SVG Gallery!" << endl;
            }
        }

    public:
    
        // Constructor
        SVGPlot(dsp* dsp,
                FAUSTFLOAT sample_rate,
                FAUSTFLOAT nb_samples,
                FAUSTFLOAT buffer_size,
                FAUSTFLOAT start_at_sample,
                FAUSTFLOAT split)
        {   
            // Args
            fDSP = dsp;
            fCountSamples = nb_samples;
            fSampleRate = sample_rate;
            fBufferSize = buffer_size;
            fSplit = split;
            fStartSample = start_at_sample;
            
            // Create the buffers
            fDSPInputs = createbuffer(fDSP->getNumInputs(), fCountSamples);
            fDSPOutputs = createbuffer(fDSP->getNumOutputs(), fCountSamples);
        }
    
        virtual ~SVGPlot()
        {
            deletebuffer(fDSPInputs, fDSP->getNumInputs());
            deletebuffer(fDSPOutputs, fDSP->getNumOutputs());
        }

        void exec()
        {
            // Custom UI functions
            ProbeCounter probe_counter;
            fDSP->buildUserInterface(&probe_counter);
        
            // Init position for max and min
            FAUSTFLOAT max = fDSPOutputs[0][0];
            FAUSTFLOAT min = fDSPOutputs[0][0];
                        
            // Init signal processor and the user interface values
            fDSP->init(fSampleRate);

            // Create the graph legend
            auto& legend = fPlot.legend(0, -1);
        
            // Compute
            fDSP->compute(fCountSamples, fDSPInputs, fDSPOutputs);

            // Go through the different channels
            for (int chan = 0; chan< fDSP->getNumOutputs(); ++chan) {
                // reset for straight line and no dotted line
                fPlot.styleCounter.dash = 0;
                // create a line for the actual channel
                auto& line = fPlot.line();
                // temporary line
                auto& sline = fStplot.line();
                auto& slegend = fStplot.legend(0, -1);
                // fixed Index colour position similar to channel
                line.styleIndex.colour = chan;
                for (int frame = fStartSample; frame < fCountSamples; ++frame) {
                    
                    FAUSTFLOAT* sub_outputs = fDSPOutputs[chan];
                    
                    // write points in line
                    line.add(frame, sub_outputs[frame]);
                
                    // check for max and min in order to create proportional axes
                    max = std::max(max, sub_outputs[frame]);
                    min = std::min(min, sub_outputs[frame]);
                     
                    sline.add(frame, sub_outputs[frame]);
                    if (fSplit) {
                        //probe_counter.returnProbe(fSplot, spr:wobe, curpos, num_probes, valuepro);
                    }
                }
                // add legend name
                legend.line(line, "Channel " + std::to_string(chan+1));
                if (fSplit) {
                    fStplot.y.blank(1);
                    fStplot.x.blank(1);
                    fStplot.y.blankLabels(1);
                    fStplot.x.blankLabels(1);
                    
                    slegend.line(sline,"Channel " + std::to_string(chan+1));
                    
                    fStplot.x.linear(fStartSample, fCountSamples).major(fStartSample).minor(fCountSamples).label("Samples");
	                fStplot.y.minors(min, max).label("Values");
                    fStplot.y.majors(0);
                
                    // create the svg file
                    fStplot.write("mydsp_Channel" + std::to_string(chan + 1) + ".svg");
                    // auto& sprobe = fSplot.fill();
                    cout << "mydsp_Channel" + std::to_string(chan+1) + ".svg;" << endl;
                
                    sline.toFrame(0);
                    sline.clearFrames();
                }
            }

            // if probe metadata in the dsp code
            if (probe_counter.getNumProbes() > 0) {
                for (int num_probes = 0; num_probes < probe_counter.getNumProbes(); ++num_probes) {
                    float minpro = 0.0;
                    float maxpro = 0.0;
                    float valuepro = 0.0;
                    auto& slegend = fSplot.legend(0, -1);
                    fDSP->init(fSampleRate);
                    // probegraph_init
                    // reset for dotted line and no straight line
                    fPlot.styleCounter.dash = 5;
                    // create a line
                    auto& probe = fPlot.line();
                    // fixed Index colour position similar to probe number
                    probe.styleIndex.colour = num_probes;

                    // temporary probe
                    fSplot.styleCounter.dash = 5;
                    auto& sprobe = fSplot.line();
                    sprobe.styleIndex.colour = num_probes;

                    // buffer counter
                    int bcount = 0;
                    // samples with start point
                    int nbsamples = int(fCountSamples)-fStartSample;
                    if (fStartSample > 0) {
                        fDSP->compute(fStartSample, fDSPInputs, fDSPOutputs);
                    }
                    do {
                        // compute on the buffer size
                        fDSP->compute(fBufferSize, fDSPInputs, fDSPOutputs);
                        // increment buffer counter
                        ++bcount;
                        // reduce total samples number by buffer size
                        nbsamples -= fBufferSize;
                        // create a current frame position for probes
                        int curpos = fStartSample - 1 + bcount * int(fBufferSize);
                      
                        probe_counter.displayProbe(fPlot, probe, curpos, num_probes, valuepro);
                        if (fSplit) {
                            probe_counter.displayProbe(fSplot, sprobe, curpos, num_probes, valuepro);
                            minpro = std::min(minpro, valuepro);
                            maxpro = std::max(maxpro, valuepro);
                        }
                    } while (nbsamples > 0);
                    
                    // create a legend for the current line
                    legend.line(probe, "Probe " + std::to_string(num_probes + 1));
                    // split option
                    if (fSplit) {
                        // reset axes
                        fSplot.y.blank(1);
                        fSplot.x.blank(1);
                        // reset axes labels
                        fSplot.y.blankLabels(1);
                        fSplot.x.blankLabels(1);
                        // line legend
                        slegend.line(sprobe, "Probe " + std::to_string(num_probes + 1));
                        // sample axe
                        fSplot.x.linear(fStartSample, fCountSamples).major(fStartSample).minor(fCountSamples).label("Samples");
	                    // value axe
                        fSplot.y.minors(minpro, maxpro).label("Values");
                        fSplot.y.majors(0);
                        // create the svg file
                        fSplot.write("mydsp_Probe" + std::to_string(num_probes + 1) + ".svg");
                        // auto& sprobe = fSplot.fill();
                        cout << "mydsp_Probe" + std::to_string(num_probes + 1) + ".svg;" << endl;
                        // reset the current plot
                        sprobe.toFrame(0);
                        sprobe.clearFrames();
                    }
                }
                generateHTML(fDSP->getNumOutputs(), probe_counter.getNumProbes(), fSplit);
            }
        
            // create the axes
            fPlot.x.linear(fStartSample, fCountSamples).major(fStartSample).minor(fCountSamples).label("Samples");
            fPlot.y.minors(min, max).label("Values");
            fPlot.y.majors(0);
        
            // create the svg file
            fPlot.write("mydsp.svg");
            cout << "mydsp.svg;" << endl;
        }

};

/******************************************************************************
 *******************************************************************************
 
 VECTOR INTRINSICS
 
 *******************************************************************************
 *******************************************************************************/

<<includeIntrinsic>>

/********************END ARCHITECTURE SECTION (part 1/2)****************/

/**************************BEGIN USER SECTION **************************/

<<includeclass>>

/***************************END USER SECTION ***************************/

/*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/


list<GUI*> GUI::fGuiList;
ztimedmap GUI::gTimedZoneMap;

#define kFrames 1

int main(int argc, char* argv[])
{
    dsp* DSP = new mydsp();
    
    FAUSTFLOAT nb_samples, sample_rate, buffer_size, start_at_sample, split;
    CMDUI* interface = new CMDUI(argc, argv);
    DSP->buildUserInterface(interface);
    
    interface->addOption("-n", &nb_samples, 4096.0, 0.0, 100000000.0);
    interface->addOption("-r", &sample_rate, 44100.0, 0.0, 192000.0);
    interface->addOption("-bs", &buffer_size, kFrames, 0.0, kFrames * 16);
    interface->addOption("-s", &start_at_sample, 0, 0.0, 100000000.0);
    interface->addOption("-sp", &split, 0, 0.0, 1.0);
    
    if (DSP->getNumInputs() > 0) {
        cerr << "no inputs allowed " << endl;
        exit(1);
    }

    // SR has to be read before DSP init
    interface->process_one_init("-r");
    
    // modify the UI values according to the command line options, after init
    interface->process_init();

#ifdef SOUNDFILE
    SoundUI soundinterface;
    DSP->buildUserInterface(&soundinterface);
#endif

    SVGPlot* svg_plot = new SVGPlot(DSP, sample_rate, nb_samples, buffer_size, start_at_sample, bool(split));
    svg_plot->exec();

    delete svg_plot;
    delete interface;
    delete DSP;
}

/******************** END svgplot.cpp ****************/