CSL  6.0
SndFileInstrument.cpp
Go to the documentation of this file.
1 //
2 // SndFileInstrument.cpp -- Sound file player instrument class
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "SndFileInstrument.h"
7 
8 using namespace csl;
9 using namespace std;
10 
11 // The constructor initializes the DSP graph's UGens
12 
13 SndFileInstrument::SndFileInstrument(string path, int start, int stop) : // initializers
14  Instrument(),
15  mPlayer(path),
16  mEnvelope((mPlayer.stopFrame() - mPlayer.startFrame()) / mPlayer.frameRate(), 0.01, 0.01),
17  mPanner(mEnvelope, 0.0, 1.0), // stereo panner
18  mRate(1.0) { // set the player's playback rate
19  this->initialize(path);
20 }
21 
22 SndFileInstrument::SndFileInstrument(string folder, string path, int start, int stop) : // initializers
23  Instrument(),
24  mPlayer(folder + path /*, start, stop */),
25  mEnvelope((mPlayer.stopFrame() - mPlayer.startFrame()) / mPlayer.frameRate(), 0.01, 0.01),
26  mPanner(mEnvelope, 0.0, 1.0), // stereo panner
27  mRate(1.0) { // set the player's playback rate
28  this->initialize(folder + path);
29 }
30 
31 void SndFileInstrument::initialize(string path) {
32  mName = path;
33  mNumChannels = 2; // I'm stereo.
35  mGraph = & mPanner; // Store the root of the graph as the inst var _graph
36  if (path.size() > 0) {
37  try {
38  mPlayer.openForRead();
39  } catch (CException & e) {
40  logMsg(kLogError, "File open exception caught: %s", e.mMessage.c_str());
41  return;
42  }
43  }
44  mUGens["Rate"] = & mRate; // add ugens that can be monitored to the map
45  mUGens["Panner"] = & mPanner;
46  mUGens["Player"] = & mPlayer;
47 
48 // mEnvelopes.push_back(& mPlayer); // list envelopes for retrigger
49  // set up accessor vector
50  mAccessors.push_back(new Accessor("am", set_amplitude_f, CSL_FLOAT_TYPE));
51  mAccessors.push_back(new Accessor("ra", set_rate_f, CSL_FLOAT_TYPE));
52  mAccessors.push_back(new Accessor("po", set_position_f, CSL_FLOAT_TYPE));
53  mAccessors.push_back(new Accessor("fi", set_file_f, CSL_STRING_TYPE));
54  mAccessors.push_back(new Accessor("st", set_start_f, CSL_INT_TYPE));
55  mAccessors.push_back(new Accessor("en", set_stop_f, CSL_INT_TYPE));
56  mAccessors.push_back(new Accessor("at", set_attack_f, CSL_FLOAT_TYPE));
57  mAccessors.push_back(new Accessor("de", set_decay_f, CSL_FLOAT_TYPE));
58 }
59 
60 // The destructor frees all the UGen pointers
61 
63 
64 // Plug function for use by OSC setter methods
65 
66 void SndFileInstrument::setParameter(unsigned selector, int argc, void **argv, const char *types) {
67  float d = * (float *) argv[0];
68  if (types[0] == 'i')
69  d = (float) (* (int *) argv[0]);
70  switch (selector) {
71  case set_amplitude_f:
72  mPanner.setScale(d); break;
73  case set_rate_f:
74  mRate.setValue(d); break;
75  case set_position_f:
76  mPanner.setPosition(d); break;
77  case set_file_f:
78  mPlayer.setPath((char *) argv[0]);
79  mPlayer.openForRead();
80  mPlayer.setToEnd();
81  logMsg("Loaded sound file %s", mPlayer.path().c_str());
82  break;
83  case set_start_f:
84  mPlayer.setStart((int ) d);
85  mEnvelope.setDuration(mPlayer.durationInSecs());
86  break;
87  case set_stop_f:
88  mPlayer.setStop((int) d);
89  mEnvelope.setDuration(mPlayer.durationInSecs());
90  break;
91  case set_attack_f:
92  mEnvelope.setAttack(d); break;
93  case set_decay_f:
94  mEnvelope.setRelease(d); break;
95  default:
96  logMsg(kLogError, "Unknown selector in SndFileInstrument set_parameter selector: %d", selector);
97  }
98 }
99 
100 // Play a note with a given arg list
101 // Formats:
102 // ampl, pos
103 // ampl, pos, rate
104 // ampl, pos, start, stop
105 // ampl, pos, rate, start, stop
106 // ampl, pos, start, stop, attack, decay
107 // ampl, pos, rate, start, stop, attack, decay
108 
109 void SndFileInstrument::playOSC(int argc, void **argv, const char *types) {
110  float ** fargs = (float **) argv;
111  int ** iargs = (int **) argv;
112  switch(argc) {
113  case 2: // ampl, pos
114  if (strcmp(types, "ff") != 0) {
115  logMsg(kLogError, "Invalid type string in OSC message, expected \"ff\" got \"%s\"", types);
116  return;
117  }
118  mPanner.setScale(*fargs[0]);
119  mPanner.setPosition(*fargs[1]);
120 // printf("\tSndFileInstr: PN: %g %g\n", fargs[0], fargs[1]);
121  break;
122  case 3: // ampl, pos, rate
123  if (strcmp(types, "fff") != 0) {
124  logMsg(kLogError, "Invalid type string in OSC message, expected \"fff\" got \"%s\"", types);
125  return;
126  }
127  mPanner.setScale(*fargs[0]);
128  mPanner.setPosition(*fargs[1]);
129  mRate.setValue(*fargs[2]);
130 // printf("\tSndFileInstr: PN: %g %g %g\n", fargs[0], fargs[1], fargs[2]);
131  break;
132  case 4: // ampl, pos, start, stop
133  if (strcmp(types, "ffii") == 0) { // start/stop at ints = sample index
134  mPanner.setScale(*fargs[0]);
135  mPanner.setPosition(*fargs[1]);
136  mPlayer.setStart(*iargs[2]);
137  mPlayer.setStop(*iargs[3]);
138 // printf("\tSndFileInstr: PN: %g %g %d - %d\n", fargs[0], fargs[1], iargs[2], iargs[3]);
139  break;
140  }
141  if (strcmp(types, "ffff") == 0) { // start/stop at floats = relative
142  mPanner.setScale(*fargs[0]);
143  mPanner.setPosition(*fargs[1]);
144  mPlayer.setStartRatio(*fargs[2]);
145  mPlayer.setStopRatio(*fargs[3]);
146 // printf("\tSndFileInstr: PN4: %g %g\t\t%g - %g\n", fargs[0], fargs[1], fargs[2], fargs[3]);
147  break;
148  }
149  logMsg(kLogError, "Invalid type string in OSC message, expected \"ffii\" got \"%s\"", types);
150  return;
151  case 5: // ampl, pos, rate, start, stop
152  if (strcmp(types, "fffii") != 0) {
153  logMsg(kLogError, "Invalid type string in OSC message, expected \"fffii\" got \"%s\"", types);
154  return;
155  }
156  mPanner.setScale(*fargs[0]);
157  mPanner.setPosition(*fargs[1]);
158  mRate.setValue(*fargs[2]);
159  mPlayer.setStartRatio(*iargs[3]);
160  mPlayer.setStopRatio(*iargs[4]);
161 // printf("\tSndFileInstr: PN: %g %g %g %g-%g\n", fargs[0], fargs[1], fargs[2], iargs[3], iargs[4]);
162  break;
163  case 6: // ampl, pos, start, stop, attack, decay
164  if (strcmp(types, "ffiiff") != 0) {
165  logMsg(kLogError, "Invalid type string in OSC message, expected \"ffiiff\" got \"%s\"", types);
166  return;
167  }
168  mPanner.setScale(*fargs[0]);
169  mPanner.setPosition(*fargs[1]);
170  mPlayer.setStart(*iargs[2]);
171  mPlayer.setStop(*iargs[3]);
172  mEnvelope.setAttack(*fargs[4]);
173  mEnvelope.setRelease(*fargs[5]);
174 // printf("\tSndFileInstr: PN: %g %g %g - %g\n", fargs[0], fargs[1], iargs[3], iargs[4]);
175  break;
176  case 7: // ampl, pos, rate, start, stop, attack, decay
177  if (strcmp(types, "fffiiff") != 0) {
178  logMsg(kLogError, "Invalid type string in OSC message, expected \"fffiiff\" got \"%s\"", types);
179  return;
180  }
181  mPanner.setScale(*fargs[0]);
182  mPanner.setPosition(*fargs[1]);
183  mRate.setValue(*fargs[2]);
184  mPlayer.setStart(*iargs[3]);
185  mPlayer.setStop(*iargs[4]);
186  mEnvelope.setAttack(*fargs[5]);
187  mEnvelope.setRelease(*fargs[6]);
188 // printf("\tSndFileInstr: PN: %g %g %d-%d\n", fargs[0], fargs[1], iargs[3], iargs[4]);
189  break;
190  default:
191  logMsg(kLogError, "Invalid type string in OSC message: \"%s\"", types);
192  return;
193  }
194  mEnvelope.setDuration(((float) (mPlayer.stopFrame() - mPlayer.startFrame())) / (float) mPlayer.frameRate());
195  mEnvelope.trigger();
196  mPlayer.trigger();
197 }
198 
200 // mEnvelope.setDuration((mPlayer.stopFrame() - mPlayer.startFrame()) / mPlayer.frameRate());
201  mEnvelope.trigger();
202  mPlayer.trigger();
203 };
204 
205 // Play a note via a direct fcn call
206 
207 void SndFileInstrument::playNote(float ampl, float rate, float pos, int start, int stop, float attack, float decay) {
208  mPanner.setScale(ampl);
209  mPanner.setPosition(pos);
210  mRate.setValue(rate);
211  mPlayer.setStart(start);
212  mPlayer.setStop(stop);
213  mEnvelope.setDuration((stop - start) / mPlayer.frameRate());
214  mEnvelope.setAttack(attack);
215  mEnvelope.setRelease(decay);
216  mEnvelope.trigger();
217  mPlayer.trigger();
218 }
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
StaticVariable mRate
plugs playback rate (ignored for now)
void playNote(float ampl=1, float rate=1, float pos=0, int start=-1, int stop=-1, float attack=0.0, float decay=0.0)
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
#define set_amplitude_f
Definition: Instrument.h:20
virtual void trigger()
reset internal time to restart envelope
Definition: Envelope.cpp:284
#define set_decay_f
Definition: Instrument.h:26
#define set_attack_f
Definition: Instrument.h:25
#define CSL_STRING_TYPE
Definition: Accessor.h:15
#define set_stop_f
Definition: Instrument.h:46
UnitGenerator * mGraph
Caches.
Definition: Instrument.h:87
AccessorVector mAccessors
the accessor vector
Definition: Instrument.h:91
#define CSL_INT_TYPE
Accessor.h – "Reflective" parameter name/setter/type acessor object. See the copyright notice and ac...
Definition: Accessor.h:13
string mName
my name
Definition: Instrument.h:88
void setValue(float x)
set/get the value (not allowed in the abstract, useful for static values)
Definition: Variable.h:63
SndFileInstrument(string path, int start=-1, int stop=-1)
Panner mPanner
stereo panner
#define set_start_f
Definition: Instrument.h:45
unsigned mNumChannels
my "expected" number of output channels
Definition: CSL_Core.h:292
void setPosition(UnitGenerator &pan)
Operations.
Definition: Mixer.cpp:279
void setAttack(float attack)
Definition: Envelope.cpp:488
void setParameter(unsigned selector, int argc, void **argv, const char *types)
Plug functions.
SoundFile mPlayer
sample player
Instrument class (abstract)
Definition: Instrument.h:56
void initialize(string path)
#define set_file_f
Definition: Instrument.h:44
#define CSL_FLOAT_TYPE
Definition: Accessor.h:14
void playOSC(int argc, void **argv, const char *types)
The Accessor class has public data members.
Definition: Accessor.h:22
#define set_rate_f
Definition: Instrument.h:43
#define set_position_f
Definition: Instrument.h:22
void setScale(UnitGenerator &scale)
set the receiver's scale member to a UGen or a float
Definition: CSL_Core.cpp:1039
void setDuration(float d)
Special accessors.
Definition: Envelope.cpp:469
void setRelease(float release)
Definition: Envelope.cpp:495
AR mEnvelope
AR envelope.
Base class of CSL exceptions (written upper-case). Has a string message.
UGenMap mUGens
the map of ugens in the graph by name
Definition: Instrument.h:89