diff options
| author | Mick Grierson <mickgrierson@strangebook.lan> | 2011-10-17 02:01:12 +0100 |
|---|---|---|
| committer | Mick Grierson <mickgrierson@strangebook.lan> | 2011-10-17 02:01:12 +0100 |
| commit | 9b352d42f4631a515345ed7c4992a736aab1fea2 (patch) | |
| tree | 557dc6cecc0becda49311cc6b4f139bee1076a1a | |
| parent | 3a90e8344192e60f65cf9a1fe821bb080a323d34 (diff) | |
added maxiEnv - easier to use ADSR and AR envelopes for those who find ramps a bit weird
| -rwxr-xr-x | main.cpp | 30 | ||||
| -rw-r--r-- | maximilian.cpp | 100 | ||||
| -rwxr-xr-x | maximilian.h | 27 |
3 files changed, 141 insertions, 16 deletions
@@ -1,21 +1,35 @@ #include "maximilian.h" -maxiSample beats;//We give our sample a name. It's called beats this time. We could have loads of them, but they have to have different names. -maxiDyn compressor; -double beat; +maxiOsc sound,timer;//We give our sample a name. It's called beats this time. We could have loads of them, but they have to have different names. +maxiEnv AR; +double out; +int trigger, lasttrigger; +int currentCount,lastCount,playHead=0; +int rhythm[8]={1,0,1,0,1,0,1,0}; void setup() {//some inits - beats.load("/Users/mickgrierson/Documents/audio/59210__suonho__abstract_electrofunkbreakbeats_124bpm_mono.wav");//load in your samples. Provide the full path to a wav file. - printf("Summary:\n%s", beats.getSummary());//get info on samples if you like. - } void play(double *output) {//this is where the magic happens. Very slow magic. + currentCount=(int)timer.phasor(16);//this sets up a metronome that ticks every 2 seconds + + if (lastCount!=currentCount) {//if we have a new timer int this sample, play the sound + + trigger=rhythm[playHead]; + playHead++; + if (playHead>7) { + playHead=0; + } + //cout << "tick\n";//the clock ticks + lastCount=0;//set lastCount to 0 + } + + +// beat=beats.play();//just play the file. Looping is default for all play functions. - beat=beats.play();//just play the file. Looping is default for all play functions. - *output=compressor.compress(beat,100,0.01)*10;//OMG an evil compressor + *output=AR.adsr(sound.pulse(440,0.5),1,0.999, 0.125, 0.9999, 1, trigger);//new, simple ADSR. // *output=beats.play(0.69);//play the file with a speed setting. 1. is normal speed. // *output=beats.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing. diff --git a/maximilian.cpp b/maximilian.cpp index 1b93489..f2ef3db 100644 --- a/maximilian.cpp +++ b/maximilian.cpp @@ -842,11 +842,11 @@ double maxiDyn::compress(double input, double ratio, double threshold) { } /*reasonably happy with this. input is the input signal to gate, threshold is the threshold of the gate (default 0.9), - hold is how long you want to hold after the attack finishes (default 1 sample). The envelopes are old school digital. + holdtime is how long you want to hold after the attack finishes (default 1 sample). The envelopes are old school digital. An attack of 1 is instant (default). An attack of 0.0015 is a few hundred ms. Likewise, a release of 0. is instant and a release of 0.9995 (default) is a few hundred ms. When I can be arsed I'll work out what this ends up as in seconds and do the conversion. I may never be arsed. It seems a waste. */ -double maxiDyn::gate(double input, double threshold, long hold, double attack, double release) { +double maxiDyn::gate(double input, double threshold, long holdtime, double attack, double release) { if (fabs(input)>threshold && attackphase!=1){ holdcount=0; @@ -865,12 +865,12 @@ double maxiDyn::gate(double input, double threshold, long hold, double attack, d holdphase=1; } - if (holdcount<hold && holdphase==1) { + if (holdcount<holdtime && holdphase==1) { output=input; holdcount++; } - if (holdcount==hold) { + if (holdcount==holdtime) { holdphase=0; releasephase=1; } @@ -883,3 +883,95 @@ double maxiDyn::gate(double input, double threshold, long hold, double attack, d return output; } +/* Lots of people struggle with the envelope generators so here's a new easy one. It takes mental numbers for attack and release tho */ +double maxiEnv::ar(double input, double attack, double release, long holdtime, int trigger) { + + if (trigger==1 && attackphase!=1 && holdphase!=1){ + holdcount=0; + releasephase=0; + attackphase=1; + } + + if (attackphase==1) { + amplitude+=(1*attack); + output=input*amplitude; + } + + if (amplitude>=1) { + amplitude=1; + attackphase=0; + holdphase=1; + } + + if (holdcount<holdtime && holdphase==1) { + output=input; + holdcount++; + } + + if (holdcount==holdtime && trigger==1) { + output=input; + } + + if (holdcount==holdtime && trigger!=1) { + holdphase=0; + releasephase=1; + } + + if (releasephase==1 && amplitude>0.) { + output=input*(amplitude*=release); + + } + + return output; +} + +double maxiEnv::adsr(double input, double attack, double decay, double sustain, double release, long holdtime, int trigger) { + + if (trigger==1 && attackphase!=1 && holdphase!=1 && decayphase!=1){ + holdcount=0; + decayphase=0; + sustainphase=0; + releasephase=0; + attackphase=1; + } + + if (attackphase==1) { + amplitude+=(1*attack); + output=input*amplitude; + } + + if (amplitude>=1) { + amplitude=1; + attackphase=0; + decayphase=1; + } + + if (decayphase==1) { + output=input*(amplitude*=decay); + if (amplitude<=sustain) { + decayphase=0; + holdphase=1; + } + } + + if (holdcount<holdtime && holdphase==1) { + output=input*amplitude; + holdcount++; + } + + if (holdcount==holdtime && trigger==1) { + output=input*amplitude; + } + + if (holdcount==holdtime && trigger!=1) { + holdphase=0; + releasephase=1; + } + + if (releasephase==1 && amplitude>0.) { + output=input*(amplitude*=release); + + } + + return output; +}
\ No newline at end of file diff --git a/maximilian.h b/maximilian.h index 38ac3dc..b7c4e9b 100755 --- a/maximilian.h +++ b/maximilian.h @@ -318,18 +318,37 @@ class maxiDyn { public: - double gate(double input, double threshold=0.9, long hold=1, double attack=1, double release=0.9995); + double gate(double input, double threshold=0.9, long holdtime=1, double attack=1, double release=0.9995); double compress(double input, double ratio=2.0, double threshold=0.5); double input; double ratio; double threshold; double output; - long attack; - long release; + double attack; + double release; double amplitude; - long hold; + long holdtime; long holdcount; int attackphase,holdphase,releasephase; }; +class maxiEnv { + + +public: + double ar(double input, double attack=1, double release=0.9, long holdtime=1, int trigger=0); + double adsr(double input, double attack=1, double decay=0.99, double sustain=0.125, double release=0.9, long holdtime=1, int trigger=0); + double input; + double output; + double attack; + double decay; + double sustain; + double release; + double amplitude; + int trigger; + long holdtime; + long holdcount; + int attackphase,decayphase,sustainphase,holdphase,releasephase; +}; + #endif |
