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.

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