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.

275 lines
5.2KB

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