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.

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