1 /*  SpiralSound
2  *  Copyleft (C) 2001 David Griffiths <dave@pawfal.org>
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 #include "BeatMatchPlugin.h"
19 #include "BeatMatchPluginGUI.h"
20 #include <FL/Fl_Button.H>
21 #include "SpiralIcon.xpm"
22 #include "../../NoteTable.h"
23 
24 using namespace std;
25 
26 extern "C" {
SpiralPlugin_CreateInstance()27 SpiralPlugin* SpiralPlugin_CreateInstance()
28 {
29 	return new BeatMatchPlugin;
30 }
31 
SpiralPlugin_GetIcon()32 char** SpiralPlugin_GetIcon()
33 {
34 	return SpiralIcon_xpm;
35 }
36 
SpiralPlugin_GetID()37 int SpiralPlugin_GetID()
38 {
39 	return 48;
40 }
41 
SpiralPlugin_GetGroupName()42 string SpiralPlugin_GetGroupName()
43 {
44 	return "Maths/Logic";
45 }
46 }
47 
48 ///////////////////////////////////////////////////////
49 
BeatMatchPlugin()50 BeatMatchPlugin::BeatMatchPlugin() :
51 m_Triggered(false),
52 m_EstimatedDuration(100),
53 m_BeatTime(100),
54 m_NextBeat(100),
55 m_OutputLevel(1.0f),
56 m_Sensitivity(0.5f)
57 {
58 	m_PluginInfo.Name="BeatMatch";
59 	m_PluginInfo.Width=80;
60 	m_PluginInfo.Height=80;
61 	m_PluginInfo.NumInputs=1;
62 	m_PluginInfo.NumOutputs=1;
63 	m_PluginInfo.PortTips.push_back("Input");
64 	m_PluginInfo.PortTips.push_back("Output");
65 
66 	m_AudioCH->Register("Sensitivity",&m_Sensitivity);
67 }
68 
~BeatMatchPlugin()69 BeatMatchPlugin::~BeatMatchPlugin()
70 {
71 }
72 
Initialise(const HostInfo * Host)73 PluginInfo &BeatMatchPlugin::Initialise(const HostInfo *Host)
74 {
75 	return SpiralPlugin::Initialise(Host);
76 }
77 
CreateGUI()78 SpiralGUIType *BeatMatchPlugin::CreateGUI()
79 {
80 	return new BeatMatchPluginGUI(m_PluginInfo.Width,
81 						     m_PluginInfo.Height,
82 							 this,m_AudioCH,m_HostInfo);
83 }
84 
Execute()85 void BeatMatchPlugin::Execute()
86 {
87 	for (int n=0; n<m_HostInfo->BUFSIZE; n++)
88 	{
89 		bool Triggered=false;
90 		if (GetInput(0,n)>0)
91 		{
92 			if(!m_Triggered)
93 			{
94 				m_Triggered=true;
95 				Triggered=true;
96 			}
97 		}
98 		else
99 		{
100 			if (m_Triggered)
101 			{
102 				m_Triggered=false;
103 				Triggered=true;
104 			}
105 		}
106 
107 		if (Triggered)
108 		{
109 			// adjust estimated duration
110 			// error = m_BeatTime
111 			m_EstimatedDuration-=(int)(m_BeatTime*m_Sensitivity);
112 			m_BeatTime=m_EstimatedDuration;
113 
114 			// push the sync closer
115 			int HalfBeat = m_EstimatedDuration/2;
116 			if (m_NextBeat<HalfBeat) m_NextBeat-=(int)(HalfBeat*m_Sensitivity);
117 			else m_NextBeat+=(int)(HalfBeat*m_Sensitivity);
118 		}
119 
120 		if (m_NextBeat<=0)
121 		{
122 			m_NextBeat=m_EstimatedDuration;
123 			m_OutputLevel=-m_OutputLevel;
124 		}
125 
126 		m_NextBeat--;
127 		m_BeatTime--;
128 
129 		SetOutput(0,n,m_OutputLevel);
130 	}
131 }
132 
ExecuteCommands()133 void BeatMatchPlugin::ExecuteCommands()
134 {
135 }
136 
StreamOut(ostream & s)137 void BeatMatchPlugin::StreamOut(ostream &s)
138 {
139 	s<<m_Version<<endl;
140 	s<<m_Sensitivity<<" ";
141 }
142 
StreamIn(istream & s)143 void BeatMatchPlugin::StreamIn(istream &s)
144 {
145 	int version;
146 	s>>version;
147 	s>>m_Sensitivity;
148 }
149