From 73184e1dc3e2378a99f7cbbb3ee3e25ff0356016 Mon Sep 17 00:00:00 2001 From: Chris Kiefer Date: Thu, 15 Mar 2012 15:16:43 +0000 Subject: Loop recording, and maxiGrain looping bug fixed --- ofxMaxim/ofxMaxim/libs/maxiGrains.h | 2 ++ ofxMaxim/ofxMaxim/libs/maximilian.cpp | 20 +++++++++++++ ofxMaxim/ofxMaxim/libs/maximilian.h | 54 +++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 2 deletions(-) (limited to 'ofxMaxim') diff --git a/ofxMaxim/ofxMaxim/libs/maxiGrains.h b/ofxMaxim/ofxMaxim/libs/maxiGrains.h index f2f63b9..d756110 100644 --- a/ofxMaxim/ofxMaxim/libs/maxiGrains.h +++ b/ofxMaxim/ofxMaxim/libs/maxiGrains.h @@ -219,6 +219,7 @@ public: short* buffer = (short *)sample->myData; if (frequency >0.) { pos += inc; + if (pos >= sample->length) pos -= sample->length; long posl = static_cast(pos); remainder = pos - posl; a=posl++; @@ -234,6 +235,7 @@ public: } else { frequency=frequency-(frequency+frequency); pos += inc; + if (pos < 0) pos += sample->length; long posl = static_cast(pos); remainder = pos - posl; if (posl-1>=0) { diff --git a/ofxMaxim/ofxMaxim/libs/maximilian.cpp b/ofxMaxim/ofxMaxim/libs/maximilian.cpp index c0e08f1..63c86cc 100644 --- a/ofxMaxim/ofxMaxim/libs/maximilian.cpp +++ b/ofxMaxim/ofxMaxim/libs/maximilian.cpp @@ -845,6 +845,26 @@ void maxiSample::getLength() { length=myDataSize*0.5; } +void maxiSample::setLength(unsigned long numSamples) { + char *newData = (char*) malloc(sizeof(short) * numSamples); + if (NULL!=myData) { + unsigned long copyLength = min((unsigned long)length, numSamples); + memcpy(newData, myData, sizeof(short) * copyLength); + } + myData = newData; + myDataSize = numSamples * 2; + length=numSamples; + position=0; + recordPosition=0; + +} + +void maxiSample::clear() { + memset(myData, 0, myDataSize); +} + + + /* OK this compressor and gate are now ready to use. The envelopes, like all the envelopes in this recent update, use stupid algorithms for incrementing - consequently a long attack is something like 0.0001 and a long release is like 0.9999. diff --git a/ofxMaxim/ofxMaxim/libs/maximilian.h b/ofxMaxim/ofxMaxim/libs/maximilian.h index 6f4d384..54e9ed6 100755 --- a/ofxMaxim/ofxMaxim/libs/maximilian.h +++ b/ofxMaxim/ofxMaxim/libs/maximilian.h @@ -167,6 +167,38 @@ public: }; +//lagging with an exponential moving average +//a lower alpha value gives a slower lag +template +class maxiLagExp { +public: + T alpha, alphaReciprocal; + T val; + + maxiLagExp() { + init(0.5, 0.0); + }; + + maxiLagExp(T initAlpha, T initVal) { + init(initAlpha, initVal); + } + + void init(T initAlpha, T initVal) { + alpha = initAlpha; + alphaReciprocal = 1.0 - alpha; + val = initVal; + } + + inline void addSample(T newVal) { + val = (alpha * newVal) + (alphaReciprocal * val); + } + + inline T value() { + return val; + } +}; + + class maxiSample { private: @@ -178,9 +210,10 @@ private: int myByteRate; short myBlockAlign; short myBitsPerSample; - double position; + double position, recordPosition; double speed; double output; + maxiLagExp loopRecordLag; public: int myDataSize; @@ -188,6 +221,7 @@ public: int mySampleRate; long length; void getLength(); + void setLength(unsigned long numSamples); char* myData; @@ -199,7 +233,7 @@ public: if (myData) delete[] myData; } - maxiSample():myData(NULL),position(0){}; + maxiSample():myData(NULL),position(0), recordPosition(0), myChannels(1), mySampleRate(maxiSettings::sampleRate) {}; bool load(string fileName, int channel=0); @@ -207,6 +241,21 @@ public: // read a wav file into this class bool read(); + + void loopRecord(double newSample, const bool recordEnabled, const double recordMix) { + loopRecordLag.addSample(recordEnabled); + if(recordEnabled) { + double currentSample = ((short*)myData)[(unsigned long)recordPosition] / 32767.0; + newSample = (recordMix * currentSample) + ((1.0 - recordMix) * newSample); + newSample *= loopRecordLag.value(); + ((short*)myData)[(unsigned long)recordPosition] = newSample * 32767; + } + ++recordPosition; + if (recordPosition == length) + recordPosition=0; + } + + void clear(); double play(); @@ -463,4 +512,5 @@ private: double attack, release, env; }; + #endif -- cgit v1.3