History

add autosampler object; default mempool declaration fix

Matthew Wang committed 6 years ago

Commit 00d43ea1 parent 4c483c68 files · +200 −20Browse files

Unified · Split

8 files
  1. LEAF/Inc/leaf-mempool.h +2 −0
  2. LEAF/Inc/leaf-sampling.h +35 −0
  3. LEAF/Src/leaf-delay.c +2 −1
  4. LEAF/Src/leaf-mempool.c +14 −10
  5. LEAF/Src/leaf-sampling.c +143 −5
  6. LEAF_JUCEPlugin/JuceLibraryCode/AppConfig.h +1 −1
  7. LEAF_JUCEPlugin/LEAF.jucer +2 −2
  8. LEAF_JUCEPlugin/Source/MyTest.cpp +1 −1
LEAF/Inc/leaf-mempool.h+2−0
@@ -113,6 +113,8 @@ extern "C" { · 112 unmodified lines
113113
114114 //==============================================================================
115115
116+ extern tMempool leaf_mempool;
117+
116118 #ifdef __cplusplus
117119 }
118120 #endif
LEAF/Inc/leaf-sampling.h+35−0
@@ -21,6 +21,7 @@ extern "C" { · 20 unmodified lines
2121 #include "leaf-mempool.h"
2222 #include "leaf-envelopes.h"
2323 #include "leaf-mempool.h"
24+#include "leaf-analysis.h"
2425
2526 //==============================================================================
2627
@@ -132,6 +133,40 @@ extern "C" { · 105 unmodified lines
132133
133134 //==============================================================================
134135
136+ typedef struct _tAutoSampler
137+ {
138+ tSampler sampler;
139+ tEnvelopeFollower ef;
140+ uint32_t windowSize;
141+ float threshold;
142+ float previousPower;
143+ uint32_t sampleCounter;
144+ uint32_t powerCounter;
145+ uint8_t sampleTriggered;
146+ } _tAutoSampler;
147+
148+ typedef _tAutoSampler* tAutoSampler;
149+
150+ void tAutoSampler_init (tAutoSampler* const, tBuffer* const);
151+ void tAutoSampler_free (tAutoSampler* const);
152+ void tAutoSampler_initToPool (tAutoSampler* const, tBuffer* const, tMempool* const);
153+ void tAutoSampler_freeFromPool (tAutoSampler* const, tMempool* const);
154+
155+ float tAutoSampler_tick (tAutoSampler* const, float input);
156+
157+ void tAutoSampler_setBuffer (tAutoSampler* const, tBuffer* const);
158+
159+ void tAutoSampler_setMode (tAutoSampler* const, PlayMode mode);
160+
161+ void tAutoSampler_play (tAutoSampler* const);
162+ void tAutoSampler_stop (tAutoSampler* const);
163+
164+ void tAutoSampler_setThreshold (tAutoSampler* const, float thresh);
165+ void tAutoSampler_setWindowSize (tAutoSampler* const, uint32_t size);
166+ void tAutoSampler_setCrossfadeLength (tAutoSampler* const, uint32_t length);
167+
168+ void tAutoSampler_setRate (tAutoSampler* const, float rate);
169+
135170 #ifdef __cplusplus
136171 }
137172 #endif
LEAF/Src/leaf-delay.c+2−1
@@ -903,7 +903,8 @@ float tTapeDelay_tick (tTapeDelay* const dl, float input) · 902 unmodified lines
903903 if (d->idx >= d->maxDelay) d->idx = 0.0f;
904904
905905 if (d->lastOut)
906− return d->lastOut;
906+ return d->lastOut;
907+ return 0.0f;
907908 }
908909
909910 void tTapeDelay_incrementInPoint(tTapeDelay* const dl)
LEAF/Src/leaf-mempool.c+14−10
@@ -48,9 +48,11 @@ · 47 unmodified lines
4848
4949 #endif
5050
51−tMempool leaf_pool;
51+_tMempool leaf_pool;
52+tMempool leaf_mempool;
5253 size_t header_size;
5354
55+
5456 /**
5557 * private function
5658 */
@@ -79,7 +81,9 @@ void mpool_create (char* memory, size_t size, mpool_t* pool) · 22 unmodified lines
7981
8082 void leaf_pool_init(char* memory, size_t size)
8183 {
82− mpool_create(memory, size, &leaf_pool->pool);
84+ mpool_create(memory, size, &leaf_pool.pool);
85+
86+ leaf_mempool = &leaf_pool;
8387 }
8488
8589 /**
@@ -206,7 +210,7 @@ void* mpool_calloc(size_t asize, mpool_t* pool) · 120 unmodified lines
206210 void* leaf_alloc(size_t size)
207211 {
208212 //printf("alloc %i\n", size);
209− void* block = mpool_alloc(size, &leaf_pool->pool);
213+ void* block = mpool_alloc(size, &leaf_pool.pool);
210214
211215 if (block == NULL) leaf_mempool_overrun();
212216
@@ -216,7 +220,7 @@ void* leaf_alloc(size_t size) · 3 unmodified lines
216220 void* leaf_calloc(size_t size)
217221 {
218222 //printf("alloc %i\n", size);
219− void* block = mpool_calloc(size, &leaf_pool->pool);
223+ void* block = mpool_calloc(size, &leaf_pool.pool);
220224
221225 if (block == NULL) leaf_mempool_overrun();
222226
@@ -288,7 +292,7 @@ void mpool_free(void* ptr, mpool_t* pool) · 65 unmodified lines
288292
289293 void leaf_free(void* ptr)
290294 {
291− mpool_free(ptr, &leaf_pool->pool);
295+ mpool_free(ptr, &leaf_pool.pool);
292296 }
293297
294298 size_t mpool_get_size(mpool_t* pool)
@@ -303,17 +307,17 @@ size_t mpool_get_used(mpool_t* pool) · 8 unmodified lines
303307
304308 size_t leaf_pool_get_size(void)
305309 {
306− return mpool_get_size(&leaf_pool->pool);
310+ return mpool_get_size(&leaf_pool.pool);
307311 }
308312
309313 size_t leaf_pool_get_used(void)
310314 {
311− return mpool_get_used(&leaf_pool->pool);
315+ return mpool_get_used(&leaf_pool.pool);
312316 }
313317
314318 void* leaf_pool_get_pool(void)
315319 {
316− float* buff = (float*)leaf_pool->pool.mpool;
320+ float* buff = (float*)leaf_pool.pool.mpool;
317321
318322 return buff;
319323 }
@@ -362,12 +366,12 @@ void leaf_mempool_overrun(void) · 42 unmodified lines
362366
363367 void tMempool_init(tMempool* const mp, char* memory, size_t size)
364368 {
365− tMempool_initToPool(mp, memory, size, &leaf_pool);
369+ tMempool_initToPool(mp, memory, size, &leaf_mempool);
366370 }
367371
368372 void tMempool_free(tMempool* const mp)
369373 {
370− tMempool_freeFromPool(mp, &leaf_pool);
374+ tMempool_freeFromPool(mp, &leaf_mempool);
371375 }
372376
373377 void tMempool_initToPool (tMempool* const mp, char* memory, size_t size, tMempool* const mem)
LEAF/Src/leaf-sampling.c+143−5
@@ -164,7 +164,18 @@ static void attemptStartEndChange(tSampler* const sp); · 163 unmodified lines
164164
165165 void tSampler_init(tSampler* const sp, tBuffer* const b)
166166 {
167− _tSampler* p = *sp = (_tSampler*) leaf_alloc(sizeof(_tSampler));
167+ tSampler_initToPool(sp, b, &leaf_mempool);
168+}
169+
170+void tSampler_free (tSampler* const sp)
171+{
172+ tSampler_freeFromPool(sp, &leaf_mempool);
173+}
174+
175+void tSampler_initToPool(tSampler* const sp, tBuffer* const b, tMempool* const mp)
176+{
177+ _tMempool* m = *mp;
178+ _tSampler* p = *sp = (_tSampler*) mpool_alloc(sizeof(_tSampler), &m->pool);
168179 _tBuffer* s = *b;
169180
170181 p->samp = s;
@@ -188,19 +199,20 @@ void tSampler_init(tSampler* const sp, tBuffer* const b) · 17 unmodified lines
188199
189200 p->cfxlen = 500; // default 300 sample crossfade
190201
191− tRamp_init(&p->gain, 7.0f, 1);
202+ tRamp_initToPool(&p->gain, 7.0f, 1, mp);
192203 tRamp_setVal(&p->gain, 0.f);
193204
194205 p->targetstart = -1;
195206 p->targetend = -1;
196207 }
197208
198−void tSampler_free (tSampler* const sp)
209+void tSampler_freeFromPool (tSampler* const sp, tMempool* const mp)
199210 {
211+ _tMempool* m = *mp;
200212 _tSampler* p = *sp;
201− tRamp_free(&p->gain);
213+ tRamp_freeFromPool(&p->gain, mp);
202214
203− leaf_free(p);
215+ mpool_free(p, &m->pool);
204216 }
205217
206218 void tSampler_setSample (tSampler* const sp, tBuffer* const b)
@@ -704,3 +716,129 @@ void tSampler_setRate (tSampler* const sp, float rate) · 497 unmodified lines
704716 }
705717
706718 //==============================================================================
719+
720+void tAutoSampler_init (tAutoSampler* const as, tBuffer* const b)
721+{
722+ tAutoSampler_initToPool(as, b, &leaf_mempool);
723+}
724+
725+void tAutoSampler_free (tAutoSampler* const as)
726+{
727+ tAutoSampler_freeFromPool(as, &leaf_mempool);
728+}
729+
730+void tAutoSampler_initToPool (tAutoSampler* const as, tBuffer* const b, tMempool* const mp)
731+{
732+ _tMempool* m = *mp;
733+ _tAutoSampler* a = *as = (_tAutoSampler*) mpool_alloc(sizeof(_tAutoSampler), &m->pool);
734+
735+ tBuffer_setRecordMode(b, RecordOneShot);
736+ tSampler_initToPool(&a->sampler, b, mp);
737+ tSampler_setMode(&a->sampler, PlayLoop);
738+ tEnvelopeFollower_initToPool(&a->ef, 0.05f, 0.9999f, mp);
739+}
740+
741+void tAutoSampler_freeFromPool (tAutoSampler* const as, tMempool* const mp)
742+{
743+ _tMempool* m = *mp;
744+ _tAutoSampler* a = *as;
745+
746+ tEnvelopeFollower_freeFromPool(&a->ef, mp);
747+ tSampler_freeFromPool(&a->sampler, mp);
748+
749+ mpool_free(a, &m->pool);
750+}
751+
752+float tAutoSampler_tick (tAutoSampler* const as, float input)
753+{
754+ _tAutoSampler* a = *as;
755+ float currentPower = tEnvelopeFollower_tick(&a->ef, input);
756+
757+ if ((currentPower > (a->threshold)) &&
758+ (currentPower > a->previousPower + 0.001f) &&
759+ (a->sampleTriggered == 0) &&
760+ (a->sampleCounter == 0))
761+ {
762+ a->sampleTriggered = 1;
763+ tBuffer_record(&a->sampler->samp);
764+ a->sampler->samp->recordedLength = a->sampler->samp->bufferLength;
765+ a->sampleCounter = a->windowSize + 24;//arbitrary extra time to avoid resampling while playing previous sample - better solution would be alternating buffers and crossfading
766+ a->powerCounter = 1000;
767+ }
768+
769+ if (a->sampleCounter > 0)
770+ {
771+ a->sampleCounter--;
772+ }
773+
774+
775+ tSampler_setEnd(&a->sampler, a->windowSize);
776+ tBuffer_tick(&a->sampler->samp, input);
777+ //on it's way down
778+ if (currentPower <= a->previousPower)
779+ {
780+ if (a->powerCounter > 0)
781+ {
782+ a->powerCounter--;
783+ }
784+ else if (a->sampleTriggered == 1)
785+ {
786+ a->sampleTriggered = 0;
787+ }
788+ }
789+
790+ a->previousPower = currentPower;
791+
792+ return tSampler_tick(&a->sampler);
793+}
794+
795+void tAutoSampler_setBuffer (tAutoSampler* const as, tBuffer* const b)
796+{
797+ _tAutoSampler* a = *as;
798+ tBuffer_setRecordMode(b, RecordOneShot);
799+ if (a->windowSize > tBuffer_getBufferLength(b))
800+ a->windowSize = tBuffer_getBufferLength(b);
801+ tSampler_setSample(&a->sampler, b);
802+}
803+
804+void tAutoSampler_setMode (tAutoSampler* const as, PlayMode mode)
805+{
806+ _tAutoSampler* a = *as;
807+ tSampler_setMode(&a->sampler, mode);
808+}
809+
810+void tAutoSampler_play (tAutoSampler* const as)
811+{
812+ _tAutoSampler* a = *as;
813+ tSampler_play(&a->sampler);
814+}
815+void tAutoSampler_stop (tAutoSampler* const as)
816+{
817+ _tAutoSampler* a = *as;
818+ tSampler_stop(&a->sampler);
819+}
820+
821+void tAutoSampler_setThreshold (tAutoSampler* const as, float thresh)
822+{
823+ _tAutoSampler* a = *as;
824+ a->threshold = thresh;
825+}
826+
827+void tAutoSampler_setWindowSize (tAutoSampler* const as, uint32_t size)
828+{
829+ _tAutoSampler* a = *as;
830+ if (size > tBuffer_getBufferLength(&a->sampler->samp))
831+ a->windowSize = tBuffer_getBufferLength(&a->sampler->samp);
832+ else a->windowSize = size;
833+}
834+
835+void tAutoSampler_setCrossfadeLength (tAutoSampler* const as, uint32_t length)
836+{
837+ _tAutoSampler* a = *as;
838+ tSampler_setCrossfadeLength(&a->sampler, length);
839+}
840+
841+void tAutoSampler_setRate (tAutoSampler* const as, float rate)
842+{
843+ ;
844+}
LEAF_JUCEPlugin/JuceLibraryCode/AppConfig.h+1−1
@@ -317,7 +317,7 @@ · 316 unmodified lines
317317 // Audio plugin settings..
318318
319319 #ifndef JucePlugin_Build_VST
320− #define JucePlugin_Build_VST 1
320+ #define JucePlugin_Build_VST 0
321321 #endif
322322 #ifndef JucePlugin_Build_VST3
323323 #define JucePlugin_Build_VST3 0
LEAF_JUCEPlugin/LEAF.jucer+2−2
@@ -2,7 +2,7 @@ · 1 unmodified line
22
33 <JUCERPROJECT id="ArQetv" name="LEAF" projectType="audioplug" version="1.0.0"
44 bundleIdentifier="com.pumusic.LEAF" includeBinaryInAppConfig="1"
5− buildVST="1" buildVST3="0" buildAU="1" buildAUv3="0" buildRTAS="0"
5+ buildVST="0" buildVST3="0" buildAU="1" buildAUv3="0" buildRTAS="0"
66 buildAAX="0" pluginName="LEAF" pluginDesc="LEAF" pluginManufacturer="pumusic"
77 pluginManufacturerCode="Manu" pluginCode="Arqe" pluginChannelConfigs="{2,2}"
88 pluginIsSynth="0" pluginWantsMidiIn="1" pluginProducesMidiOut="0"
@@ -11,7 +11,7 @@ · 2 unmodified lines
1111 jucerVersion="5.4.7" companyName="Princeton University" companyEmail="mrmulshine@gmail.com"
1212 displaySplashScreen="1" reportAppUsage="1" splashScreenColour="Dark"
1313 buildStandalone="1" enableIAA="0" cppLanguageStandard="11" companyCopyright="Princeton University"
14− pluginFormats="buildVST,buildAU,buildStandalone" pluginCharacteristicsValue="pluginWantsMidiIn">
14+ pluginFormats="buildAU,buildStandalone" pluginCharacteristicsValue="pluginWantsMidiIn">
1515 <MAINGROUP id="F7Bywq" name="LEAF">
1616 <GROUP id="{14C5E2CD-5D51-3FD3-5677-28BECA29E95E}" name="Source">
1717 <GROUP id="{C9F79DF0-2C57-F2B0-7B1B-78A14937E038}" name="LEAF">
LEAF_JUCEPlugin/Source/MyTest.cpp+1−1
@@ -46,7 +46,7 @@ void LEAFTest_init (float sampleRate, int blockSize) · 45 unmodified lines
4646 tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
4747 tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
4848
49− tFormantShifter_init(&fs, 2048, 20);
49+ tFormantShifter_init(&fs, 20);
5050
5151 // Init and set record
5252 tBuffer_init (&buff, leaf.sampleRate); // init, 1 second buffer

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t