CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Instrument.cpp
Go to the documentation of this file.
1 //
2 // Instrument.cpp -- The CSL pluggable instrument class.
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5
6 #include "Instrument.h"
7
8 using namespace csl;
9
10 // Constructors
11
12 Instrument::Instrument() :
13 UnitGenerator(),
14 mGraph(0),
15 mName("New"),
16 mUGens(),
17 mEnvelopes(),
18 mAccessors() { }
19
20 // copy constructor
21
22 Instrument::Instrument(Instrument& in) :
23 UnitGenerator(in.frameRate(), in.numChannels()),
24 mGraph(in.graph()),
25 mName(in.name()) {
26 UGenVector * en = in.envelopes();
27 for (unsigned i = 0; i < en->size(); i++)
28 mEnvelopes.push_back((*en)[i]);
29 AccessorVector av = in.getAccessors();
30 for (unsigned i = 0; i < av.size(); i++)
31 mAccessors.push_back(av[i]);
32 UGenMap * um = in.genMap();
33 for (UGenMap::iterator it = um->begin(); it != um->end(); it++)
34 mUGens[it->first] = it->second;
35 }
36
37 Instrument::~Instrument() { }
38
39 // Next_buffer is simply delegated to the receiver's graph
40
41 void Instrument::nextBuffer(Buffer & outputBuffer) throw (CException) {
42 return (mGraph->nextBuffer(outputBuffer));
43 }
44
45 ///< get a UGen from the graph
46
47 UnitGenerator * Instrument:: genNamed(string nam) {
48 UGenMap::iterator pos = mUGens.find(nam);
49 if (pos == mUGens.end())
50 return 0;
51 return (pos->second);
52 }
53
54 // Answer whether any of the envelopes are active
55 // NB: This is a rare use of std::vector iteration (instead of an Accessor* for () loop) at call-back time.
56
57 bool Instrument::isActive() {
58 if (mEnvelopes.empty()) {
59 // printf(" -- No envs -- "); fflush(stdout);
60 return (mGraph->isActive());
61 }
62 for (UGenVector::iterator pos = mEnvelopes.begin(); pos != mEnvelopes.end(); ++pos)
63 if (((Envelope *) *pos)->isActive())
64 return TRUE;
65 return FALSE;
66 }
67
68 // Playing a note just means triggering all the instrument's envelopes
69
70 void Instrument::play() {
71 // printf("\tInstr play\n");
72 for (UGenVector::iterator pos = mEnvelopes.begin(); pos != mEnvelopes.end(); ++pos)
73 ((Envelope *) *pos)->trigger();
74 }
75
76 void Instrument::release() {
77 for (UGenVector::iterator pos = mEnvelopes.begin(); pos != mEnvelopes.end(); ++pos)
78 ((Envelope *) *pos)->trigger();
79 }