1 /*
2  *  LPCAnalyzer.cpp
3  *  xSC3ExtPlugins-Universal
4  *
5  *  Created by Nick Collins on 10/04/2009.
6  *  Copyright 2009 Nick Collins. All rights reserved.
7  *
8  */
9 
10 #include "NCAnalysis.h"
11 
12 #define SCMEMORYALLOC 1
13 #include "LPCAnalysis.h"
14 
15 struct LPCAnalyzer : public Unit
16 {
17 	LPCAnalysis * lpc;
18 	LPCAnalysis * lpc2; //two of them, for corssfading when changing filter
19 };
20 
21 
22 extern "C"
23 {
24 	void LPCAnalyzer_next(LPCAnalyzer *unit, int inNumSamples);
25 	void LPCAnalyzer_Ctor(LPCAnalyzer* unit);
26 	void LPCAnalyzer_Dtor(LPCAnalyzer* unit);
27 }
28 
29 
30 
31 
32 //also can do test to return convergence point; clue to transient vs tonal?
33 //toggle to freeze or not on current filter coefficients?
34 
35 void LPCAnalyzer_Ctor(LPCAnalyzer* unit) {
36 
37 	int windowsize= (int)ZIN0(2);
38 	int windowtype= (int)ZIN0(6);
39 
40 	int blocksize= unit->mWorld->mFullRate.mBufLength;
41 
42 	if(windowsize<blocksize) windowsize=blocksize;
43 	//must be divisible by two?
44 	if((windowsize & 0x01))
45 		windowsize= windowsize+1;
46 	if(windowsize>1024) windowsize=1024;
47 
48 
49 	//overloaded new operator so realtime safe
50 	//unit->mWorld->mFullRate.mBufLength, no need to pass blocksize now
51 	unit->lpc= (LPCAnalysis*) new(unit->mWorld, ft) LPCAnalysis(windowsize,windowtype,0,unit->mWorld, ft);
52 
53 	if(windowtype>0)
54 		unit->lpc2= (LPCAnalysis*) new(unit->mWorld, ft) LPCAnalysis(windowsize,windowtype,windowsize/2,unit->mWorld, ft);
55 	else
56 		unit->lpc2=NULL;
57 
58 	//put them out of sync by half window for crossfading purposes
59 	//unit->lpc2->pos= windowsize/2;
60 
61 	SETCALC(LPCAnalyzer_next);
62 
63 }
64 
65 void LPCAnalyzer_Dtor(LPCAnalyzer* unit) {
66 
67 	//should work via overloaded delete
68 	delete unit->lpc;
69 	//should be OK even if NULL
70 	delete unit->lpc2;
71 }
72 
73 void LPCAnalyzer_next(LPCAnalyzer *unit, int inNumSamples) {
74 
75 	float * inoriginal= IN(0);
76 	float * indriver= IN(1);
77 	int p= (int)ZIN0(3);
78 	float * out= OUT(0);
79 
80 	int testE= (int)ZIN0(4);
81 	LPCfloat delta= (LPCfloat)ZIN0(5);
82 
83 	for (int i=0; i<inNumSamples; ++i) {
84 		out[i]= 0.0;
85 	}
86 
87 	unit->lpc->testdelta= testE;
88 	unit->lpc->delta= delta;
89 	unit->lpc->update(inoriginal, indriver, out, inNumSamples, p);
90 
91 	if(unit->lpc2) {
92 		unit->lpc2->testdelta= testE;
93 		unit->lpc2->delta= delta;
94 		unit->lpc2->update(inoriginal, indriver, out, inNumSamples, p);
95 	}
96 
97 }
98 
99 void loadLPCAnalyzer(InterfaceTable *inTable)
100 {
101 
102 	//ft= inTable;
103 
104 	DefineDtorCantAliasUnit(LPCAnalyzer);
105 }
106