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.

498 lines
12KB

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