From 91b659bcdd975343c0b9447524f1e2347b971cd9 Mon Sep 17 00:00:00 2001 From: aj_genius Date: Sat, 3 Jan 2004 13:09:43 +0000 Subject: [PATCH] fix formant to allow multiple instances, prevent cpu spike on disconnect input --- .../FormantFilterPlugin/FormantFilterPlugin.C | 105 +++++++++++------- .../FormantFilterPlugin/FormantFilterPlugin.h | 2 + 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.C b/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.C index 5c5bd35..eee48d3 100644 --- a/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.C +++ b/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.C @@ -54,12 +54,6 @@ const double coeff[5][11]= { } }; //--------------------------------------------------------------------------------- -static double memory[5][10]={{0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0}}; -//--------------------------------------------------------------------------------- extern "C" { SpiralPlugin* SpiralPlugin_CreateInstance() @@ -88,6 +82,14 @@ string SpiralPlugin_GetGroupName() FormantFilterPlugin::FormantFilterPlugin() : m_Vowel(0) { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 10; j++) + { + memory[i][j] = 0; + } + } + m_PluginInfo.Name="FormantFilter"; m_PluginInfo.Width=90; m_PluginInfo.Height=110; @@ -118,47 +120,66 @@ SpiralGUIType *FormantFilterPlugin::CreateGUI() void FormantFilterPlugin::Execute() { - float res,o[5],out=0; + float res,o[5],out=0, in=0; - for (int n=0; nBUFSIZE; n++) + for (int n=0; nBUFSIZE; n++) { - for (int v=0; v<5; v++) + //reset memory if disconnected, and skip out (prevents CPU spike) + if (! InputExists(0)) + { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 10; j++) + { + memory[i][j] = 0; + } + } + out = 0; + } + else { - res= (float) (coeff[v][0]*(GetInput(0,n)*0.1f) + - coeff[v][1]*memory[v][0] + - coeff[v][2]*memory[v][1] + - coeff[v][3]*memory[v][2] + - coeff[v][4]*memory[v][3] + - coeff[v][5]*memory[v][4] + - coeff[v][6]*memory[v][5] + - coeff[v][7]*memory[v][6] + - coeff[v][8]*memory[v][7] + - coeff[v][9]*memory[v][8] + - coeff[v][10]*memory[v][9] ); - - memory[v][9]=memory[v][8]; - memory[v][8]=memory[v][7]; - memory[v][7]=memory[v][6]; - memory[v][6]=memory[v][5]; - memory[v][5]=memory[v][4]; - memory[v][4]=memory[v][3]; - memory[v][3]=memory[v][2]; - memory[v][2]=memory[v][1]; - memory[v][1]=memory[v][0]; - memory[v][0]=(double) res; + in = GetInput(0,n); - o[v]=res; - } - - if (InputExists(1)) m_Vowel=fabs(GetInput(1,n))*4.0f; - - // mix between vowel sounds - if (m_Vowel<1) out=Linear(0,1,m_Vowel,o[1],o[0]); - else if (m_Vowel>1 && m_Vowel<2) out=Linear(0,1,m_Vowel-1.0f,o[2],o[1]); - else if (m_Vowel>2 && m_Vowel<3) out=Linear(0,1,m_Vowel-2.0f,o[3],o[2]); - else if (m_Vowel>3 && m_Vowel<4) out=Linear(0,1,m_Vowel-3.0f,o[4],o[3]); - else if (m_Vowel==4) out=o[4]; + for (int v=0; v<5; v++) + { + res= (float) (coeff[v][0]*(in*0.1f) + + coeff[v][1]*memory[v][0] + + coeff[v][2]*memory[v][1] + + coeff[v][3]*memory[v][2] + + coeff[v][4]*memory[v][3] + + coeff[v][5]*memory[v][4] + + coeff[v][6]*memory[v][5] + + coeff[v][7]*memory[v][6] + + coeff[v][8]*memory[v][7] + + coeff[v][9]*memory[v][8] + + coeff[v][10]*memory[v][9] ); + + memory[v][9]=memory[v][8]; + memory[v][8]=memory[v][7]; + memory[v][7]=memory[v][6]; + memory[v][6]=memory[v][5]; + memory[v][5]=memory[v][4]; + memory[v][4]=memory[v][3]; + memory[v][3]=memory[v][2]; + memory[v][2]=memory[v][1]; + memory[v][1]=memory[v][0]; + memory[v][0]=(double) res; + + o[v]=res; + } + + if (InputExists(1)) + { + m_Vowel=fabs(GetInput(1,n))*4.0f; + } + // mix between vowel sounds + if (m_Vowel<1) out=Linear(0,1,m_Vowel,o[1],o[0]); + else if (m_Vowel>1 && m_Vowel<2) out=Linear(0,1,m_Vowel-1.0f,o[2],o[1]); + else if (m_Vowel>2 && m_Vowel<3) out=Linear(0,1,m_Vowel-2.0f,o[3],o[2]); + else if (m_Vowel>3 && m_Vowel<4) out=Linear(0,1,m_Vowel-3.0f,o[4],o[3]); + else if (m_Vowel==4) out=o[4]; + } SetOutput(0,n,out); } diff --git a/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.h b/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.h index c1f53ca..1f14ed6 100644 --- a/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.h +++ b/SpiralSound/Plugins/FormantFilterPlugin/FormantFilterPlugin.h @@ -41,6 +41,8 @@ public: private: float m_Vowel; + double memory[5][10]; + friend std::istream &operator>>(std::istream &s, FormantFilterPlugin &o); friend std::ostream &operator<<(std::ostream &s, FormantFilterPlugin &o); };