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.

510 lines
14KB

  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 "PoshSamplerPlugin.h"
  19. #include "PoshSamplerPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../NoteTable.h"
  23. #include <stdio.h>
  24. #include "../../RiffWav.h"
  25. using namespace std;
  26. static const int NOTETRIG = NUM_SAMPLES*2+1;
  27. static const int REC_INPUT = 16;
  28. static const int S1_INPUT = 18;
  29. static const int S2_INPUT = 19;
  30. static const int S3_INPUT = 20;
  31. extern "C" {
  32. SpiralPlugin* SpiralPlugin_CreateInstance()
  33. {
  34. return new PoshSamplerPlugin;
  35. }
  36. char** SpiralPlugin_GetIcon()
  37. {
  38. return SpiralIcon_xpm;
  39. }
  40. int SpiralPlugin_GetID()
  41. {
  42. return 32;
  43. }
  44. string SpiralPlugin_GetGroupName()
  45. {
  46. return "Delay/Sampling";
  47. }
  48. }
  49. ///////////////////////////////////////////////////////
  50. static void
  51. InitializeSampleDescription(SampleDesc* NewDesc, const string &Pathname, int Note)
  52. {
  53. if (NewDesc) {
  54. NewDesc->Pathname = Pathname;
  55. NewDesc->Volume = 1.0f;
  56. NewDesc->Velocity = 1.0f;
  57. NewDesc->Pitch = 1.0f;
  58. NewDesc->PitchMod = 1.0f;
  59. NewDesc->SamplePos = -1;
  60. NewDesc->Loop = false;
  61. NewDesc->PingPong = false;
  62. NewDesc->Note = Note;
  63. NewDesc->Octave = 0;
  64. NewDesc->TriggerUp = true;
  65. NewDesc->SamplePos = -1;
  66. NewDesc->SampleRate = 44100;
  67. NewDesc->Stereo = false;
  68. NewDesc->PlayStart = 0;
  69. NewDesc->LoopStart = 0;
  70. NewDesc->LoopEnd = INT_MAX;
  71. }
  72. }
  73. PoshSamplerPlugin::PoshSamplerPlugin() :
  74. m_Recording(false)
  75. {
  76. m_PluginInfo.Name="PoshSampler";
  77. m_PluginInfo.Width=400;
  78. m_PluginInfo.Height=215;
  79. m_PluginInfo.NumInputs=21;
  80. m_PluginInfo.NumOutputs=9;
  81. m_PluginInfo.PortTips.push_back("Sample 1 Pitch");
  82. m_PluginInfo.PortTips.push_back("Sample 1 Trigger");
  83. m_PluginInfo.PortTips.push_back("Sample 2 Pitch");
  84. m_PluginInfo.PortTips.push_back("Sample 2 Trigger");
  85. m_PluginInfo.PortTips.push_back("Sample 3 Pitch");
  86. m_PluginInfo.PortTips.push_back("Sample 3 Trigger");
  87. m_PluginInfo.PortTips.push_back("Sample 4 Pitch");
  88. m_PluginInfo.PortTips.push_back("Sample 4 Trigger");
  89. m_PluginInfo.PortTips.push_back("Sample 5 Pitch");
  90. m_PluginInfo.PortTips.push_back("Sample 5 Trigger");
  91. m_PluginInfo.PortTips.push_back("Sample 6 Pitch");
  92. m_PluginInfo.PortTips.push_back("Sample 6 Trigger");
  93. m_PluginInfo.PortTips.push_back("Sample 7 Pitch");
  94. m_PluginInfo.PortTips.push_back("Sample 7 Trigger");
  95. m_PluginInfo.PortTips.push_back("Sample 8 Pitch");
  96. m_PluginInfo.PortTips.push_back("Sample 8 Trigger");
  97. m_PluginInfo.PortTips.push_back("Input");
  98. m_PluginInfo.PortTips.push_back("Sample trigger pitch");
  99. m_PluginInfo.PortTips.push_back("Sample 1 Start Pos");
  100. m_PluginInfo.PortTips.push_back("Sample 2 Start Pos");
  101. m_PluginInfo.PortTips.push_back("Sample 3 Start Pos");
  102. m_PluginInfo.PortTips.push_back("Mixed Output");
  103. m_PluginInfo.PortTips.push_back("Sample 1 Output");
  104. m_PluginInfo.PortTips.push_back("Sample 2 Output");
  105. m_PluginInfo.PortTips.push_back("Sample 3 Output");
  106. m_PluginInfo.PortTips.push_back("Sample 4 Output");
  107. m_PluginInfo.PortTips.push_back("Sample 5 Output");
  108. m_PluginInfo.PortTips.push_back("Sample 6 Output");
  109. m_PluginInfo.PortTips.push_back("Sample 7 Output");
  110. m_PluginInfo.PortTips.push_back("Sample 8 Output");
  111. for (int n=0; n<NUM_SAMPLES; n++)
  112. {
  113. Sample* NewSample = new Sample;
  114. m_SampleVec.push_back(NewSample);
  115. SampleDesc* NewDesc = new SampleDesc;
  116. char temp[256];
  117. sprintf (temp, "PoshSampler%d_%d", GetID(), n);
  118. InitializeSampleDescription(NewDesc, temp, n);
  119. m_SampleDescVec.push_back(NewDesc);
  120. }
  121. m_Version=3;
  122. m_Current = 0;
  123. m_AudioCH->Register("Num",&m_GUIArgs.Num);
  124. m_AudioCH->Register("Value",&m_GUIArgs.Value);
  125. m_AudioCH->Register("Bool",&m_GUIArgs.Boole);
  126. m_AudioCH->Register("Int",&m_GUIArgs.Int);
  127. m_AudioCH->Register("Start",&m_GUIArgs.Start);
  128. m_AudioCH->Register("End",&m_GUIArgs.End);
  129. m_AudioCH->Register("LoopStart",&m_GUIArgs.LoopStart);
  130. m_AudioCH->RegisterData("Name",ChannelHandler::INPUT,&m_GUIArgs.Name,sizeof(m_GUIArgs.Name));
  131. m_AudioCH->Register("PlayPos",&m_CurrentPlayPos,ChannelHandler::OUTPUT);
  132. m_AudioCH->RegisterData("SampleBuffer",ChannelHandler::OUTPUT_REQUEST,&m_SampleBuffer,TRANSBUF_SIZE);
  133. m_AudioCH->Register("SampleSize",&m_SampleSize,ChannelHandler::OUTPUT_REQUEST);
  134. }
  135. PoshSamplerPlugin::~PoshSamplerPlugin()
  136. {
  137. for (vector<Sample*>::iterator i=m_SampleVec.begin();
  138. i!=m_SampleVec.end(); i++)
  139. {
  140. delete(*i);
  141. }
  142. for (vector<SampleDesc*>::iterator i=m_SampleDescVec.begin();
  143. i!=m_SampleDescVec.end(); i++)
  144. {
  145. delete(*i);
  146. }
  147. }
  148. PluginInfo &PoshSamplerPlugin::Initialise(const HostInfo *Host)
  149. {
  150. return SpiralPlugin::Initialise(Host);;
  151. }
  152. SpiralGUIType *PoshSamplerPlugin::CreateGUI()
  153. {
  154. return new PoshSamplerPluginGUI(m_PluginInfo.Width,
  155. m_PluginInfo.Height,
  156. this,m_AudioCH,m_HostInfo);
  157. }
  158. void PoshSamplerPlugin::Execute()
  159. {
  160. static bool Pong=false;
  161. for (int s=0; s<NUM_SAMPLES+1; s++)
  162. {
  163. GetOutputBuf(s)->Zero();
  164. }
  165. float Freq=0;
  166. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  167. {
  168. Freq=GetInputPitch(NOTETRIG,n);
  169. for (int s=0; s<NUM_SAMPLES; s++)
  170. {
  171. SampleDesc* S=m_SampleDescVec[s];
  172. // if we have a sample here
  173. if (m_SampleVec[s]->GetLength())
  174. {
  175. // Convert the CV input into a useable trigger
  176. if (GetInput(s*2+1,n)>0 || feq(Freq,NoteTable[S->Note],0.01f))
  177. {
  178. if (S->TriggerUp)
  179. {
  180. if (s==0 && InputExists(S1_INPUT))
  181. S->PlayStart=(long int)((GetInput(S1_INPUT,n)*0.5+0.5f)*(S->LoopEnd-S->LoopStart))+S->LoopStart;
  182. if (s==1 && InputExists(S2_INPUT))
  183. S->PlayStart=(long int)((GetInput(S2_INPUT,n)*0.5+0.5f)*(S->LoopEnd-S->LoopStart))+S->LoopStart;
  184. if (s==2 && InputExists(S3_INPUT))
  185. S->PlayStart=(long int)((GetInput(S3_INPUT,n)*0.5+0.5f)*(S->LoopEnd-S->LoopStart))+S->LoopStart;
  186. if (S->PlayStart<0) S->PlayStart=0;
  187. S->SamplePos=S->PlayStart;
  188. S->TriggerUp=false;
  189. S->Velocity=GetInput(s*2+1,n);
  190. }
  191. }
  192. else
  193. {
  194. S->TriggerUp=true;
  195. // end it if it's looping
  196. if (S->Loop)
  197. {
  198. S->SamplePos=-1;
  199. }
  200. }
  201. // if the sample has ended
  202. if (S->SamplePos>=S->LoopEnd || S->SamplePos>=m_SampleVec[s]->GetLength())
  203. {
  204. if (S->Loop)
  205. {
  206. if (S->PingPong) Pong=true;
  207. else S->SamplePos=S->LoopStart;
  208. }
  209. else
  210. {
  211. S->SamplePos=-1;
  212. }
  213. }
  214. // if the sample has ended ponging
  215. if (Pong && S->SamplePos<=S->LoopStart)
  216. {
  217. Pong=false;
  218. }
  219. if (S->SamplePos!=-1)
  220. {
  221. if (InputExists(s*2))
  222. {
  223. // Get the pitch from the CV
  224. float PlayFreq=GetInputPitch(s*2,n);
  225. // assumtion: base frequency = 440 (middle A)
  226. S->Pitch = PlayFreq/440;
  227. S->Pitch *= S->SampleRate/(float)m_HostInfo->SAMPLERATE;
  228. }
  229. // mix the sample to the output.
  230. MixOutput(0,n,(*m_SampleVec[s])[S->SamplePos]*S->Volume*S->Velocity);
  231. // copy the sample to it's individual output.
  232. SetOutput(s+1,n,((*m_SampleVec[s])[S->SamplePos]*S->Volume));
  233. float Freq=S->Pitch;
  234. if (S->Octave>0) Freq*=1<<(S->Octave);
  235. if (S->Octave<0) Freq/=1<<(-S->Octave);
  236. if (Pong) S->SamplePos-=Freq*S->PitchMod;
  237. else S->SamplePos+=Freq*S->PitchMod;
  238. }
  239. }
  240. }
  241. }
  242. // record
  243. static int LastRecording=false;
  244. if(m_Recording && InputExists(REC_INPUT))
  245. {
  246. int s=0;//GUI->GetCurrentSample();
  247. if (!LastRecording) m_SampleVec[s]->Clear();
  248. // new sample
  249. if (m_SampleVec[s]->GetLength()==0)
  250. {
  251. *m_SampleVec[s]=*GetInput(REC_INPUT);
  252. m_SampleDescVec[s]->SampleRate=m_HostInfo->SAMPLERATE;
  253. m_SampleDescVec[s]->Stereo=false;
  254. m_SampleDescVec[s]->Pitch *= 1.0f;
  255. m_SampleDescVec[s]->LoopEnd=m_SampleVec[s]->GetLength();
  256. }
  257. else
  258. {
  259. m_SampleVec[s]->Add(*GetInput(REC_INPUT));
  260. m_SampleDescVec[s]->LoopEnd=m_SampleVec[s]->GetLength();
  261. }
  262. }
  263. LastRecording=m_Recording;
  264. if (m_SampleDescVec[m_Current]->SamplePos>0)
  265. {
  266. m_CurrentPlayPos=(long)m_SampleDescVec[m_Current]->SamplePos;
  267. }
  268. }
  269. void PoshSamplerPlugin::ExecuteCommands()
  270. {
  271. if (m_AudioCH->IsCommandWaiting())
  272. {
  273. switch(m_AudioCH->GetCommand())
  274. {
  275. case (LOAD) : LoadSample(m_GUIArgs.Num,m_GUIArgs.Name); break;
  276. case (SAVE) : SaveSample(m_GUIArgs.Num,m_GUIArgs.Name); break;
  277. case (SETVOL) : SetVolume(m_GUIArgs.Num,m_GUIArgs.Value); break;
  278. case (SETPITCH) : SetPitch(m_GUIArgs.Num,m_GUIArgs.Value); break;
  279. case (SETLOOP) : SetLoop(m_GUIArgs.Num,m_GUIArgs.Boole); break;
  280. case (SETPING) : SetPingPong(m_GUIArgs.Num,m_GUIArgs.Boole); break;
  281. case (SETNOTE) : SetNote(m_GUIArgs.Num,m_GUIArgs.Int); break;
  282. case (SETOCT) : SetOctave(m_GUIArgs.Num,m_GUIArgs.Int); break;
  283. case (SETPLAYPOINTS):
  284. {
  285. SetPlayStart(m_GUIArgs.Num,m_GUIArgs.Start);
  286. SetLoopStart(m_GUIArgs.Num,m_GUIArgs.LoopStart);
  287. SetLoopEnd(m_GUIArgs.Num,m_GUIArgs.End);
  288. } break;
  289. case (SETREC) : SetRecord(m_GUIArgs.Boole); break;
  290. case (CUT) : Cut(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  291. case (COPY) : Copy(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  292. case (PASTE) : Paste(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  293. case (MIX) : Mix(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  294. case (CROP) : Crop(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  295. case (REV) : Reverse(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  296. case (AMP) : Amp(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  297. case (SETCURRENT) : m_Current = m_GUIArgs.Num; break;
  298. case (GETSAMPLE) :
  299. {
  300. m_AudioCH->SetupBulkTransfer((void*)m_SampleVec[m_Current]->GetBuffer());
  301. m_SampleSize=m_SampleVec[m_Current]->GetLengthInBytes();
  302. } break;
  303. };
  304. }
  305. }
  306. void PoshSamplerPlugin::StreamOut(ostream &s)
  307. {
  308. s<<m_Version<<" ";
  309. for (int n=0; n<NUM_SAMPLES; n++)
  310. {
  311. s<<m_SampleDescVec[n]->Volume<<" "<<
  312. m_SampleDescVec[n]->PitchMod<<" "<<
  313. m_SampleDescVec[n]->Loop<<" "<<
  314. m_SampleDescVec[n]->PingPong<<" "<<
  315. m_SampleDescVec[n]->Note<<" "<<
  316. m_SampleDescVec[n]->Octave<<" "<<
  317. m_SampleDescVec[n]->SamplePos<<" "<<
  318. m_SampleDescVec[n]->PlayStart<<" "<<
  319. m_SampleDescVec[n]->LoopStart<<" "<<
  320. m_SampleDescVec[n]->LoopEnd<<" "<<
  321. m_SampleDescVec[n]->Note<<" ";
  322. }
  323. }
  324. void PoshSamplerPlugin::StreamIn(istream &s)
  325. {
  326. int version;
  327. s>>version;
  328. for (int n=0; n<NUM_SAMPLES; n++)
  329. {
  330. s>>m_SampleDescVec[n]->Volume>>
  331. m_SampleDescVec[n]->PitchMod>>
  332. m_SampleDescVec[n]->Loop>>
  333. m_SampleDescVec[n]->PingPong>>
  334. m_SampleDescVec[n]->Note>>
  335. m_SampleDescVec[n]->Octave>>
  336. m_SampleDescVec[n]->SamplePos>>
  337. m_SampleDescVec[n]->PlayStart>>
  338. m_SampleDescVec[n]->LoopStart>>
  339. m_SampleDescVec[n]->LoopEnd>>
  340. m_SampleDescVec[n]->Note;
  341. if (version<3)
  342. {
  343. int size;
  344. s>>size;
  345. s.ignore(1);
  346. char Buf[4096];
  347. s.get(Buf,size+1);
  348. }
  349. }
  350. }
  351. void PoshSamplerPlugin::LoadSample(int n, const string &Name)
  352. {
  353. WavFile Wav;
  354. if (Wav.Open(Name,WavFile::READ))
  355. {
  356. m_SampleVec[n]->Allocate(Wav.GetSize());
  357. Wav.Load(*m_SampleVec[n]);
  358. InitializeSampleDescription(m_SampleDescVec[n], Name, n);
  359. m_SampleDescVec[n]->SampleRate=Wav.GetSamplerate();
  360. m_SampleDescVec[n]->Stereo=Wav.IsStereo();
  361. m_SampleDescVec[n]->Pitch *= m_SampleDescVec[n]->SampleRate/(float)m_HostInfo->SAMPLERATE;
  362. m_SampleDescVec[n]->LoopEnd=m_SampleVec[n]->GetLength()-1;
  363. }
  364. }
  365. void PoshSamplerPlugin::SaveSample(int n, const string &Name)
  366. {
  367. if (m_SampleVec[n]->GetLength()==0) return;
  368. WavFile Wav;
  369. Wav.Open(Name,WavFile::WRITE,WavFile::MONO);
  370. Wav.Save(*m_SampleVec[n]);
  371. }
  372. void PoshSamplerPlugin::Cut(int n, long s, long e)
  373. {
  374. if (m_SampleVec[n]->GetLength()==0) return;
  375. m_SampleVec[n]->GetRegion(m_CopyBuffer, s, e);
  376. m_SampleVec[n]->Remove(s, e);
  377. }
  378. void PoshSamplerPlugin::Copy(int n, long s, long e)
  379. {
  380. if (m_SampleVec[n]->GetLength()==0) return;
  381. m_SampleVec[n]->GetRegion(m_CopyBuffer, s, e);
  382. }
  383. void PoshSamplerPlugin::Paste(int n, long s, long e)
  384. {
  385. if (m_SampleVec[n]->GetLength()==0) return;
  386. m_SampleVec[n]->Insert(m_CopyBuffer, s);
  387. }
  388. void PoshSamplerPlugin::Mix(int n, long s, long e)
  389. {
  390. if (m_SampleVec[n]->GetLength()==0) return;
  391. m_SampleVec[n]->Mix(m_CopyBuffer, s);
  392. }
  393. void PoshSamplerPlugin::Crop(int n, long s, long e)
  394. {
  395. if (m_SampleVec[n]->GetLength()==0) return;
  396. m_SampleVec[n]->Remove(0, s);
  397. m_SampleVec[n]->Remove(e, m_SampleVec[n]->GetLength()-1);
  398. }
  399. void PoshSamplerPlugin::Reverse(int n, long s, long e)
  400. {
  401. if (m_SampleVec[n]->GetLength()==0) return;
  402. m_SampleVec[n]->Reverse(s, e);
  403. }
  404. void PoshSamplerPlugin::Amp(int n, long s, long e)
  405. {
  406. if (m_SampleVec[n]->GetLength()==0) return;
  407. for (int m=0; m<m_SampleVec[n]->GetLength(); m++)
  408. {
  409. m_SampleVec[n]->Set(m,(*m_SampleVec[n])[m]*m_SampleDescVec[n]->Volume);
  410. }
  411. }
  412. bool PoshSamplerPlugin::SaveExternalFiles(const string &Dir)
  413. {
  414. for (int n=0; n<NUM_SAMPLES; n++)
  415. {
  416. char temp[256];
  417. // Andy's fix for bug 766594
  418. // sprintf (temp, "PoshSampler%d_%d.wav", SpiralPlugin_GetID(), n);
  419. sprintf (temp, "PoshSampler%d_%d.wav", GetID(), n);
  420. m_SampleDescVec[n]->Pathname = temp;
  421. }
  422. for (int n=0; n<NUM_SAMPLES; n++)
  423. {
  424. // if it's not empty
  425. if (m_SampleVec[n]->GetLength()!=0)
  426. {
  427. SaveSample(n,Dir+m_SampleDescVec[n]->Pathname);
  428. }
  429. }
  430. return true;
  431. }
  432. void PoshSamplerPlugin::LoadExternalFiles(const string &Dir)
  433. {
  434. for (int n=0; n<NUM_SAMPLES; n++)
  435. {
  436. char temp[256];
  437. // Andy's fix for bug 766594
  438. // sprintf (temp, "PoshSampler%d_%d.wav", SpiralPlugin_GetID(), n);
  439. sprintf (temp, "PoshSampler%d_%d.wav", GetID(), n);
  440. m_SampleDescVec[n]->Pathname = temp;
  441. }
  442. for (int n=0; n<NUM_SAMPLES; n++)
  443. {
  444. LoadSample(n,Dir+m_SampleDescVec[n]->Pathname);
  445. }
  446. }