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.

599 lines
13KB

  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 "SpiralLoopPlugin.h"
  19. #include "SpiralLoopPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../RiffWav.h"
  23. #include "../../NoteTable.h"
  24. static const float TRIG_THRESH = 0.1;
  25. static const int RECBUFFERSIZE = 16384;
  26. static const float RECORD_GAIN = 1.0f;
  27. extern "C" {
  28. SpiralPlugin* CreateInstance()
  29. {
  30. return new SpiralLoopPlugin;
  31. }
  32. char** GetIcon()
  33. {
  34. return SpiralIcon_xpm;
  35. }
  36. int GetID()
  37. {
  38. return 0x001a;
  39. }
  40. string GetGroupName()
  41. {
  42. return "SpiralSound";
  43. }
  44. }
  45. ///////////////////////////////////////////////////////
  46. SpiralLoopPlugin::SpiralLoopPlugin() :
  47. m_Id(0),
  48. m_Pos(0),
  49. m_IntPos(0),
  50. m_PlayBufPos(0),
  51. m_Playing(true),
  52. m_Recording(false),
  53. m_DelMe(false),
  54. m_LoopPoint(0),
  55. m_Speed(1.0f),
  56. m_Volume(1.0f),
  57. m_RecordingSource(NULL),
  58. m_FirstRecord(true),
  59. m_FixedRecord(false),
  60. m_RecLength(0),
  61. m_TickTime(0),
  62. m_TickCurrent(0),
  63. m_TicksPerLoop(64),
  64. m_TickOutput(1.0f),
  65. m_Triggered(false)
  66. {
  67. m_PluginInfo.Name="SpiralLoop";
  68. m_PluginInfo.Width=300;
  69. m_PluginInfo.Height=320;
  70. m_PluginInfo.NumInputs=2;
  71. m_PluginInfo.NumOutputs=10;
  72. m_PluginInfo.PortTips.push_back("Input");
  73. m_PluginInfo.PortTips.push_back("Play Trigger");
  74. m_PluginInfo.PortTips.push_back("Output");
  75. m_PluginInfo.PortTips.push_back("Clock");
  76. m_PluginInfo.PortTips.push_back("LoopTrigger 0");
  77. m_PluginInfo.PortTips.push_back("LoopTrigger 1");
  78. m_PluginInfo.PortTips.push_back("LoopTrigger 2");
  79. m_PluginInfo.PortTips.push_back("LoopTrigger 3");
  80. m_PluginInfo.PortTips.push_back("LoopTrigger 4");
  81. m_PluginInfo.PortTips.push_back("LoopTrigger 5");
  82. m_PluginInfo.PortTips.push_back("LoopTrigger 6");
  83. m_PluginInfo.PortTips.push_back("LoopTrigger 7");
  84. m_AudioCH->Register("TicksPerLoop",&m_TicksPerLoop);
  85. m_AudioCH->Register("Volume",&m_Volume);
  86. m_AudioCH->Register("Speed",&m_Speed);
  87. m_AudioCH->Register("Length",&m_GUIArgs.Length);
  88. m_AudioCH->Register("Start",&m_GUIArgs.Start);
  89. m_AudioCH->Register("End",&m_GUIArgs.End);
  90. m_AudioCH->Register("Pos",&m_Pos,ChannelHandler::OUTPUT);
  91. m_AudioCH->RegisterData("Name",ChannelHandler::INPUT,&m_GUIArgs.Name,sizeof(m_GUIArgs.Name));
  92. m_AudioCH->RegisterData("SampleBuffer",ChannelHandler::OUTPUT_REQUEST,&m_SampleBuffer,TRANSBUF_SIZE);
  93. m_AudioCH->Register("SampleSize",&m_SampleSize,ChannelHandler::OUTPUT_REQUEST);
  94. m_Version=2;
  95. }
  96. SpiralLoopPlugin::~SpiralLoopPlugin()
  97. {
  98. }
  99. PluginInfo &SpiralLoopPlugin::Initialise(const HostInfo *Host)
  100. {
  101. return SpiralPlugin::Initialise(Host);
  102. }
  103. SpiralGUIType *SpiralLoopPlugin::CreateGUI()
  104. {
  105. return new SpiralLoopPluginGUI(m_PluginInfo.Width,
  106. m_PluginInfo.Height,
  107. this,m_AudioCH,m_HostInfo);
  108. }
  109. void SpiralLoopPlugin::Execute()
  110. {
  111. if (InputExists(0)) SetRecordingSource(GetInput(0)->GetBuffer());
  112. else SetRecordingSource(NULL);
  113. for (int n=0; n<8; n++) GetOutputBuf(n+1)->Zero();
  114. // get the triggers active this frame
  115. for (vector<TriggerInfo>::iterator i=m_TriggerVec.begin();
  116. i!=m_TriggerVec.end(); i++)
  117. {
  118. if (m_Pos > i->Time*m_StoreBuffer.GetLength() &&
  119. !i->Triggered)
  120. {
  121. GetOutputBuf(i->Channel+2)->Set(1);
  122. i->Triggered=true;
  123. }
  124. }
  125. if (GetOutput(*GetOutputBuf(0)))
  126. {
  127. // if it's looped - reset the triggers
  128. for (vector<TriggerInfo>::iterator i=m_TriggerVec.begin();
  129. i!=m_TriggerVec.end(); i++)
  130. {
  131. i->Triggered=false;
  132. }
  133. m_TickCurrent=m_TickTime;
  134. }
  135. if (GetInput(1,0)>TRIG_THRESH)
  136. {
  137. if (!m_Triggered)
  138. {
  139. Trigger();
  140. m_Triggered=true;
  141. }
  142. }
  143. else m_Triggered=false;
  144. m_TickCurrent+=m_HostInfo->BUFSIZE;
  145. if (m_TickCurrent>=m_TickTime)
  146. {
  147. m_TickOutput=-m_TickOutput;
  148. m_TickTime=m_StoreBuffer.GetLength()/m_TicksPerLoop;
  149. m_TickCurrent=0;
  150. }
  151. GetOutputBuf(1)->Set(m_TickOutput);
  152. }
  153. void SpiralLoopPlugin::ExecuteCommands()
  154. {
  155. if (m_AudioCH->IsCommandWaiting())
  156. {
  157. switch(m_AudioCH->GetCommand())
  158. {
  159. case START : SetPlaying(true); break;
  160. case STOP : SetPlaying(false); break;
  161. case RECORD : Clear(); Record(true); break;
  162. case OVERDUB : Record(true); break;
  163. case ENDRECORD : Record(false); break;
  164. case LOAD : LoadWav(m_GUIArgs.Name); break;
  165. case SAVE : SaveWav(m_GUIArgs.Name); break;
  166. case CUT : Cut(m_GUIArgs.Start, m_GUIArgs.End); break;
  167. case COPY : Copy(m_GUIArgs.Start, m_GUIArgs.End); break;
  168. case PASTE : Paste(m_GUIArgs.Start); break;
  169. case PASTEMIX : PasteMix(m_GUIArgs.Start); break;
  170. case ZERO_RANGE : ZeroRange(m_GUIArgs.Start, m_GUIArgs.End); break;
  171. case REVERSE_RANGE : ReverseRange(m_GUIArgs.Start, m_GUIArgs.End); break;
  172. case SELECT_ALL : SelectAll(); break;
  173. case DOUBLE : Double(); break;
  174. case HALF : Halve(); break;
  175. case MOVE : Move(m_GUIArgs.Start); break;
  176. case CROP : Crop(); break;
  177. case KEEPDUB : MixDub(); break;
  178. case UNDODUB : ClearDub(); break;
  179. case CHANGE_LENGTH : m_LoopPoint=(int)(m_StoreBuffer.GetLength()*m_GUIArgs.Length); break;
  180. case NEW_TRIGGER :
  181. {
  182. TriggerInfo NewTrigger;
  183. NewTrigger.Channel = m_GUIArgs.End;
  184. NewTrigger.Time = m_GUIArgs.Length;
  185. if ((int)m_TriggerVec.size()!=m_GUIArgs.Start) cerr<<"no of triggers error!"<<endl;
  186. m_TriggerVec.push_back(NewTrigger); break;
  187. }
  188. case UPDATE_TRIGGER :
  189. {
  190. m_TriggerVec[m_GUIArgs.Start].Channel = m_GUIArgs.End;
  191. m_TriggerVec[m_GUIArgs.Start].Time = m_GUIArgs.Length;
  192. }
  193. case GETSAMPLE :
  194. {
  195. m_AudioCH->SetupBulkTransfer((void*)m_StoreBuffer.GetBuffer());
  196. m_SampleSize=m_StoreBuffer.GetLength();
  197. } break;
  198. }
  199. }
  200. }
  201. void SpiralLoopPlugin::StreamOut(ostream &s)
  202. {
  203. s<<m_Version;
  204. s<<m_LoopPoint<<" "<<m_Speed<<" "<<m_Volume<<" "<<m_TicksPerLoop<<" ";
  205. s<<m_TriggerVec.size()<<" ";
  206. for (vector<TriggerInfo>::iterator i=m_TriggerVec.begin();
  207. i!=m_TriggerVec.end(); i++)
  208. {
  209. s<<i->Channel<<" "<<i->Time<<" ";
  210. }
  211. }
  212. void SpiralLoopPlugin::StreamIn(istream &s)
  213. {
  214. int version;
  215. s>>version;
  216. s>>m_LoopPoint>>m_Speed>>m_Volume>>m_TicksPerLoop;
  217. int size;
  218. s>>size;
  219. for (int n=0; n<size; n++)
  220. {
  221. TriggerInfo t;
  222. s>>t.Channel>>t.Time;
  223. m_TriggerVec.push_back(t);
  224. }
  225. }
  226. bool SpiralLoopPlugin::SaveExternalFiles(const string &Dir)
  227. {
  228. char temp[256];
  229. sprintf(temp,"%sSpiralLoopSample%d.wav",Dir.c_str(),GetID());
  230. SaveWav(temp);
  231. return true;
  232. }
  233. void SpiralLoopPlugin::LoadExternalFiles(const string &Dir)
  234. {
  235. char temp[256];
  236. sprintf(temp,"%sSpiralLoopSample%d.wav",Dir.c_str(),GetID());
  237. LoadWav(temp);
  238. }
  239. void SpiralLoopPlugin::LoadWav(const char *Filename)
  240. {
  241. WavFile wav;
  242. if (wav.Open(Filename, WavFile::READ))
  243. {
  244. Clear();
  245. AllocateMem(wav.GetSize());
  246. wav.Load(m_StoreBuffer);
  247. }
  248. }
  249. void SpiralLoopPlugin::SaveWav(const char *Filename)
  250. {
  251. WavFile wav;
  252. if (wav.Open(Filename, WavFile::WRITE, WavFile::MONO))
  253. {
  254. wav.Save(m_StoreBuffer);
  255. }
  256. m_Sample=Filename;
  257. }
  258. bool SpiralLoopPlugin::GetOutput(Sample &data)
  259. {
  260. if (!m_Recording && !m_Playing)
  261. {
  262. return false;
  263. }
  264. if (!m_Recording && m_StoreBuffer.GetLength()==0)
  265. {
  266. return false;
  267. }
  268. if (m_FixedRecord && m_DubBuffer.GetLength()!=m_StoreBuffer.GetLength())
  269. {
  270. cerr<<"eek! dub and store buffers don't match!"<<endl;
  271. }
  272. if (m_Recording)
  273. {
  274. RecordBuf(m_Pos, data.GetLength());
  275. if (!m_StoreBuffer.GetLength())
  276. {
  277. return false;
  278. }
  279. }
  280. int Pos;
  281. bool ret=false;
  282. for (int n=0; n<data.GetLength(); n++)
  283. {
  284. Pos=static_cast<int>(m_Pos);
  285. // brute force fix
  286. if (Pos>0 && Pos<m_LoopPoint)
  287. {
  288. data.Set(n,m_StoreBuffer[m_Pos]+m_DubBuffer[m_Pos]*m_Volume);
  289. }
  290. else data.Set(n,0);
  291. m_Pos+=m_Speed;
  292. if (static_cast<int>(m_Pos)>=m_LoopPoint)
  293. {
  294. ret=true;
  295. m_Pos=0;
  296. }
  297. }
  298. m_IntPos=static_cast<int>(m_Pos);
  299. return ret;
  300. }
  301. void SpiralLoopPlugin::AllocateMem(int Length)
  302. {
  303. // We might need to keep these values (if loading workspace)
  304. if (m_LoopPoint>Length) m_LoopPoint=Length;
  305. if (m_Pos>Length) m_Pos=0;
  306. if (m_LoopPoint==0) m_LoopPoint=Length;
  307. if (!m_StoreBuffer.Allocate(Length) ||
  308. !m_DubBuffer.Allocate(Length))
  309. {
  310. cerr<<"AllocateMem can't allocate any more memory!"<<endl;
  311. Clear();
  312. }
  313. }
  314. void SpiralLoopPlugin::Clear()
  315. {
  316. m_StoreBuffer.Clear();
  317. m_DubBuffer.Clear();
  318. m_FixedRecord=false;
  319. m_FirstRecord=true;
  320. m_LoopPoint=0;
  321. }
  322. void SpiralLoopPlugin::RecordBuf(float Pos, int Length)
  323. {
  324. if (!m_RecordingSource) return;
  325. static float OldPos=Pos;
  326. if (m_FirstRecord)
  327. {
  328. // Find out if we want a fixed length record
  329. // based on the last sample length, or not
  330. if (m_StoreBuffer.GetLength())
  331. {
  332. m_FixedRecord=true;
  333. }
  334. else
  335. {
  336. m_FixedRecord=false;
  337. m_RecBuffer.Allocate(RECBUFFERSIZE);
  338. m_StoreBuffer.Clear();
  339. m_RecPos=0;
  340. }
  341. m_FirstRecord=false;
  342. m_RecLength=0;
  343. OldPos=Pos;
  344. }
  345. if (m_FixedRecord)
  346. {
  347. m_RecLength=m_LoopPoint;
  348. if (Pos>=m_StoreBuffer.GetLength())
  349. {
  350. Pos=0;
  351. }
  352. for (int n=0; n<Length; n++)
  353. {
  354. // just add directly to the old buffer
  355. float temp=m_DubBuffer[static_cast<int>(Pos)]+m_RecordingSource[n]*RECORD_GAIN;
  356. // fill in all the samples between the speed jump with the same value
  357. m_DubBuffer.Set((int)Pos,temp);
  358. for (int i=static_cast<int>(OldPos); i<=static_cast<int>(Pos); i++)
  359. {
  360. m_DubBuffer.Set(i,temp);
  361. }
  362. OldPos=Pos;
  363. Pos+=m_Speed;
  364. if (Pos>=m_StoreBuffer.GetLength())
  365. {
  366. Pos-=m_StoreBuffer.GetLength();
  367. // remember to fill up to the end of the last buffer
  368. for (int i=static_cast<int>(OldPos); i<m_StoreBuffer.GetLength(); i++)
  369. {
  370. m_DubBuffer.Set(i,temp);
  371. }
  372. // and the beggining of this one
  373. for (int i=0; i<Pos; i++)
  374. {
  375. m_DubBuffer.Set(i,temp);
  376. }
  377. OldPos=0;
  378. }
  379. }
  380. }
  381. else
  382. {
  383. for (int n=0; n<Length; n++)
  384. {
  385. // see if we need a new buffer
  386. if (m_RecPos>=RECBUFFERSIZE)
  387. {
  388. // put the two buffers together
  389. m_StoreBuffer.Add(m_RecBuffer);
  390. m_RecPos=0;
  391. }
  392. m_RecBuffer.Set(m_RecPos,m_RecordingSource[n]*RECORD_GAIN);
  393. m_RecLength++;
  394. m_RecPos++;
  395. }
  396. }
  397. }
  398. void SpiralLoopPlugin::EndRecordBuf()
  399. {
  400. m_FirstRecord=true;
  401. m_LoopPoint=m_StoreBuffer.GetLength();
  402. if (!m_FixedRecord)
  403. {
  404. // reallocate the hold buffer for the new size
  405. // (if the size has changed)
  406. m_DubBuffer.Allocate(m_LoopPoint);
  407. }
  408. }
  409. void SpiralLoopPlugin::Crop()
  410. {
  411. if (m_LoopPoint<m_StoreBuffer.GetLength())
  412. {
  413. m_StoreBuffer.CropTo(m_LoopPoint);
  414. m_DubBuffer.CropTo(m_LoopPoint);
  415. }
  416. }
  417. void SpiralLoopPlugin::Double()
  418. {
  419. Crop();
  420. m_StoreBuffer.Add(m_StoreBuffer);
  421. m_DubBuffer.Add(m_DubBuffer);
  422. m_LoopPoint=m_StoreBuffer.GetLength();
  423. }
  424. void SpiralLoopPlugin::MatchLength(int Len)
  425. {
  426. // set the length, and make sure enough data is allocated
  427. if (m_StoreBuffer.GetLength()>Len)
  428. {
  429. SetLength(Len);
  430. return;
  431. }
  432. else
  433. {
  434. // if it's empty
  435. if (!m_StoreBuffer.GetLength())
  436. {
  437. AllocateMem(Len);
  438. m_StoreBuffer.Zero();
  439. }
  440. else
  441. // there is something in the buffer already, but we need to
  442. // add on some extra data to make the length the same
  443. {
  444. int ExtraLen=Len-m_StoreBuffer.GetLength();
  445. m_StoreBuffer.Expand(ExtraLen);
  446. m_DubBuffer.Expand(ExtraLen);
  447. }
  448. }
  449. }
  450. void SpiralLoopPlugin::Cut(int Start, int End)
  451. {
  452. m_StoreBuffer.GetRegion(m_CopyBuffer,Start,End);
  453. m_StoreBuffer.Remove(Start,End);
  454. if (m_StoreBuffer.GetLength()<m_LoopPoint)
  455. {
  456. m_LoopPoint=m_StoreBuffer.GetLength();
  457. }
  458. if (m_Pos>m_LoopPoint)
  459. {
  460. m_Pos=0;
  461. }
  462. m_DubBuffer.Allocate(m_StoreBuffer.GetLength());
  463. }
  464. void SpiralLoopPlugin::Copy(int Start, int End)
  465. {
  466. m_StoreBuffer.GetRegion(m_CopyBuffer,Start,End);
  467. }
  468. void SpiralLoopPlugin::Paste(int Start)
  469. {
  470. m_StoreBuffer.Insert(m_CopyBuffer,Start);
  471. if (m_StoreBuffer.GetLength()<m_LoopPoint)
  472. {
  473. m_LoopPoint=m_StoreBuffer.GetLength();
  474. }
  475. if (m_Pos>m_LoopPoint)
  476. {
  477. m_Pos=0;
  478. }
  479. m_DubBuffer.Allocate(m_StoreBuffer.GetLength());
  480. }
  481. void SpiralLoopPlugin::PasteMix(int Start)
  482. {
  483. m_StoreBuffer.Mix(m_CopyBuffer,Start);
  484. }
  485. void SpiralLoopPlugin::ZeroRange(int Start, int End)
  486. {
  487. for (int n=Start; n<End; n++)
  488. {
  489. m_StoreBuffer.Set(n,0);
  490. }
  491. }
  492. void SpiralLoopPlugin::ReverseRange(int Start, int End)
  493. {
  494. m_StoreBuffer.Reverse(Start,End);
  495. }
  496. void SpiralLoopPlugin::Halve()
  497. {
  498. m_StoreBuffer.Shrink(m_StoreBuffer.GetLength()/2);
  499. m_DubBuffer.Shrink(m_DubBuffer.GetLength()/2);
  500. if (m_StoreBuffer.GetLength()<m_LoopPoint)
  501. {
  502. m_LoopPoint=m_StoreBuffer.GetLength();
  503. }
  504. if (m_Pos>m_LoopPoint)
  505. {
  506. m_Pos=0;
  507. }
  508. }
  509. void SpiralLoopPlugin::SelectAll()
  510. {
  511. }
  512. void SpiralLoopPlugin::Move(int Start)
  513. {
  514. m_StoreBuffer.Move(Start);
  515. }