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.

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