CSL 5.2

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

- File List
- File Members

WhiteNoiseInstrument.cpp
Go to the documentation of this file.
1 //
2 // WhiteNoiseInstrument.cpp -- Simple WhiteNoise example instrument class.
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 // Implementation of the filtered WhiteNoise example as an instrument class.
6 // This example shows how to create an instrument class from a DSP graph and set up its accessors for use with OSC.
7
8 #include "WhiteNoiseInstrument.h"
9
10 using namespace csl;
11
12 // The constructor initializes the DSP graph's UGens
13
14 WhiteNoiseInstrument::WhiteNoiseInstrument() : // initializers for the UGens
15 _a_env(3, 0.1, 0.1, 0.5, 1), // set up a standard ADSR (3-sec duration)
16 _sig(), // init the white noise generator
17 _lpfilter(_sig, _sig.sample_rate(),Butter::BW_LOW_PASS, 1000), // and filter the output
18
19 _a_mul(_lpfilter, _a_env) // scale the filter's output by the amplitude envelope
20 {
21 _name = (char *) malloc(CSL_NAME_LEN);
22 strcpy(_name, "Basic_WhiteNoise");
23 _graph = & _a_mul; // store the root of the graph as the inst var _graph
24 // now set up the null-terminated list of envelopes
25 _envelopes = (Envelope **) malloc (2 * sizeof(void *));
26 _envelopes[0] = & _a_env;
27 _envelopes[1] = 0;
28 }
29
30 // The destructor frees the stuff we allocated
31
32 WhiteNoiseInstrument::~BasicWhiteNoiseInstrument() {
33 free(_name); free(_envelopes);
34 }
35
36 // Plug function for use by OSC setter methods
37
38 void WhiteNoiseInstrument::set_parameter(unsigned selector, void * d) {
39 switch (selector) { // switch on which parameter is being set
40 case set_duration_f:
41 _a_env.set_duration(* (float *)d); break;
42 case set_amplitude_f:
43 _a_env.scale_values(* (float *)d); break;
44 case set_c_freq_f:
45 _lpfilter.set_cutoff_frequency(* (float *)d); break;
46 case set_attack_f:
47 _a_env.set_attack(* (float *)d); break;
48 case set_decay_f:
49 _a_env.set_decay(* (float *)d); break;
50 case set_sustain_f:
51 _a_env.set_sustain(* (float *)d); break;
52 case set_release_f:
53 _a_env.set_release(* (float *)d); break;
54 default:
55 logMsg(kLogError, "Unknown selector in BasicWhiteNoiseInstrument set_parameter selector: %d\n", selector);
56 }
57 }
58
59 // Answer the number of and the list of accessors
60
61 unsigned BasicWhiteNoiseInstrument::num_accessors() { return 13; };
62
63 // Fill in the list of accessor objects
64
65 void WhiteNoiseInstrument::get_accessors(Accessor ** list) {
66 unsigned int n = 0;
67 list[n++] = new Accessor("du", set_duration_f, CSL_FLOAT_TYPE);
68 list[n++] = new Accessor("am", set_amplitude_f, CSL_FLOAT_TYPE);
69 list[n++] = new Accessor("cf", set_c_freq_f, CSL_FLOAT_TYPE);
70 list[n++] = new Accessor("aa", set_attack_f, CSL_FLOAT_TYPE);
71 list[n++] = new Accessor("ad", set_decay_f, CSL_FLOAT_TYPE);
72 list[n++] = new Accessor("as", set_sustain_f, CSL_FLOAT_TYPE);
73 list[n++] = new Accessor("ar", set_release_f, CSL_FLOAT_TYPE);
74 }
75
76 // Play a note with a given arg list
77 // Formats (5 or 13 arguments):
78 // dur, ampl, c_fr, m_fr, ind
79 // dur, ampl, c_fr, m_fr, ind, att, dec, sus, rel, i_att, i_dec, i_sus, i_rel
80
81 void WhiteNoiseInstrument::play_osc_note(char * types, void * args, char * endOfArgs) {
82 float ** fargs = (float **) args;
83 unsigned nargs;
84 if (strcmp(types, ",fff") == 0)
85 nargs = 3;
86 else if (strcmp(types, ",fffffff") == 0)
87 nargs = 7;
88 else {
89 logMsg(kLogError, "Invalid type string in OSC message, expected \",i\" got \"%s\"\n", types);
90 return;
91 }
92 printf("\tWhiteNoiseInstr: PN: %g %g %g %g %g\n", *fargs[0], *fargs[1], *fargs[2]);
93 _a_env.set_duration(*fargs[0]);
94 _a_env.scale_values(*fargs[1]);
95 _lpfilter.set_cutoff_frequency(*fargs[2]);
96 if (nargs == 13) {
97 _a_env.set_attack(*fargs[3]);
98 _a_env.set_decay(*fargs[4]);
99 _a_env.set_sustain(*fargs[5]);
100 _a_env.set_release(*fargs[6]);
101 }
102 _a_env.reset(); // now trigger the envelopes!
103 }