You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
7.1KB

  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 "MidiPlugin.h"
  19. #include "MidiPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "../../NoteTable.h"
  22. #include "../../Midi.h"
  23. #include "SpiralIcon.xpm"
  24. using namespace std;
  25. int MidiPlugin::m_RefCount = 0;
  26. extern "C" {
  27. SpiralPlugin* SpiralPlugin_CreateInstance()
  28. {
  29. return new MidiPlugin;
  30. }
  31. char** SpiralPlugin_GetIcon()
  32. {
  33. return SpiralIcon_xpm;
  34. }
  35. int SpiralPlugin_GetID()
  36. {
  37. return 0x0002;
  38. }
  39. string SpiralPlugin_GetGroupName()
  40. {
  41. return "InputOutput";
  42. }
  43. }
  44. ///////////////////////////////////////////////////////
  45. MidiPlugin::MidiPlugin() :
  46. m_DeviceNum(0),
  47. m_NoteLevel(0),
  48. m_TriggerLevel(0),
  49. m_PitchBendLevel(0),
  50. m_ChannelPressureLevel(0),
  51. m_AfterTouchLevel(0),
  52. m_NoteCut(false),
  53. m_ContinuousNotes(false),
  54. m_CurrentNote(0)
  55. {
  56. m_Version=2;
  57. if (m_RefCount==0)
  58. {
  59. MidiDevice::Init("SpiralModular",MidiDevice::READ);
  60. }
  61. m_RefCount++;
  62. m_PluginInfo.Name="Midi";
  63. m_PluginInfo.Width=80;
  64. m_PluginInfo.Height=140;
  65. m_PluginInfo.NumInputs=2;
  66. m_PluginInfo.NumOutputs=6;
  67. m_PluginInfo.PortTips.push_back("Note CV");
  68. m_PluginInfo.PortTips.push_back("Trigger CV");
  69. m_PluginInfo.PortTips.push_back("Note CV");
  70. m_PluginInfo.PortTips.push_back("Trigger CV");
  71. m_PluginInfo.PortTips.push_back("PitchBend CV");
  72. m_PluginInfo.PortTips.push_back("ChannelPressure CV");
  73. m_PluginInfo.PortTips.push_back("Aftertouch CV");
  74. m_PluginInfo.PortTips.push_back("Clock CV");
  75. for (int n=0; n<128; n++) m_ControlLevel[n]=0;
  76. m_AudioCH->Register("DeviceNum",&m_DeviceNum);
  77. m_AudioCH->Register("NoteCut",&m_NoteCut);
  78. m_AudioCH->Register("CC",&m_GUIArgs.s);
  79. m_AudioCH->RegisterData("Name",ChannelHandler::INPUT,
  80. &m_GUIArgs.Name,sizeof(m_GUIArgs.Name));
  81. }
  82. MidiPlugin::~MidiPlugin()
  83. {
  84. m_RefCount--;
  85. if (m_RefCount==0) MidiDevice::PackUpAndGoHome();
  86. }
  87. PluginInfo &MidiPlugin::Initialise(const HostInfo *Host)
  88. {
  89. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  90. MidiDevice::SetDeviceName(Host->MIDIFILE);
  91. return Info;
  92. }
  93. SpiralGUIType *MidiPlugin::CreateGUI()
  94. {
  95. return new MidiPluginGUI(m_PluginInfo.Width,
  96. m_PluginInfo.Height,
  97. this,m_AudioCH,m_HostInfo);
  98. }
  99. void MidiPlugin::Execute()
  100. {
  101. // Done to clear IsEmpty field...
  102. GetOutputBuf(0)->Zero();
  103. GetOutputBuf(1)->Zero();
  104. GetOutputBuf(2)->Zero();
  105. GetOutputBuf(3)->Zero();
  106. GetOutputBuf(4)->Zero();
  107. GetOutputBuf(5)->Zero();
  108. for (unsigned int c=0; c<m_ControlList.size(); c++)
  109. {
  110. GetOutputBuf(c+5)->Zero();
  111. }
  112. bool Triggered=false;
  113. // midi output
  114. if (InputExists(0) && InputExists(1))
  115. {
  116. static bool TriggeredOut=false;
  117. if (GetInput(1,0)>0)
  118. {
  119. if (!TriggeredOut) // note on
  120. {
  121. // get the midi note
  122. float Freq=GetInputPitch(0,0);
  123. int Note=0;
  124. for (int n=0; n<132; n++)
  125. {
  126. if (feq(Freq,NoteTable[n],0.01f))
  127. {
  128. Note=n;
  129. break;
  130. }
  131. }
  132. MidiEvent NewEvent(MidiEvent::ON,Note,GetInput(1,0)*128.0f);
  133. MidiDevice::Get()->SendEvent(m_DeviceNum,NewEvent);
  134. TriggeredOut=true;
  135. }
  136. }
  137. else
  138. {
  139. if (TriggeredOut) // note off
  140. {
  141. // get the midi note
  142. float Freq=GetInputPitch(0,0);
  143. int Note=0;
  144. for (int n=0; n<132; n++)
  145. {
  146. if (feq(Freq,NoteTable[n],0.01f))
  147. {
  148. Note=n;
  149. break;
  150. }
  151. }
  152. MidiEvent NewEvent(MidiEvent::OFF,Note,0.0f);
  153. MidiDevice::Get()->SendEvent(m_DeviceNum,NewEvent);
  154. TriggeredOut=false;
  155. }
  156. }
  157. }
  158. MidiEvent Event=MidiDevice::Get()->GetEvent(m_DeviceNum);
  159. // get all the midi events since the last check
  160. while(Event.GetType()!=MidiEvent::NONE)
  161. {
  162. if (Event.GetType()==MidiEvent::ON)
  163. {
  164. Triggered=true;
  165. m_CurrentNote=Event.GetNote();
  166. m_NoteLevel=NoteTable[m_CurrentNote];
  167. m_TriggerLevel=Event.GetVolume()/127.0f;
  168. }
  169. if (Event.GetType()==MidiEvent::OFF)
  170. {
  171. if (Event.GetNote()==m_CurrentNote)
  172. {
  173. m_TriggerLevel=0;
  174. if (m_NoteCut) m_NoteLevel=0;
  175. }
  176. }
  177. if (Event.GetType()==MidiEvent::PITCHBEND)
  178. {
  179. m_PitchBendLevel=Event.GetVolume()/127.0f*2.0f-1.0f;
  180. }
  181. if (Event.GetType()==MidiEvent::CHANNELPRESSURE)
  182. {
  183. m_ChannelPressureLevel=Event.GetVolume()/127.0f;
  184. }
  185. if (Event.GetType()==MidiEvent::AFTERTOUCH)
  186. {
  187. m_AfterTouchLevel=Event.GetVolume()/127.0f;
  188. }
  189. if (Event.GetType()==MidiEvent::PARAMETER)
  190. {
  191. // just to make sure
  192. if (Event.GetNote()>=0 && Event.GetNote()<128)
  193. {
  194. m_ControlLevel[Event.GetNote()]=Event.GetVolume()/127.0f;
  195. }
  196. }
  197. Event=MidiDevice::Get()->GetEvent(m_DeviceNum);
  198. }
  199. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  200. {
  201. SetOutputPitch(0,n,m_NoteLevel);
  202. SetOutput(1,n,m_TriggerLevel);
  203. SetOutput(2,n,m_PitchBendLevel);
  204. SetOutput(3,n,m_ChannelPressureLevel);
  205. SetOutput(4,n,m_AfterTouchLevel);
  206. SetOutput(5,n,MidiDevice::Get()->GetClock());
  207. }
  208. for (unsigned int c=0; c<m_ControlList.size(); c++)
  209. {
  210. GetOutputBuf(c+5)->Set(m_ControlLevel[m_ControlList[c]]);
  211. }
  212. // make sure the trigger is registered if it's
  213. // note is pressed before releasing the previous one.
  214. if (Triggered && !m_ContinuousNotes) SetOutput(1,0,0);
  215. }
  216. void MidiPlugin::ExecuteCommands()
  217. {
  218. // Process any commands from the GUI
  219. if (m_AudioCH->IsCommandWaiting())
  220. {
  221. switch (m_AudioCH->GetCommand())
  222. {
  223. case (ADDCONTROL) : AddControl(m_GUIArgs.s,m_GUIArgs.Name); break;
  224. case (DELCONTROL) : DeleteControl();
  225. };
  226. }
  227. }
  228. void MidiPlugin::AddControl(int s, const string &Name)
  229. {
  230. m_ControlList.push_back(s);
  231. AddOutput();
  232. m_PluginInfo.NumOutputs++;
  233. m_PluginInfo.PortTips.push_back(Name);
  234. UpdatePluginInfoWithHost();
  235. }
  236. void MidiPlugin::DeleteControl()
  237. {
  238. if (m_ControlList.size()==0) return;
  239. m_ControlList.pop_back();
  240. RemoveOutput();
  241. m_PluginInfo.NumOutputs--;
  242. m_PluginInfo.PortTips.pop_back();
  243. UpdatePluginInfoWithHost();
  244. }
  245. void MidiPlugin::StreamOut(ostream &s)
  246. {
  247. s<<m_Version<<" "<<m_DeviceNum<<" "<<m_NoteCut<<" ";
  248. s<<m_ControlList.size()<<endl;
  249. for (unsigned int n=0; n<m_ControlList.size(); n++)
  250. {
  251. string PortTip=m_PluginInfo.PortTips[5+n];
  252. s<<m_ControlList[n]<<" "<<PortTip.size()<<" "<<PortTip<<endl;
  253. }
  254. }
  255. void MidiPlugin::StreamIn(istream &s)
  256. {
  257. int version;
  258. s>>version;
  259. switch (version)
  260. {
  261. case 1: s>>m_DeviceNum>>m_NoteCut; break;
  262. case 2:
  263. {
  264. s>>m_DeviceNum>>m_NoteCut;
  265. int Num;
  266. s>>Num;
  267. for (int n=0; n<Num; n++)
  268. {
  269. int Control;
  270. s>>Control;
  271. char Buf[4096];
  272. int size;
  273. s>>size;
  274. s.ignore(1);
  275. s.get(Buf,size+1);
  276. AddControl(Control, Buf);
  277. }
  278. }
  279. }
  280. }