|
| 1 | +#include "SC_PlugIn.h" |
| 2 | + |
| 3 | +// InterfaceTable contains pointers to functions in the host |
| 4 | +static InterfaceTable *ft; |
| 5 | + |
| 6 | +struct VoxRes : public Unit |
| 7 | +{ |
| 8 | + float* resampled; |
| 9 | + float* local_buf; |
| 10 | + int resampledFrames; |
| 11 | + float* autocor_buffer; |
| 12 | + float* lpc; |
| 13 | + float* lpc_work; |
| 14 | + float* res; |
| 15 | + float* res_work; |
| 16 | + int lpc_size; |
| 17 | + int max_resonances; |
| 18 | + SndBuf *m_buf; |
| 19 | + float m_fbufnum; |
| 20 | + int m_framepos; |
| 21 | + int offset; |
| 22 | +}; |
| 23 | + |
| 24 | +static void VoxRes_next_k(VoxRes *unit, int inNumSamples); |
| 25 | +static void VoxRes_Ctor(VoxRes *unit); |
| 26 | + |
| 27 | +extern "C" |
| 28 | +{ |
| 29 | + float* vox_box_autocorrelate_mut_f32(const float*, size_t, size_t, float*); |
| 30 | + void vox_box_resample_mut_f32(const float*, size_t, size_t, float*); |
| 31 | + void vox_box_normalize_f32(void*, size_t); |
| 32 | + void vox_box_lpc_mut_f32(const float*, size_t, size_t, float*, float*); |
| 33 | + float* vox_box_resonances_mut_f32(float*, size_t, float, int*, float*, float*); |
| 34 | +} |
| 35 | + |
| 36 | +void VoxRes_Ctor(VoxRes *unit) |
| 37 | +{ |
| 38 | + SETCALC(VoxRes_next_k); |
| 39 | + unit->lpc_size = ZIN0(1); |
| 40 | + unit->resampledFrames = ZIN0(2); |
| 41 | + unit->m_buf = unit->mWorld->mSndBufs; |
| 42 | + unit->m_fbufnum = -1.f; |
| 43 | + unit->m_framepos = 0; |
| 44 | + GET_BUF_SHARED; |
| 45 | + unit->max_resonances = 4; |
| 46 | + unit->offset = 0; |
| 47 | + unit->local_buf = (float*)RTAlloc(unit->mWorld, sizeof(float) * bufFrames); |
| 48 | + unit->autocor_buffer = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->resampledFrames); |
| 49 | + unit->resampled = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->resampledFrames); |
| 50 | + unit->lpc = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size + 1); |
| 51 | + unit->lpc_work = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size * 4); |
| 52 | + unit->res = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size + 1); |
| 53 | + unit->res_work = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size * 16); |
| 54 | + for (int i = 0; i < 4; i++) { |
| 55 | + *OUT(i) = 0.f; |
| 56 | + } |
| 57 | + VoxRes_next_k(unit, 1); |
| 58 | +} |
| 59 | + |
| 60 | +void VoxRes_next_k(VoxRes *unit, int inNumSamples) |
| 61 | +{ |
| 62 | + GET_BUF_SHARED; |
| 63 | + for (int i = 0; i < bufFrames; i++) { |
| 64 | + unit->local_buf[i] = bufData[(i + unit->offset) & (bufFrames - 1)]; |
| 65 | + } |
| 66 | + vox_box_resample_mut_f32(unit->local_buf, (size_t)bufFrames, unit->resampledFrames, unit->resampled); |
| 67 | + vox_box_autocorrelate_mut_f32(unit->resampled, (size_t)unit->resampledFrames, unit->lpc_size + 2, unit->autocor_buffer); |
| 68 | + vox_box_normalize_f32(unit->autocor_buffer, unit->lpc_size + 2); |
| 69 | + vox_box_lpc_mut_f32(unit->autocor_buffer, unit->lpc_size + 2, unit->lpc_size, unit->lpc, unit->lpc_work); |
| 70 | + int resonance_count = 0; |
| 71 | + vox_box_resonances_mut_f32(unit->lpc, unit->lpc_size + 1, FULLRATE * unit->resampledFrames / (float)bufFrames, &resonance_count, unit->res_work, unit->res); |
| 72 | + for (int i = 0; (i < resonance_count) && (i < unit->max_resonances); i++) { |
| 73 | + *OUT(i) = unit->res[i]; |
| 74 | + } |
| 75 | + unit->offset += (int)BUFRATE; |
| 76 | +} |
| 77 | + |
| 78 | +void VoxRes_Dtor(VoxRes *unit) |
| 79 | +{ |
| 80 | + RTFree(unit->mWorld, unit->local_buf); |
| 81 | + RTFree(unit->mWorld, unit->autocor_buffer); |
| 82 | + RTFree(unit->mWorld, unit->lpc); |
| 83 | + RTFree(unit->mWorld, unit->lpc_work); |
| 84 | + RTFree(unit->mWorld, unit->res); |
| 85 | + RTFree(unit->mWorld, unit->res_work); |
| 86 | +} |
| 87 | + |
| 88 | +PluginLoad(VoxRes) |
| 89 | +{ |
| 90 | + ft = inTable; |
| 91 | + DefineSimpleUnit(VoxRes); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments