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.

318 lines
6.8KB

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