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.

418 lines
9.9KB

  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. m_Version=2;
  54. m_PluginInfo.Name="Matrix";
  55. m_PluginInfo.Width=555;
  56. m_PluginInfo.Height=270;
  57. m_PluginInfo.NumInputs=5;
  58. m_PluginInfo.NumOutputs=19;
  59. m_PluginInfo.PortTips.push_back("Play Trigger");
  60. m_PluginInfo.PortTips.push_back("StepTime CV");
  61. m_PluginInfo.PortTips.push_back("Input Pitch CV");
  62. m_PluginInfo.PortTips.push_back("Input Trigger CV");
  63. m_PluginInfo.PortTips.push_back("External Clock");
  64. m_PluginInfo.PortTips.push_back("Output Pitch");
  65. m_PluginInfo.PortTips.push_back("Output Trigger");
  66. m_PluginInfo.PortTips.push_back("Trigger 1");
  67. m_PluginInfo.PortTips.push_back("Trigger 2");
  68. m_PluginInfo.PortTips.push_back("Trigger 3");
  69. m_PluginInfo.PortTips.push_back("Trigger 4");
  70. m_PluginInfo.PortTips.push_back("Trigger 5");
  71. m_PluginInfo.PortTips.push_back("Trigger 6");
  72. m_PluginInfo.PortTips.push_back("Trigger 7");
  73. m_PluginInfo.PortTips.push_back("Trigger 8");
  74. m_PluginInfo.PortTips.push_back("Trigger 9");
  75. m_PluginInfo.PortTips.push_back("Trigger 10");
  76. m_PluginInfo.PortTips.push_back("Trigger 11");
  77. m_PluginInfo.PortTips.push_back("Trigger 12");
  78. m_PluginInfo.PortTips.push_back("Trigger 13");
  79. m_PluginInfo.PortTips.push_back("Trigger 14");
  80. m_PluginInfo.PortTips.push_back("Trigger 15");
  81. m_PluginInfo.PortTips.push_back("Trigger 16");
  82. m_PluginInfo.PortTips.push_back("Reset Trigger");
  83. for (int n=0; n<NUM_PATTERNS; n++)
  84. {
  85. m_Matrix[n].Length=32;
  86. m_Matrix[n].Speed=1.0f;
  87. m_Matrix[n].Octave=0;
  88. for (int x=0; x<MATX; x++)
  89. for (int y=0; y<MATY; y++)
  90. {
  91. m_Matrix[n].Matrix[x][y]=false;
  92. }
  93. m_TriggerLevel[n]=0;
  94. }
  95. m_AudioCH->Register("NoteCut",&m_NoteCut,ChannelHandler::INPUT);
  96. m_AudioCH->Register("Current",&m_Current,ChannelHandler::INPUT);
  97. m_AudioCH->Register("StepTime",&m_StepTime,ChannelHandler::INPUT);
  98. m_AudioCH->Register("Num",&m_GUIArgs.Num,ChannelHandler::INPUT);
  99. m_AudioCH->Register("Length",&m_GUIArgs.Length,ChannelHandler::INPUT);
  100. m_AudioCH->Register("Speed",&m_GUIArgs.Speed,ChannelHandler::INPUT);
  101. m_AudioCH->Register("X",&m_GUIArgs.X,ChannelHandler::INPUT);
  102. m_AudioCH->Register("Y",&m_GUIArgs.Y,ChannelHandler::INPUT);
  103. m_AudioCH->Register("Octave",&m_GUIArgs.Octave,ChannelHandler::INPUT);
  104. m_AudioCH->Register("Step",&m_Step,ChannelHandler::OUTPUT);
  105. m_AudioCH->RegisterData("Matrix",ChannelHandler::OUTPUT_REQUEST,&m_Matrix,sizeof(m_Matrix));
  106. }
  107. MatrixPlugin::~MatrixPlugin()
  108. {
  109. }
  110. PluginInfo &MatrixPlugin::Initialise(const HostInfo *Host)
  111. {
  112. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  113. m_TickTime = 1.0f/(float)m_HostInfo->SAMPLERATE;
  114. return Info;
  115. }
  116. SpiralGUIType *MatrixPlugin::CreateGUI()
  117. {
  118. return new MatrixPluginGUI(m_PluginInfo.Width,
  119. m_PluginInfo.Height,
  120. this,m_AudioCH,m_HostInfo);
  121. }
  122. void MatrixPlugin::Execute()
  123. {
  124. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  125. {
  126. if (InputExists(1)) m_StepTime = GetInput(1,n);
  127. // inc time
  128. m_Time+=m_TickTime;
  129. SetOutputPitch(0,n,m_CurrentNoteCV);
  130. SetOutput(1,n,m_CurrentTriggerCV);
  131. if (m_Step+1 >= m_Matrix[m_Current].Length) SetOutput(18, n, 1);
  132. else SetOutput(18, n, 0);
  133. if (GetInputPitch(0,n)>0)
  134. {
  135. if (!m_Triggered)
  136. {
  137. float Freq=GetInputPitch(0,n);
  138. // Notes 0 to 16 trigger patterns 0 to 16
  139. // No other notes catered for
  140. for (int i=0; i<NUM_PATTERNS; i++)
  141. {
  142. if (feq(Freq,NoteTable[i],0.01f))
  143. {
  144. m_Current=i;
  145. break;
  146. }
  147. }
  148. // make it so the next note to trigger
  149. // will be the first one
  150. //if (m_GUI) ((MatrixPluginGUI*)m_GUI)->UpdateValues();
  151. m_Time=m_StepTime*(1/m_Matrix[m_Current].Speed);
  152. m_Step=-1;
  153. m_Triggered=true;
  154. }
  155. }
  156. else
  157. {
  158. m_Triggered=false;
  159. }
  160. // set the individual triggers
  161. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,m_TriggerLevel[t]);
  162. bool ExternalClock = InputExists(4);
  163. bool ExternalClockTriggered=false;
  164. if (ExternalClock)
  165. {
  166. if (GetInput(4,n)>0)
  167. {
  168. if(!m_ClockHigh)
  169. {
  170. m_ClockHigh=true;
  171. ExternalClockTriggered=true;
  172. }
  173. }
  174. else
  175. {
  176. if (m_ClockHigh)
  177. {
  178. m_ClockHigh=false;
  179. ExternalClockTriggered=true;
  180. }
  181. }
  182. }
  183. // An external clock pulse overrides the internal timing
  184. if ((!ExternalClock && m_Time>=m_StepTime*(1/m_Matrix[m_Current].Speed)) ||
  185. (ExternalClock && ExternalClockTriggered))
  186. {
  187. m_Time=0;
  188. m_Step++;
  189. if (m_Step >= m_Matrix[m_Current].Length) m_Step=0;
  190. //if (m_GUI) ((MatrixPluginGUI*)m_GUI)->SetLED(m_Step);
  191. // Reset the values
  192. m_CurrentTriggerCV=0;
  193. if (m_NoteCut) m_CurrentNoteCV=0;
  194. for (int t=0; t<NUM_PATTERNS; t++)
  195. {
  196. SetOutput(t+2,n,0);
  197. m_TriggerLevel[t]=0;
  198. }
  199. // Scan the matrix at current time
  200. for (int i=0; i<MATY; i++)
  201. {
  202. if (m_Matrix[m_Current].Matrix[m_Step][i])
  203. {
  204. m_CurrentNoteCV=NoteTable[i+m_Matrix[m_Current].Octave*12];
  205. m_CurrentTriggerCV=1;
  206. m_TriggerLevel[i]=1;
  207. }
  208. }
  209. // reset the triggers between steps to clear them
  210. // otherwise consecutive events wont get triggered
  211. SetOutput(1,n,0);
  212. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,0);
  213. }
  214. }
  215. }
  216. void MatrixPlugin::ExecuteCommands()
  217. {
  218. if (m_AudioCH->IsCommandWaiting())
  219. {
  220. switch (m_AudioCH->GetCommand())
  221. {
  222. case MAT_LENGTH :
  223. cerr<<m_GUIArgs.Length<<endl;
  224. m_Matrix[m_Current].Length=m_GUIArgs.Length;
  225. break;
  226. case MAT_SPEED :
  227. m_Matrix[m_Current].Speed=m_GUIArgs.Speed;
  228. break;
  229. case MAT_ACTIVATE :
  230. m_Matrix[m_Current].Matrix[m_GUIArgs.X][m_GUIArgs.Y]=true;
  231. break;
  232. case MAT_DEACTIVATE :
  233. m_Matrix[m_Current].Matrix[m_GUIArgs.X][m_GUIArgs.Y]=false;
  234. break;
  235. case MAT_OCTAVE :
  236. m_Matrix[m_Current].Octave=m_GUIArgs.Octave;
  237. break;
  238. case COPY :
  239. CopyPattern();
  240. break;
  241. case PASTE :
  242. PastePattern();
  243. break;
  244. case CLEAR :
  245. ClearPattern();
  246. break;
  247. case TUP :
  248. if (CanTransposeUp()) TransposeUp();
  249. break;
  250. case TDOWN :
  251. if (CanTransposeDown()) TransposeDown();
  252. break;
  253. }
  254. }
  255. }
  256. void MatrixPlugin::PastePattern() {
  257. m_Matrix[m_Current].Length = m_Matrix[m_CopyPattern].Length;
  258. m_Matrix[m_Current].Speed = m_Matrix[m_CopyPattern].Speed;
  259. m_Matrix[m_Current].Octave = m_Matrix[m_CopyPattern].Octave;
  260. for (int y=0; y<MATY; y++) {
  261. for (int x=0; x<MATX; x++) {
  262. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_CopyPattern].Matrix[x][y];
  263. }
  264. }
  265. }
  266. void MatrixPlugin::ClearPattern() {
  267. for (int y=0; y<MATY; y++) {
  268. for (int x=0; x<MATX; x++) {
  269. m_Matrix[m_Current].Matrix[x][y] = 0;
  270. }
  271. }
  272. }
  273. void MatrixPlugin::TransposeUp() {
  274. int x, y;
  275. for (y=MATY-1; y>=0; y--) {
  276. for (x=0; x<MATX; x++) {
  277. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_Current].Matrix[x][y-1];
  278. }
  279. }
  280. for (x=0; x<MATX; x++) {
  281. m_Matrix[m_Current].Matrix[x][0] = 0;
  282. }
  283. }
  284. void MatrixPlugin::TransposeDown()
  285. {
  286. int x, y;
  287. for (y=0; y<MATY-1; y++)
  288. {
  289. for (x=0; x<MATX; x++)
  290. {
  291. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_Current].Matrix[x][y+1];
  292. }
  293. }
  294. for (x=0; x<MATX; x++)
  295. {
  296. m_Matrix[m_Current].Matrix[x][MATY-1] = 0;
  297. }
  298. }
  299. bool MatrixPlugin::CanTransposeDown()
  300. {
  301. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][0]) return False;
  302. return True;
  303. }
  304. bool MatrixPlugin::CanTransposeUp()
  305. {
  306. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][MATY-1]) return False;
  307. return True;
  308. }
  309. void MatrixPlugin::StreamOut(ostream &s)
  310. {
  311. s<<m_Version<<" ";
  312. s<<m_Current<<" "<<m_Time<<" "<<m_Step<<" "<<m_Loop<<" "<<m_NoteCut<<" "<<endl;
  313. for (int n=0; n<NUM_PATTERNS; n++)
  314. {
  315. s<<m_Matrix[n].Length<<" "<<m_Matrix[n].Speed<<" "<<m_Matrix[n].Octave<<endl;
  316. for (int y=0; y<MATY; y++)
  317. {
  318. for (int x=0; x<MATX; x++)
  319. {
  320. if (m_Matrix[n].Matrix[x][y]) s<<x<<" "<<y<<" ";
  321. }
  322. }
  323. s<<"-1 ";
  324. }
  325. }
  326. void MatrixPlugin::StreamIn(istream &s)
  327. {
  328. int version;
  329. s>>version;
  330. switch (version)
  331. {
  332. case 1:
  333. {
  334. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  335. for (int n=0; n<NUM_PATTERNS; n++)
  336. {
  337. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  338. for (int y=0; y<MATY; y++)
  339. for (int x=0; x<MATX; x++)
  340. {
  341. s>>m_Matrix[n].Matrix[x][y];
  342. }
  343. }
  344. } break;
  345. case 2:
  346. {
  347. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  348. for (int n=0; n<NUM_PATTERNS; n++)
  349. {
  350. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  351. int x=0,y=0;
  352. while(x!=-1)
  353. {
  354. s>>x;
  355. if (x!=-1)
  356. {
  357. s>>y;
  358. if (y!=-1) m_Matrix[n].Matrix[x][y]=true;
  359. }
  360. }
  361. }
  362. } break;
  363. }
  364. }