ofxPDSP
BasicNodes.h
1 
2 // BasicNodes.h
3 // ofxPDSP
4 // Nicola Pisanti, MIT License, 2016
5 
6 
7 #ifndef PDSP_CORE_BASICNODES_H_INCLUDED
8 #define PDSP_CORE_BASICNODES_H_INCLUDED
9 
10 
11 
12 #include <vector>
13 #include <math.h>
14 #include <cassert>
15 #include <stdint.h>
16 #include <limits>
17 #include "../pdspFunctions.h"
18 #include "../pdspConstants.h"
19 #include "../../math/header.h"
20 #include "Preparable.h"
21 #include <cstring>
22 #include <atomic>
23 #include <iostream>
24 #include "../../flags.h"
25 
26 namespace pdsp {
27 
28 class Patchable;
29 class InputNode;
30 class OutputNode;
31 class ValueNode;
32 class NullInput;
33 class NullOutput;
34 
39 //-------------------------------------OUTPUT DATA------------------------------------------------
40 class OutputData {
41 public:
42  OutputData( OutputNode* node, bool multiply, float multiplier )
43  : node( node ), multiply( multiply ), multiplier( multiplier ) { };
44 
45  OutputNode* node;
46  bool multiply;
47  float multiplier;
48 };
49 
50 //----------------------------------NODE REFERENCES--------------------------
51 
52 class NamedInput {
53 public:
54  NamedInput( const char* tag, InputNode* pInput ) : tag( tag ), input( pInput ) {};
55 
56  const char* tag;
57  InputNode* input;
58 };
59 
60 class NamedOutput {
61 public:
62  NamedOutput( const char* tag, OutputNode* pOutput ) : tag( tag ), output( pOutput ) {};
63 
64  const char* tag;
65  OutputNode* output;
66 };
67 
72 //--------------------------------PATCHABLE------------------------------------------
73 
74 
75 
82 class Patchable {
83  friend class Unit;
84  friend class InputNode;
85  friend class Switch;
86 
87 public:
88  Patchable();
89 
93  void resetInputToDefault();
97  void resetOutputToDefault();
98 
105  Patchable& in( const char* tag );
106 
107 
114  Patchable& out( const char* tag );
115 
119  void disconnectIn();
120 
124  void disconnectOut();
125 
129  void disconnectAll();
130 
134  InputNode& getSelectedInput();
135 
139  OutputNode& getSelectedOutput();
140 
144  std::vector<std::string> getInputsList();
145 
149  std::vector<std::string> getOutputsList();
150 
151 protected:
152 
160  void addOutput( const char* tag, OutputNode & output );
161 
169  void addInput( const char* tag, InputNode & input );
170 
178  void addModuleOutput( const char* tag, Patchable & unit );
179 
187  void addModuleInput( const char* tag, Patchable & unit );
188 
192  [[deprecated("Replaced by addModuleOutput() for a less ambigous nomenclature")]]
193  void addUnitOutput( const char* tag, Patchable & unit );
194 
195  [[deprecated("Replaced by addModuleInput() for a less ambigous nomenclature")]]
196  void addUnitInput( const char* tag, Patchable & unit );
201 private:
202  std::vector<NamedInput> inputs;
203  std::vector<NamedOutput> outputs;
204 
205  static NullInput invalidInput;
206  static NullOutput invalidOutput;
207 
208  InputNode* selectedInput;
209  OutputNode* selectedOutput;
210 
211  void clearAllAddedNodes();
212 };
213 
214 
221 //-------------------------------------Unit------------------------------------------------
222 
223 class Unit : public Preparable, public Patchable {
224  friend class InputNode;
225 
226 public:
227  Unit();
228  Unit(const Unit & other);
229  Unit& operator= (const Unit & other);
230  Unit (Unit&& other);
231  Unit& operator= (Unit&& other);
232 
239  virtual void setOversampleLevel( int newOversampleLevel );
240 
244  int getOversampleLevel() const;
245 
246 protected:
247 
254  virtual void process( int bufferSize ) noexcept = 0;
255 
263  virtual void prepareUnit( int expectedBufferSize, double sampleRate ) = 0;
264 
265 
271  virtual void releaseResources( ) override = 0;
272 
280  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
281 
282  //functions to manipulate ins/outs---------------------------------------------
283 
291  const float* processInput( InputNode& node, int& stateNow );
292 
300  float processAndGetSingleValue( InputNode& input, int pos ); //WARNING! pos is not checked
301 
302  //const float* getOutputBuffer( OutputNode& node );
303 
304 
311  float* getOutputBufferToFill( OutputNode& node );
312 
320  void setControlRateOutput( OutputNode& node, float scalarValue );
321 
328  void setOutputToZero( OutputNode& node );
329 
333  void updateOutputNodes();
334 
335  virtual ~Unit() {};
336 
337 
338 
339 private:
340 
341  int oversample;
342 
343 };
344 
345 
346 //-------------------------------------OUTPUT NODE------------------------------------------------
347 
354 class OutputNode : public Preparable {
355  friend class InputNode;
356  friend class Unit;
357  friend class OutputData;
358  friend class PatchNode;
359  friend class Switch;
360  friend class ExternalInput;
361  friend class Processor;
362  friend class UpSampler;
363  friend class DownSampler;
364  friend class Patchable;
365 
366 public:
367 
368  OutputNode( int oversample );
369  OutputNode();
370  OutputNode( const OutputNode& other );
371  OutputNode& operator= ( const OutputNode& other );
372 
373  virtual ~OutputNode();
374 
379  void connect( InputNode& inputNode );
380 
385  void disconnect( InputNode& inputNode );
386 
390  void disconnectAll();
391 
395  virtual float getCRValue() const;
396 
400  const float* getBuffer() const;
401 
405  const int getState() const;
406 
410  const std::vector<InputNode*> & getOutputs() const;
411 
415  bool isConnected();
416 
420  bool notConnected();
421 
425  const int getOversampleLevel() const;
426 
430  OutputNode& setMultiplyOnConnection( float multiplier );
431 
435  static const int getGlobalProcessingTurnId();
440 protected:
441 
442  float* buffer;
443  int state;
444  Unit* parent;
445  int oversampleLevel;
446 
447 private:
448 
449  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
450  void releaseResources() override;
451  virtual void setOversampleLevel( int newOversample );
452 
453  virtual bool exists();
454 
455  void addInputToList( InputNode* node );
456  void removeOutputFromList( InputNode* toRemove );
457 
458  int connections;
459  std::vector<InputNode*> outputs;
460 
461  static std::atomic<int> globalProcessingTurnId;
462 
463  int baseBufferSize;
464 
465  static void setTurn( int newTurn ) ;
466 
467  static void nextTurn();
468 
469 
470  Unit* getParent() const;
471  void setParent( Unit* parent );
472  void updateTurnId();
473 
474  bool multiply;
475  float nextMultiplier;
476 
477  int lastProcessedTurnId;
478 
479 };
480 
481 
482 
483 //-------------------------------------VALUE NODE------------------------------------------------
490 class ValueNode : public OutputNode {
491 
492 public:
493  ValueNode();
494  ValueNode( const ValueNode& other );
495  ValueNode& operator= ( const ValueNode& other );
496  ~ValueNode();
497 
498 
502  float getCRValue() const override;
503 
508  void set( float value );
509 
513  float value();
514 
519  OutputNode& setAndGetNode( float value );
520 
521 private:
522  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
523  void releaseResources() override;
524  void setOversampleLevel( int newOversample ) override;
525  std::atomic<float> nodeValue;
526 
527 };
528 
529 
530 //-------------------------------------INPUT NODE------------------------------------------------
536 class InputNode : public Preparable
537  //public Input
538  {
539  friend class OutputNode;
540  friend class Unit;
541  friend class PatchNode;
542  friend class Switch;
543  friend class UpSampler;
544  friend class DownSampler;
545  friend class Patchable;
546 
547 public:
548  InputNode( int oversample );
549  InputNode();
550  virtual ~InputNode();
551  InputNode( const InputNode& other ); // copy constructor
552  InputNode& operator= ( const InputNode& other ); // copy assignment
553 
554 
559  void connect( OutputNode& outputNode );
560 
561 
562 
567  virtual void disconnect( OutputNode& outputNode );
568 
572  virtual void disconnectAll();
573 
580  virtual void connectFloat( float value );
581 
585  void disconnectFloat();
586 
590  const float* getBuffer() const;
591 
595  const int getState() const;
596 
603  void setDefaultValue( float newDefault );
604 
605 
609  bool isConnected();
610 
616  void enableBoundaries( float lo, float hi );
617 
621  void disableBoundaries();
622 
626  int getRequiredOversampleLevel() const;
627 
628  //void connectExclusive( OutputNode& outputNode );
629  //virtual void connectFloatExclusive( float value );
630 protected:
631 
632  virtual void setRequiredOversampleLevel( int newOversample );
633 
634 private:
635 
636  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
637  void releaseResources() override;
638  void process() noexcept;
639 
640  virtual void removeInputUnilateral( const OutputNode& outputNode );
641 
642  virtual bool exists();
643 
644  float* buffer;
645  float* sumBuffer;
646  int state;
647 
648  bool clampToBoundaries;
649  float lowBoundary;
650  float highBoundary;
651 
652  int connections;
653  std::vector<OutputData> inputs;
654 
655  bool internalScalarIsConnected;
656  ValueNode internalScalar;
657 
658  int baseBufferSize;
659 
660  float lastValue;
661  float defaultValue;
662 
663  int requiredOversampleLevel;
664 };
665 
670 //-----------------------------------NULL OUTPUT----------------------------
671 
672 class NullOutput : public ValueNode {
673 
674 private:
675  virtual bool exists() override {
676  return false;
677  }
678 
679 };
680 
681 
682 //-------------------------------------NULL INPUT---------------------------------------
683 
684 class NullInput : public InputNode {
685 
686 public:
687  NullInput() {}
688 
689 private:
690  bool exists()override {
691  return false;
692  }
693 
694  void prepareToPlay( int expectedBufferSize, double sampleRate ) override {}
695  void releaseResources() override {}
696 
697 };
698 
703 } // namespace pdps END
704 
705 
706 
707 #endif // BASICNODES_H_INCLUDED
A Unit with no dsp inside, it let you switch between different connected inputs and process and pass ...
Definition: Switch.h:20
Abstract class for implementing Units.
Definition: BasicNodes.h:223
Abstract class to implement upsampling resamplers.
Definition: UpSampler.h:18
Abstract class for implementing Units and Modules.
Definition: BasicNodes.h:82
Input of a Unit, process all the connected Outputs and sum them. Has an internal variable state...
Definition: BasicNodes.h:536
Abstract class to implement downsampling resamplers.
Definition: DownSampler.h:16
Thread-safe value control with smoothing.
Definition: SequencerBridge.h:11
A Unit with no dsp inside, it just pass it&#39;s input to the output. Patching float to this Unit is thre...
Definition: PatchNode.h:41
Unit to copy values from an external audio input and convert them to a patchable output.
Definition: ExternalInput.h:21
Output of a Unit, contains a buffer of rendered floats and has a variable state flag.
Definition: BasicNodes.h:354
An OutputNode that is always set at control rate with a set value. Setting or patching float to this ...
Definition: BasicNodes.h:490
The bridge between pdsp and the audio callback.
Definition: Processor.h:21
Definition: Preparable.h:36