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.

368 lines
8.4KB

  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 "MatrixPlugin.h"
  19. #include "MatrixPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../RiffWav.h"
  23. #include "../../NoteTable.h"
  24. extern "C" {
  25. SpiralPlugin* CreateInstance()
  26. {
  27. return new MatrixPlugin;
  28. }
  29. char** GetIcon()
  30. {
  31. return SpiralIcon_xpm;
  32. }
  33. int GetID()
  34. {
  35. return 0x0012;
  36. }
  37. }
  38. ///////////////////////////////////////////////////////
  39. MatrixPlugin::MatrixPlugin() :
  40. m_TickTime(1.0f),
  41. m_StepTime(1.0f),
  42. m_Time(0.0f),
  43. m_Step(0),
  44. m_Loop(true),
  45. m_NoteCut(false),
  46. m_Current(0),
  47. m_CurrentNoteCV(0),
  48. m_CurrentTriggerCV(0),
  49. m_Triggered(false),
  50. m_ClockHigh(false),
  51. m_CopyPattern(0)
  52. {
  53. cerr<<hex<<this<<dec<<endl;
  54. m_Version=2;
  55. m_PluginInfo.Name="Matrix";
  56. m_PluginInfo.Width=555;
  57. m_PluginInfo.Height=270;
  58. m_PluginInfo.NumInputs=5;
  59. m_PluginInfo.NumOutputs=19;
  60. m_PluginInfo.PortTips.push_back("Play Trigger");
  61. m_PluginInfo.PortTips.push_back("StepTime CV");
  62. m_PluginInfo.PortTips.push_back("Input Pitch CV");
  63. m_PluginInfo.PortTips.push_back("Input Trigger CV");
  64. m_PluginInfo.PortTips.push_back("External Clock");
  65. m_PluginInfo.PortTips.push_back("Output Pitch");
  66. m_PluginInfo.PortTips.push_back("Output Trigger");
  67. m_PluginInfo.PortTips.push_back("Trigger 1");
  68. m_PluginInfo.PortTips.push_back("Trigger 2");
  69. m_PluginInfo.PortTips.push_back("Trigger 3");
  70. m_PluginInfo.PortTips.push_back("Trigger 4");
  71. m_PluginInfo.PortTips.push_back("Trigger 5");
  72. m_PluginInfo.PortTips.push_back("Trigger 6");
  73. m_PluginInfo.PortTips.push_back("Trigger 7");
  74. m_PluginInfo.PortTips.push_back("Trigger 8");
  75. m_PluginInfo.PortTips.push_back("Trigger 9");
  76. m_PluginInfo.PortTips.push_back("Trigger 10");
  77. m_PluginInfo.PortTips.push_back("Trigger 11");
  78. m_PluginInfo.PortTips.push_back("Trigger 12");
  79. m_PluginInfo.PortTips.push_back("Trigger 13");
  80. m_PluginInfo.PortTips.push_back("Trigger 14");
  81. m_PluginInfo.PortTips.push_back("Trigger 15");
  82. m_PluginInfo.PortTips.push_back("Trigger 16");
  83. m_PluginInfo.PortTips.push_back("Reset Trigger");
  84. for (int n=0; n<NUM_PATTERNS; n++)
  85. {
  86. m_Matrix[n].Length=32;
  87. m_Matrix[n].Speed=1.0f;
  88. m_Matrix[n].Octave=0;
  89. for (int x=0; x<MATX; x++)
  90. for (int y=0; y<MATY; y++)
  91. {
  92. m_Matrix[n].Matrix[x][y]=false;
  93. }
  94. m_TriggerLevel[n]=0;
  95. }
  96. }
  97. MatrixPlugin::~MatrixPlugin()
  98. {
  99. }
  100. PluginInfo &MatrixPlugin::Initialise(const HostInfo *Host)
  101. {
  102. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  103. m_TickTime = 1.0f/(float)m_HostInfo->SAMPLERATE;
  104. return Info;
  105. }
  106. SpiralGUIType *MatrixPlugin::CreateGUI()
  107. {
  108. m_GUI = new MatrixPluginGUI(m_PluginInfo.Width,
  109. m_PluginInfo.Height,
  110. this,m_HostInfo);
  111. m_GUI->hide();
  112. return m_GUI;
  113. }
  114. void MatrixPlugin::Execute()
  115. {
  116. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  117. {
  118. if (InputExists(1)) m_StepTime = GetInput(1,n);
  119. // inc time
  120. m_Time+=m_TickTime;
  121. SetOutputPitch(0,n,m_CurrentNoteCV);
  122. SetOutput(1,n,m_CurrentTriggerCV);
  123. if (m_Step+1 >= m_Matrix[m_Current].Length) SetOutput(18, n, 1);
  124. else SetOutput(18, n, 0);
  125. if (GetInputPitch(0,n)>0)
  126. {
  127. if (!m_Triggered)
  128. {
  129. float Freq=GetInputPitch(0,n);
  130. // Notes 0 to 16 trigger patterns 0 to 16
  131. // No other notes catered for
  132. for (int i=0; i<NUM_PATTERNS; i++)
  133. {
  134. if (feq(Freq,NoteTable[i],0.01f))
  135. {
  136. m_Current=i;
  137. break;
  138. }
  139. }
  140. // make it so the next note to trigger
  141. // will be the first one
  142. if (m_GUI) ((MatrixPluginGUI*)m_GUI)->UpdateValues();
  143. m_Time=m_StepTime*(1/m_Matrix[m_Current].Speed);
  144. m_Step=-1;
  145. m_Triggered=true;
  146. }
  147. }
  148. else
  149. {
  150. m_Triggered=false;
  151. }
  152. // set the individual triggers
  153. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,m_TriggerLevel[t]);
  154. bool ExternalClock = InputExists(4);
  155. bool ExternalClockTriggered=false;
  156. if (ExternalClock)
  157. {
  158. if (GetInput(4,n)>0)
  159. {
  160. if(!m_ClockHigh)
  161. {
  162. m_ClockHigh=true;
  163. ExternalClockTriggered=true;
  164. }
  165. }
  166. else
  167. {
  168. if (m_ClockHigh)
  169. {
  170. m_ClockHigh=false;
  171. ExternalClockTriggered=true;
  172. }
  173. }
  174. }
  175. // An external clock pulse overrides the internal timing
  176. if ((!ExternalClock && m_Time>=m_StepTime*(1/m_Matrix[m_Current].Speed)) ||
  177. (ExternalClock && ExternalClockTriggered))
  178. {
  179. m_Time=0;
  180. m_Step++;
  181. if (m_Step >= m_Matrix[m_Current].Length) m_Step=0;
  182. if (m_GUI) ((MatrixPluginGUI*)m_GUI)->SetLED(m_Step);
  183. // Reset the values
  184. m_CurrentTriggerCV=0;
  185. if (m_NoteCut) m_CurrentNoteCV=0;
  186. for (int t=0; t<NUM_PATTERNS; t++)
  187. {
  188. SetOutput(t+2,n,0);
  189. m_TriggerLevel[t]=0;
  190. }
  191. // Scan the matrix at current time
  192. for (int i=0; i<MATY; i++)
  193. {
  194. if (m_Matrix[m_Current].Matrix[m_Step][i])
  195. {
  196. m_CurrentNoteCV=NoteTable[i+m_Matrix[m_Current].Octave*12];
  197. m_CurrentTriggerCV=1;
  198. m_TriggerLevel[i]=1;
  199. }
  200. }
  201. // reset the triggers between steps to clear them
  202. // otherwise consecutive events wont get triggered
  203. SetOutput(1,n,0);
  204. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,0);
  205. }
  206. }
  207. }
  208. void MatrixPlugin::PastePattern() {
  209. cerr<<hex<<this<<dec<<endl;
  210. cerr<<m_CopyPattern<<endl;
  211. m_Matrix[m_Current].Length = m_Matrix[m_CopyPattern].Length;
  212. m_Matrix[m_Current].Speed = m_Matrix[m_CopyPattern].Speed;
  213. m_Matrix[m_Current].Octave = m_Matrix[m_CopyPattern].Octave;
  214. for (int y=0; y<MATY; y++) {
  215. for (int x=0; x<MATX; x++) {
  216. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_CopyPattern].Matrix[x][y];
  217. }
  218. }
  219. }
  220. void MatrixPlugin::ClearPattern() {
  221. for (int y=0; y<MATY; y++) {
  222. for (int x=0; x<MATX; x++) {
  223. m_Matrix[m_Current].Matrix[x][y] = 0;
  224. }
  225. }
  226. }
  227. void MatrixPlugin::TransposeUp() {
  228. int x, y;
  229. for (y=MATY-1; y>=0; y--) {
  230. for (x=0; x<MATX; x++) {
  231. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_Current].Matrix[x][y-1];
  232. }
  233. }
  234. for (x=0; x<MATX; x++) {
  235. m_Matrix[m_Current].Matrix[x][0] = 0;
  236. }
  237. }
  238. void MatrixPlugin::TransposeDown()
  239. {
  240. int x, y;
  241. for (y=0; y<MATY-1; y++)
  242. {
  243. for (x=0; x<MATX; x++)
  244. {
  245. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_Current].Matrix[x][y+1];
  246. }
  247. }
  248. for (x=0; x<MATX; x++)
  249. {
  250. m_Matrix[m_Current].Matrix[x][MATY-1] = 0;
  251. }
  252. }
  253. bool MatrixPlugin::CanTransposeDown()
  254. {
  255. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][0]) return False;
  256. return True;
  257. }
  258. bool MatrixPlugin::CanTransposeUp()
  259. {
  260. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][MATY-1]) return False;
  261. return True;
  262. }
  263. void MatrixPlugin::StreamOut(ostream &s)
  264. {
  265. s<<m_Version<<" ";
  266. s<<m_Current<<" "<<m_Time<<" "<<m_Step<<" "<<m_Loop<<" "<<m_NoteCut<<" "<<endl;
  267. for (int n=0; n<NUM_PATTERNS; n++)
  268. {
  269. s<<m_Matrix[n].Length<<" "<<m_Matrix[n].Speed<<" "<<m_Matrix[n].Octave<<endl;
  270. for (int y=0; y<MATY; y++)
  271. {
  272. for (int x=0; x<MATX; x++)
  273. {
  274. if (m_Matrix[n].Matrix[x][y]) s<<x<<" "<<y<<" ";
  275. }
  276. }
  277. s<<"-1 ";
  278. }
  279. }
  280. void MatrixPlugin::StreamIn(istream &s)
  281. {
  282. int version;
  283. s>>version;
  284. switch (version)
  285. {
  286. case 1:
  287. {
  288. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  289. for (int n=0; n<NUM_PATTERNS; n++)
  290. {
  291. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  292. for (int y=0; y<MATY; y++)
  293. for (int x=0; x<MATX; x++)
  294. {
  295. s>>m_Matrix[n].Matrix[x][y];
  296. }
  297. }
  298. } break;
  299. case 2:
  300. {
  301. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  302. for (int n=0; n<NUM_PATTERNS; n++)
  303. {
  304. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  305. int x=0,y=0;
  306. while(x!=-1)
  307. {
  308. s>>x;
  309. if (x!=-1)
  310. {
  311. s>>y;
  312. if (y!=-1) m_Matrix[n].Matrix[x][y]=true;
  313. }
  314. }
  315. }
  316. } break;
  317. }
  318. }