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.

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