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.

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