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.

488 lines
11KB

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