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.

500 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 "../../NoteTable.h"
  23. using namespace std;
  24. extern "C" {
  25. SpiralPlugin* SpiralPlugin_CreateInstance()
  26. {
  27. return new MatrixPlugin;
  28. }
  29. char** SpiralPlugin_GetIcon()
  30. {
  31. return SpiralIcon_xpm;
  32. }
  33. int SpiralPlugin_GetID()
  34. {
  35. return 0x0012;
  36. }
  37. string SpiralPlugin_GetGroupName()
  38. {
  39. return "Sequencing";
  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->Register("EchoCur", &m_Current, ChannelHandler::OUTPUT);
  123. m_AudioCH->RegisterData("Matrix",ChannelHandler::OUTPUT_REQUEST,&m_Matrix,sizeof(m_Matrix));
  124. }
  125. MatrixPlugin::~MatrixPlugin()
  126. {
  127. }
  128. PluginInfo &MatrixPlugin::Initialise(const HostInfo *Host)
  129. {
  130. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  131. m_TickTime = 1.0f/(float)m_HostInfo->SAMPLERATE;
  132. return Info;
  133. }
  134. SpiralGUIType *MatrixPlugin::CreateGUI()
  135. {
  136. return new MatrixPluginGUI(m_PluginInfo.Width,
  137. m_PluginInfo.Height,
  138. this,m_AudioCH,m_HostInfo);
  139. }
  140. void MatrixPlugin::Execute()
  141. {
  142. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  143. {
  144. if (InputExists(1)) m_StepTime = GetInput(1,n);
  145. // inc time
  146. m_Time+=m_TickTime;
  147. SetOutputPitch(0,n,m_CurrentNoteCV);
  148. SetOutput(1,n,m_CurrentTriggerCV);
  149. // clear the pattern sync
  150. SetOutput(18, n, 0);
  151. bool ExternalClock = InputExists(4);
  152. bool ExternalClockTriggered=false;
  153. if (GetInputPitch(0,n)>0)
  154. {
  155. if (!m_Triggered)
  156. {
  157. float Freq=GetInputPitch(0,n);
  158. // Notes 0 to 16 trigger patterns 0 to 16
  159. // No other notes catered for
  160. for (int i=0; i<NUM_PATTERNS; i++)
  161. {
  162. if (feq(Freq,NoteTable[i],0.01f))
  163. {
  164. m_Current=i;
  165. break;
  166. }
  167. }
  168. // make it so the next note to trigger
  169. // will be the first one
  170. m_Time=m_StepTime*(1/m_Matrix[m_Current].Speed);
  171. m_Step=-1;
  172. ExternalClockTriggered=true;
  173. m_Triggered=true;
  174. }
  175. }
  176. else
  177. {
  178. m_Triggered=false;
  179. }
  180. // set the individual triggers
  181. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,m_TriggerLevel[t]);
  182. if (ExternalClock)
  183. {
  184. if (GetInput(4,n)>0)
  185. {
  186. if(!m_ClockHigh)
  187. {
  188. m_ClockHigh=true;
  189. ExternalClockTriggered=true;
  190. }
  191. }
  192. else
  193. {
  194. if (m_ClockHigh)
  195. {
  196. m_ClockHigh=false;
  197. ExternalClockTriggered=true;
  198. }
  199. }
  200. // reset the position on a signal from input 1
  201. if (InputExists(0) && GetInput(0,n)!=0)
  202. {
  203. m_Step=-1;
  204. ExternalClockTriggered=true;
  205. m_CurPatSeq++;
  206. if (m_PatSeq[m_CurPatSeq]==-1 || m_CurPatSeq==NUM_PATSEQ) m_CurPatSeq=0;
  207. m_Current=m_PatSeq[m_CurPatSeq];
  208. }
  209. }
  210. // An external clock pulse overrides the internal timing
  211. if ((!ExternalClock && m_Time>=m_StepTime*(1/m_Matrix[m_Current].Speed)) ||
  212. (ExternalClock && ExternalClockTriggered))
  213. {
  214. m_Time=0;
  215. m_Step++;
  216. if (m_Step >= m_Matrix[m_Current].Length)
  217. {
  218. SetOutput(18, n, 1);
  219. m_Step=0;
  220. m_CurPatSeq++;
  221. if (m_PatSeq[m_CurPatSeq]==-1 || m_CurPatSeq==NUM_PATSEQ) m_CurPatSeq=0;
  222. m_Current=m_PatSeq[m_CurPatSeq];
  223. }
  224. // Reset the values
  225. m_CurrentTriggerCV=0;
  226. if (m_NoteCut) m_CurrentNoteCV=0;
  227. for (int t=0; t<NUM_PATTERNS; t++)
  228. {
  229. SetOutput(t+2,n,0);
  230. m_TriggerLevel[t]=0;
  231. }
  232. // Scan the matrix at current time
  233. for (int i=0; i<MATY; i++)
  234. {
  235. if (m_Matrix[m_Current].Matrix[m_Step][i])
  236. {
  237. m_CurrentNoteCV=NoteTable[i+m_Matrix[m_Current].Octave*12];
  238. m_CurrentTriggerCV=m_Matrix[m_Current].Volume[m_Step][i];
  239. m_TriggerLevel[i]=m_Matrix[m_Current].Volume[m_Step][i];
  240. }
  241. }
  242. // reset the triggers between steps to clear them
  243. // otherwise consecutive events wont get triggered
  244. SetOutput(1,n,0);
  245. for (int t=0; t<NUM_PATTERNS; t++) SetOutput(t+2,n,0);
  246. }
  247. }
  248. }
  249. void MatrixPlugin::ExecuteCommands()
  250. {
  251. if (m_AudioCH->IsCommandWaiting())
  252. {
  253. switch (m_AudioCH->GetCommand())
  254. {
  255. case MAT_LENGTH :
  256. m_Matrix[m_GUICurrent].Length=m_GUIArgs.Length;
  257. break;
  258. case MAT_SPEED :
  259. m_Matrix[m_GUICurrent].Speed=m_GUIArgs.Speed;
  260. break;
  261. case MAT_ACTIVATE :
  262. m_Matrix[m_GUICurrent].Matrix[m_GUIArgs.X][m_GUIArgs.Y]=true;
  263. break;
  264. case MAT_DEACTIVATE :
  265. m_Matrix[m_GUICurrent].Matrix[m_GUIArgs.X][m_GUIArgs.Y]=false;
  266. break;
  267. case MAT_OCTAVE :
  268. m_Matrix[m_GUICurrent].Octave=m_GUIArgs.Octave;
  269. break;
  270. case COPY :
  271. m_CopyPattern = m_GUICurrent;
  272. break;
  273. case PASTE :
  274. PastePattern();
  275. break;
  276. case CLEAR :
  277. ClearPattern();
  278. break;
  279. case TUP :
  280. if (CanTransposeUp()) TransposeUp();
  281. break;
  282. case TDOWN :
  283. if (CanTransposeDown()) TransposeDown();
  284. break;
  285. case MAT_VOLUME :
  286. m_Matrix[m_GUICurrent].Volume[m_GUIArgs.X][m_GUIArgs.Y]=m_GUIArgs.Volume;
  287. break;
  288. case SET_CURRENT :
  289. m_Current=m_GUIArgs.Num;
  290. break;
  291. case SET_PATSEQ :
  292. m_PatSeq[m_GUIArgs.Y]=m_GUIArgs.Num;
  293. break;
  294. }
  295. }
  296. }
  297. void MatrixPlugin::PastePattern() {
  298. m_Matrix[m_GUICurrent].Length = m_Matrix[m_CopyPattern].Length;
  299. m_Matrix[m_GUICurrent].Speed = m_Matrix[m_CopyPattern].Speed;
  300. m_Matrix[m_GUICurrent].Octave = m_Matrix[m_CopyPattern].Octave;
  301. for (int y=0; y<MATY; y++) {
  302. for (int x=0; x<MATX; x++) {
  303. m_Matrix[m_GUICurrent].Matrix[x][y] = m_Matrix[m_CopyPattern].Matrix[x][y];
  304. }
  305. }
  306. }
  307. void MatrixPlugin::ClearPattern() {
  308. for (int y=0; y<MATY; y++) {
  309. for (int x=0; x<MATX; x++) {
  310. m_Matrix[m_GUICurrent].Matrix[x][y] = 0;
  311. }
  312. }
  313. }
  314. void MatrixPlugin::TransposeUp() {
  315. int x, y;
  316. for (y=MATY-1; y>=0; y--) {
  317. for (x=0; x<MATX; x++) {
  318. m_Matrix[m_GUICurrent].Matrix[x][y] = m_Matrix[m_GUICurrent].Matrix[x][y-1];
  319. }
  320. }
  321. for (x=0; x<MATX; x++) {
  322. m_Matrix[m_GUICurrent].Matrix[x][0] = 0;
  323. }
  324. }
  325. void MatrixPlugin::TransposeDown()
  326. {
  327. int x, y;
  328. for (y=0; y<MATY-1; y++)
  329. {
  330. for (x=0; x<MATX; x++)
  331. {
  332. m_Matrix[m_GUICurrent].Matrix[x][y] = m_Matrix[m_GUICurrent].Matrix[x][y+1];
  333. }
  334. }
  335. for (x=0; x<MATX; x++)
  336. {
  337. m_Matrix[m_GUICurrent].Matrix[x][MATY-1] = 0;
  338. }
  339. }
  340. bool MatrixPlugin::CanTransposeDown()
  341. {
  342. for (int x=0; x<MATX; x++) if (m_Matrix[m_GUICurrent].Matrix[x][0]) return false;
  343. return true;
  344. }
  345. bool MatrixPlugin::CanTransposeUp()
  346. {
  347. for (int x=0; x<MATX; x++) if (m_Matrix[m_Current].Matrix[x][MATY-1]) return false;
  348. return true;
  349. }
  350. void MatrixPlugin::StreamOut(ostream &s)
  351. {
  352. s<<m_Version<<" ";
  353. s<<m_Current<<" "<<m_Time<<" "<<m_Step<<" "<<m_Loop<<" "<<m_NoteCut<<" "<<endl;
  354. for (int n=0; n<NUM_PATTERNS; n++)
  355. {
  356. s<<m_Matrix[n].Length<<" "<<m_Matrix[n].Speed<<" "<<m_Matrix[n].Octave<<endl;
  357. for (int y=0; y<MATY; y++)
  358. {
  359. for (int x=0; x<MATX; x++)
  360. {
  361. if (m_Matrix[n].Matrix[x][y]) s<<x<<" "<<y<<" "<<m_Matrix[n].Volume[x][y]<<" ";
  362. }
  363. }
  364. s<<"-1 ";
  365. }
  366. s<<endl;
  367. for (int n=0; n<NUM_PATSEQ; n++)
  368. {
  369. s<<m_PatSeq[n]<<" ";
  370. }
  371. }
  372. void MatrixPlugin::StreamIn(istream &s)
  373. {
  374. int version;
  375. s>>version;
  376. switch (version)
  377. {
  378. case 1:
  379. {
  380. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  381. for (int n=0; n<NUM_PATTERNS; n++)
  382. {
  383. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  384. for (int y=0; y<MATY; y++)
  385. for (int x=0; x<MATX; x++)
  386. {
  387. s>>m_Matrix[n].Matrix[x][y];
  388. }
  389. }
  390. } break;
  391. case 2:
  392. {
  393. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  394. for (int n=0; n<NUM_PATTERNS; n++)
  395. {
  396. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  397. int x=0,y=0;
  398. while(x!=-1)
  399. {
  400. s>>x;
  401. if (x!=-1)
  402. {
  403. s>>y;
  404. if (y!=-1) m_Matrix[n].Matrix[x][y]=true;
  405. }
  406. }
  407. }
  408. } break;
  409. case 3:
  410. case 4:
  411. {
  412. s>>m_Current>>m_Time>>m_Step>>m_Loop>>m_NoteCut;
  413. for (int n=0; n<NUM_PATTERNS; n++)
  414. {
  415. s>>m_Matrix[n].Length>>m_Matrix[n].Speed>>m_Matrix[n].Octave;
  416. int x=0,y=0;
  417. float v;
  418. while(x!=-1)
  419. {
  420. s>>x;
  421. if (x!=-1)
  422. {
  423. s>>y>>v;
  424. if (y!=-1)
  425. {
  426. m_Matrix[n].Matrix[x][y]=true;
  427. m_Matrix[n].Volume[x][y]=v;
  428. }
  429. }
  430. }
  431. }
  432. if (version>3)
  433. {
  434. for (int n=0; n<NUM_PATSEQ; n++)
  435. {
  436. s>>m_PatSeq[n];
  437. }
  438. }
  439. } break;
  440. }
  441. }