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.

492 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=560;
  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. // clear the pattern sync
  138. 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)
  234. {
  235. SetOutput(18, n, 1);
  236. m_Step=0;
  237. }
  238. // Reset the values
  239. m_CurrentTriggerCV=0;
  240. if (m_NoteCut) m_CurrentNoteCV=0;
  241. for (int t=0; t<NUM_PATTERNS; t++)
  242. {
  243. SetOutput(t+2,n,0);
  244. m_TriggerLevel[t]=0;
  245. }
  246. // Scan the matrix at current time
  247. for (int i=0; i<MATY; i++)
  248. {
  249. if (m_Matrix[m_Current].Matrix[m_Step][i])
  250. {
  251. m_CurrentNoteCV=NoteTable[i+m_Matrix[m_Current].Octave*12];
  252. m_CurrentTriggerCV=m_Matrix[m_Current].Volume[m_Step][i];
  253. m_TriggerLevel[i]=m_Matrix[m_Current].Volume[m_Step][i];
  254. }
  255. }
  256. // reset the triggers between steps to clear them
  257. // otherwise consecutive events wont get triggered
  258. SetOutput(1,n,0);
  259. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,0);
  260. }
  261. }
  262. }
  263. void MatrixPlugin::ExecuteCommands()
  264. {
  265. if (m_AudioCH->IsCommandWaiting())
  266. {
  267. switch (m_AudioCH->GetCommand())
  268. {
  269. case MAT_LENGTH :
  270. m_Matrix[m_Current].Length=m_GUIArgs.Length;
  271. break;
  272. case MAT_SPEED :
  273. m_Matrix[m_Current].Speed=m_GUIArgs.Speed/8.0f;
  274. break;
  275. case MAT_ACTIVATE :
  276. m_Matrix[m_Current].Matrix[m_GUIArgs.X][m_GUIArgs.Y]=true;
  277. break;
  278. case MAT_DEACTIVATE :
  279. m_Matrix[m_Current].Matrix[m_GUIArgs.X][m_GUIArgs.Y]=false;
  280. break;
  281. case MAT_OCTAVE :
  282. m_Matrix[m_Current].Octave=m_GUIArgs.Octave;
  283. break;
  284. case COPY :
  285. CopyPattern();
  286. break;
  287. case PASTE :
  288. PastePattern();
  289. break;
  290. case CLEAR :
  291. ClearPattern();
  292. break;
  293. case TUP :
  294. if (CanTransposeUp()) TransposeUp();
  295. break;
  296. case TDOWN :
  297. if (CanTransposeDown()) TransposeDown();
  298. break;
  299. case MAT_VOLUME :
  300. m_Matrix[m_Current].Volume[m_GUIArgs.X][m_GUIArgs.Y]=m_GUIArgs.Volume;
  301. break;
  302. }
  303. }
  304. }
  305. void MatrixPlugin::PastePattern() {
  306. m_Matrix[m_Current].Length = m_Matrix[m_CopyPattern].Length;
  307. m_Matrix[m_Current].Speed = m_Matrix[m_CopyPattern].Speed;
  308. m_Matrix[m_Current].Octave = m_Matrix[m_CopyPattern].Octave;
  309. for (int y=0; y<MATY; y++) {
  310. for (int x=0; x<MATX; x++) {
  311. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_CopyPattern].Matrix[x][y];
  312. }
  313. }
  314. }
  315. void MatrixPlugin::ClearPattern() {
  316. for (int y=0; y<MATY; y++) {
  317. for (int x=0; x<MATX; x++) {
  318. m_Matrix[m_Current].Matrix[x][y] = 0;
  319. }
  320. }
  321. }
  322. void MatrixPlugin::TransposeUp() {
  323. int x, y;
  324. for (y=MATY-1; y>=0; y--) {
  325. for (x=0; x<MATX; x++) {
  326. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_Current].Matrix[x][y-1];
  327. }
  328. }
  329. for (x=0; x<MATX; x++) {
  330. m_Matrix[m_Current].Matrix[x][0] = 0;
  331. }
  332. }
  333. void MatrixPlugin::TransposeDown()
  334. {
  335. int x, y;
  336. for (y=0; y<MATY-1; y++)
  337. {
  338. for (x=0; x<MATX; x++)
  339. {
  340. m_Matrix[m_Current].Matrix[x][y] = m_Matrix[m_Current].Matrix[x][y+1];
  341. }
  342. }
  343. for (x=0; x<MATX; x++)
  344. {
  345. m_Matrix[m_Current].Matrix[x][MATY-1] = 0;
  346. }
  347. }
  348. bool MatrixPlugin::CanTransposeDown()
  349. {
  350. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][0]) return False;
  351. return True;
  352. }
  353. bool MatrixPlugin::CanTransposeUp()
  354. {
  355. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][MATY-1]) return False;
  356. return True;
  357. }
  358. void MatrixPlugin::StreamOut(ostream &s)
  359. {
  360. s<<m_Version<<" ";
  361. s<<m_Current<<" "<<m_Time<<" "<<m_Step<<" "<<m_Loop<<" "<<m_NoteCut<<" "<<endl;
  362. for (int n=0; n<NUM_PATTERNS; n++)
  363. {
  364. s<<m_Matrix[n].Length<<" "<<m_Matrix[n].Speed<<" "<<m_Matrix[n].Octave<<endl;
  365. for (int y=0; y<MATY; y++)
  366. {
  367. for (int x=0; x<MATX; x++)
  368. {
  369. if (m_Matrix[n].Matrix[x][y]) s<<x<<" "<<y<<" "<<m_Matrix[n].Volume[x][y]<<" ";
  370. }
  371. }
  372. s<<"-1 ";
  373. }
  374. }
  375. void MatrixPlugin::StreamIn(istream &s)
  376. {
  377. int version;
  378. s>>version;
  379. switch (version)
  380. {
  381. case 1:
  382. {
  383. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  384. for (int n=0; n<NUM_PATTERNS; n++)
  385. {
  386. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  387. for (int y=0; y<MATY; y++)
  388. for (int x=0; x<MATX; x++)
  389. {
  390. s>>m_Matrix[n].Matrix[x][y];
  391. }
  392. }
  393. } break;
  394. case 2:
  395. {
  396. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  397. for (int n=0; n<NUM_PATTERNS; n++)
  398. {
  399. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  400. int x=0,y=0;
  401. while(x!=-1)
  402. {
  403. s>>x;
  404. if (x!=-1)
  405. {
  406. s>>y;
  407. if (y!=-1) m_Matrix[n].Matrix[x][y]=true;
  408. }
  409. }
  410. }
  411. } break;
  412. case 3:
  413. {
  414. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  415. for (int n=0; n<NUM_PATTERNS; n++)
  416. {
  417. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  418. int x=0,y=0;
  419. float v;
  420. while(x!=-1)
  421. {
  422. s>>x;
  423. if (x!=-1)
  424. {
  425. s>>y>>v;
  426. if (y!=-1)
  427. {
  428. m_Matrix[n].Matrix[x][y]=true;
  429. m_Matrix[n].Volume[x][y]=v;
  430. }
  431. }
  432. }
  433. }
  434. } break;
  435. }
  436. }