Unified · Split
| @@ -90,6 +90,72 @@ extern "C" { · 89 unmodified lines | ||
| 90 | 90 | |
| 91 | 91 | /*! @} */ |
| 92 | 92 | |
| 93 | + //============================================================================== | |
| 94 | + | |
| 95 | + typedef struct _tSine | |
| 96 | + { | |
| 97 | + float* sine; | |
| 98 | + int size; | |
| 99 | + float phase; | |
| 100 | + float inc,freq; | |
| 101 | + } _tSine; | |
| 102 | + | |
| 103 | + typedef _tSine* tSine; | |
| 104 | + | |
| 105 | + /*! | |
| 106 | + * @defgroup tsine tSine | |
| 107 | + * @ingroup oscillators | |
| 108 | + * @brief A cycle/sine waveform oscillator. | |
| 109 | + * @{ | |
| 110 | + */ | |
| 111 | + | |
| 112 | + //! Initialize a tSine to the default LEAF mempool. | |
| 113 | + /*! | |
| 114 | + @param osc A pointer to the tSine to be initialized. | |
| 115 | + */ | |
| 116 | + void tSine_init (tSine* const osc, int size); | |
| 117 | + | |
| 118 | + | |
| 119 | + //! Free a tSine from the default LEAF mempool. | |
| 120 | + /*! | |
| 121 | + @param osc A pointer to the tSine to be freed. | |
| 122 | + */ | |
| 123 | + void tSine_free (tSine* const osc); | |
| 124 | + | |
| 125 | + | |
| 126 | + //! Initialize a tSine to a specified mempool. | |
| 127 | + /*! | |
| 128 | + @param osc A pointer to the tSine to be initialized. | |
| 129 | + @param pool A pointer to the tMempool to which the tSine should be initialized. | |
| 130 | + */ | |
| 131 | + void tSine_initToPool (tSine* const osc, int size, tMempool* const pool); | |
| 132 | + | |
| 133 | + | |
| 134 | + //! Free a tSine from a specified mempool. | |
| 135 | + /*! | |
| 136 | + @param osc A pointer to the tSine to be freed. | |
| 137 | + @param pool A pointer to the tMempool from which the tSine should be freed. | |
| 138 | + */ | |
| 139 | + void tSine_freeFromPool (tSine* const osc, tMempool* const pool); | |
| 140 | + | |
| 141 | + | |
| 142 | + //! Tick a tSine oscillator. | |
| 143 | + /*! | |
| 144 | + @param osc A pointer to the relevant tSine. | |
| 145 | + @return The ticked sample as a float from -1 to 1. | |
| 146 | + */ | |
| 147 | + float tSine_tick (tSine* const osc); | |
| 148 | + | |
| 149 | + | |
| 150 | + //! Set the frequency of a tSine oscillator. | |
| 151 | + /*! | |
| 152 | + @param osc A pointer to the relevant tSine. | |
| 153 | + @param freq The frequency to set the oscillator to. | |
| 154 | + */ | |
| 155 | + void tSine_setFreq (tSine* const osc, float freq); | |
| 156 | + | |
| 157 | + /*! @} */ | |
| 158 | + | |
| 93 | 159 | |
| 94 | 160 | //============================================================================== |
| 95 | 161 | |
| @@ -137,6 +137,7 @@ extern "C" { · 136 unmodified lines | ||
| 137 | 137 | |
| 138 | 138 | void tSampler_setStart (tSampler* const, int32_t start); |
| 139 | 139 | void tSampler_setEnd (tSampler* const, int32_t end); |
| 140 | + void tSampler_setLength (tSampler* const, int32_t length); | |
| 140 | 141 | |
| 141 | 142 | static void handleStartEndChange (tSampler* const sp); |
| 142 | 143 | |
| @@ -262,8 +262,8 @@ float LEAF_softClip(float val, float thresh) · 261 unmodified lines | ||
| 262 | 262 | |
| 263 | 263 | float LEAF_clip(float min, float val, float max) |
| 264 | 264 | { |
| 265 | − int tempmin = min; | |
| 266 | − int tempmax = max; | |
| 265 | + float tempmin = min; | |
| 266 | + float tempmax = max; | |
| 267 | 267 | if (min > max) |
| 268 | 268 | { |
| 269 | 269 | tempmin = max; |
| @@ -91,6 +91,85 @@ void tCycleSampleRateChanged (tCycle* const cy) · 90 unmodified lines | ||
| 91 | 91 | c->inc = c->freq * leaf.invSampleRate; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | +//======================================================================== | |
| 95 | +// Sine | |
| 96 | +void tSine_init(tSine* const cy, int size) | |
| 97 | +{ | |
| 98 | + tSine_initToPool(cy, size, &leaf.mempool); | |
| 99 | +} | |
| 100 | + | |
| 101 | +void tSine_free(tSine* const cy) | |
| 102 | +{ | |
| 103 | + tSine_freeFromPool(cy, &leaf.mempool); | |
| 104 | +} | |
| 105 | + | |
| 106 | +void tSine_initToPool (tSine* const cy, int size, tMempool* const mp) | |
| 107 | +{ | |
| 108 | + _tMempool* m = *mp; | |
| 109 | + _tSine* c = *cy = (_tSine*) mpool_alloc(sizeof(_tSine), m); | |
| 110 | + | |
| 111 | + c->size = size; | |
| 112 | + c->sine = (float*) mpool_alloc(sizeof(float) * c->size, m); | |
| 113 | + LEAF_generate_sine(c->sine, size); | |
| 114 | + c->inc = 0.0f; | |
| 115 | + c->phase = 0.0f; | |
| 116 | + | |
| 117 | +} | |
| 118 | + | |
| 119 | +void tSine_freeFromPool (tSine* const cy, tMempool* const mp) | |
| 120 | +{ | |
| 121 | + _tMempool* m = *mp; | |
| 122 | + _tSine* c = *cy; | |
| 123 | + | |
| 124 | + mpool_free(c->sine, m); | |
| 125 | + mpool_free(c, m); | |
| 126 | +} | |
| 127 | + | |
| 128 | +void tSine_setFreq(tSine* const cy, float freq) | |
| 129 | +{ | |
| 130 | + _tSine* c = *cy; | |
| 131 | + | |
| 132 | + if (freq < 0.0f) c->freq = 0.0f; | |
| 133 | + else if (freq > 20480.0f) c->freq = 20480.0f; | |
| 134 | + else c->freq = freq; | |
| 135 | + | |
| 136 | + c->inc = freq * leaf.invSampleRate; | |
| 137 | +} | |
| 138 | + | |
| 139 | +float tSine_tick(tSine* const cy) | |
| 140 | +{ | |
| 141 | + _tSine* c = *cy; | |
| 142 | + float temp; | |
| 143 | + int intPart; | |
| 144 | + float fracPart; | |
| 145 | + float samp0; | |
| 146 | + float samp1; | |
| 147 | + | |
| 148 | + // Phasor increment | |
| 149 | + c->phase += c->inc; | |
| 150 | + while (c->phase >= 1.0f) c->phase -= 1.0f; | |
| 151 | + while (c->phase < 0.0f) c->phase += 1.0f; | |
| 152 | + | |
| 153 | + // Wavetable synthesis | |
| 154 | + | |
| 155 | + temp = c->size * c->phase; | |
| 156 | + intPart = (int)temp; | |
| 157 | + fracPart = temp - (float)intPart; | |
| 158 | + samp0 = c->sine[intPart]; | |
| 159 | + if (++intPart >= c->size) intPart = 0; | |
| 160 | + samp1 = c->sine[intPart]; | |
| 161 | + | |
| 162 | + return (samp0 + (samp1 - samp0) * fracPart); | |
| 163 | + | |
| 164 | +} | |
| 165 | + | |
| 166 | +void tSineSampleRateChanged (tSine* const cy) | |
| 167 | +{ | |
| 168 | + _tSine* c = *cy; | |
| 169 | + | |
| 170 | + c->inc = c->freq * leaf.invSampleRate; | |
| 171 | +} | |
| 172 | + | |
| 94 | 173 | //======================================================================== |
| 95 | 174 | /* Triangle */ |
| 96 | 175 | void tTriangle_init(tTriangle* const cy) |
| @@ -685,6 +685,13 @@ void tSampler_setEnd (tSampler* const sp, int32_t end) · 684 unmodified lines | ||
| 685 | 685 | p->targetend = -1; |
| 686 | 686 | } |
| 687 | 687 | |
| 688 | +void tSampler_setLength (tSampler* const sp, int32_t length) | |
| 689 | +{ | |
| 690 | + _tSampler* p = *sp; | |
| 691 | + if (length == 0) length = 1; | |
| 692 | + tSampler_setEnd(sp, p->start + length); | |
| 693 | +} | |
| 694 | + | |
| 688 | 695 | void tSampler_setRate (tSampler* const sp, float rate) |
| 689 | 696 | { |
| 690 | 697 | _tSampler* p = *sp; |
| @@ -16,22 +16,7 @@ static void leaf_pool_report(void); · 15 unmodified lines | ||
| 16 | 16 | static void leaf_pool_dump(void); |
| 17 | 17 | static void run_pool_test(void); |
| 18 | 18 | |
| 19 | −tNoise noise; | |
| 20 | −tSVF bp1; | |
| 21 | −tSVF bp2; | |
| 22 | −tFormantShifter fs; | |
| 23 | − | |
| 24 | −tSampler samp; | |
| 25 | −tBuffer buff; | |
| 26 | −tEnvelope env; | |
| 27 | − | |
| 28 | −tAutotune at; | |
| 29 | − | |
| 30 | −tTriangle tri; | |
| 31 | − | |
| 32 | −tMinBLEP minblep; | |
| 33 | − | |
| 34 | −tPhasor phasor; | |
| 19 | +tSine sine; | |
| 35 | 20 | |
| 36 | 21 | float gain; |
| 37 | 22 | float freq; |
| @@ -48,68 +33,16 @@ char memory[MSIZE]; · 10 unmodified lines | ||
| 48 | 33 | void LEAFTest_init (float sampleRate, int blockSize) |
| 49 | 34 | { |
| 50 | 35 | LEAF_init(sampleRate, blockSize, memory, MSIZE, &getRandomFloat); |
| 51 | − | |
| 52 | − tTriangle_init(&tri); | |
| 53 | − | |
| 54 | − tMinBLEP_init(&minblep); | |
| 55 | − | |
| 56 | − tPhasor_init(&phasor); | |
| 57 | −// tNoise_init(&noise, WhiteNoise); | |
| 58 | −// | |
| 59 | −// tAutotune_init(&at, 1, 1024, 512); | |
| 60 | − | |
| 61 | −// tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f); | |
| 62 | −// tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f); | |
| 63 | −// | |
| 64 | −// tFormantShifter_init(&fs, 20); | |
| 65 | −// | |
| 66 | −// // Init and set record | |
| 67 | −// tBuffer_init (&buff, leaf.sampleRate); // init, 1 second buffer | |
| 68 | −// tBuffer_setRecordMode (&buff, RecordOneShot); // RecordOneShot records once through | |
| 69 | −// | |
| 70 | −// // Init and set play | |
| 71 | −// tSampler_init (&samp, &buff); // init, give address of record buffer | |
| 72 | −// tSampler_setMode (&samp, PlayLoop); //set in Loop Mode | |
| 73 | −// tSampler_setRate(&samp, 1.763f); // Rate of 1.0 | |
| 74 | −} | |
| 75 | 36 | |
| 76 | −inline double getSawFall(double angle) { | |
| 77 | − | |
| 78 | − angle = fmod(angle + double_Pi, 2*double_Pi); // shift x | |
| 79 | − double sample = angle/double_Pi - double(1); // computer as remainder | |
| 80 | − | |
| 81 | − return sample; | |
| 82 | 37 | |
| 38 | + tSine_init(&sine, 1000); | |
| 39 | + tSine_setFreq(&sine, 400); | |
| 83 | 40 | } |
| 84 | 41 | |
| 85 | 42 | float LEAFTest_tick (float input) |
| 86 | 43 | { |
| 87 | −// float sample = tNoise_tick(&noise); | |
| 88 | −// sample *= 0.5f; | |
| 89 | −// float b = tSVF_tick(&bp1, sample); | |
| 90 | −// b += tSVF_tick(&bp2, sample); | |
| 91 | −// | |
| 92 | −// return (tFormantShifter_tick(&fs, input)); | |
| 93 | −// | |
| 94 | −// tBuffer_tick(&buff, input); | |
| 95 | − | |
| 96 | −// return tSampler_tick(&samp); | |
| 97 | − | |
| 98 | −// tAutotune_setFreq(&at, 440.0f, 0); | |
| 99 | − | |
| 100 | −// return tAutotune_tick(&at, input)[0]; | |
| 101 | − | |
| 102 | − tPhasor_setFreq(&phasor, y); | |
| 103 | − | |
| 104 | − | |
| 105 | − float sample = tPhasor_tick(&phasor) * 2.0f - 1.0f; | |
| 106 | − | |
| 107 | − if (phasor->phaseDidReset) | |
| 108 | − { | |
| 109 | − tMinBLEP_addBLEP(&minblep, 0.0f, 2, 0.0f); | |
| 110 | − } | |
| 111 | 44 | |
| 112 | − return tMinBLEP_tick(&minblep, sample); | |
| 45 | + return tSine_tick(&sine); | |
| 113 | 46 | } |
| 114 | 47 | |
| 115 | 48 | int firstFrame = 1; |