1 /*  SpiralSound
2  *  Copyleft (C) 2003 Andy Preston <andy@clublinux.co.uk>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 
19 #include "TransposePlugin.h"
20 #include "TransposePluginGUI.h"
21 #include "SpiralIcon.xpm"
22 #include "../../NoteTable.h"
23 
24 using namespace std;
25 
26 extern "C" {
27 
28 SpiralPlugin* SpiralPlugin_CreateInstance () {
29     return new TransposePlugin;
30 }
31 
32 char** SpiralPlugin_GetIcon () {
33     return SpiralIcon_xpm;
34 }
35 
36 int SpiralPlugin_GetID () {
37     return 122;
38 }
39 
40 string SpiralPlugin_GetGroupName() {
41     return "Control";
42 }
43 
44 }
45 
46 ///////////////////////////////////////////////////////
47 
48 TransposePlugin::TransposePlugin () :
49 m_Amount(0),
50 m_Out(0.0)
51 {
52     m_PluginInfo.Name = "Transpose";
53     m_PluginInfo.Width = 80;
TableFuncNext(TableFuncScanState * node)54     m_PluginInfo.Height = 60;
55     m_PluginInfo.NumInputs = 2;
56     m_PluginInfo.NumOutputs = 1;
57     m_PluginInfo.PortTips.push_back ("Input");
58     m_PluginInfo.PortTips.push_back ("Transpose CV");
59     m_PluginInfo.PortTips.push_back ("Output");
60     m_AudioCH->Register ("Amount", &m_Amount);
61 }
62 
63 TransposePlugin::~TransposePlugin () {
64 }
65 
66 PluginInfo &TransposePlugin::Initialise (const HostInfo *Host) {
67     return SpiralPlugin::Initialise (Host);
68 }
69 
70 SpiralGUIType *TransposePlugin::CreateGUI() {
71     return new TransposePluginGUI (m_PluginInfo.Width, m_PluginInfo.Height, this, m_AudioCH, m_HostInfo);
72 }
73 
74 void TransposePlugin::Execute () {
75      int Match [2];
76      for (int n=0; n<m_HostInfo->BUFSIZE; n++) {
77          Match[0]=0;
78          Match[1]=0;
79          for (int i=0; i<2 && InputExists(i); i++) {
80              float Freq, Dif, MinDif = 30000;
TableFuncRecheck(TableFuncScanState * node,TupleTableSlot * slot)81              Freq = GetInputPitch (i, n);
82              for (int c=0; c<131; c++) {
83                  Dif = fabs (NoteTable[c] - Freq);
84                  if (Dif > MinDif) break;
85                  MinDif=Dif;
86                  Match[i]=c;
87              }
88          }
89          if (! InputExists (1)) Match[1] = m_Amount;
90          m_Out = NoteTable [(Match[0] + Match[1]) % 132];
91          SetOutputPitch (0, n, m_Out);
92      }
93 }
94 
95 void TransposePlugin::StreamOut (ostream &s) {
96      s << m_Version << endl;
ExecTableFuncScan(PlanState * pstate)97      s << m_Amount;
98 }
99 
100 void TransposePlugin::StreamIn (istream &s) {
101      int version;
102      s >> version;
103      s >> m_Amount;
104 }
105