-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_drum.cpp
389 lines (324 loc) · 9.83 KB
/
single_drum.cpp
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
#include<math.h>
#include "single_drum.h"
#include "mono_sample.h"
#include <stdio.h>
#include <Delay.h>
#include <stdlib.h>
#include <sstream>
#include "common.h"
#include <FL/Fl.H>
#include <FL/Fl_Pack.H>
#include <FL/Fl_Box.H>
#define SD_INITIAL_VOLUME 0
#define SD_INITIAL_PITCH 1
#define SD_VOLUME_ENVELOPE 2
#define SD_PITCH_ENVELOPE 3
#define SD_LP_FILTER 4
#define SD_BOOST 5
#define SD_MIDI_NOTE 6
#define SD_MIDI_VOL 7
#define SD_MIDI_ENV 8
single_drum::~single_drum()
{
cout << "destructor called on single_drum" << endl;
delete _msample;
}
void single_drum::write_config(Setting &pads)
{
Setting &pad = pads.add(Setting::TypeGroup);
pad.add("name", Setting::TypeString) = _name;
pad.add("sample_filename",Setting::TypeString) = _sample_filename;
pad.add("interpolation_type",Setting::TypeInt) = _interpolation_type;
Setting ¶meters = pad.add("parameters",Setting::TypeList);
for(int i=0;i<_pv.size();i++)
{
cout << "in " << _name << " writing: " << i << endl;
_pv[i]->write_config(parameters);
}
}
float calc_hz_from_midinote(int midi_note)
{
return 440.0*pow(2.0,(midi_note-69.0)/12.0);
}
void single_drum::set_gate(int val)
{
if(val==127)
gate_value=-1;
else
{
gate_value=100*val;
printf("setting gate time to: %d samples\n",gate_value);
}
}
single_drum::single_drum(Setting &pad, int sample_rate)
{
string sval;
int ival;
if(pad.lookupValue("name", sval))
{
_name=sval;
cout << "found pad name: " << _name << endl;
}
if(pad.lookupValue("sample_filename", sval))
{
_sample_filename=sval;
cout << "found sample file name: " << _sample_filename << endl;
}
if(pad.lookupValue("interpolation_type", ival))
{
_interpolation_type=ival;
cout << "found interpolation_type: " << _interpolation_type << endl;
}
init(_name,_sample_filename,sample_rate, _interpolation_type);
const Setting ¶meters = pad["parameters"];
int count = parameters.getLength();
for(int i=0;i<count;i++)
{
single_parameter *sp=new single_parameter(parameters[i]);
_pv.push_back(sp);
}
gate_value=-1;
boost_offset=0.0f;
bonus_vol=1.0f;
}
single_drum::single_drum(string name, string filename, int sample_rate)
{
init(name,filename,sample_rate,4); //4 is best quality
init_parameters();
}
void single_drum::init_gui()
{
Fl_Pack* p = new Fl_Pack(0, 0, 300, 300);
p->spacing(20);
p->type(uchar(Fl_Pack::VERTICAL));
char *n=new char[100];
sprintf(n,"%s",_name.c_str());
Fl_Box *box = new Fl_Box(0,0,50,10,n);
for(int i=0;i<_pv.size();i++)
_pv[i]->init_gui();
p->end();
}
void single_drum::init(string name, string filename, int sample_rate, int interp_type)
{
printf("initing single drum: %s\n",name.c_str());
_name=name;
_sample_filename=filename;
_sample_rate=sample_rate;
_msample=new mono_sample(filename.c_str(),_sample_rate);
_filter=new stk::OnePole(0.0);
_is_playing=false;
_interpolation_type=interp_type;
_wg=new wave_generator(sample_rate);
//_wg_active=false;
_wg_env=new simple_envelope();
_wg_vol=1.0;
_p_env=new simple_envelope();
_v_env=new simple_envelope();
_is_in_active_queue=false;
retrig_count=0;
_user_pitch=1.0f;
}
void single_drum::init_parameters()
{
_pv.push_back(new single_parameter("initial_volume",0,1,0,2));
_pv.push_back(new single_parameter("initial_pitch",0,0,0,2));
_pv.push_back(new single_parameter("volume_envelope",0,1,0,2));
_pv.push_back(new single_parameter("pitch_envelope",0,0,0,2));
_pv.push_back(new single_parameter("lp_filter",0.5,1,0,1));
_pv.push_back(new single_parameter("boost",0,0,0,1000));
_pv.push_back(new single_parameter("midi_note",0,0,0,100));
_pv.push_back(new single_parameter("midi_vol",0,0,0,1));
_pv.push_back(new single_parameter("midi_env",0,1,0,2));
gate_value=-1;
}
void single_drum::setup_wg(int midi_note, float wg_env_time)
{
_wg->set_speed(calc_hz_from_midinote(midi_note));
_wg_active=true;
//_wg_env_time=wg_env_time;
}
void single_drum::send_all_cc(void *midi_port_buf)
{
//printf("_ev size: %d\n",_ev.size());
//for(int i=0;i<_ev.size();i++)
// _ev[i]->send_all_cc(midi_port_buf,i);
}
void single_drum::receive_cc_message(int cc, float val)
{
//printf(" in single drum, received cc message\n");
/* int effect=0;
int corrected_cc=0;
if(cc>0 && cc<9)
{
effect=ceil((float)cc/2.0)-1;
corrected_cc=cc-effect*2;
}
else if(cc>80 && cc<89)
{
effect=ceil((float)(cc-80)/2.0)-1;
corrected_cc=cc-effect*2;
}
else if(cc>88 && cc<97)
{
effect=ceil((float)(cc-88)/2.0)-1;
corrected_cc=cc-effect*2;
}
else if(cc>96 && cc<105)
{
effect=ceil((float)(cc-96)/2.0)-1;
corrected_cc=cc-effect*2;
}
//printf("effect: %d cc: %d\n",effect,corrected_cc);
if(effect>=3) return;
_ev[effect]->receive_cc(corrected_cc,val);
*/
}
void single_drum::set_boost(int val)
{
_pv[SD_BOOST]->set_max((float)val/127.0f*_pv[SD_BOOST]->get_gmax());
}
void single_drum::set_boost_offset(int val)
{
boost_offset=(float)val/127.0f;
}
void single_drum::note_on()
{
note_on(_initial_velocity); //just use most recent velocity
}
void single_drum::note_on(float velocity)
{
_position.reset();
_is_playing=true;
_initial_velocity=velocity;
_initial_volume=1.0;
_initial_pitch=1.0;
_initial_filter=0.0;
_initial_boost=1.0;
_initial_delay_send=0.0;
_initial_rev_send=0.0;
current_sample=0;
if(_pv[SD_INITIAL_VOLUME]->is_active())
_initial_volume=_pv[SD_INITIAL_VOLUME]->calc_value(_initial_velocity);
if(_pv[SD_INITIAL_PITCH]->is_active())
_initial_pitch=_pv[SD_INITIAL_PITCH]->calc_value(_initial_velocity);
else
_initial_pitch=_user_pitch;
if(_pv[SD_INITIAL_PITCH]->is_active())
_initial_filter=_pv[SD_LP_FILTER]->calc_value(_initial_velocity);
if(_pv[SD_BOOST]->is_active())
_initial_boost=_pv[SD_BOOST]->calc_value(_initial_velocity);
//if(_pv[SD_DELAY_SEND]->is_active())
// _initial_delay_send=_pv[SD_DELAY_SEND]->calc_value(_initial_velocity);
//if(_pv[SD_REV_SEND]->is_active())
// _initial_rev_send=_pv[SD_REV_SEND]->calc_value(_initial_velocity);
if(_pv[SD_LP_FILTER]->is_active())
{
_initial_filter=1.0f-_pv[SD_LP_FILTER]->calc_value(_initial_velocity);
_filter->setPole(_initial_filter);
}
if(_pv[SD_VOLUME_ENVELOPE]->is_active())
{
_v_env->reset();
_v_env->set_envelope(_pv[SD_VOLUME_ENVELOPE]->calc_value(_initial_velocity)*_sample_rate);
}
if(_pv[SD_PITCH_ENVELOPE]->is_active())
{
_p_env->set_envelope(_pv[SD_PITCH_ENVELOPE]->calc_value(_initial_velocity)*_sample_rate);
_p_env->reset();
}
if(_pv[SD_MIDI_NOTE]->is_active())
{
_wg->set_speed(calc_hz_from_midinote(_pv[SD_MIDI_NOTE]->calc_value(_initial_velocity)));
}
if(_pv[SD_MIDI_ENV]->is_active())
{
_wg_env->set_envelope(_pv[SD_MIDI_ENV]->calc_value(_initial_velocity)*_sample_rate);
_wg_env->reset();
_wg->reset();
//printf("wg env time: %f\n",_wg_env->get_decay_time());
}
if(_pv[SD_MIDI_VOL]->is_active())
{
_wg_vol=_pv[SD_MIDI_VOL]->calc_value(_initial_velocity);
//printf("midi vol: %f\n",_wg_vol);
}
/*printf("note on with velocity: %f\n",_initial_velocity);
printf(" volume: %f\n",_initial_volume);
if(_pv[SD_INITIAL_PITCH]->is_active())
printf(" pitch: %f\n",_initial_pitch);
if(_pv[SD_LP_FILTER]->is_active())
printf(" filter: %f\n",_initial_filter);
//printf(" boost: %f\n",_initial_boost);
printf(" delay send: %f\n",_initial_delay_send);
if(_pv[SD_VOLUME_ENVELOPE]->is_active())
printf(" volume envelope active\n");
if(_pv[SD_PITCH_ENVELOPE]->is_active())
printf(" pitch envelope active\n");
*/
//printf(" rev send: %f\n",_initial_rev_send);
fflush(stdout);
}
void single_drum::note_off()
{
_is_playing=false;
//queue_up_single_cc(73+_index,0);
}
float single_drum::tick(float gspeed)
{
if(_is_playing==false) return 0.0;
/*if(retrig_count>0 && current_sample>3000)
{
if(retrig_count>5) //number of -retrigs
{
retrig_count=0;
_is_playing=false; //hmmm, do we want tail?
return 0.0;
}
else
{
retrig_count++;
note_on();
//_initial_pitch=_initial_pitch-(retrig_count-1.0f)*0.2f;
// if(_initial_pitch<0.0) _initial_pitch=0.0;
}
}*/
if(gate_value!=-1 && current_sample>gate_value) { printf("activatating gate\n"); _is_playing=false; return 0.0;}
float sample=_msample->get_sample(_position,_interpolation_type);
if(_pv[SD_BOOST]->is_active())
sample=atan(sample*_initial_boost+boost_offset);
float volume=_initial_volume;
if(_pv[SD_VOLUME_ENVELOPE]->is_active())
volume=volume*_v_env->tick();
// if(volume<0.01)
// printf("less than 0\n");
sample=sample*volume; //*bonus_vol;
//if(_pv[SD_LP_FILTER]->is_active())
// sample=_filter->tick(sample);
float speed=_initial_pitch;
if(_pv[SD_PITCH_ENVELOPE]->is_active())
speed=speed*_p_env->tick();
_position.increment(speed*gspeed);
if(_msample->is_past_end(_position) || volume<0.01) // || _position.whole_part>2000)
{
if(_pv[SD_MIDI_NOTE]->is_active())
{
if(_wg_env->get_state()==1)
_is_playing=false;
}
else
{
_is_playing=false;
// printf("past end position. stopping\n");
}
}
if(_pv[SD_MIDI_NOTE]->is_active())
{
float wg_e=_wg_env->tick();
sample+=(_wg->tick()*2.0-1.0)*_wg_vol*wg_e;
}
//printf("generated sample: %.02f\n",sample); fflush(stdout);
//rev_send+=sample*_initial_rev_send;
//delay_send+=sample*_initial_delay_send;
current_sample++;
return sample;
}