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.

322 lines
7.0KB

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