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.

522 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::Reset()
  159. {
  160. ResetPorts();
  161. m_Current = 0;
  162. for (int s=0; s<NUM_SAMPLES; s++)
  163. {
  164. m_SampleDescVec[s]->Pitch *= m_SampleDescVec[s]->SampleRate/(float)m_HostInfo->SAMPLERATE;
  165. m_SampleDescVec[s]->LoopEnd=m_SampleVec[s]->GetLength()-1;
  166. }
  167. }
  168. void PoshSamplerPlugin::Execute()
  169. {
  170. static bool Pong=false;
  171. for (int s=0; s<NUM_SAMPLES+1; s++)
  172. {
  173. GetOutputBuf(s)->Zero();
  174. }
  175. float Freq=0;
  176. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  177. {
  178. Freq=GetInputPitch(NOTETRIG,n);
  179. for (int s=0; s<NUM_SAMPLES; s++)
  180. {
  181. SampleDesc* S=m_SampleDescVec[s];
  182. // if we have a sample here
  183. if (m_SampleVec[s]->GetLength())
  184. {
  185. // Convert the CV input into a useable trigger
  186. if (GetInput(s*2+1,n)>0 || feq(Freq,NoteTable[S->Note],0.01f))
  187. {
  188. if (S->TriggerUp)
  189. {
  190. if (s==0 && InputExists(S1_INPUT))
  191. S->PlayStart=(long int)((GetInput(S1_INPUT,n)*0.5+0.5f)*(S->LoopEnd-S->LoopStart))+S->LoopStart;
  192. if (s==1 && InputExists(S2_INPUT))
  193. S->PlayStart=(long int)((GetInput(S2_INPUT,n)*0.5+0.5f)*(S->LoopEnd-S->LoopStart))+S->LoopStart;
  194. if (s==2 && InputExists(S3_INPUT))
  195. S->PlayStart=(long int)((GetInput(S3_INPUT,n)*0.5+0.5f)*(S->LoopEnd-S->LoopStart))+S->LoopStart;
  196. if (S->PlayStart<0) S->PlayStart=0;
  197. S->SamplePos=S->PlayStart;
  198. S->TriggerUp=false;
  199. S->Velocity=GetInput(s*2+1,n);
  200. }
  201. }
  202. else
  203. {
  204. S->TriggerUp=true;
  205. // end it if it's looping
  206. if (S->Loop)
  207. {
  208. S->SamplePos=-1;
  209. }
  210. }
  211. // if the sample has ended
  212. if (S->SamplePos>=S->LoopEnd || S->SamplePos>=m_SampleVec[s]->GetLength())
  213. {
  214. if (S->Loop)
  215. {
  216. if (S->PingPong) Pong=true;
  217. else S->SamplePos=S->LoopStart;
  218. }
  219. else
  220. {
  221. S->SamplePos=-1;
  222. }
  223. }
  224. // if the sample has ended ponging
  225. if (Pong && S->SamplePos<=S->LoopStart)
  226. {
  227. Pong=false;
  228. }
  229. if (S->SamplePos!=-1)
  230. {
  231. if (InputExists(s*2))
  232. {
  233. // Get the pitch from the CV
  234. float PlayFreq=GetInputPitch(s*2,n);
  235. // assumtion: base frequency = 440 (middle A)
  236. S->Pitch = PlayFreq/440;
  237. S->Pitch *= S->SampleRate/(float)m_HostInfo->SAMPLERATE;
  238. }
  239. // mix the sample to the output.
  240. MixOutput(0,n,(*m_SampleVec[s])[S->SamplePos]*S->Volume*S->Velocity);
  241. // copy the sample to it's individual output.
  242. SetOutput(s+1,n,((*m_SampleVec[s])[S->SamplePos]*S->Volume));
  243. float Freq=S->Pitch;
  244. if (S->Octave>0) Freq*=1<<(S->Octave);
  245. if (S->Octave<0) Freq/=1<<(-S->Octave);
  246. if (Pong) S->SamplePos-=Freq*S->PitchMod;
  247. else S->SamplePos+=Freq*S->PitchMod;
  248. }
  249. }
  250. }
  251. }
  252. // record
  253. static int LastRecording=false;
  254. if(m_Recording && InputExists(REC_INPUT))
  255. {
  256. int s=0;//GUI->GetCurrentSample();
  257. if (!LastRecording) m_SampleVec[s]->Clear();
  258. // new sample
  259. if (m_SampleVec[s]->GetLength()==0)
  260. {
  261. *m_SampleVec[s]=*GetInput(REC_INPUT);
  262. m_SampleDescVec[s]->SampleRate=m_HostInfo->SAMPLERATE;
  263. m_SampleDescVec[s]->Stereo=false;
  264. m_SampleDescVec[s]->Pitch *= 1.0f;
  265. m_SampleDescVec[s]->LoopEnd=m_SampleVec[s]->GetLength();
  266. }
  267. else
  268. {
  269. m_SampleVec[s]->Add(*GetInput(REC_INPUT));
  270. m_SampleDescVec[s]->LoopEnd=m_SampleVec[s]->GetLength();
  271. }
  272. }
  273. LastRecording=m_Recording;
  274. if (m_SampleDescVec[m_Current]->SamplePos>0)
  275. {
  276. m_CurrentPlayPos=(long)m_SampleDescVec[m_Current]->SamplePos;
  277. }
  278. }
  279. void PoshSamplerPlugin::ExecuteCommands()
  280. {
  281. if (m_AudioCH->IsCommandWaiting())
  282. {
  283. switch(m_AudioCH->GetCommand())
  284. {
  285. case (LOAD) : LoadSample(m_GUIArgs.Num,m_GUIArgs.Name); break;
  286. case (SAVE) : SaveSample(m_GUIArgs.Num,m_GUIArgs.Name); break;
  287. case (SETVOL) : SetVolume(m_GUIArgs.Num,m_GUIArgs.Value); break;
  288. case (SETPITCH) : SetPitch(m_GUIArgs.Num,m_GUIArgs.Value); break;
  289. case (SETLOOP) : SetLoop(m_GUIArgs.Num,m_GUIArgs.Boole); break;
  290. case (SETPING) : SetPingPong(m_GUIArgs.Num,m_GUIArgs.Boole); break;
  291. case (SETNOTE) : SetNote(m_GUIArgs.Num,m_GUIArgs.Int); break;
  292. case (SETOCT) : SetOctave(m_GUIArgs.Num,m_GUIArgs.Int); break;
  293. case (SETPLAYPOINTS):
  294. {
  295. SetPlayStart(m_GUIArgs.Num,m_GUIArgs.Start);
  296. SetLoopStart(m_GUIArgs.Num,m_GUIArgs.LoopStart);
  297. SetLoopEnd(m_GUIArgs.Num,m_GUIArgs.End);
  298. } break;
  299. case (SETREC) : SetRecord(m_GUIArgs.Boole); break;
  300. case (CUT) : Cut(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  301. case (COPY) : Copy(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  302. case (PASTE) : Paste(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  303. case (MIX) : Mix(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  304. case (CROP) : Crop(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  305. case (REV) : Reverse(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  306. case (AMP) : Amp(m_GUIArgs.Num,m_GUIArgs.Start,m_GUIArgs.End); break;
  307. case (SETCURRENT) : m_Current = m_GUIArgs.Num; break;
  308. case (GETSAMPLE) :
  309. {
  310. m_AudioCH->SetupBulkTransfer((void*)m_SampleVec[m_Current]->GetBuffer());
  311. m_SampleSize=m_SampleVec[m_Current]->GetLengthInBytes();
  312. } break;
  313. };
  314. }
  315. }
  316. void PoshSamplerPlugin::StreamOut(ostream &s)
  317. {
  318. s<<m_Version<<" ";
  319. for (int n=0; n<NUM_SAMPLES; n++)
  320. {
  321. s<<m_SampleDescVec[n]->Volume<<" "<<
  322. m_SampleDescVec[n]->PitchMod<<" "<<
  323. m_SampleDescVec[n]->Loop<<" "<<
  324. m_SampleDescVec[n]->PingPong<<" "<<
  325. m_SampleDescVec[n]->Note<<" "<<
  326. m_SampleDescVec[n]->Octave<<" "<<
  327. m_SampleDescVec[n]->SamplePos<<" "<<
  328. m_SampleDescVec[n]->PlayStart<<" "<<
  329. m_SampleDescVec[n]->LoopStart<<" "<<
  330. m_SampleDescVec[n]->LoopEnd<<" "<<
  331. m_SampleDescVec[n]->Note<<" ";
  332. }
  333. }
  334. void PoshSamplerPlugin::StreamIn(istream &s)
  335. {
  336. int version;
  337. s>>version;
  338. for (int n=0; n<NUM_SAMPLES; n++)
  339. {
  340. s>>m_SampleDescVec[n]->Volume>>
  341. m_SampleDescVec[n]->PitchMod>>
  342. m_SampleDescVec[n]->Loop>>
  343. m_SampleDescVec[n]->PingPong>>
  344. m_SampleDescVec[n]->Note>>
  345. m_SampleDescVec[n]->Octave>>
  346. m_SampleDescVec[n]->SamplePos>>
  347. m_SampleDescVec[n]->PlayStart>>
  348. m_SampleDescVec[n]->LoopStart>>
  349. m_SampleDescVec[n]->LoopEnd>>
  350. m_SampleDescVec[n]->Note;
  351. if (version<3)
  352. {
  353. int size;
  354. s>>size;
  355. s.ignore(1);
  356. char Buf[4096];
  357. s.get(Buf,size+1);
  358. }
  359. }
  360. }
  361. void PoshSamplerPlugin::LoadSample(int n, const string &Name)
  362. {
  363. WavFile Wav;
  364. if (Wav.Open(Name,WavFile::READ))
  365. {
  366. m_SampleVec[n]->Allocate(Wav.GetSize());
  367. Wav.Load(*m_SampleVec[n]);
  368. InitializeSampleDescription(m_SampleDescVec[n], Name, n);
  369. m_SampleDescVec[n]->SampleRate=Wav.GetSamplerate();
  370. m_SampleDescVec[n]->Stereo=Wav.IsStereo();
  371. m_SampleDescVec[n]->Pitch *= m_SampleDescVec[n]->SampleRate/(float)m_HostInfo->SAMPLERATE;
  372. m_SampleDescVec[n]->LoopEnd=m_SampleVec[n]->GetLength()-1;
  373. }
  374. }
  375. void PoshSamplerPlugin::SaveSample(int n, const string &Name)
  376. {
  377. if (m_SampleVec[n]->GetLength()==0) return;
  378. WavFile Wav;
  379. Wav.Open(Name,WavFile::WRITE,WavFile::MONO);
  380. Wav.Save(*m_SampleVec[n]);
  381. }
  382. void PoshSamplerPlugin::Cut(int n, long s, long e)
  383. {
  384. if (m_SampleVec[n]->GetLength()==0) return;
  385. m_SampleVec[n]->GetRegion(m_CopyBuffer, s, e);
  386. m_SampleVec[n]->Remove(s, e);
  387. }
  388. void PoshSamplerPlugin::Copy(int n, long s, long e)
  389. {
  390. if (m_SampleVec[n]->GetLength()==0) return;
  391. m_SampleVec[n]->GetRegion(m_CopyBuffer, s, e);
  392. }
  393. void PoshSamplerPlugin::Paste(int n, long s, long e)
  394. {
  395. if (m_SampleVec[n]->GetLength()==0) return;
  396. m_SampleVec[n]->Insert(m_CopyBuffer, s);
  397. }
  398. void PoshSamplerPlugin::Mix(int n, long s, long e)
  399. {
  400. if (m_SampleVec[n]->GetLength()==0) return;
  401. m_SampleVec[n]->Mix(m_CopyBuffer, s);
  402. }
  403. void PoshSamplerPlugin::Crop(int n, long s, long e)
  404. {
  405. if (m_SampleVec[n]->GetLength()==0) return;
  406. m_SampleVec[n]->Remove(0, s);
  407. m_SampleVec[n]->Remove(e, m_SampleVec[n]->GetLength()-1);
  408. }
  409. void PoshSamplerPlugin::Reverse(int n, long s, long e)
  410. {
  411. if (m_SampleVec[n]->GetLength()==0) return;
  412. m_SampleVec[n]->Reverse(s, e);
  413. }
  414. void PoshSamplerPlugin::Amp(int n, long s, long e)
  415. {
  416. if (m_SampleVec[n]->GetLength()==0) return;
  417. for (int m=0; m<m_SampleVec[n]->GetLength(); m++)
  418. {
  419. m_SampleVec[n]->Set(m,(*m_SampleVec[n])[m]*m_SampleDescVec[n]->Volume);
  420. }
  421. }
  422. bool PoshSamplerPlugin::SaveExternalFiles(const string &Dir)
  423. {
  424. for (int n=0; n<NUM_SAMPLES; n++)
  425. {
  426. char temp[256];
  427. // Andy's fix for bug 766594
  428. // sprintf (temp, "PoshSampler%d_%d.wav", SpiralPlugin_GetID(), n);
  429. sprintf (temp, "PoshSampler%d_%d.wav", GetID(), n);
  430. m_SampleDescVec[n]->Pathname = temp;
  431. }
  432. for (int n=0; n<NUM_SAMPLES; n++)
  433. {
  434. // if it's not empty
  435. if (m_SampleVec[n]->GetLength()!=0)
  436. {
  437. SaveSample(n,Dir+m_SampleDescVec[n]->Pathname);
  438. }
  439. }
  440. return true;
  441. }
  442. void PoshSamplerPlugin::LoadExternalFiles(const string &Dir, int withID)
  443. {
  444. for (int n=0; n<NUM_SAMPLES; n++)
  445. {
  446. char temp[256];
  447. // Andy's fix for bug 766594
  448. // sprintf (temp, "PoshSampler%d_%d.wav", SpiralPlugin_GetID(), n);
  449. sprintf (temp, "PoshSampler%d_%d.wav", ((withID==-1)?GetID():withID), n);
  450. m_SampleDescVec[n]->Pathname = temp;
  451. }
  452. for (int n=0; n<NUM_SAMPLES; n++)
  453. {
  454. LoadSample(n,Dir+m_SampleDescVec[n]->Pathname);
  455. }
  456. }