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.

297 lines
6.3KB

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