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.

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