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.

268 lines
5.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 "SeqPlugin.h"
  19. #include "SeqPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../RiffWav.h"
  23. #include "../../NoteTable.h"
  24. // for note on's
  25. static const float TRIGGER_LEV=0.1;
  26. extern "C" {
  27. SpiralPlugin* CreateInstance()
  28. {
  29. return new SeqPlugin;
  30. }
  31. char** GetIcon()
  32. {
  33. return SpiralIcon_xpm;
  34. }
  35. int GetID()
  36. {
  37. return 0x0011;
  38. }
  39. }
  40. ///////////////////////////////////////////////////////
  41. SeqPlugin::SeqPlugin() :
  42. m_Time(0.0f),
  43. m_Length(10.0f),
  44. m_SpeedMod(1.0f),
  45. m_CurrentNoteCV(0),
  46. m_CurrentTriggerCV(0),
  47. m_InNoteDown(false),
  48. m_InNoteID(0),
  49. m_CurrentPattern(0),
  50. m_Triggered(false)
  51. {
  52. m_Version=2;
  53. m_PluginInfo.Name="Sequencer";
  54. m_PluginInfo.Width=540;
  55. m_PluginInfo.Height=290;
  56. m_PluginInfo.NumInputs=4;
  57. m_PluginInfo.NumOutputs=2;
  58. m_PluginInfo.PortTips.push_back("Play Trigger");
  59. m_PluginInfo.PortTips.push_back("Speed CV");
  60. m_PluginInfo.PortTips.push_back("Input Pitch CV");
  61. m_PluginInfo.PortTips.push_back("Input Trigger CV");
  62. m_PluginInfo.PortTips.push_back("Output Pitch");
  63. m_PluginInfo.PortTips.push_back("Output Trigger");
  64. }
  65. SeqPlugin::~SeqPlugin()
  66. {
  67. }
  68. PluginInfo &SeqPlugin::Initialise(const HostInfo *Host)
  69. {
  70. return SpiralPlugin::Initialise(Host);
  71. }
  72. SpiralGUIType *SeqPlugin::CreateGUI()
  73. {
  74. m_GUI = new SeqPluginGUI(m_PluginInfo.Width,
  75. m_PluginInfo.Height,
  76. this,m_HostInfo);
  77. m_GUI->hide();
  78. return m_GUI;
  79. }
  80. void SeqPlugin::SetPattern(int s)
  81. {
  82. m_Eventmap[m_CurrentPattern]->hide();
  83. m_CurrentPattern=s;
  84. m_Eventmap[m_CurrentPattern]->show();
  85. }
  86. void SeqPlugin::Execute()
  87. {
  88. float Speed;
  89. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  90. {
  91. if (GetInputPitch(0,n)>0)
  92. {
  93. if (!m_Triggered)
  94. {
  95. float Freq=GetInputPitch(0,n);
  96. // Notes 0 to 16 trigger patterns 0 to 16
  97. // No other notes catered for
  98. for (int i=0; i<NUM_PATTERNS; i++)
  99. {
  100. if (feq(Freq,NoteTable[i],0.01f))
  101. {
  102. SetPattern(i);
  103. break;
  104. }
  105. }
  106. m_Time=0;
  107. m_Triggered=true;
  108. }
  109. }
  110. else
  111. {
  112. m_Triggered=false;
  113. }
  114. if (InputExists(1))
  115. Speed =(1.1025f/m_HostInfo->SAMPLERATE) * (GetInput(1,n)+1.0f);
  116. else
  117. Speed =1.1025f/m_HostInfo->SAMPLERATE;
  118. if (!m_InNoteDown)
  119. {
  120. // Check trigger
  121. if (GetInput(3,n)>TRIGGER_LEV)
  122. {
  123. m_InNoteDown=true;
  124. float Freq=GetInputPitch(2,n);
  125. int NoteNum=0;
  126. for (int i=0; i<131; i++)
  127. {
  128. if (feq(Freq,NoteTable[i],0.01f))
  129. {
  130. NoteNum=i;
  131. break;
  132. }
  133. }
  134. /*cerr<<"note recieved ="<<NoteNum<<" f="<<Freq
  135. <<" t-1="<<NoteTable[NoteNum-1]
  136. <<" t="<<NoteTable[NoteNum]
  137. <<" t+1="<<NoteTable[NoteNum+1]<<endl;
  138. */
  139. m_InNoteID=m_Eventmap[m_CurrentPattern]->AddEventTime(m_Time, NoteNum, 0.5, Fl_SEvent::MELODY, false);
  140. m_InNoteTime=m_Time;
  141. }
  142. }
  143. else
  144. {
  145. // Check trigger
  146. if (GetInput(3,n)<TRIGGER_LEV)
  147. {
  148. m_Eventmap[m_CurrentPattern]->SetEventLength(m_InNoteID, m_Time-m_InNoteTime);
  149. m_InNoteDown=false;
  150. }
  151. }
  152. // Get the notes from the map
  153. vector<EventInfo> NoteVec=m_Eventmap[m_CurrentPattern]->GetEvents(m_Time);
  154. // play all the notes found
  155. for (vector<EventInfo>::iterator i=NoteVec.begin();
  156. i!=NoteVec.end(); i++)
  157. {
  158. if (i->m_Type==EventInfo::START)
  159. {
  160. m_CurrentNoteCV=NoteTable[i->m_Group];
  161. m_CurrentTriggerCV=1;
  162. }
  163. if (i->m_Type==EventInfo::END)
  164. {
  165. m_CurrentTriggerCV=0;
  166. if (m_NoteCut) m_CurrentNoteCV=0;
  167. }
  168. }
  169. SetOutputPitch(0,n,m_CurrentNoteCV);
  170. SetOutput(1,n,m_CurrentTriggerCV);
  171. m_Time+=Speed*m_SpeedMod;
  172. if (m_Time>m_Length)
  173. {
  174. m_Time=0;
  175. }
  176. if (m_Time<0)
  177. {
  178. m_Time=m_Length;
  179. }
  180. }
  181. }
  182. void SeqPlugin::StreamOut(ostream &s)
  183. {
  184. s<<m_Version<<" ";
  185. switch (m_Version)
  186. {
  187. case 2:
  188. {
  189. s<<m_Time<<" ";
  190. s<<m_Length<<" ";
  191. s<<m_SpeedMod<<" ";
  192. s<<m_Loop<<" ";
  193. s<<m_NoteCut<<" ";
  194. s<<m_CurrentPattern<<" ";
  195. }
  196. // fallthrough
  197. case 1:
  198. {
  199. for(int n=0; n<NUM_PATTERNS; n++)
  200. {
  201. s<<*m_Eventmap[n]<<" ";
  202. }
  203. } break;
  204. }
  205. }
  206. void SeqPlugin::StreamIn(istream &s)
  207. {
  208. int version;
  209. s>>version;
  210. switch (version)
  211. {
  212. case 2:
  213. {
  214. s>>m_Time;
  215. s>>m_Length;
  216. s>>m_SpeedMod;
  217. s>>m_Loop;
  218. s>>m_NoteCut;
  219. s>>m_CurrentPattern;
  220. }
  221. // fallthrough
  222. case 1:
  223. {
  224. for(int n=0; n<NUM_PATTERNS; n++)
  225. {
  226. s>>*m_Eventmap[n];
  227. }
  228. } break;
  229. }
  230. }