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.

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